diff --git a/src/render_site.py b/src/render_site.py index b7ae60a..9d62fac 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -1,7 +1,8 @@ from pathlib import Path -from typing import Dict +from typing import Dict, List import jinja2 +import datetime import psycopg2.extras import constants @@ -12,14 +13,6 @@ from dataclasses import dataclass from database import conn_manager -@dataclass -class Leader: - title: str - player_name: str - user_accounts: str - score: int - - @dataclass class PlayerEntry: player_name: str @@ -27,44 +20,52 @@ class PlayerEntry: score: int -def get_rating_lists(): - cur = conn_manager.get_connection().cursor(cursor_factory=psycopg2.extras.DictCursor) - cur.execute( - "SELECT * FROM (" - " SELECT DISTINCT ON (type, user_accounts.user_id)" - " type," - " player_name," - " string_agg(user_accounts.username, %s ORDER BY user_accounts.username) AS user_accounts," - " value_after AS current_rating" - " FROM user_ratings " - " INNER JOIN users " - " ON user_ratings.user_id = users.id " - " LEFT OUTER JOIN user_accounts " - " ON users.id = user_accounts.user_id " - " GROUP BY (user_accounts.user_id, player_name, value_after, league_id, type) " - " ORDER BY type, user_accounts.user_id, league_id DESC" - " ) AS ratings " - "ORDER BY type ASC, current_rating DESC", - (", ",) - ) +@dataclass +class Leader: + title: str + entry: PlayerEntry + + +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]] leaderboard = { utils.get_rating_type(x): [] for x in [True, False] } - for row in cur.fetchall(): - rating_type = row['type'] - leaderboard[rating_type].append(row) + for rating_type in rating_types: + cur.execute( + "SELECT * FROM (" + " SELECT DISTINCT ON (user_accounts.user_id)" + " player_name," + " string_agg(user_accounts.username, %s 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 " + "ORDER BY current_rating DESC", + (", ", rating_type, rating_type) + ) + for (player_name, user_accounts, current_rating) in cur.fetchall(): + leaderboard[rating_type].append(PlayerEntry(player_name, user_accounts, round(current_rating))) return leaderboard -def get_leaders(rating_lists: Dict): +def get_leaders(rating_lists: Dict) -> Dict[int, Dict[str, Leader]]: leaders = {} for rating_type, rating_list in rating_lists.items(): if len(rating_list) != 0: leader = rating_list[0] leaders[rating_type] = { - 'Player Rating': Leader('Highest Rating', leader['player_name'], leader['user_accounts'], round(leader['current_rating'])) + 'Player Rating': Leader('Highest Rating', leader) } return leaders @@ -87,11 +88,18 @@ def get_streak_leaderboards(): def get_total_games(): cur = conn_manager.get_new_cursor() - cur.execute("SELECT MAX(league_id) FROM games") + cur.execute("SELECT COUNT(league_id) FROM games") (num_games,) = cur.fetchone() return num_games +def get_num_players(): + cur = conn_manager.get_new_cursor() + cur.execute("SELECT COUNT(id) FROM users") + (num_users,) = cur.fetchone() + return num_users + + def render_leaderboard(): rating_lists = get_rating_lists() leaders = get_leaders(rating_lists) @@ -105,13 +113,11 @@ def render_leaderboard(): # rendered_html = template.render(leaders=leaders, leaderboards=leaderboards, variants=variants) rendered_html = template.render( leaders=leaders, - leaderboards=leaderboards - # leaders=leaders, - # leaderboards=leaderboards, + leaderboards=leaderboards, + total_games_played=get_total_games(), + total_players=get_num_players(), + latest_run=datetime.datetime.now().isoformat() # variants=variants, - # total_games_played=constants['total_games_played'], - # latest_run=latest_run_utc_formatted, - # total_players=total_players ) output_file = Path(constants.WEBSITE_OUTPUT_DIRECTORY) / 'index.html' output_file.parent.mkdir(exist_ok=True, parents=True) diff --git a/templates/content.html b/templates/content.html index 746eb0f..c7a98dd 100644 --- a/templates/content.html +++ b/templates/content.html @@ -10,9 +10,9 @@
{{ data.title }}
-

{{ data.player_name }}

-

{{ data.user_accounts }}

-

{{ data.score }}

+

{{ data.entry.player_name }}

+

{{ data.entry.user_accounts }}

+

{{ data.entry.score }}

{% endfor %} @@ -30,13 +30,13 @@
- {% for row in leaderboard[rating_type] %} + {% for entry in leaderboard[rating_type] %} - + {% endfor %}
-
{{ row['player_name'] }}
-
{{ row['user_accounts'] }}
+
{{ entry.player_name }}
+
{{ entry.user_accounts }}
{{ row['current_rating'] }}{{ entry.score }}
diff --git a/templates/layout.html b/templates/layout.html index c261608..3c23195 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -10,7 +10,7 @@