diff --git a/src/game_tables.py b/src/game_tables.py index 8fe43f1..81e1167 100644 --- a/src/game_tables.py +++ b/src/game_tables.py @@ -44,6 +44,7 @@ class GameTable: ), ) self.created = True + self.updated = True return if not self.updated: cursor.execute( diff --git a/src/key_tables.py b/src/key_tables.py index e69de29..c58af4f 100644 --- a/src/key_tables.py +++ b/src/key_tables.py @@ -0,0 +1,77 @@ +class KeyTable: + key: str + game_table_id: number + + def __init__( + self, key: str, game_table_id: int, created: bool = True, updated: bool = True + ): + self.key = key + self.game_table_id = game_table_id + + def toJson(self): + return {"key": self.key, "table_id": self.game_table_id} + + def reserve(self, game_table_id, cursor): + self.game_table_id = game_table_id + this.updated = False + + def free(self, cursor): + self.game_table_id = None + self.updated = False + + def commit(cursor): + if not self.created: + cursor.execute( + """INSERT INTO game_keys + (key, game_table_id) + VALUES (?, ?) + """, + ( + self.key, + self.game_table_id if self.game_table_id != None else "NULL", + ), + ) + self.created = True + self.updated = True + if not self.updated: + cursor.execute( + """UPDATE game_keys + SET game_table_id = ? + WHERE key = ?""", + ( + self.game_table_id if self.game_table_id != None else "NULL", + self.key, + ), + ) + self.updated = True + + +def get_key_for_table(game_table_id, cursor): + cursor.execute( + """SELECT key, game_table_id + FROM game_keys + WHERE game_table_id = ?""", + (game_table_id,), + ) + row = cursor.fetchone() + + if len(row) == 0: + return None + + return KeyTable(row[0], row[1]) + + +def get_reserved_keys(cursor): + curosr.execute("""SELECT key, game_table_id + FROM game_keys + WHERE game_table_id IS NOT NULL""") + rows = cursor.fetchall() + return [KeyTable(row[0], row[1]) for row in rows] + + +def get_free_keys(cursor): + cursor.execute("""SELECT key, game_table_id + FROM game_keys + WHERE game_table_id IS NULL""") + rows = cursor.fetchall() + return [KeyTable(row[0], row[1]) for row in rows] diff --git a/src/main.py b/src/main.py index 58f2557..7cbc488 100644 --- a/src/main.py +++ b/src/main.py @@ -38,7 +38,7 @@ def get_active_tables(): cursor = db.cursor() tables = game_tables.read_active(cursor) cursor.close() - + return jsonify([table.toJson() for table in tables]) @@ -47,7 +47,7 @@ def get_tables(): db = get_db() cursor = db.cursor() tables = game_tables.read_all(cursor) - cursor.close(); + cursor.close() return jsonify([table.toJson() for table in tables])