From 5e537423e93d6c885cff2b1263019ae3d6692ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 2 Mar 2023 13:03:05 +0100 Subject: [PATCH] add storing / loading of games to database --- database.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/database.py b/database.py index e3fa7c2..f4661e9 100644 --- a/database.py +++ b/database.py @@ -1,4 +1,5 @@ import psycopg2 +from typing import Optional ## global connection conn = psycopg2.connect("dbname=hanab-live user=postgres") @@ -6,6 +7,10 @@ conn = psycopg2.connect("dbname=hanab-live user=postgres") ## cursor cur = conn.cursor() +# cur.execute("DROP TABLE games;") +# conn.commit() +# exit(0) + ## check if table exists, else create it cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'games');") a = cur.fetchone() @@ -16,8 +21,8 @@ if a[0] is False: "CREATE TABLE games (" "id SERIAL PRIMARY KEY," "num_players SMALLINT NOT NULL," - "seed TEXT NOT NULL," "score SMALLINT NOT NULL," + "seed TEXT NOT NULL," "variant_id SMALLINT NOT NULL," "deck_plays BOOLEAN," "one_extra_card BOOLEAN," @@ -28,3 +33,66 @@ if a[0] is False: conn.commit() else: print("table already exists") + + +class Game(): + def __init__(self, info=None): + self.id = -1 + self.num_players = -1 + self.score = -1 + self.seed = "" + self.variant_id = -1 + self.deck_plays = None + self.one_extra_card = None + self.one_less_card = None + self.all_or_nothing = None + self.num_turns = None + if type(info) == dict: + self.__dict__.update(info) + + @staticmethod + def from_tuple(t): + g = Game() + g.id = t[0] + g.num_players = t[1] + g.score = t[2] + g.seed = t[3] + g.variant_id = t[4] + g.deck_plays = t[5] + g.one_extra_card = t[6] + g.one_less_card = t[7] + g.all_or_nothing = t[8] + g.num_turns = t[9] + return g + + def __eq__(self, other): + return self.__dict__ == other.__dict__ + + +def load(game_id: int) -> Optional[Game]: + cur.execute("SELECT * from games WHERE id = {};".format(game_id)) + a = cur.fetchone() + if a is None: + return None + else: + return Game.from_tuple(a) + +def store(game: Game): + stored = load(game.id) + if stored is None: + print("inserting game with id {} into DB".format(game.id)) + cur.execute( + "INSERT INTO games" + "(id, num_players, score, seed, variant_id)" + "VALUES" + "(%s, %s, %s, %s, %s);", + (game.id, game.num_players, game.score, game.seed, game.variant_id) + ) + else: + if not stored == game: + print("Already stored game with id {}, aborting".format(game.id)) + print("Stored game is: {}".format(stored.__dict__)) + print("New game is: {}".format(game.__dict__)) + +def commit(): + conn.commit()