hanabi-league/src/render_site.py

129 lines
3.9 KiB
Python
Raw Normal View History

2023-12-04 00:40:29 +01:00
from pathlib import Path
from typing import Dict, List
2023-12-04 00:40:29 +01:00
2023-12-03 21:19:36 +01:00
import jinja2
import datetime
2023-12-03 21:19:36 +01:00
import psycopg2.extras
import constants
import utils
import ratings
2023-12-04 00:40:29 +01:00
from dataclasses import dataclass
2023-12-03 21:19:36 +01:00
from database import conn_manager
2023-12-04 00:40:29 +01:00
@dataclass
class PlayerEntry:
2023-12-04 00:40:29 +01:00
player_name: str
user_accounts: str
score: int
@dataclass
class Leader:
title: str
entry: PlayerEntry
2023-12-04 00:40:29 +01:00
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]]
2023-12-04 00:40:29 +01:00
leaderboard = {
utils.get_rating_type(x): []
for x in [True, False]
}
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)))
2023-12-03 21:19:36 +01:00
2023-12-04 00:40:29 +01:00
return leaderboard
def get_leaders(rating_lists: Dict) -> Dict[int, Dict[str, Leader]]:
2023-12-04 00:40:29 +01:00
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)
2023-12-04 00:40:29 +01:00
}
return leaders
2023-12-03 21:19:36 +01:00
def get_streak_leaderboards():
cur = conn_manager.get_connection().cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute(
"SELECT * FROM "
)
novar_leaderboard = []
clue_starved_leaderboard = []
for row in cur.fetchall():
if row['type'] == utils.get_rating_type(False):
novar_leaderboard.append(row)
else:
clue_starved_leaderboard.append(row)
return novar_leaderboard, clue_starved_leaderboard
def get_total_games():
cur = conn_manager.get_new_cursor()
cur.execute("SELECT COUNT(league_id) FROM games")
2023-12-04 00:40:29 +01:00
(num_games,) = cur.fetchone()
2023-12-03 21:19:36 +01:00
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
2023-12-03 21:19:36 +01:00
def render_leaderboard():
2023-12-04 00:40:29 +01:00
rating_lists = get_rating_lists()
leaders = get_leaders(rating_lists)
leaderboards = {
'Player Rating': rating_lists
}
2023-12-03 21:19:36 +01:00
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
template = env.get_template('content.html')
# rendered_html = template.render(leaders=leaders, leaderboards=leaderboards, variants=variants)
rendered_html = template.render(
2023-12-04 00:40:29 +01:00
leaders=leaders,
leaderboards=leaderboards,
total_games_played=get_total_games(),
total_players=get_num_players(),
latest_run=datetime.datetime.now().isoformat()
2023-12-04 00:40:29 +01:00
# variants=variants,
2023-12-03 21:19:36 +01:00
)
2023-12-04 00:40:29 +01:00
output_file = Path(constants.WEBSITE_OUTPUT_DIRECTORY) / 'index.html'
output_file.parent.mkdir(exist_ok=True, parents=True)
with open(output_file, 'w') as f:
2023-12-03 21:19:36 +01:00
f.write(rendered_html)
2023-12-04 00:40:29 +01:00
render_leaderboard()