include rating leaderboard

This commit is contained in:
Maximilian Keßler 2023-12-04 03:02:26 +01:00
parent e8ba8ecee3
commit 97f5a8459a
Signed by: max
GPG key ID: BCC5A619923C0BA5

View file

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