From f31a0ecf82207d9a8a12f782defc25bee195c2c7 Mon Sep 17 00:00:00 2001 From: Brennan Bibic Date: Mon, 23 Sep 2024 14:34:48 -0400 Subject: [PATCH] Revert "Correct way to get total matches" This reverts commit d38205d5341012df9dade7e95aa38185506f7a7f. --- ranked/views.py | 54 +++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/ranked/views.py b/ranked/views.py index 917f9aa..aca8f40 100644 --- a/ranked/views.py +++ b/ranked/views.py @@ -117,57 +117,39 @@ def mmr_calc(elo, matches_played, delta_hours): return elo * 2 / ((1 + pow(math.e, 1/168 * pow(delta_hours, 0.63))) * (1 + pow(math.e, -0.33 * matches_played))) def global_leaderboard(request): - # Annotate each game type with the total number of matches across all players - total_matches_per_game = GameMode.objects.values('game').annotate(total_matches=Count('match')) - game_to_total_matches = {item['game']: item['total_matches'] for item in total_matches_per_game} - - if not game_to_total_matches: - context = { - 'leaderboard_code': 'global', - 'leaderboard_name': 'Global Elo Leaderboard', - 'players_with_rank': [], - } - return render(request, "ranked/global_leaderboard.html", context) - - max_total_matches = max(game_to_total_matches.values()) - - # Calculate scaling factors based on total matches per game type - scaling_factors = { - game: (matches / max_total_matches) * (2/3) + (1/3) - for game, matches in game_to_total_matches.items() - } - # Fetch all players players = PlayerElo.objects.all() global_scores = [] for player in players: - # Fetch PlayerElo instances for the player without time filtering - player_game_elos = PlayerElo.objects.filter(player=player).select_related('game_mode') + # Fetch all game modes for the player + game_modes = GameMode.objects.filter(playerelo=player) - if not player_game_elos.exists(): + if not game_modes.exists(): continue - # Determine the most played game mode per game type for the player + # Determine the most played game mode per game type game_type_dict = {} - for player_game_elo in player_game_elos: - game_type = player_game_elo.game_mode.game - match_count = player_game_elo.matches_played - mode = player_game_elo.game_mode - if ( - game_type not in game_type_dict or - match_count > game_type_dict[game_type]['match_count'] - ): + for mode in game_modes: + game_type = mode.game + match_count = mode.match_count # Assuming 'match_count' exists + if game_type not in game_type_dict or match_count > game_type_dict[game_type]['match_count']: game_type_dict[game_type] = {'mode': mode, 'match_count': match_count} - # Calculate weighted Elo based on scaling factors + # Calculate weighted Elo total_weight = 0 weighted_elo = 0 for game_type, data in game_type_dict.items(): - scaling_factor = scaling_factors.get(game_type, 1/3) # Default to minimum weight - weighted_elo += data['mode'].elo * scaling_factor - total_weight += scaling_factor + weight = data['match_count'] # Full weight for the most played game + weighted_elo += data['mode'].elo * weight + total_weight += weight + + # Adjust weights for less played games (1/3 weight) + for game_type, data in game_type_dict.items(): + if data['match_count'] < max(d['match_count'] for d in game_type_dict.values()): + weighted_elo += data['mode'].elo * (data['match_count'] / 3) + total_weight += data['match_count'] / 3 if total_weight > 0: global_elo = weighted_elo / total_weight