add storing / loading of games to database
This commit is contained in:
parent
9a771b0782
commit
5e537423e9
1 changed files with 69 additions and 1 deletions
70
database.py
70
database.py
|
@ -1,4 +1,5 @@
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
## global connection
|
## global connection
|
||||||
conn = psycopg2.connect("dbname=hanab-live user=postgres")
|
conn = psycopg2.connect("dbname=hanab-live user=postgres")
|
||||||
|
@ -6,6 +7,10 @@ conn = psycopg2.connect("dbname=hanab-live user=postgres")
|
||||||
## cursor
|
## cursor
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
# cur.execute("DROP TABLE games;")
|
||||||
|
# conn.commit()
|
||||||
|
# exit(0)
|
||||||
|
|
||||||
## check if table exists, else create it
|
## check if table exists, else create it
|
||||||
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'games');")
|
cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'games');")
|
||||||
a = cur.fetchone()
|
a = cur.fetchone()
|
||||||
|
@ -16,8 +21,8 @@ if a[0] is False:
|
||||||
"CREATE TABLE games ("
|
"CREATE TABLE games ("
|
||||||
"id SERIAL PRIMARY KEY,"
|
"id SERIAL PRIMARY KEY,"
|
||||||
"num_players SMALLINT NOT NULL,"
|
"num_players SMALLINT NOT NULL,"
|
||||||
"seed TEXT NOT NULL,"
|
|
||||||
"score SMALLINT NOT NULL,"
|
"score SMALLINT NOT NULL,"
|
||||||
|
"seed TEXT NOT NULL,"
|
||||||
"variant_id SMALLINT NOT NULL,"
|
"variant_id SMALLINT NOT NULL,"
|
||||||
"deck_plays BOOLEAN,"
|
"deck_plays BOOLEAN,"
|
||||||
"one_extra_card BOOLEAN,"
|
"one_extra_card BOOLEAN,"
|
||||||
|
@ -28,3 +33,66 @@ if a[0] is False:
|
||||||
conn.commit()
|
conn.commit()
|
||||||
else:
|
else:
|
||||||
print("table already exists")
|
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()
|
||||||
|
|
Loading…
Reference in a new issue