From 0b67cea089796f58bbfd2dc474011e5b45305b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 4 Dec 2023 22:02:08 +0100 Subject: [PATCH] Move leader cards to separate page. add current streaks --- css/leaderboards.css | 5 +++++ src/render_site.py | 45 +++++++++++++++++++++--------------------- templates/content.html | 36 ++++++++++++++++++++------------- templates/layout.html | 5 ++++- templates/variant.html | 20 +++++++++---------- 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/css/leaderboards.css b/css/leaderboards.css index a935c5e..35f8cfa 100644 --- a/css/leaderboards.css +++ b/css/leaderboards.css @@ -11,6 +11,11 @@ font-weight: bold; color: #17a2b8; /* Bootstrap's teal color */ } +.current-streak { + font-size: 1.5rem; + font-weight: bold; + color: #676767; /* Bootstrap's teal color */ +} .table td, .table th { vertical-align: middle; } diff --git a/src/render_site.py b/src/render_site.py index 7e17f6c..41977da 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -20,6 +20,11 @@ class PlayerEntry: score: int +@dataclass +class PlayerStreakEntry(PlayerEntry): + current_streak: int + + @dataclass class Leader: title: str @@ -59,6 +64,7 @@ class VariantStats: return 0 return round(float(self.total_moves) / self.games_played, 3) + @dataclass class VariantRow: variant_id: int @@ -117,7 +123,7 @@ def get_leaders(rating_lists: Dict, streak_lists: Dict) -> Dict[int, Dict[str, L return leaders -def get_stat_lists(stat_type: str, order_type: str = 'DESC', precision: int = 0, default: float | int = 0, percents: bool = False): +def get_streak_list(): cur = conn_manager.get_connection().cursor(cursor_factory=psycopg2.extras.DictCursor) rating_types = [utils.get_rating_type(x) for x in [False, True]] leaderboard = { @@ -128,27 +134,23 @@ def get_stat_lists(stat_type: str, order_type: str = 'DESC', precision: int = 0, cur.execute( "SELECT * FROM (" " SELECT " - " player_name," - " string_agg(user_accounts.username, %s ORDER BY user_accounts.username) AS user_accounts," - f" COALESCE({stat_type}, %s) AS value" - " 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 " - f" GROUP BY (user_accounts.user_id, player_name, {stat_type}) " + " player_name," + " STRING_AGG(user_accounts.username, %s ORDER BY user_accounts.username) AS user_accounts," + " COALESCE(maximum_streak, 0) AS maximum_streak," + " COALESCE(current_streak, 0) AS current_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, current_streak) " " ) AS streaks " # As a tiebreak, just sort by player name for now for determinancy - f"ORDER BY value {order_type}, player_name", - (", ", default, rating_type) + "ORDER BY maximum_streak, player_name", + (", ", rating_type) ) - for (player_name, user_accounts, value) in cur.fetchall(): - if percents: - value = round(100 * value, precision) - value = str(value) + ' %' - else: - value = round(value, precision) - leaderboard[rating_type].append(PlayerEntry(player_name, user_accounts, value)) + for (player_name, user_accounts, max_streak, current_streak) in cur.fetchall(): + leaderboard[rating_type].append(PlayerStreakEntry(player_name, user_accounts, max_streak, current_streak)) return leaderboard @@ -228,7 +230,7 @@ def build_variant_stats_per_variant(variant_rows: List[VariantRow]): def render_leaderboard(): rating_lists = get_rating_lists() - streak_lists = get_stat_lists("maximum_streak") + streak_lists = get_streak_list() leaders = get_leaders(rating_lists, streak_lists) variant_rows: List[VariantRow] = get_variant_rows() @@ -237,9 +239,6 @@ def render_leaderboard(): leaderboards = { 'Player Rating': rating_lists, 'Maximum Streak': streak_lists, - 'Current Streak': get_stat_lists("current_streak", order_type="DESC"), - 'Average Bottom Deck Risk': get_stat_lists("average_bdr", 'ASC', precision=2, default=float("inf")), - 'Winrate': get_stat_lists('winrate', order_type='DESC', precision=1, percents=True) } env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates')) diff --git a/templates/content.html b/templates/content.html index f39bfd4..195b884 100644 --- a/templates/content.html +++ b/templates/content.html @@ -1,22 +1,27 @@ {% extends "layout.html" %} {% block content %}
- {% for rating_type, leaders in leaders.items() %} -
-
- -
- {% for category, data in leaders.items() %} -
-
-
{{ data.title }}
-

{{ data.entry.player_name }}

-

{{ data.entry.user_accounts }}

-

{{ data.entry.score }}

-
+
+ +
+ {% for rating_type, leaders in leaders.items() %} + {% for category, data in leaders.items() %} +
+
+
{{ data.title }}
+

{{ data.entry.player_name }}

+

{{ data.entry.user_accounts }}

+

{{ data.entry.score }}

- {% endfor %}
+ {% endfor %} + {% endfor %} +
+
+ + {% for rating_type, leaders in leaders.items() %} +
+
@@ -37,6 +42,9 @@
{{ entry.user_accounts }}
{{ entry.score }} + {% if entry.current_streak is defined %} + ({{ entry.current_streak }}) + {% endif %} {% endfor %} diff --git a/templates/layout.html b/templates/layout.html index 3c23195..120708c 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -17,7 +17,10 @@