Jay-Rajput commited on
Commit
053d053
·
1 Parent(s): f489bfe

redistributed bonus

Browse files
Files changed (1) hide show
  1. app.py +151 -148
app.py CHANGED
@@ -491,126 +491,11 @@ def redistribute_lost_points(match_id):
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
- # outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
496
- # outcomes_df = pd.DataFrame(outcomes)
497
-
498
- # # Update or add match outcome
499
- # outcome_exists = False
500
- # for idx, outcome in outcomes_df.iterrows():
501
- # if outcome['match_id'] == match_id:
502
- # outcomes_df.at[idx, 'winning_team'] = winning_team
503
- # outcomes_df.at[idx, 'man_of_the_match'] = man_of_the_match
504
- # outcome_exists = True
505
- # break
506
- # if not outcome_exists:
507
- # new_outcome = {"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match}
508
- # outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
509
- # outcomes = Dataset.from_pandas(outcomes_df)
510
-
511
- # if not outcome_only:
512
- # predictions = fetch_latest_predictions(match_id)
513
- # users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
514
- # users_df = pd.DataFrame(users)
515
-
516
- # # Capture previous leaderboard (top 5 users and their points)
517
- # prev_scores = [(user, users_df[user][0]['points']) for user in users_df.columns]
518
- # prev_scores.sort(key=lambda x: x[1], reverse=True)
519
- # prev_top_5 = prev_scores[:5]
520
- # top5_usernames = [user for user, _ in prev_top_5]
521
-
522
- # lost_points_by_top5 = 0
523
- # user_outcomes = {}
524
-
525
- # # Step 1: Apply current match outcomes
526
- # for user_name in users_df.columns:
527
- # user_data = users_df[user_name][0]
528
- # user_points = user_data['points']
529
- # user_initial_points = user_points
530
-
531
- # if user_name in set(predictions['user_name']):
532
- # prediction = predictions[predictions['user_name'] == user_name].iloc[0]
533
- # predicted_winner = prediction['predicted_winner']
534
- # predicted_motm = prediction['predicted_motm']
535
- # bid_points = prediction['bid_points']
536
-
537
- # if predicted_winner == winning_team:
538
- # user_points += 2000 + bid_points
539
- # result_indicator = "🟢"
540
- # if predicted_motm == man_of_the_match:
541
- # user_points += 500
542
- # else:
543
- # user_points -= 200 + bid_points
544
- # result_indicator = "🔴"
545
- # if user_name in top5_usernames:
546
- # lost_points_by_top5 += (200 + bid_points)
547
- # else:
548
- # user_points -= 200
549
- # result_indicator = "⚪"
550
- # if user_name in top5_usernames:
551
- # lost_points_by_top5 += 200
552
-
553
- # user_points = max(user_points, 0)
554
- # user_outcomes[user_name] = {
555
- # "updated_points": user_points,
556
- # "result_indicator": result_indicator,
557
- # "initial_points": user_initial_points
558
- # }
559
-
560
- # # Step 2: Build new leaderboard after applying outcome
561
- # new_leaderboard = [(u, d["updated_points"]) for u, d in user_outcomes.items()]
562
- # new_leaderboard.sort(key=lambda x: x[1], reverse=True)
563
-
564
- # # Step 3: Redistribute lost points with reverse leaderboard weighting
565
- # bonus_distribution = {}
566
-
567
- # remaining_users = [u for u in user_outcomes if u not in top5_usernames]
568
- # if remaining_users and lost_points_by_top5 > 0:
569
- # sorted_remaining = sorted(remaining_users, key=lambda u: user_outcomes[u]['updated_points'])
570
- # weights = {u: 1 / (i + 1) for i, u in enumerate(sorted_remaining)}
571
- # total_weight = sum(weights.values())
572
-
573
- # cumulative_bonus = 0
574
- # for i, user in enumerate(sorted_remaining):
575
- # if i == len(sorted_remaining) - 1:
576
- # bonus = lost_points_by_top5 - cumulative_bonus
577
- # else:
578
- # share_fraction = weights[user] / total_weight
579
- # bonus = int(lost_points_by_top5 * share_fraction)
580
- # cumulative_bonus += bonus
581
- # bonus_distribution[user] = bonus
582
-
583
- # # Step 4: Apply the appropriate update
584
- # for user in users_df.columns:
585
- # bonus = bonus_distribution.get(user, 0)
586
- # final_points = user_outcomes[user]["updated_points"] + bonus
587
-
588
- # users_df[user][0]['points'] = final_points
589
- # users_df[user][0]['redistributed_bonus'] = bonus
590
-
591
- # result = user_outcomes[user]["result_indicator"]
592
- # if "last_5_results" not in users_df[user][0]:
593
- # users_df[user][0]["last_5_results"] = []
594
- # users_df[user][0]["last_5_results"].insert(0, result)
595
- # users_df[user][0]["last_5_results"] = users_df[user][0]["last_5_results"][:5]
596
-
597
- # # Save updated leaderboard
598
- # users.to_json(USERS_JSON)
599
- # updated_dataset = Dataset.from_pandas(users_df)
600
- # updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
601
-
602
- # # Save match outcome
603
- # outcomes.to_json(OUTCOMES)
604
- # outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
605
-
606
-
607
  def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
