From dd13cd4ae90951b524f80f983ff3af0ad7288feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 11 Dec 2023 00:16:52 +0100 Subject: [PATCH] player pages: add rating, fix bug on rating type --- src/render_site.py | 70 +++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/src/render_site.py b/src/render_site.py index 82caf3b..9b2fde2 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -12,7 +12,6 @@ import stats import constants import config import utils -import ratings from dataclasses import dataclass from database import conn_manager @@ -122,6 +121,7 @@ class Player: class PlayerStats(GeneralStats): current_streak: int = 0 max_streak: int = 0 + rating: int = 0 @dataclass @@ -353,31 +353,51 @@ def get_variant_rows() -> List[VariantRow]: ] -def get_player_stats() -> Dict[int, Dict[int, PlayerStats]]: +def get_player_stats() -> Dict[str, Dict[int, PlayerStats]]: cur = conn_manager.get_new_cursor() - cur.execute( - "SELECT " - " user_id," - " player_name," - " variant_type," - " games_played," - " games_won," - " total_bdr," - " total_crits_lots," - " total_game_moves," - " current_streak," - " maximum_streak " - "FROM user_statistics " - "INNER JOIN users " - " ON user_statistics.user_id = users.id " - "ORDER BY user_id, variant_type" - ) ret = {} - for (user_id, player_name, variant_type, games_played, games_won, total_bdr, total_crits_lost, total_game_moves, current_streak, maximum_streak) in cur.fetchall(): - if player_name not in ret.keys(): - ret[player_name] = {} - ret[player_name][variant_type] = PlayerStats(games_played, games_won, total_bdr, total_crits_lost, total_game_moves, current_streak, maximum_streak) - + rating_types = [utils.get_rating_type(x) for x in [False, True]] + for rating_type in rating_types: + cur.execute( + "SELECT " + " users.id," + " player_name," + " games_played," + " games_won," + " total_bdr," + " total_crits_lots," + " total_game_moves," + " current_streak," + " maximum_streak," + " user_accounts," + " current_rating " + "FROM user_statistics " + "INNER JOIN users " + " ON user_statistics.user_id = users.id AND user_statistics.variant_type = %s " + "INNER JOIN (" + " SELECT DISTINCT ON (user_accounts.user_id)" + " user_accounts.user_id," + " array_agg(user_accounts.username ORDER BY user_accounts.username) AS user_accounts," + " COALESCE(value_after, rating) AS current_rating" + " FROM users " + " LEFT OUTER JOIN user_ratings " + " ON user_ratings.user_id = users.id AND user_ratings.type = %s " + " LEFT OUTER JOIN user_accounts " + " ON users.id = user_accounts.user_id " + " INNER JOIN user_base_ratings " + " ON users.id = user_base_ratings.user_id AND user_base_ratings.type = %s" + " GROUP BY (user_accounts.user_id, player_name, value_after, league_id, rating) " + " ORDER BY user_accounts.user_id, league_id DESC" + " ) AS ratings " + " ON ratings.user_id = users.id " + "ORDER BY users.id, variant_type", + (rating_type, rating_type, rating_type) + ) + for (user_id, player_name, games_played, games_won, total_bdr, total_crits_lost, total_game_moves, current_streak, maximum_streak, user_accounts, current_rating) in cur.fetchall(): + if player_name not in ret.keys(): + ret[player_name] = {} + row = PlayerRow(user_id, player_name, user_accounts, PlayerStats(games_played, games_won, total_bdr, total_crits_lost, total_game_moves, current_streak, maximum_streak, round(current_rating, 1))) + ret[player_name][rating_type] = row return ret @@ -483,7 +503,7 @@ def render_leaderboard(): player_stat=player_stat ) - output_file = out_dir / 'player' / str(player_name) / 'index.html' + output_file = out_dir / 'player' / urllib.parse.quote_plus(player_name) / 'index.html' output_file.parent.mkdir(exist_ok=True, parents=True) with open(output_file, 'w') as f: