add summary stats

This commit is contained in:
Maximilian Keßler 2023-12-05 18:17:06 +01:00
parent 4461a680fd
commit 89a14b6bc1
Signed by: max
GPG key ID: BCC5A619923C0BA5
4 changed files with 60 additions and 21 deletions

View file

@ -67,7 +67,7 @@ body {
}
.stat-description {
display: inline-block;
width: 20em;
width: 16em;
}
.stat-list {
padding-left: 1em;

View file

@ -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'

View file

@ -3,7 +3,11 @@
<ul class="stat-list">
<li>
<span class="stat-description">Rating:</span>
{% if show_rating %}
{{stats.rating}}
{% else %}
-
{% endif %}
</li>
<li>
<span class="stat-description">Total Perfect Scores:</span>

View file

@ -12,7 +12,7 @@
<li class="nav-item">
<a class="nav-link active" id="overview-tab" data-toggle="tab" href="#overview">Overview</a>
</li>
{% for num_players in variant_stats.keys() %}
{% for num_players in variant_stats_by_player.keys() %}
<li class="nav-item">
<a class="nav-link" id="stats-{{num_players}}p-tab" data-toggle="tab" href="#stats-{{num_players}}p">{{num_players}} Players</a>
</li>
@ -25,13 +25,27 @@
{% block content %}
<div class="tab-content" id="myTabContent">
{% for num_players, stats in variant_stats.items() %}
<div class="tab-pane fade show active" id="overview">
<div class="container my-5">
<h3>
League Statistics for {{variant_name}}
</h3>
{% with stats=variant_stats, show_rating=False %}
{% include "stats_table.html" %}
{% endwith %}
</div>
</div>
{% for num_players, stats in variant_stats_by_player.items() %}
<div class="tab-pane fade" id="stats-{{num_players}}p">
<div class="container my-5">
<h3>
League Statistics for {{variant_name}} - {{num_players}} Players
</h3>
{% with show_rating=True %}
{% include "stats_table.html" %}
{% endwith %}
<h4>
List of Played Games
</h4>
@ -47,7 +61,7 @@
<!-- JavaScript part for the interactive tables -->
<script>
{% for num_players, games in variant_games.items() %}
{% for num_players, games in variant_games_by_player.items() %}
let tabledata_{{num_players}} = [
{% for game in games %}
{{game }},