608
-
609
- # Load existing match outcomes
610
- outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
611
  outcomes_df = pd.DataFrame(outcomes)
612
 
613
- # Directly update or add the match outcome
614
  outcome_exists = False
615
  for idx, outcome in outcomes_df.iterrows():
616
  if outcome['match_id'] == match_id:
@@ -623,64 +508,182 @@ def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, ou
623
  outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
624
  outcomes = Dataset.from_pandas(outcomes_df)
625
 
626
- if not outcome_only: # Update user scores only if outcome_only is False
627
- # Load predictions only if necessary
628
  predictions = fetch_latest_predictions(match_id)
629
-
630
- # Load users' data only if necessary
631
  users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
632
  users_df = pd.DataFrame(users)
633
 
634
- # Update user points based on prediction accuracy
635
- users_with_predictions = set(predictions['user_name'])
 
 
 
 
 
 
 
 
636
  for user_name in users_df.columns:
637
- user_points = users_df[user_name][0]['points']
638
- if user_name in users_with_predictions:
 
 
 
639
  prediction = predictions[predictions['user_name'] == user_name].iloc[0]
640
  predicted_winner = prediction['predicted_winner']
641
  predicted_motm = prediction['predicted_motm']
642
  bid_points = prediction['bid_points']
643
 
644
- # Update points based on prediction accuracy
645
  if predicted_winner == winning_team:
646
  user_points += 2000 + bid_points
647
- result_indicator = "🟢" # Correct Prediction
648
  if predicted_motm == man_of_the_match:
649
  user_points += 500
650
  else:
651
  user_points -= 200 + bid_points
652
- result_indicator = "🔴" # Wrong Prediction
 
 
653
  else:
654
- # Deduct 200 points for not submitting a prediction
655
  user_points -= 200
