Add dao's

This commit is contained in:
iamBadgers
2026-03-22 20:26:46 -07:00
parent de7c604f91
commit 64c1f52cdf
4 changed files with 152 additions and 22 deletions

View File

@@ -1,74 +1,111 @@
from flask import Flask, g, jsonify, request
import sqlite3
import game_tables
app = Flask(__name__)
def convert(tupple):
def convertTable(tupple):
return {
"id": tupple[0],
"table_name": tupple[1],
"table_link": tupple[2],
"active": tupple[3] != 0
"active": tupple[3] != 0,
}
def convertKey(tupple):
return {"key": tupple[0], "table_id": tupple[1]}
def get_db():
db = getattr(g, '_database', None)
db = getattr(g, "_database", None)
if db is None:
db = sqlite3.connect("/data/tables.db")
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
db = getattr(g, "_database", None)
if db is not None:
db.close()
@app.route("/api/active_tables")
def get_active_tables():
db = get_db()
c = db.execute('select game_table_id as id, game_table_name as table_name, game_table_link as table_link, active from game_tables where active')
v = c.fetchall()
c.close()
cursor = db.cursor()
tables = game_tables.read_active(cursor)
cursor.close()
return jsonify([table.toJson() for table in tables])
return jsonify([convert(x) for x in v])
@app.route("/api/tables")
def get_tables():
db = get_db()
c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables')
v = c.fetchall()
c.close()
cursor = db.cursor()
tables = game_tables.read_all(cursor)
cursor.close();
return jsonify([table.toJson() for table in tables])
@app.route("/api/tables/<table_id>")
def get_table(table_id):
db = get_db()
cursor = db.cursor()
table = game_tables.read_by_id(table_id, cursor)
cursor.close()
return jsonify(table.toJson())
return jsonify([convert(x) for x in v])
@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')
v = c.fetchall()
c = db.execute("select * from game_keys where game_table_id is null")
keys = c.fetchall()
c.close()
if (len(v) == 0):
if len(keys) == 0:
return "No more unused keys", 400
c = db.execute('select game_table_id as id, game_table_name, game_table_link, active from game_tables')
v = c.fetchall()
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(v) == 0):
if len(tables) == 0:
return "No such table", 404
table = convert(v[0])
if (table["active"]):
table = convertTable(tables[0])
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()
return jsonify(table)
return jsonify(v)
@app.route("/api/tables/<table_id>:stop")
def deactivate_table(table_id):
return
if __name__ == '__main__':
if __name__ == "__main__":
app.run(debug=True)