From e475dbc36ba4384ac29c8dfd045bf6d0a1b48023 Mon Sep 17 00:00:00 2001 From: iamBadgers Date: Thu, 9 Apr 2026 20:19:23 -0700 Subject: [PATCH] move stuff into tables.py. --- src/database.py | 2 +- src/game_tables.py | 6 +-- src/main.py | 112 +-------------------------------------------- src/tables.py | 112 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 115 insertions(+), 117 deletions(-) diff --git a/src/database.py b/src/database.py index 0b791b1..bc2aa76 100644 --- a/src/database.py +++ b/src/database.py @@ -5,7 +5,7 @@ from sqlite3 import connect, Connection, Cursor def get_db(): db = getattr(g, "_database", None) if db is None: - db = connect("/data/tables.db") + db = g._database = connect("/data/tables.db") return db diff --git a/src/game_tables.py b/src/game_tables.py index 313c178..a1feaca 100644 --- a/src/game_tables.py +++ b/src/game_tables.py @@ -133,10 +133,10 @@ class GameService: (game_table_id,), ).fetchone() - if len(rows) == 0: - return None + if len(rows) == 0: + return None - return GameTable(rows[0], rows[1], rows[2], rows[3], rows[4], True, True) + return GameTable(rows[0], rows[1], rows[2], rows[3], rows[4], True, True) def read_all(self, cursor=None): with SmartCursor(cursor, self.connection) as smartCursor: diff --git a/src/main.py b/src/main.py index 696fec1..14844c6 100644 --- a/src/main.py +++ b/src/main.py @@ -1,9 +1,8 @@ from flask import Flask, g, jsonify, request import sqlite3 -from game_tables import GameService, GameTable -from key_tables import KeyService, KeyTable import container_managment from database import SmartCursor +import tables app = Flask(__name__) @@ -16,11 +15,6 @@ def convertTable(tupple): "active": tupple[3] != 0, } - -def convertKey(tupple): - return {"key": tupple[0], "table_id": tupple[1]} - - def get_db(): db = getattr(g, "_database", None) if db is None: @@ -34,109 +28,7 @@ def close_connection(exception): if db is not None: db.close() - -@app.route("/api/active_tables") -def get_active_tables(): - gameService = GameService(get_db()) - tables = gameService.read_active() - return jsonify([table.toJson() for table in tables]) - - -@app.route("/api/tables") -def get_tables(): - gameService = GameService(get_db()) - tables = gameService.read_all() - return jsonify([table.toJson() for table in tables]) - - -@app.route("/api/tables", methods=["POST"]) -def create_table(): - app.logger.info(request.get_json()) - app.logger.info(request.get_json()["table_name"]) - db = get_db() - with SmartCursor(connection=db) as smartCursor: - table = GameTable( - game_table_id=0, - game_table_name=request.get_json()["table_name"], - game_table_link=request.get_json()["table_link"], - active=False, - docker_id=None, - ) - app.logger.info(table._created) - app.logger.info(table._updated) - table.commit(smartCursor) - app.logger.info(table._created) - app.logger.info(table._updated) - db.commit() - return jsonify({}), 200 - - -@app.route("/api/tables/") -def get_table(table_id): - gameService = GameService(get_db()) - table = gameService.read_by_id(table_id) - return jsonify(table.toJson()) - - -@app.route("/api/tables/:start") -def activate_table(table_id): - db = get_db() - cursor = db.cursor() - keyService = KeyService(db) - gameService = GameService(db) - - keys = keyService.get_free_keys(cursor) - table = gameService.read_by_id(table_id, cursor) - - if table == None: - return "No such table", 404 - - if table.active: - return "Table already active", 400 - - if len(keys) == 0: - return "No more keys", 400 - - keys[0].reserve(table.game_table_id) - table.active = True - table.docker_id = container_managment.start_foundry_container(table) - app.logger.info(table.docker_id) - keys[0].commit(cursor) - table.commit(cursor) - cursor.close() - db.commit() - return jsonify(table.toJson()) - - -@app.route("/api/tables/:stop") -def deactivate_table(table_id): - db = get_db() - cursor = db.cursor() - keyService = KeyService(db) - gameService = GameService(db) - - table = gameService.read_by_id(table_id, cursor) - key = keyService.get_key_for_table(table_id, cursor) - - if table == None: - return "No such table", 404 - - if not table.active: - return "Table not active", 400 - - table.active = False - if key != None: - key.release() - key.commit(cursor) - - table.commit(cursor) - app.logger.info(table.docker_id) - container_managment.stop_container(table.docker_id) - - cursor.close() - db.commit() - return jsonify(table.toJson()) - +app.register_blueprint(tables.tables, url_prefix="/api") if __name__ == "__main__": app.run(debug=True) diff --git a/src/tables.py b/src/tables.py index 4668262..882a731 100644 --- a/src/tables.py +++ b/src/tables.py @@ -1,5 +1,111 @@ from flask import Blueprint, Flask, g, jsonify, request -import KeyTables -import GameTables +from game_tables import GameService, GameTable +from key_tables import KeyService, KeyTable +import container_managment +import random +from database import get_db, SmartCursor + +tables = Blueprint("tables_api", __name__) + +@tables.route("/tables", methods=["POST"]) +def create_table(): + db = get_db() + with SmartCursor(connection=db) as smartCursor: + table = GameTable( + game_table_id=random.randrange(1, 100000), + game_table_name=request.get_json()["table_name"], + game_table_link=request.get_json()["table_link"], + active=False, + docker_id=None, + ) + table.commit(smartCursor) + db.commit() + return jsonify({}), 200 + +@tables.route("/tables/") +def get_table(table_id): + gameService = GameService(get_db()) + table = gameService.read_by_id(table_id) + return jsonify(table.toJson()) + +@tables.route("/tables/", methods=["POST"]) +def update_table(table_id): + return + +@tables.route("tables/", methods=["DELETE"]) +def delete_table(table_id): + return + +@tables.route("/tables/active") +def list_active_tables(): + gameService = GameService(get_db()) + tables = gameService.read_active() + return jsonify([table.toJson() for table in tables]) + +@tables.route("/tables/all") +def list_all_tables(): + gameService = GameService(get_db()) + tables = gameService.read_all() + return jsonify([table.toJson() for table in tables]) + + +@tables.route("/tables/inactive") +def list_inactive_tables(): + return + +@tables.route("/tables/:start", methods=["POST"]) +def activate_table(table_id): + db = get_db() + cursor = db.cursor() + keyService = KeyService(db) + gameService = GameService(db) + + keys = keyService.get_free_keys(cursor) + table = gameService.read_by_id(table_id, cursor) + + if table == None: + return "No such table", 404 + + if table.active: + return "Table already active", 400 + + if len(keys) == 0: + return "No more keys", 400 + + keys[0].reserve(table.game_table_id) + table.active = True + table.docker_id = container_managment.start_foundry_container(table) + keys[0].commit(cursor) + table.commit(cursor) + cursor.close() + db.commit() + return jsonify(table.toJson()) + +@tables.route("/tables/:stop", methods=["POST"]) +def deactivate_table(table_id): + db = get_db() + cursor = db.cursor() + keyService = KeyService(db) + gameService = GameService(db) + + table = gameService.read_by_id(table_id, cursor) + key = keyService.get_key_for_table(table_id, cursor) + + if table == None: + return "No such table", 404 + + if not table.active: + return "Table not active", 400 + + table.active = False + if key != None: + key.release() + key.commit(cursor) + + table.commit(cursor) + container_managment.stop_container(table.docker_id) + + cursor.close() + db.commit() + return jsonify(table.toJson()) -tables_apt = Blueprint("tables_api", __name__)