From 181dd35ea657aa2c5e4d0db9ced3f38a7e4d7f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 26 Dec 2023 14:11:53 +0100 Subject: [PATCH] Revert "Add time stamps to games in tables" This reverts commit 41e8fb9c85f44e75240f94b335938556e6ca78ee. --- install/database_schema.sql | 2 -- src/fetch_games.py | 40 +++++++++++++++++++------------------ src/render_site.py | 33 ++++++------------------------ templates/layout.html | 2 -- templates/stats_table.html | 5 ----- 5 files changed, 27 insertions(+), 55 deletions(-) diff --git a/install/database_schema.sql b/install/database_schema.sql index f468e43..8ac697a 100644 --- a/install/database_schema.sql +++ b/install/database_schema.sql @@ -133,8 +133,6 @@ 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 a6c8df8..fea64d9 100644 --- a/src/fetch_games.py +++ b/src/fetch_games.py @@ -1,5 +1,4 @@ import json -from dataclasses import dataclass from typing import Dict, List, Optional import platformdirs @@ -29,18 +28,17 @@ session = requests_cache.CachedSession( } ) -@dataclass class GameInfo: - 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 __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 def fetch_games_for_player(username: str, latest_game_id: int): @@ -66,7 +64,6 @@ 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"] @@ -96,7 +93,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, start_time, end_time) + return GameInfo(game_id, num_players, var_id, seed, score, num_turns, user_ids, normalized_usernames) def fetch_games_for_all_players(): @@ -148,7 +145,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, game.datetime_started, game.datetime_ended) + tup = (game.game_id, game.num_players, game.variant_id, game.seed, game.score, game.num_turns) games_vals.append(tup) for player_id in game.user_ids: tup = (game.game_id, player_id) @@ -163,7 +160,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, datetime_started, datetime_finished) " + "INSERT INTO games (id, num_players, variant_id, seed, score, num_turns) " "VALUES %s " "ON CONFLICT (id) DO NOTHING", games_vals @@ -193,7 +190,6 @@ 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 """ @@ -246,8 +242,14 @@ def detailed_fetch_game(game_id: int) -> bool: conn = conn_manager.get_connection() cur = conn_manager.get_new_cursor() - # Note that we assume the game is present in the database already - + # 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 game_participants_vals = [] for seat, user_id in enumerate(user_ids): diff --git a/src/render_site.py b/src/render_site.py index 211c202..b73f942 100644 --- a/src/render_site.py +++ b/src/render_site.py @@ -7,9 +7,6 @@ import urllib.parse import jinja2 import datetime import psycopg2.extras -import requests_cache -import platformdirs - import stats import constants @@ -20,7 +17,6 @@ from dataclasses import dataclass from database import conn_manager - @dataclass class PlayerEntry: player_name: str @@ -114,7 +110,6 @@ class GameRow: seed: str score: int num_turns: int - datetime_finished: datetime.datetime league_id: int num_bdrs: int num_crits_lost: int @@ -178,7 +173,6 @@ 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," @@ -198,7 +192,6 @@ def get_games() -> List[GameRow]: " seed," " score," " num_turns," - " datetime_finished," " games.league_id," " num_bottom_deck_risks," " num_crits_lost " @@ -224,7 +217,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, datetime_finished" + " game_data.league_id, num_bottom_deck_risks, num_crits_lost, change, value_after, rating_type, user_rating_changes, user_ratings_after" " ) " "ORDER BY league_id DESC", (True,) @@ -232,13 +225,11 @@ def get_games() -> List[GameRow]: res = [] for row in cur.fetchall(): row['game_outcomes'] = [stats.GameOutcome(outcome).string for outcome in row['game_outcomes']] - 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) + 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)) return res @@ -597,18 +588,6 @@ 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') diff --git a/templates/layout.html b/templates/layout.html index 6639961..253fbfd 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -17,8 +17,6 @@ - - {% block navbar %}{% endblock %} {% block content %}{% endblock %} diff --git a/templates/stats_table.html b/templates/stats_table.html index df063ec..61106bd 100644 --- a/templates/stats_table.html +++ b/templates/stats_table.html @@ -75,11 +75,6 @@ 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 %}