From 89a14b6bc16589309c41d230a2105573d0d63009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 5 Dec 2023 18:17:06 +0100 Subject: [PATCH] add summary stats --- css/leaderboards.css | 2 +- src/render_site.py | 51 +++++++++++++++++++++++++++----------- templates/stats_table.html | 6 ++++- templates/variant.html | 22 +++++++++++++--- 4 files changed, 60 insertions(+), 21 deletions(-) diff --git a/css/leaderboards.css b/css/leaderboards.css index 2f70b9c..8f3539d 100644 --- a/css/leaderboards.css +++ b/css/leaderboards.css @@ -67,7 +67,7 @@ body { } .stat-description { display: inline-block; - width: 20em; + width: 16em; } .stat-list { padding-left: 1em; diff --git a/src/render_site.py b/src/render_site.py index b371a55..a22c9d9 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -37,12 +37,12 @@ class Leader: @dataclass class VariantStats: - rating: int - games_played: int - games_won: int - total_bdr: int - total_crits_lost: int - total_moves: int + rating: int = 0 + games_played: int = 0 + games_won: int = 0 + total_bdr: int = 0 + total_crits_lost: int = 0 + total_moves: int = 0 @property def winrate(self): @@ -68,6 +68,11 @@ class VariantStats: return 0 return round(float(self.total_moves) / self.games_played, 3) + def __iadd__(self, other): + for field in dataclasses.fields(self): + setattr(self, field.name, getattr(self, field.name) + getattr(other, field.name)) + return self + @dataclass class VariantRow: @@ -175,6 +180,15 @@ def group_games_by_var_id_and_num_players(games: List[GameRow]): return ret +def group_games_by_var_id(games: List[GameRow]): + ret = {} + for game in games: + if game.variant_id not in ret.keys(): + ret[game.variant_id] = [] + ret[game.variant_id].append(dataclasses.asdict(game)) + return ret + + def get_rating_lists() -> Dict[int, List[PlayerEntry]]: cur = conn_manager.get_connection().cursor() rating_types = [utils.get_rating_type(x) for x in [False, True]] @@ -317,17 +331,21 @@ def get_num_players(): return num_users -def build_variant_stats_per_variant(variant_rows: List[VariantRow]): +def build_variant_stats_by_variant(variant_rows: List[VariantRow]): + variant_stats_per_player = {} variant_stats = {} variant_names = {} for row in variant_rows: + if row.variant_id not in variant_stats_per_player.keys(): + variant_stats_per_player[row.variant_id] = {} if row.variant_id not in variant_stats.keys(): - variant_stats[row.variant_id] = {} + variant_stats[row.variant_id] = VariantStats() if row.variant_id not in variant_names.keys(): variant_names[row.variant_id] = {} - variant_stats[row.variant_id][row.num_players] = row.stats + variant_stats_per_player[row.variant_id][row.num_players] = row.stats variant_names[row.variant_id] = row.name - return variant_stats, variant_names + variant_stats[row.variant_id] += row.stats + return variant_stats, variant_stats_per_player, variant_names def render_leaderboard(): @@ -336,7 +354,7 @@ def render_leaderboard(): leaders = get_leaders(rating_lists, streak_lists) variant_rows: List[VariantRow] = get_variant_rows() - variant_stats, variant_names = build_variant_stats_per_variant(variant_rows) + variant_stats, variant_stats_by_player, variant_names = build_variant_stats_by_variant(variant_rows) leaderboards = { 'Player Rating': rating_lists, @@ -364,17 +382,20 @@ def render_leaderboard(): f.write(rendered_html) games = get_games() - grouped_games = group_games_by_var_id_and_num_players(games) + grouped_games_var_players = group_games_by_var_id_and_num_players(games) + grouped_games_var = group_games_by_var_id(games) variant_template = env.get_template('variant.html') - for variant_id, stats in variant_stats.items(): + for variant_id, by_player_stats in variant_stats_by_player.items(): rendered_var = variant_template.render( total_games_played=get_total_games(), total_players=get_num_players(), latest_run=datetime.datetime.now().isoformat(), - variant_stats=stats, + variant_stats_by_player=by_player_stats, + variant_stats=variant_stats.get(variant_id, VariantStats), variant_name=variant_names[variant_id], - variant_games=grouped_games.get(variant_id,{}) + variant_games_by_player=grouped_games_var_players.get(variant_id, {}), + variant_games=grouped_games_var.get(variant_id, []) ) output_file = out_dir / 'variant' / str(variant_id) / 'index.html' diff --git a/templates/stats_table.html b/templates/stats_table.html index 42ec306..2331b07 100644 --- a/templates/stats_table.html +++ b/templates/stats_table.html @@ -3,7 +3,11 @@