Get close working as well
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
class GameTable:
|
||||
game_table_id: int
|
||||
game_table_name: str
|
||||
game_table_link: str
|
||||
active: bool
|
||||
created: bool
|
||||
updated: bool
|
||||
_game_table_id: int
|
||||
_game_table_name: str
|
||||
_game_table_link: str
|
||||
_active: bool
|
||||
_created: bool
|
||||
_updated: bool
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -15,59 +15,82 @@ class GameTable:
|
||||
created: bool = False,
|
||||
updated: bool = False,
|
||||
):
|
||||
self.game_table_id = game_table_id
|
||||
self.game_table_name = game_table_name
|
||||
self.game_table_link = game_table_link
|
||||
self.active = active
|
||||
self.created = created
|
||||
self.updated = updated
|
||||
self._game_table_id = game_table_id
|
||||
self._game_table_name = game_table_name
|
||||
self._game_table_link = game_table_link
|
||||
self._active = active
|
||||
self._created = created
|
||||
self._updated = updated
|
||||
|
||||
def toJson(self):
|
||||
return {
|
||||
"id": self.game_table_id,
|
||||
"table_name": self.game_table_name,
|
||||
"table_link": self.game_table_link,
|
||||
"active": self.active,
|
||||
"id": self._game_table_id,
|
||||
"table_name": self._game_table_name,
|
||||
"table_link": self._game_table_link,
|
||||
"active": self._active,
|
||||
}
|
||||
|
||||
def commit(self, cursor):
|
||||
if not self.created:
|
||||
if not self._created:
|
||||
cursor.execute(
|
||||
"""INSERT INTO game_tables
|
||||
(game_table_id, game_table_name, game_table_link, active)
|
||||
VALUES (?, ?, ?, ?)""",
|
||||
(
|
||||
self.game_table_id,
|
||||
self.game_table_name,
|
||||
self.game_table_link,
|
||||
self.active,
|
||||
self._game_table_id,
|
||||
self._game_table_name,
|
||||
self._game_table_link,
|
||||
self._active,
|
||||
),
|
||||
)
|
||||
self.created = True
|
||||
self.updated = True
|
||||
self._created = True
|
||||
self._updated = True
|
||||
return
|
||||
if not self.updated:
|
||||
if not self._updated:
|
||||
cursor.execute(
|
||||
"""UPDATE game_tables
|
||||
SET game_table_name = ?, game_table_link = ?, active = ?
|
||||
WHERE game_table_id = ?""",
|
||||
(
|
||||
self.game_table_name,
|
||||
self.game_table_link,
|
||||
self.active,
|
||||
self.game_table_id,
|
||||
self._game_table_name,
|
||||
self._game_table_link,
|
||||
self._active,
|
||||
self._game_table_id,
|
||||
),
|
||||
)
|
||||
self.updated = False
|
||||
self._updated = False
|
||||
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):
|
||||
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,),
|
||||
).fetchone()
|
||||
|
||||
if len(rows) == 0:
|
||||
return None
|
||||
|
||||
return GameTable(rows[0], rows[1], rows[2], rows[3], True, True)
|
||||
|
||||
|
||||
|
||||
@@ -1,25 +1,29 @@
|
||||
class KeyTable:
|
||||
key: str
|
||||
game_table_id: number
|
||||
created: bool
|
||||
updated: bool
|
||||
|
||||
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
|
||||
self.created = created
|
||||
self.updated = updated
|
||||
|
||||
def toJson(self):
|
||||
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
|
||||
this.updated = False
|
||||
self.updated = False
|
||||
|
||||
def free(self, cursor):
|
||||
def release(self):
|
||||
self.game_table_id = None
|
||||
self.updated = False
|
||||
|
||||
def commit(cursor):
|
||||
def commit(self, cursor):
|
||||
if not self.created:
|
||||
cursor.execute(
|
||||
"""INSERT INTO game_keys
|
||||
@@ -28,7 +32,7 @@ class KeyTable:
|
||||
""",
|
||||
(
|
||||
self.key,
|
||||
self.game_table_id if self.game_table_id != None else "NULL",
|
||||
self.game_table_id,
|
||||
),
|
||||
)
|
||||
self.created = True
|
||||
@@ -39,7 +43,7 @@ class KeyTable:
|
||||
SET game_table_id = ?
|
||||
WHERE key = ?""",
|
||||
(
|
||||
self.game_table_id if self.game_table_id != None else "NULL",
|
||||
self.game_table_id,
|
||||
self.key,
|
||||
),
|
||||
)
|
||||
|
||||
62
src/main.py
62
src/main.py
@@ -1,6 +1,7 @@
|
||||
from flask import Flask, g, jsonify, request
|
||||
import sqlite3
|
||||
import game_tables
|
||||
import key_tables
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -65,46 +66,49 @@ def get_table(table_id):
|
||||
@app.route("/api/tables/<table_id>:start")
|
||||
def activate_table(table_id):
|
||||
db = get_db()
|
||||
c = db.execute("select * from game_keys where game_table_id is null")
|
||||
keys = c.fetchall()
|
||||
c.close()
|
||||
cursor = db.cursor()
|
||||
|
||||
if len(keys) == 0:
|
||||
return "No more unused keys", 400
|
||||
keys = key_tables.get_free_keys(cursor)
|
||||
table = game_tables.read_by_id(table_id, cursor)
|
||||
|
||||
key = convertKey(keys[0])
|
||||
|
||||
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:
|
||||
if table == None:
|
||||
return "No such table", 404
|
||||
|
||||
table = convertTable(tables[0])
|
||||
if table["active"]:
|
||||
if table.active:
|
||||
return "Table already active", 400
|
||||
|
||||
c = db.cursor()
|
||||
c.execute(
|
||||
"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()
|
||||
if len(keys) == 0:
|
||||
return "No more keys", 400
|
||||
|
||||
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")
|
||||
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__":
|
||||
|
||||
Reference in New Issue
Block a user