Spaces:
Sleeping
Sleeping
Commit
·
0c15d99
1
Parent(s):
332f07c
add bonus
Browse files
app.py
CHANGED
|
@@ -299,93 +299,24 @@ def display_predictions():
|
|
| 299 |
else:
|
| 300 |
st.write("No predictions for today's matches yet.")
|
| 301 |
|
| 302 |
-
def redistribute_lost_points(match_id) -> dict:
|
| 303 |
-
# Load already processed matches
|
| 304 |
-
done_matches = load_bonus(BONUS_JSON)
|
| 305 |
-
|
| 306 |
-
if match_id in done_matches:
|
| 307 |
-
return {}
|
| 308 |
-
|
| 309 |
-
users = load_users(USERS_JSON)
|
| 310 |
-
predictions = []
|
| 311 |
-
|
| 312 |
-
for file in PREDICTIONS_FOLDER.glob(f"prediction_{match_id}_*.json"):
|
| 313 |
-
with open(file, "r") as f:
|
| 314 |
-
for line in f:
|
| 315 |
-
predictions.append(json.loads(line.strip()))
|
| 316 |
-
|
| 317 |
-
outcomes = load_data(OUTCOMES_JSON)
|
| 318 |
-
outcome = next((m for m in outcomes if m["match_id"] == match_id), None)
|
| 319 |
-
if not outcome:
|
| 320 |
-
st.error("Match outcome not found.")
|
| 321 |
-
return {}
|
| 322 |
-
|
| 323 |
-
correct_winner = outcome["winner"]
|
| 324 |
-
correct_motm = outcome["man_of_the_match"]
|
| 325 |
-
|
| 326 |
-
user_losses = {}
|
| 327 |
-
for pred in predictions:
|
| 328 |
-
user = pred["user_name"]
|
| 329 |
-
bid = pred["bid_points"]
|
| 330 |
-
if (pred["predicted_winner"] != correct_winner) or (pred["predicted_motm"] != correct_motm):
|
| 331 |
-
user_losses[user] = user_losses.get(user, 0) + bid
|
| 332 |
-
|
| 333 |
-
top_5_users = sorted(users.items(), key=lambda x: x[1]["points"], reverse=True)[:5]
|
| 334 |
-
top_5_usernames = [user for user, _ in top_5_users]
|
| 335 |
-
|
| 336 |
-
lost_points_by_top5 = sum(user_losses.get(user, 0) for user in top_5_usernames)
|
| 337 |
-
if lost_points_by_top5 == 0:
|
| 338 |
-
return {}
|
| 339 |
-
|
| 340 |
-
rest_users = [user for user in users if user not in top_5_usernames]
|
| 341 |
-
total_points_rest_users = sum(users[u]["points"] for u in rest_users if users[u]["points"] > 0)
|
| 342 |
-
|
| 343 |
-
bonus_map = {}
|
| 344 |
-
for user in rest_users:
|
| 345 |
-
user_points = users[user]["points"]
|
| 346 |
-
if user_points <= 0:
|
| 347 |
-
continue
|
| 348 |
-
share_ratio = user_points / total_points_rest_users
|
| 349 |
-
bonus = round(share_ratio * lost_points_by_top5)
|
| 350 |
-
users[user]["points"] += bonus
|
| 351 |
-
bonus_map[user] = bonus
|
| 352 |
-
|
| 353 |
-
with open(USERS_JSON, "w") as f:
|
| 354 |
-
json.dump(users, f, indent=2)
|
| 355 |
-
|
| 356 |
-
# Record this match as done
|
| 357 |
-
done_matches.append(match_id)
|
| 358 |
-
with open(BONUS_JSON, "w") as f:
|
| 359 |
-
json.dump(done_matches, f, indent=2)
|
| 360 |
-
|
| 361 |
-
return bonus_map
|
| 362 |
|
| 363 |
def display_leaderboard():
|
| 364 |
if st.button("Show Leaderboard"):
|
| 365 |
try:
|
| 366 |
# # Load the 'leaders' configuration
|
| 367 |
dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
|
| 368 |
-
|
| 369 |
-
# Load outcomes data from HF instead of local file
|
| 370 |
-
# outcome_dataset = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split='train')
|
| 371 |
-
# latest_match_id = outcome_dataset[-1]["match_id"] if outcome_dataset else None
|
| 372 |
-
|
| 373 |
-
# # Redistribute points only once per match
|
| 374 |
-
# bonus_map = {}
|
| 375 |
-
# if latest_match_id:
|
| 376 |
-
# bonus_map = redistribute_lost_points(latest_match_id)
|
| 377 |
|
| 378 |
users_data = []
|
| 379 |
if dataset:
|
| 380 |
for user, points_dict in dataset[0].items():
|
| 381 |
points = points_dict.get("points", 0)
|
| 382 |
last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
|
| 383 |
-
|
| 384 |
users_data.append({
|
| 385 |
'User': user,
|
| 386 |
'Points': points,
|
|
|
|
| 387 |
'Last 5 Bids': last_5_results
|
| 388 |
-
# 'Redistribution Bonus': bonus
|
| 389 |
})
|
| 390 |
else:
|
| 391 |
st.warning("No leaderboard data found.")
|
|
@@ -399,7 +330,7 @@ def display_leaderboard():
|
|
| 399 |
leaderboard['Rank'] = range(1, len(leaderboard) + 1)
|
| 400 |
|
| 401 |
# Select and order the columns for display
|
| 402 |
-
leaderboard = leaderboard[['Rank', 'User', 'Points', 'Last 5 Bids']]
|
| 403 |
|
| 404 |
st.dataframe(leaderboard, hide_index=True)
|
| 405 |
except Exception as e:
|
|
@@ -498,6 +429,68 @@ def fetch_latest_predictions(match_id):
|
|
| 498 |
return pd.DataFrame()
|
| 499 |
|
| 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
|
| 502 |
|
| 503 |
# Load existing match outcomes
|
|
@@ -570,6 +563,7 @@ def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, ou
|
|
| 570 |
users.to_json(USERS_JSON)
|
| 571 |
updated_dataset = Dataset.from_pandas(users_df)
|
| 572 |
updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
|
|
|
| 573 |
|
| 574 |
outcomes.to_json(OUTCOMES)
|
| 575 |
outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|
|
|
|
| 299 |
else:
|
| 300 |
st.write("No predictions for today's matches yet.")
|
| 301 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
def display_leaderboard():
|
| 304 |
if st.button("Show Leaderboard"):
|
| 305 |
try:
|
| 306 |
# # Load the 'leaders' configuration
|
| 307 |
dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
users_data = []
|
| 310 |
if dataset:
|
| 311 |
for user, points_dict in dataset[0].items():
|
| 312 |
points = points_dict.get("points", 0)
|
| 313 |
last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
|
| 314 |
+
bonus = points_dict.get("redistributed_bonus", 0)
|
| 315 |
users_data.append({
|
| 316 |
'User': user,
|
| 317 |
'Points': points,
|
| 318 |
+
'Redistribution Bonus': bonus,
|
| 319 |
'Last 5 Bids': last_5_results
|
|
|
|
| 320 |
})
|
| 321 |
else:
|
| 322 |
st.warning("No leaderboard data found.")
|
|
|
|
| 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:
|
|
|
|
| 429 |
return pd.DataFrame()
|
| 430 |
|
| 431 |
|
| 432 |
+
def redistribute_lost_points(match_id):
|
| 433 |
+
predictions = fetch_latest_predictions(match_id)
|
| 434 |
+
users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 435 |
+
users_df = pd.DataFrame(users)
|
| 436 |
+
|
| 437 |
+
# Build current leaderboard (after score updates)
|
| 438 |
+
leaderboard = []
|
| 439 |
+
for user_name in users_df.columns:
|
| 440 |
+
points = users_df[user_name][0]['points']
|
| 441 |
+
leaderboard.append((user_name, points))
|
| 442 |
+
|
| 443 |
+
leaderboard.sort(key=lambda x: x[1], reverse=True)
|
| 444 |
+
|
| 445 |
+
top_5 = leaderboard[:5]
|
| 446 |
+
others = leaderboard[5:]
|
| 447 |
+
|
| 448 |
+
# Fetch match outcome
|
| 449 |
+
outcomes_df = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train").to_pandas()
|
| 450 |
+
match_row = outcomes_df[outcomes_df['match_id'] == match_id].iloc[0]
|
| 451 |
+
winning_team = match_row['winning_team']
|
| 452 |
+
|
| 453 |
+
# Calculate lost points from top 5 users who predicted incorrectly
|
| 454 |
+
total_lost_points = 0
|
| 455 |
+
lost_points_per_user = {}
|
| 456 |
+
|
| 457 |
+
for user_name, _ in top_5:
|
| 458 |
+
if user_name in predictions['user_name'].values:
|
| 459 |
+
pred = predictions[predictions['user_name'] == user_name].iloc[0]
|
| 460 |
+
if pred['predicted_winner'] != winning_team:
|
| 461 |
+
lost_points = 200 + pred['bid_points']
|
| 462 |
+
total_lost_points += lost_points
|
| 463 |
+
lost_points_per_user[user_name] = lost_points
|
| 464 |
+
|
| 465 |
+
if total_lost_points == 0 or not others:
|
| 466 |
+
return # Nothing to redistribute
|
| 467 |
+
|
| 468 |
+
# Total points of eligible users (position 6 to last)
|
| 469 |
+
total_eligible_points = sum([points for (_, points) in others])
|
| 470 |
+
if total_eligible_points == 0:
|
| 471 |
+
return
|
| 472 |
+
|
| 473 |
+
# Distribute lost points proportionally
|
| 474 |
+
for user_name, user_points in others:
|
| 475 |
+
share_ratio = user_points / total_eligible_points
|
| 476 |
+
bonus = int(total_lost_points * share_ratio)
|
| 477 |
+
|
| 478 |
+
# Update bonus in leads
|
| 479 |
+
users_df[user_name][0]['points'] += bonus
|
| 480 |
+
users[user_name][0]['points'] = users_df[user_name][0]['points']
|
| 481 |
+
|
| 482 |
+
# Track redistributed bonus (initialize or accumulate)
|
| 483 |
+
prev_bonus_df = users_df[user_name][0].get("redistributed_bonus", 0)
|
| 484 |
+
prev_bonus_dict = users[user_name][0].get("redistributed_bonus", 0)
|
| 485 |
+
users_df[user_name][0]["redistributed_bonus"] = prev_bonus_df + bonus
|
| 486 |
+
users[user_name][0]["redistributed_bonus"] = prev_bonus_dict + bonus
|
| 487 |
+
|
| 488 |
+
# Push updated dataset
|
| 489 |
+
users.to_json(USERS_JSON)
|
| 490 |
+
updated_dataset = Dataset.from_pandas(users_df)
|
| 491 |
+
updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 492 |
+
|
| 493 |
+
|
| 494 |
def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
|
| 495 |
|
| 496 |
# Load existing match outcomes
|
|
|
|
| 563 |
users.to_json(USERS_JSON)
|
| 564 |
updated_dataset = Dataset.from_pandas(users_df)
|
| 565 |
updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
|
| 566 |
+
redistribute_lost_points(match_id)
|
| 567 |
|
| 568 |
outcomes.to_json(OUTCOMES)
|
| 569 |
outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
|