From 498f460ccdba40248dfda89fef71db3f2df2fd44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 26 Dec 2023 12:49:39 +0100 Subject: [PATCH] leader cards: implement ties --- src/render_site.py | 38 ++++++++++++++++++++++++-------------- templates/main.html | 4 +++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/render_site.py b/src/render_site.py index 46a4565..c6a9b30 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -22,6 +22,7 @@ class PlayerEntry: player_name: str user_accounts: str score: int + rank: int @property def player_name_encoded(self): @@ -259,7 +260,10 @@ def get_rating_lists() -> Dict[int, List[PlayerEntry]]: } for rating_type in rating_types: cur.execute( - "SELECT * FROM (" + "SELECT" + " *," + " rank() OVER (ORDER BY current_rating DESC)" + "FROM (" " SELECT DISTINCT ON (user_accounts.user_id)" " player_name," " string_agg(user_accounts.username, %s ORDER BY user_accounts.username) AS user_accounts," @@ -275,38 +279,42 @@ def get_rating_lists() -> Dict[int, List[PlayerEntry]]: "ORDER BY current_rating DESC", (", ", rating_type) ) - for (player_name, user_accounts, current_rating) in cur.fetchall(): - leaderboard[rating_type].append(PlayerEntry(player_name, user_accounts, round(current_rating))) + for (player_name, user_accounts, current_rating, rank) in cur.fetchall(): + leaderboard[rating_type].append(PlayerEntry(player_name, user_accounts, round(current_rating), rank)) return leaderboard -def get_leaders(rating_lists: Dict, streak_lists: Dict) -> Dict[int, Dict[str, Leader]]: +def get_leaders(rating_lists: Dict[int, List[PlayerEntry]], streak_lists: Dict[int, List[PlayerStreakEntry]]) -> Dict[int, Dict[str, List[Leader]]]: leaders = {} for rating_type, rating_list in rating_lists.items(): if len(rating_list) != 0: - leader = rating_list[0] leaders[rating_type] = { - 'Player Rating': Leader('Highest Rating', utils.describe_rating_type(rating_type), leader) + 'Player Rating': [ + Leader('Highest Rating', utils.describe_rating_type(rating_type), leader) + for leader in rating_list if leader.rank == 1 + ] } else: leaders[rating_type] = { - 'Player Rating': Leader('Highest Rating', utils.describe_rating_type(rating_type), PlayerEntry('-', '', 0)) + 'Player Rating': [Leader('Highest Rating', utils.describe_rating_type(rating_type), PlayerEntry('-', '', 0, 1))] } for rating_type, streak_list in streak_lists.items(): if len(streak_list) != 0: - leader = streak_list[0] if rating_type not in leaders.keys(): leaders[rating_type] = {} - leaders[rating_type]['Top Streak'] = Leader('Longest Win Streak', utils.describe_rating_type(rating_type), leader) + leaders[rating_type]['Top Streak'] = [ + Leader('Longest Win Streak', utils.describe_rating_type(rating_type), leader) + for leader in streak_list if leader.rank == 1 + ] else: leaders[rating_type]['Top Streak'] = \ - Leader('Longest Win Streak', utils.describe_rating_type(rating_type), PlayerEntry('-', '', 0)) + [Leader('Longest Win Streak', utils.describe_rating_type(rating_type), PlayerEntry('-', '', 0, 1))] return leaders -def get_streak_list(): +def get_streak_list() -> Dict[int, List[PlayerStreakEntry]]: cur = conn_manager.get_connection().cursor(cursor_factory=psycopg2.extras.DictCursor) rating_types = [utils.get_rating_type(x) for x in [False, True]] leaderboard = { @@ -317,6 +325,7 @@ def get_streak_list(): cur.execute( "SELECT" " player_name," + " rank() over (order by maximum_streak DESC) AS rank," " user_accounts," " maximum_streak," " current_streak " @@ -337,11 +346,12 @@ def get_streak_list(): " ) AS streaks " # As a tiebreak, the streak that was obtained first will be shown. # If this is still the same, then we just use player name for determinancy - "ORDER BY maximum_streak DESC, maximum_streak_last_game ASC, player_name", + "ORDER BY maximum_streak DESC, current_streak DESC, player_name", (", ", rating_type) ) - for (player_name, user_accounts, max_streak, current_streak) in cur.fetchall(): - leaderboard[rating_type].append(PlayerStreakEntry(player_name, user_accounts, max_streak, current_streak)) + for (player_name, rank, user_accounts, max_streak, current_streak) in cur.fetchall(): + leaderboard[rating_type].append(PlayerStreakEntry(player_name, user_accounts, max_streak, rank, current_streak)) + print(leaderboard) return leaderboard diff --git a/templates/main.html b/templates/main.html index 989a066..1f2ebf5 100644 --- a/templates/main.html +++ b/templates/main.html @@ -50,7 +50,8 @@ {% for rating_type, leaders in leaders.items() %}
- {% for category, data in leaders.items() %} + {% for category, leaders in leaders.items() %} + {% for data in leaders %}
{{ data.title }}
@@ -63,6 +64,7 @@
{% endfor %} + {% endfor %}
{% endfor %}