From fec81a6c155c68403fa564fddd2758915e8a157e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 3 Dec 2023 21:19:36 +0100 Subject: [PATCH] start website implementation --- install/database_schema.sql | 2 +- requirements.txt | 1 + src/constants.py | 3 ++ src/render_site.py | 84 +++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/render_site.py diff --git a/install/database_schema.sql b/install/database_schema.sql index 72a72db..6ae7c03 100644 --- a/install/database_schema.sql +++ b/install/database_schema.sql @@ -293,7 +293,7 @@ CREATE TABLE user_ratings ( * I would use the league_id here for proper ordering. * Also note that this can then be used to identify whether a given league game has already been processed for rating change. */ - league_id INTEGER REFERENCES games (league_id), + league_id INTEGER NOT NULL REFERENCES games (league_id), user_id INTEGER NOT NULL, type SMALLINT NOT NULL, diff --git a/requirements.txt b/requirements.txt index 3666148..1f30fd8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ unidecode requests requests_cache termcolor +jinja2 diff --git a/src/constants.py b/src/constants.py index a21feb8..c7de618 100644 --- a/src/constants.py +++ b/src/constants.py @@ -47,3 +47,6 @@ USER_HISTORY_CACHE_TIME = 5 * 60 # Fraction of seeds which is assumed to be unwinnable UNWINNABLE_SEED_FRACTION = 0.02 + + +WEBSITE_OUTPUT_DIRECTORY = 'build' diff --git a/src/render_site.py b/src/render_site.py new file mode 100644 index 0000000..db3030c --- /dev/null +++ b/src/render_site.py @@ -0,0 +1,84 @@ +import jinja2 +import psycopg2.extras + +import constants +import utils +import ratings + +from database import conn_manager + + +def get_rating_leaderboards(): + 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", + (", ",) + ) + 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_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 MAX(league_id) FROM games") + (num_games, ) = cur.fetchone() + return num_games + + +def render_leaderboard(): + novar_leaderboard, clue_starved_leaderboard = get_rating_leaderboards() + + 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( +# leaders=leaders, +# leaderboards=leaderboards, +# variants=variants, +# total_games_played=constants['total_games_played'], +# latest_run=latest_run_utc_formatted, +# total_players=total_players + ) + with open(constants.WEBSITE_OUTPUT_DIRECTORY / 'index.html') as f: + f.write(rendered_html) + + +get_leaderboards() +#render_leaderboard()