diff --git a/src/render_site.py b/src/render_site.py index 9d62fac..a29c1c2 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -59,7 +59,7 @@ def get_rating_lists() -> Dict[int, List[PlayerEntry]]: return leaderboard -def get_leaders(rating_lists: Dict) -> Dict[int, Dict[str, Leader]]: +def get_leaders(rating_lists: Dict, streak_lists: Dict) -> Dict[int, Dict[str, Leader]]: leaders = {} for rating_type, rating_list in rating_lists.items(): if len(rating_list) != 0: @@ -67,23 +67,42 @@ def get_leaders(rating_lists: Dict) -> Dict[int, Dict[str, Leader]]: leaders[rating_type] = { 'Player Rating': Leader('Highest Rating', leader) } + 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', leader) return leaders -def get_streak_leaderboards(): +def get_streak_lists(): cur = conn_manager.get_connection().cursor(cursor_factory=psycopg2.extras.DictCursor) - cur.execute( - "SELECT * FROM " - ) - novar_leaderboard = [] - clue_starved_leaderboard = [] - for row in cur.fetchall(): - if row['type'] == utils.get_rating_type(False): - novar_leaderboard.append(row) - else: - clue_starved_leaderboard.append(row) - - return novar_leaderboard, clue_starved_leaderboard + rating_types = [utils.get_rating_type(x) for x in [False, True]] + leaderboard = { + utils.get_rating_type(x): [] + for x in [True, False] + } + for rating_type in rating_types: + cur.execute( + "SELECT * FROM (" + " SELECT " + " player_name," + " string_agg(user_accounts.username, %s ORDER BY user_accounts.username) AS user_accounts," + " COALESCE(maximum_streak, %s) AS maximum_streak" + " FROM users " + " LEFT OUTER JOIN user_statistics" + " ON users.id = user_statistics.user_id AND variant_type = %s" + " LEFT OUTER JOIN user_accounts " + " ON users.id = user_accounts.user_id " + " GROUP BY (user_accounts.user_id, player_name, maximum_streak) " + " ) AS streaks " + "ORDER BY maximum_streak DESC", + (", ", 0, rating_type) + ) + for (player_name, user_accounts, maximum_streak) in cur.fetchall(): + leaderboard[rating_type].append(PlayerEntry(player_name, user_accounts, maximum_streak)) + return leaderboard def get_total_games(): @@ -102,10 +121,12 @@ def get_num_players(): def render_leaderboard(): rating_lists = get_rating_lists() - leaders = get_leaders(rating_lists) + streak_lists = get_streak_lists() + leaders = get_leaders(rating_lists, streak_lists) leaderboards = { - 'Player Rating': rating_lists + 'Player Rating': rating_lists, + 'Maximum Streak': streak_lists } env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates')) @@ -124,5 +145,4 @@ def render_leaderboard(): with open(output_file, 'w') as f: f.write(rendered_html) - render_leaderboard()