656
- result_indicator = "⚪" # No Prediction
657
-
658
- # Ensure user_points is never negative
659
- user_points = max(user_points, 0)
660
-
661
- # Update user's points in the DataFrame
662
- users_df[user_name][0]['points'] = user_points
663
- users[user_name][0]['points'] = user_points
664
-
665
- # Maintain last 5 prediction results
666
- if "last_5_results" not in users_df[user_name][0]:
667
- users_df[user_name][0]["last_5_results"] = []
668
- users_df[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
669
- users_df[user_name][0]["last_5_results"] = users_df[user_name][0]["last_5_results"][:5] # Keep only last 5
670
-
671
- if "last_5_results" not in users[user_name][0]:
672
- users[user_name][0]["last_5_results"] = []
673
- users[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
674
- users[user_name][0]["last_5_results"] = users[user_name][0]["last_5_results"][:5] # Keep only last 5
675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
676
  users.to_json(USERS_JSON)
677
  updated_dataset = Dataset.from_pandas(users_df)
678
  updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
679
 
 
680
  outcomes.to_json(OUTCOMES)
681
  outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
682
 
683
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
684
  # Function to fetch matches for a given date
685
  def fetch_matches_by_date(matches, selected_date):
686
  return [match for match in matches if datetime.strptime(match['date'], '%Y-%m-%d').date() == selected_date]
 
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
+ outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
 
 
496
  outcomes_df = pd.DataFrame(outcomes)
497
 
498
+ # Update or add match outcome
499
  outcome_exists = False
500
  for idx, outcome in outcomes_df.iterrows():
501
  if outcome['match_id'] == match_id:
 
508
  outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
509
  outcomes = Dataset.from_pandas(outcomes_df)
510
 
511
+ if not outcome_only:
 
512
  predictions = fetch_latest_predictions(match_id)
 
 
513
  users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
514
  users_df = pd.DataFrame(users)
515
 
516
+ # Capture previous leaderboard (top 3 users and their points)
517
+ prev_scores = [(user, users_df[user][0]['points']) for user in users_df.columns]
518
+ prev_scores.sort(key=lambda x: x[1], reverse=True)
519
+ prev_top_3 = prev_scores[:3]
520
+ top3_usernames = [user for user, _ in prev_top_3]
521
+
522
+ lost_points_by_top3 = 0
523
+ user_outcomes = {}
524
+
525
+ # Step 1: Apply current match outcomes
526
  for user_name in users_df.columns:
527
+ user_data = users_df[user_name][0]
528
+ user_points = user_data['points']
529
+ user_initial_points = user_points
530
+
531
+ if user_name in set(predictions['user_name']):
532
  prediction = predictions[predictions['user_name'] == user_name].iloc[0]
533
  predicted_winner = prediction['predicted_winner']
534
  predicted_motm = prediction['predicted_motm']
535
  bid_points = prediction['bid_points']
536
 
 
537
  if predicted_winner == winning_team:
538
  user_points += 2000 + bid_points
539
+ result_indicator = "🟢"
540
  if predicted_motm == man_of_the_match:
541
  user_points += 500
542
  else:
543
  user_points -= 200 + bid_points
544
+ result_indicator = "🔴"
545
+ if user_name in top3_usernames:
546
+ lost_points_by_top3 += (200 + bid_points)
547
  else:
 
548
  user_points -= 200
549
+ result_indicator = "⚪"
550
+ if user_name in top3_usernames:
551
+ lost_points_by_top3 += 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
552
 
553
+ user_points = max(user_points, 0)
554
+ user_outcomes[user_name] = {
555
+ "updated_points": user_points,
556
+ "result_indicator": result_indicator,
557
+ "initial_points": user_initial_points
558
+ }
559
+
560
+ # Step 2: Build new leaderboard after applying outcome
561
+ new_leaderboard = [(u, d["updated_points"]) for u, d in user_outcomes.items()]
562
+ new_leaderboard.sort(key=lambda x: x[1], reverse=True)
563
+ third_place_points = new_leaderboard[2][1] if len(new_leaderboard) >= 3 else 0
564
+
565
+ # Step 3: Redistribute lost points using difference-from-3rd-place logic
566
+ redistribution_pool = lost_points_by_top3
567
+ redistribution_weights = {}
568
+ redistribution_total_weight = 0
569
+
570
+ for user, data in user_outcomes.items():
571
+ if user not in top3_usernames:
572
+ diff_from_3rd = max(third_place_points - data['updated_points'], 0)
573
+ redistribution_weights[user] = diff_from_3rd
574
+ redistribution_total_weight += diff_from_3rd
575
+
576
+ bonus_distribution = {}
577
+ for user, weight in redistribution_weights.items():
578
+ if redistribution_total_weight == 0:
579
+ bonus = 0
580
+ else:
581
+ bonus = int(redistribution_pool * (weight / redistribution_total_weight))
582
+ bonus_distribution[user] = bonus
583
+
584
+ # Step 4: Apply bonus and update dataset
585
+ for user in users_df.columns:
586
+ base_points = user_outcomes[user]["updated_points"]
587
+ bonus = bonus_distribution.get(user, 0)
588
+ final_points = base_points + bonus
589
+
590
+ users_df[user][0]['points'] = final_points
591
+ users_df[user][0]['redistributed_bonus'] = bonus
592
+
593
+ # Maintain last 5 results
594
+ result = user_outcomes[user]["result_indicator"]
595
+ if "last_5_results" not in users_df[user][0]:
596
+ users_df[user][0]["last_5_results"] = []
597
+ users_df[user][0]["last_5_results"].insert(0, result)
598
+ users_df[user][0]["last_5_results"] = users_df[user][0]["last_5_results"][:5]
599
+
600
+ # Save updated leaderboard
601
  users.to_json(USERS_JSON)
602
  updated_dataset = Dataset.from_pandas(users_df)
603
  updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
604
 
605
+ # Save match outcome
606
  outcomes.to_json(OUTCOMES)
607
  outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
608
 
609
 
610
+ # def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match, outcome_only=False):
611
+
612
+ # # Load existing match outcomes
613
+ # outcomes = load_dataset("Jay-Rajput/DIS_IPL_Outcomes", split="train")
614
+ # outcomes_df = pd.DataFrame(outcomes)
615
+
616
+ # # Directly update or add the match outcome
617
+ # outcome_exists = False
618
+ # for idx, outcome in outcomes_df.iterrows():
619
+ # if outcome['match_id'] == match_id:
620
+ # outcomes_df.at[idx, 'winning_team'] = winning_team
621
+ # outcomes_df.at[idx, 'man_of_the_match'] = man_of_the_match
622
+ # outcome_exists = True
623
+ # break
624
+ # if not outcome_exists:
625
+ # new_outcome = {"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match}
626
+ # outcomes_df = pd.concat([outcomes_df, pd.DataFrame([new_outcome])], ignore_index=True)
627
+ # outcomes = Dataset.from_pandas(outcomes_df)
628
+
629
+ # if not outcome_only: # Update user scores only if outcome_only is False
630
+ # # Load predictions only if necessary
631
+ # predictions = fetch_latest_predictions(match_id)
632
+
633
+ # # Load users' data only if necessary
634
+ # users = load_dataset("Jay-Rajput/DIS_IPL_Leads", split="train")
635
+ # users_df = pd.DataFrame(users)
636
+
637
+ # # Update user points based on prediction accuracy
638
+ # users_with_predictions = set(predictions['user_name'])
639
+ # for user_name in users_df.columns:
640
+ # user_points = users_df[user_name][0]['points']
641
+ # if user_name in users_with_predictions:
642
+ # prediction = predictions[predictions['user_name'] == user_name].iloc[0]
643
+ # predicted_winner = prediction['predicted_winner']
644
+ # predicted_motm = prediction['predicted_motm']
645
+ # bid_points = prediction['bid_points']
646
+
647
+ # # Update points based on prediction accuracy
648
+ # if predicted_winner == winning_team:
649
+ # user_points += 2000 + bid_points
650
+ # result_indicator = "🟢" # Correct Prediction
651
+ # if predicted_motm == man_of_the_match:
652
+ # user_points += 500
653
+ # else:
654
+ # user_points -= 200 + bid_points
655
+ # result_indicator = "🔴" # Wrong Prediction
656
+ # else:
657
+ # # Deduct 200 points for not submitting a prediction
658
+ # user_points -= 200
659
+ # result_indicator = "⚪" # No Prediction
660
+
661
+ # # Ensure user_points is never negative
662
+ # user_points = max(user_points, 0)
663
+
664
+ # # Update user's points in the DataFrame
665
+ # users_df[user_name][0]['points'] = user_points
666
+ # users[user_name][0]['points'] = user_points
667
+
668
+ # # Maintain last 5 prediction results
669
+ # if "last_5_results" not in users_df[user_name][0]:
670
+ # users_df[user_name][0]["last_5_results"] = []
671
+ # users_df[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
672
+ # users_df[user_name][0]["last_5_results"] = users_df[user_name][0]["last_5_results"][:5] # Keep only last 5
673
+
674
+ # if "last_5_results" not in users[user_name][0]:
675
+ # users[user_name][0]["last_5_results"] = []
676
+ # users[user_name][0]["last_5_results"].insert(0, result_indicator) # Insert at beginning
677
+ # users[user_name][0]["last_5_results"] = users[user_name][0]["last_5_results"][:5] # Keep only last 5
678
+
679
+ # users.to_json(USERS_JSON)
680
+ # updated_dataset = Dataset.from_pandas(users_df)
681
+ # updated_dataset.push_to_hub("Jay-Rajput/DIS_IPL_Leads", split="train")
682
+
683
+ # outcomes.to_json(OUTCOMES)
684
+ # outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
685
+
686
+
687
  # Function to fetch matches for a given date
688
  def fetch_matches_by_date(matches, selected_date):
689
  return [match for match in matches if datetime.strptime(match['date'], '%Y-%m-%d').date() == selected_date]