From e01b7fe82350052fbfef09903f8b32a0508f8b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 26 Dec 2023 11:54:40 +0100 Subject: [PATCH] Add time stamps to games in tables --- install/database_schema.sql | 2 ++ src/fetch_games.py | 40 ++++++++++++++++++------------------- src/render_site.py | 34 +++++++++++++++++++++++++------ templates/layout.html | 2 ++ templates/stats_table.html | 5 +++++ 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/install/database_schema.sql b/install/database_schema.sql index 8ac697a..f468e43 100644 --- a/install/database_schema.sql +++ b/install/database_schema.sql @@ -133,6 +133,8 @@ CREATE TABLE games ( seed TEXT NOT NULL, score SMALLINT NOT NULL, num_turns SMALLINT NOT NULL, + datetime_started TIMESTAMPTZ NOT NULL, + datetime_finished TIMESTAMPTZ NOT NULL, /** * This is the league id mentioned above that will represent the ordering of games regarding being processed by ELO. * Note that this means when fetching new data from hanab.live, we have to fetch *all* of it and insert the games sorted diff --git a/src/fetch_games.py b/src/fetch_games.py index fea64d9..a6c8df8 100644 --- a/src/fetch_games.py +++ b/src/fetch_games.py @@ -1,4 +1,5 @@ import json +from dataclasses import dataclass from typing import Dict, List, Optional import platformdirs @@ -28,17 +29,18 @@ session = requests_cache.CachedSession( } ) +@dataclass class GameInfo: - def __init__(self, game_id: int, num_players: int, variant_id: int, seed: str, score: int, num_turns: int, - user_ids: List[int], normalized_usernames: List[str]): - self.game_id = game_id - self.num_players = num_players - self.variant_id = variant_id - self.seed = seed - self.score = score - self.num_turns = num_turns - self.user_ids = user_ids - self.normalized_usernames = normalized_usernames + game_id: int + num_players: int + variant_id: int + seed: str + score: int + num_turns: int + user_ids: List[int] + normalized_usernames: List[str] + datetime_started: str + datetime_ended: str def fetch_games_for_player(username: str, latest_game_id: int): @@ -64,6 +66,7 @@ def process_game_entry(game_json: Dict, username_dict: Dict, variant_ids: List[i score = game_json["score"] num_turns = game_json["numTurns"] start_time = game_json["datetimeStarted"] + end_time = game_json["datetimeFinished"] game_options = game_json["options"] var_id = game_options["variantID"] @@ -93,7 +96,7 @@ def process_game_entry(game_json: Dict, username_dict: Dict, variant_ids: List[i return user_ids.append(user_id) - return GameInfo(game_id, num_players, var_id, seed, score, num_turns, user_ids, normalized_usernames) + return GameInfo(game_id, num_players, var_id, seed, score, num_turns, user_ids, normalized_usernames, start_time, end_time) def fetch_games_for_all_players(): @@ -145,7 +148,7 @@ def store_new_games(games: Dict[int, GameInfo]): # Now, iterate over all games and convert to tuples for insertion for game in sorted(games.values(), key=lambda game_info: game_info.game_id): - tup = (game.game_id, game.num_players, game.variant_id, game.seed, game.score, game.num_turns) + tup = (game.game_id, game.num_players, game.variant_id, game.seed, game.score, game.num_turns, game.datetime_started, game.datetime_ended) games_vals.append(tup) for player_id in game.user_ids: tup = (game.game_id, player_id) @@ -160,7 +163,7 @@ def store_new_games(games: Dict[int, GameInfo]): # (for example, because we forced a download refresh) psycopg2.extras.execute_values( cur, - "INSERT INTO games (id, num_players, variant_id, seed, score, num_turns) " + "INSERT INTO games (id, num_players, variant_id, seed, score, num_turns, datetime_started, datetime_finished) " "VALUES %s " "ON CONFLICT (id) DO NOTHING", games_vals @@ -190,6 +193,7 @@ def store_new_games(games: Dict[int, GameInfo]): def detailed_fetch_game(game_id: int) -> bool: """ Fetches full game details from the server and stores it in local DB if this game is a league game. + @warning: Game data has to be present in database already, game details will then be added. @param game_id: Game ID from hanab.live @return: Whether the processed game was accepted as a league game, i.e. inserted into the DB """ @@ -242,14 +246,8 @@ def detailed_fetch_game(game_id: int) -> bool: conn = conn_manager.get_connection() cur = conn_manager.get_new_cursor() - # Insert the game into the database if not present - cur.execute("INSERT INTO games " - "(id, num_players, variant_id, seed, score, num_turns) " - "VALUES " - "(%s, %s, %s, %s, %s, %s) " - "ON CONFLICT (id) DO NOTHING", - (game_id, num_players, var_id, seed, game.score, len(actions))) - # TODO Max: Check if len(actions) is the correct number here, in case there was a VTK action, we might want to subtract one + # Note that we assume the game is present in the database already + game_participants_vals = [] for seat, user_id in enumerate(user_ids): diff --git a/src/render_site.py b/src/render_site.py index b73f942..934c9ce 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -7,6 +7,9 @@ import urllib.parse import jinja2 import datetime import psycopg2.extras +import requests_cache +import platformdirs + import stats import constants @@ -17,6 +20,7 @@ from dataclasses import dataclass from database import conn_manager + @dataclass class PlayerEntry: player_name: str @@ -110,6 +114,7 @@ class GameRow: seed: str score: int num_turns: int + datetime_finished: datetime.datetime league_id: int num_bdrs: int num_crits_lost: int @@ -173,6 +178,7 @@ def get_games() -> List[GameRow]: " seed," " score," " num_turns," + " datetime_finished," " game_data.league_id," " num_bottom_deck_risks AS num_bdrs," " num_crits_lost," @@ -192,6 +198,7 @@ def get_games() -> List[GameRow]: " seed," " score," " num_turns," + " datetime_finished," " games.league_id," " num_bottom_deck_risks," " num_crits_lost " @@ -217,7 +224,7 @@ def get_games() -> List[GameRow]: " ON variant_ratings.league_id = game_data.league_id AND variant_ratings.primary_change = true " "GROUP BY (" " game_data.game_id, game_data.num_players, users, user_ids, game_data.variant_id, variants.name, seed, score, num_turns," - " game_data.league_id, num_bottom_deck_risks, num_crits_lost, change, value_after, rating_type, user_rating_changes, user_ratings_after" + " game_data.league_id, num_bottom_deck_risks, num_crits_lost, change, value_after, rating_type, user_rating_changes, user_ratings_after, datetime_finished" " ) " "ORDER BY league_id DESC", (True,) @@ -225,11 +232,13 @@ def get_games() -> List[GameRow]: res = [] for row in cur.fetchall(): row['game_outcomes'] = [stats.GameOutcome(outcome).string for outcome in row['game_outcomes']] - if row['variant_rating_change'] is not None: - row['variant_rating_change'] = round(row['variant_rating_change'], 2) - if row['variant_rating_after'] is not None: - row['variant_rating_after'] = round(row['variant_rating_after'], 2) - res.append(GameRow(**row)) + game_row = GameRow(**row) + if game_row.variant_rating_change is not None: + game_row.variant_rating_change = round(game_row.variant_rating_change, 2) + if game_row.variant_rating_after is not None: + game_row.variant_rating_after = round(game_row.variant_rating_after, 2) + game_row.datetime_finished = game_row.datetime_finished.astimezone(datetime.timezone.utc).isoformat() + res.append(game_row) return res @@ -588,6 +597,18 @@ def render_main_site(env: jinja2.Environment, out_dir: Path): shutil.copytree('deps/tabulator/dist/css', 'build/css', dirs_exist_ok=True) shutil.copytree('deps/tabulator/dist/js', 'build/js', dirs_exist_ok=True) + session = requests_cache.CachedSession( + platformdirs.user_cache_dir(constants.APP_NAME) + '/luxon.cache', + urls_expire_after={ + '*': requests_cache.NEVER_EXPIRE, + } + ) + response = session.get("https://moment.github.io/luxon/global/luxon.min.js") + if not response.status_code == 200: + raise ValueError("Failed to get luxon library.") + with open('build/js/luxon.min.js', 'w') as file: + file.write(response.text) + def render_variant_pages(env: jinja2.Environment, out_dir: Path): variant_template = env.get_template('variant.html') @@ -621,6 +642,7 @@ def render_player_pages(env: jinja2.Environment, out_dir: Path): games = get_games() games_grouped_by_player = group_games_by_player(games) + print(games_grouped_by_player) player_template = env.get_template('player.html') for player_name, player_stat in player_stats.items(): diff --git a/templates/layout.html b/templates/layout.html index 253fbfd..6639961 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -17,6 +17,8 @@ + + {% block navbar %}{% endblock %} {% block content %}{% endblock %} diff --git a/templates/stats_table.html b/templates/stats_table.html index 61106bd..df063ec 100644 --- a/templates/stats_table.html +++ b/templates/stats_table.html @@ -75,6 +75,11 @@ var table_{{div_id}} = new Tabulator("#table-{{div_id}}", { urlPrefix: "https://hanab.live/replay/", target:"_blank" }}, + {title: "Played", field: "datetime_finished", formatter: "datetime", formatterParams:{ + inputFormat: "iso", + timezone: "system", + outputFormat: "dd/MM HH:mm" + }}, {% if show_player_num %} {title: "# Players", field: "num_players"}, {% endif %}