Jay-Rajput commited on
Commit
d50e2d0
·
1 Parent(s): b0796a0

points redistribution

Browse files
Files changed (1) hide show
  1. app.py +83 -7
app.py CHANGED
@@ -31,6 +31,10 @@ outcomes_file = Path("outcomes") / f"match_outcomes.json"
31
  OUTCOMES_FOLDER = outcomes_file.parent
32
  OUTCOMES_FOLDER.mkdir(parents=True, exist_ok=True)
33
 
 
 
 
 
34
  # Initialize CommitScheduler
35
  scheduler = CommitScheduler(
36
  repo_id="DIS_IPL_Preds",
@@ -286,25 +290,97 @@ def display_predictions():
286
  else:
287
  st.write("No predictions for today's matches yet.")
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
  def display_leaderboard():
291
  if st.button("Show Leaderboard"):
292
  try:
293
  # # Load the 'leaders' configuration
294
  dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
 
 
 
 
 
 
 
 
 
295
 
296
  users_data = []
297
  if dataset:
298
  for user, points_dict in dataset[0].items():
299
  points = points_dict.get("points", 0)
300
  last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
301
- users_data.append({'User': user, 'Points': points, "Last 5 Bids": last_5_results})
 
 
 
 
 
 
302
  else:
303
- data = load_users(USERS_JSON)
304
- for user, points_dict in data.items():
305
- points = points_dict.get("points", 0)
306
- last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
307
- users_data.append({'User': user, 'Points': points, "Last 5 Bids": last_5_results})
308
 
309
  leaderboard = pd.DataFrame(users_data)
310
 
@@ -315,7 +391,7 @@ def display_leaderboard():
315
  leaderboard['Rank'] = range(1, len(leaderboard) + 1)
316
 
317
  # Select and order the columns for display
318
- leaderboard = leaderboard[['Rank', 'User', 'Points', 'Last 5 Bids']]
319
 
320
  st.dataframe(leaderboard, hide_index=True)
321
  except Exception as e:
 
31
  OUTCOMES_FOLDER = outcomes_file.parent
32
  OUTCOMES_FOLDER.mkdir(parents=True, exist_ok=True)
33
 
34
+ REDISTRIBUTED_JSON = Path("redistributed_matches.json")
35
+ if not REDISTRIBUTED_JSON.exists():
36
+ REDISTRIBUTED_JSON.write_text("[]")
37
+
38
  # Initialize CommitScheduler
39
  scheduler = CommitScheduler(
40
  repo_id="DIS_IPL_Preds",
 
290
  else:
291
  st.write("No predictions for today's matches yet.")
292
 
293
+ def redistribute_lost_points(match_id) -> dict:
294
+ # Load already processed matches
295
+ with open(REDISTRIBUTED_JSON, "r") as f:
296
+ done_matches = json.load(f)
297
+
298
+ if match_id in done_matches:
299
+ return {}
300
+
301
+ users = load_users(USERS_JSON)
302
+ predictions = []
303
+
304
+ for file in PREDICTIONS_FOLDER.glob(f"prediction_{match_id}_*.json"):
305
+ with open(file, "r") as f:
306
+ for line in f:
307
+ predictions.append(json.loads(line.strip()))
308
+
309
+ outcomes = load_data(OUTCOMES_JSON)
310
+ outcome = next((m for m in outcomes if m["match_id"] == match_id), None)
311
+ if not outcome:
312
+ st.error("Match outcome not found.")
313
+ return {}
314
+
315
+ correct_winner = outcome["winner"]
316
+ correct_motm = outcome["man_of_the_match"]
317
+
318
+ user_losses = {}
319
+ for pred in predictions:
320
+ user = pred["user_name"]
321
+ bid = pred["bid_points"]
322
+ if (pred["predicted_winner"] != correct_winner) or (pred["predicted_motm"] != correct_motm):
323
+ user_losses[user] = user_losses.get(user, 0) + bid
324
+
325
+ top_5_users = sorted(users.items(), key=lambda x: x[1]["points"], reverse=True)[:5]
326
+ top_5_usernames = [user for user, _ in top_5_users]
327
+
328
+ lost_points_by_top5 = sum(user_losses.get(user, 0) for user in top_5_usernames)
329
+ if lost_points_by_top5 == 0:
330
+ return {}
331
+
332
+ rest_users = [user for user in users if user not in top_5_usernames]
333
+ total_points_rest_users = sum(users[u]["points"] for u in rest_users if users[u]["points"] > 0)
334
+
335
+ bonus_map = {}
336
+ for user in rest_users:
337
+ user_points = users[user]["points"]
338
+ if user_points <= 0:
339
+ continue
340
+ share_ratio = user_points / total_points_rest_users
341
+ bonus = round(share_ratio * lost_points_by_top5)
342
+ users[user]["points"] += bonus
343
+ bonus_map[user] = bonus
344
+
345
+ with open(USERS_JSON, "w") as f:
346
+ json.dump(users, f, indent=2)
347
+
348
+ # Record this match as done
349
+ done_matches.append(match_id)
350
+ with open(REDISTRIBUTED_JSON, "w") as f:
351
+ json.dump(done_matches, f, indent=2)
352
+
353
+ return bonus_map
354
 
355
  def display_leaderboard():
356
  if st.button("Show Leaderboard"):
357
  try:
358
  # # Load the 'leaders' configuration
359
  dataset = load_dataset("Jay-Rajput/DIS_IPL_Leads", split='train')
360
+
361
+ # Load outcomes data from HF instead of local file
362
+ outcome_dataset = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split='train')
363
+ latest_match_id = outcome_dataset[-1]["match_id"] if outcome_dataset else None
364
+
365
+ # Redistribute points only once per match
366
+ bonus_map = {}
367
+ if latest_match_id:
368
+ bonus_map = redistribute_lost_points(latest_match_id)
369
 
370
  users_data = []
371
  if dataset:
372
  for user, points_dict in dataset[0].items():
373
  points = points_dict.get("points", 0)
374
  last_5_results = " ".join(points_dict.get("last_5_results", ["⚪"] * 5)) # Default: 5 white circles
375
+ bonus = bonus_map.get(user, 0)
376
+ users_data.append({
377
+ 'User': user,
378
+ 'Points': points,
379
+ 'Last 5 Bids': last_5_results,
380
+ 'Redistribution Bonus': bonus
381
+ })
382
  else:
383
+ st.warning("No leaderboard data found.")
 
 
 
 
384
 
385
  leaderboard = pd.DataFrame(users_data)
386
 
 
391
  leaderboard['Rank'] = range(1, len(leaderboard) + 1)
392
 
393
  # Select and order the columns for display
394
+ leaderboard = leaderboard[['Rank', 'User', 'Points', 'Redistribution Bonus', 'Last 5 Bids']]
395
 
396
  st.dataframe(leaderboard, hide_index=True)
397
  except Exception as e: