Add keytable dao
This commit is contained in:
@@ -44,6 +44,7 @@ class GameTable:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
self.created = True
|
self.created = True
|
||||||
|
self.updated = True
|
||||||
return
|
return
|
||||||
if not self.updated:
|
if not self.updated:
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
|
|||||||
@@ -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]
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ def get_active_tables():
|
|||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
tables = game_tables.read_active(cursor)
|
tables = game_tables.read_active(cursor)
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
return jsonify([table.toJson() for table in tables])
|
return jsonify([table.toJson() for table in tables])
|
||||||
|
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ def get_tables():
|
|||||||
db = get_db()
|
db = get_db()
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
tables = game_tables.read_all(cursor)
|
tables = game_tables.read_all(cursor)
|
||||||
cursor.close();
|
cursor.close()
|
||||||
|
|
||||||
return jsonify([table.toJson() for table in tables])
|
return jsonify([table.toJson() for table in tables])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user