Spaces:
Sleeping
Sleeping
Commit
·
03a4400
1
Parent(s):
fb451d5
bonus
Browse files
app.py
CHANGED
|
@@ -330,7 +330,7 @@ def display_leaderboard():
|
|
| 330 |
leaderboard['Rank'] = range(1, len(leaderboard) + 1)
|
| 331 |
|
| 332 |
# Select and order the columns for display
|
| 333 |
-
leaderboard = leaderboard[['Rank', 'User', 'Points', 'Last 5 Bids']]
|
| 334 |
|
| 335 |
st.dataframe(leaderboard, hide_index=True)
|
| 336 |
except Exception as e:
|
|
@@ -561,29 +561,55 @@ def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, ou
|
|
| 561 |
new_leaderboard.sort(key=lambda x: x[1], reverse=True)
|
| 562 |
new_ranks = {user: rank for rank, (user, _) in enumerate(new_leaderboard)}
|
| 563 |
|
| 564 |
-
# Step 3:
|
| 565 |
-
remaining_users = [
|
| 566 |
-
redistribution_possible =
|
| 567 |
-
redistributed_outcomes = {}
|
| 568 |
-
|
| 569 |
-
if remaining_users and lost_points_by_top5 > 0:
|
| 570 |
-
share = lost_points_by_top5 // len(remaining_users)
|
| 571 |
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
redistributed_outcomes[user] = user_outcomes[user]["updated_points"]
|
| 575 |
-
if user in remaining_users:
|
| 576 |
-
redistributed_outcomes[user] += share
|
| 577 |
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|
| 588 |
# Step 4: Apply the appropriate update
|
| 589 |
for user in users_df.columns:
|
|
|
|
| 330 |
leaderboard['Rank'] = range(1, len(leaderboard) + 1)
|
| 331 |
|
| 332 |
# Select and order the columns for display
|
| 333 |
+
leaderboard = leaderboard[['Rank', 'User', 'Points', 'Redistribution Bonus', 'Last 5 Bids']]
|
| 334 |
|
| 335 |
st.dataframe(leaderboard, hide_index=True)
|
| 336 |
except Exception as e:
|
|
|
|
| 561 |
new_leaderboard.sort(key=lambda x: x[1], reverse=True)
|
| 562 |
new_ranks = {user: rank for rank, (user, _) in enumerate(new_leaderboard)}
|
| 563 |
|
| 564 |
+
# Step 3: Redistribute proportionally based on users' updated points
|
| 565 |
+
remaining_users = [user for user in users_df.columns if user not in top5_usernames]
|
| 566 |
+
redistribution_possible = lost_points_by_top5 > 0 and len(remaining_users) > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
|
| 568 |
+
redistributed_outcomes = {}
|
| 569 |
+
bonus_distribution = {}
|
|
|
|
|
|
|
|
|
|
| 570 |
|
| 571 |
+
if redistribution_possible:
|
| 572 |
+
remaining_users_points = {
|
| 573 |
+
u: user_outcomes[u]["updated_points"] for u in remaining_users
|
| 574 |
+
}
|
| 575 |
+
total_remaining_points = sum(remaining_users_points.values())
|
| 576 |
+
|
| 577 |
+
# Edge case: all remaining users have 0 points
|
| 578 |
+
if total_remaining_points == 0:
|
| 579 |
+
share = lost_points_by_top5 // len(remaining_users)
|
| 580 |
+
for i, user in enumerate(remaining_users):
|
| 581 |
+
bonus = share if i < len(remaining_users) - 1 else lost_points_by_top5 - share * (len(remaining_users) - 1)
|
| 582 |
+
bonus_distribution[user] = bonus
|
| 583 |
+
else:
|
| 584 |
+
cumulative_bonus = 0
|
| 585 |
+
for i, user in enumerate(remaining_users):
|
| 586 |
+
if i == len(remaining_users) - 1:
|
| 587 |
+
bonus = lost_points_by_top5 - cumulative_bonus # Remaining bonus
|
| 588 |
+
else:
|
| 589 |
+
share_fraction = remaining_users_points[user] / total_remaining_points
|
| 590 |
+
bonus = int(lost_points_by_top5 * share_fraction)
|
| 591 |
+
cumulative_bonus += bonus
|
| 592 |
+
bonus_distribution[user] = bonus
|
| 593 |
+
|
| 594 |
+
# Final points with bonus, but only update if leaderboard stays unchanged
|
| 595 |
+
redistributed_outcomes = {
|
| 596 |
+
user: user_outcomes[user]["updated_points"] + bonus_distribution.get(user, 0)
|
| 597 |
+
for user in users_df.columns
|
| 598 |
+
}
|
| 599 |
|
| 600 |
+
# Check if new leaderboard ranks remain the same
|
| 601 |
+
post_redistribution = [(u, redistributed_outcomes[u]) for u in redistributed_outcomes]
|
| 602 |
+
post_redistribution.sort(key=lambda x: x[1], reverse=True)
|
| 603 |
+
redistributed_ranks = {user: rank for rank, (user, _) in enumerate(post_redistribution)}
|
| 604 |
+
|
| 605 |
+
rank_changes = any(new_ranks[u] != redistributed_ranks[u] for u in new_ranks)
|
| 606 |
+
if rank_changes:
|
| 607 |
+
# Rollback redistribution
|
| 608 |
+
bonus_distribution = {}
|
| 609 |
+
redistributed_outcomes = {
|
| 610 |
+
user: user_outcomes[user]["updated_points"]
|
| 611 |
+
for user in users_df.columns
|
| 612 |
+
}
|
| 613 |
|
| 614 |
# Step 4: Apply the appropriate update
|
| 615 |
for user in users_df.columns:
|