Get close working as well

This commit is contained in:
iamBadgers
2026-03-24 01:06:02 -07:00
parent 5a6756dabc
commit b91f94956a
3 changed files with 96 additions and 65 deletions

View File

@@ -1,10 +1,10 @@
class GameTable: class GameTable:
game_table_id: int _game_table_id: int
game_table_name: str _game_table_name: str
game_table_link: str _game_table_link: str
active: bool _active: bool
created: bool _created: bool
updated: bool _updated: bool
def __init__( def __init__(
self, self,
@@ -15,59 +15,82 @@ class GameTable:
created: bool = False, created: bool = False,
updated: bool = False, updated: bool = False,
): ):
self.game_table_id = game_table_id self._game_table_id = game_table_id
self.game_table_name = game_table_name self._game_table_name = game_table_name
self.game_table_link = game_table_link self._game_table_link = game_table_link
self.active = active self._active = active
self.created = created self._created = created
self.updated = updated self._updated = updated
def toJson(self): def toJson(self):
return { return {
"id": self.game_table_id, "id": self._game_table_id,
"table_name": self.game_table_name, "table_name": self._game_table_name,
"table_link": self.game_table_link, "table_link": self._game_table_link,
"active": self.active, "active": self._active,
} }
def commit(self, cursor): def commit(self, cursor):
if not self.created: if not self._created:
cursor.execute( cursor.execute(
"""INSERT INTO game_tables """INSERT INTO game_tables
(game_table_id, game_table_name, game_table_link, active) (game_table_id, game_table_name, game_table_link, active)
VALUES (?, ?, ?, ?)""", VALUES (?, ?, ?, ?)""",
( (
self.game_table_id, self._game_table_id,
self.game_table_name, self._game_table_name,
self.game_table_link, self._game_table_link,
self.active, self._active,
), ),
) )
self.created = True self._created = True
self.updated = True self._updated = True
return return
if not self.updated: if not self._updated:
cursor.execute( cursor.execute(
"""UPDATE game_tables """UPDATE game_tables
SET game_table_name = ?, game_table_link = ?, active = ? SET game_table_name = ?, game_table_link = ?, active = ?
WHERE game_table_id = ?""", WHERE game_table_id = ?""",
( (
self.game_table_name, self._game_table_name,
self.game_table_link, self._game_table_link,
self.active, self._active,
self.game_table_id, self._game_table_id,
), ),
) )
self.updated = False self._updated = False
return return
@property
def game_table_id(self):
return self._game_table_id
@game_table_id.setter
def set_game_table_id(self, game_table_id: int):
self._updated = False
self._game_table_id = game_table_id
@property
def active(self):
return self._active
@active.setter
def active(self, active: bool):
self._updated = False
self._active = active
def read_by_id(game_table_id, cursor): def read_by_id(game_table_id, cursor):
rows = cursor.execute( rows = cursor.execute(
"SELECT game_table_id, game_table_name, game_table_link, active FROM game_tables WHERE game_table_id = ?", """SELECT game_table_id, game_table_name, game_table_link, active
FROM game_tables
WHERE game_table_id = ?""",
(game_table_id,), (game_table_id,),
).fetchone() ).fetchone()
if len(rows) == 0:
return None
return GameTable(rows[0], rows[1], rows[2], rows[3], True, True) return GameTable(rows[0], rows[1], rows[2], rows[3], True, True)

View File

@@ -1,25 +1,29 @@
class KeyTable: class KeyTable:
key: str key: str
game_table_id: number game_table_id: number
created: bool
updated: bool
def __init__( def __init__(
self, key: str, game_table_id: int, created: bool = True, updated: bool = True self, key: str, game_table_id: int, created: bool = True, updated: bool = True
): ):
self.key = key self.key = key
self.game_table_id = game_table_id self.game_table_id = game_table_id
self.created = created
self.updated = updated
def toJson(self): def toJson(self):
return {"key": self.key, "table_id": self.game_table_id} return {"key": self.key, "table_id": self.game_table_id}
def reserve(self, game_table_id, cursor): def reserve(self, game_table_id):
self.game_table_id = game_table_id self.game_table_id = game_table_id
this.updated = False self.updated = False
def free(self, cursor): def release(self):
self.game_table_id = None self.game_table_id = None
self.updated = False self.updated = False
def commit(cursor): def commit(self, cursor):
if not self.created: if not self.created:
cursor.execute( cursor.execute(
"""INSERT INTO game_keys """INSERT INTO game_keys
@@ -28,7 +32,7 @@ class KeyTable:
""", """,
( (
self.key, self.key,
self.game_table_id if self.game_table_id != None else "NULL", self.game_table_id,
), ),
) )
self.created = True self.created = True
@@ -39,7 +43,7 @@ class KeyTable:
SET game_table_id = ? SET game_table_id = ?
WHERE key = ?""", WHERE key = ?""",
( (
self.game_table_id if self.game_table_id != None else "NULL", self.game_table_id,
self.key, self.key,
), ),
) )

View File

@@ -1,6 +1,7 @@
from flask import Flask, g, jsonify, request from flask import Flask, g, jsonify, request
import sqlite3 import sqlite3
import game_tables import game_tables
import key_tables
app = Flask(__name__) app = Flask(__name__)
@@ -65,46 +66,49 @@ def get_table(table_id):
@app.route("/api/tables/<table_id>:start") @app.route("/api/tables/<table_id>:start")
def activate_table(table_id): def activate_table(table_id):
db = get_db() db = get_db()
c = db.execute("select * from game_keys where game_table_id is null") cursor = db.cursor()
keys = c.fetchall()
c.close()
if len(keys) == 0: keys = key_tables.get_free_keys(cursor)
return "No more unused keys", 400 table = game_tables.read_by_id(table_id, cursor)
key = convertKey(keys[0]) if table == None:
c = db.execute(
"select game_table_id as id, game_table_name, game_table_link, active from game_tables where game_table_id = ?",
(table_id,),
)
tables = c.fetchall()
c.close()
if len(tables) == 0:
return "No such table", 404 return "No such table", 404
table = convertTable(tables[0]) if table.active:
if table["active"]:
return "Table already active", 400 return "Table already active", 400
c = db.cursor() if len(keys) == 0:
c.execute( return "No more keys", 400
"UPDATE game_tables SET active = 1 WHERE game_table_id = ?", (table["id"],)
)
c.execute(
"UPDATE game_keys SET game_table_id = ? WHERE key = ?",
(table["id"], key["key"]),
)
c.close()
db.commit()
return jsonify(table) keys[0].reserve(table.game_table_id)
table.active = True
keys[0].commit(cursor)
table.commit(cursor)
cursor.close()
db.commit()
return jsonify(table.toJson())
@app.route("/api/tables/<table_id>:stop") @app.route("/api/tables/<table_id>:stop")
def deactivate_table(table_id): def deactivate_table(table_id):
return db = get_db()
cursor = db.cursor()
table = game_tables.read_by_id(table_id, cursor)
key = key_tables.get_key_for_table(table_id, cursor)
if table == None:
"No such table", 404
table.active = False
if key != None:
key.release()
table.commit(cursor)
key.commit(cursor)
cursor.close()
db.commit()
return jsonify(table.toJson())
if __name__ == "__main__": if __name__ == "__main__":