Jay-Rajput commited on
Commit
adc5edf
·
1 Parent(s): 52ba891
Files changed (1) hide show
  1. app.py +148 -149
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,65 +508,179 @@ 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
- # # redistribute_lost_points(match_id)
680
 
 
681
  # outcomes.to_json(OUTCOMES)
682
  # outcomes.push_to_hub("Jay-Rajput/DIS_IPL_Outcomes", split="train")
683
 
684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685
  # Function to fetch matches for a given date
686
  def fetch_matches_by_date(matches, selected_date):
687
  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 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:
617
+ outcomes_df.at[idx, 'winning_team'] = winning_team
618
+ outcomes_df.at[idx, 'man_of_the_match'] = man_of_the_match
619
+ outcome_exists = True
620
+ break
621
+ if not outcome_exists:
622
+ new_outcome = {"match_id": match_id, "winning_team": winning_team, "man_of_the_match": man_of_the_match}
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]