From 9a8318bc6ded8fc2d27ed0ab4e84d51052e72ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 23 Nov 2023 18:31:59 +0100 Subject: [PATCH] Update database schemea: add stats --- install/database_schema.sql | 41 +++++++++++++++++++++++++++++-------- src/statistics.py | 11 ++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/install/database_schema.sql b/install/database_schema.sql index d51724f..00fdaf8 100644 --- a/install/database_schema.sql +++ b/install/database_schema.sql @@ -272,7 +272,7 @@ CREATE TABLE variant_ratings ( DROP TABLE IF EXISTS user_base_ratings CASCADE; CREATE TABLE user_base_ratings ( - user_id SMALLINT, + user_id INTEGER, /** * Since a user has different ratings now, this should represent which of the ratings we mean. * For now, I suggest 0 = NoVar, 1 = ClueStarved. @@ -295,7 +295,7 @@ CREATE TABLE user_ratings ( */ league_id INTEGER REFERENCES games (league_id), - user_id SMALLINT NOT NULL, + user_id INTEGER NOT NULL, type SMALLINT NOT NULL, /** @@ -316,13 +316,38 @@ CREATE TABLE user_ratings ( /* TABLES RELATED TO STATISTICS */ /** This is a rough outline, not really happy with it right now. */ -DROP TABLE IF EXISTS statistics CASCADE; -CREATE TABLE statistics ( - game_id INTEGER PRIMARY KEY, +DROP TABLE IF EXISTS game_statistics CASCADE; +CREATE TABLE game_statistics ( + game_id INTEGER PRIMARY KEY REFERENCES games (id), /** I'd say all of the following can just be null in case we have not evaluated them yet. */ - bdr SMALLINT, - moves SMALLINT, - strikeout BOOLEAN + bottom_deck_risk SMALLINT, + num_crits_lost SMALLINT +); + +/** + * Need a new table here, since a single game might have several outcomes: + * Think of losing a crit and striking out, for example + */ +DROP TABLE IF EXISTS game_outcomes CASCADE; +CREATE TABLE game_outcomes ( + game_id INTEGER REFERENCES games (id), + /** This stores the game outcome, corresponding to the values of the statistics.GameOutcome enum */ + outcome SMALLINT +); + +DROP TABLE IF EXISTS user_statistics; +CREATE TABLE user_statistics ( + user_id INTEGER NOT NULL REFERENCES users (id), + /** We track stats separately for each variant type */ + variant_type SMALLINT NOT NULL, + current_streak INTEGER NOT NULL, + games_played INTEGER NOT NULL, + games_won INTEGER NOT NULL, + total_bdr INTEGER NOT NULL, + /** Number of critical cards that were either discarded or misplayed */ + total_crits_lots INTEGER NOT NULL, + total_game_moves INTEGER NOT NULL, + PRIMARY KEY (user_id, variant_type) ); /** diff --git a/src/statistics.py b/src/statistics.py index f56311e..826047e 100644 --- a/src/statistics.py +++ b/src/statistics.py @@ -3,6 +3,8 @@ from typing import List, Tuple from hanabi import hanab_game +from database import conn_manager + class GameOutcome(enum.Enum): win = 0 @@ -59,3 +61,12 @@ def analyze_game(instance: hanab_game.HanabiInstance, actions: List[hanab_game.A outcomes.append(GameOutcome.win) return GameAnalysisResult(outcomes, bdrs) + + +def update_user_statistics(user_ids: List[int]): + """ + Update the cumulative user statistics for this user, assuming that the corresponding game statistics have + been computed already. + @param user_ids: + @return: + """