From d549f7d83dc5677ee90cdc6a511b50021fe469e3 Mon Sep 17 00:00:00 2001 From: iamBadgers Date: Sat, 4 Apr 2026 16:48:34 -0700 Subject: [PATCH] Add smart cursor stuff --- src/container_managment.py | 31 ++++++++++---------- src/database.py | 24 ++++++++++++++-- src/key_tables.py | 59 +++++++++++++++++++++++--------------- src/main.py | 13 +++++---- src/tables.py | 5 ++++ 5 files changed, 86 insertions(+), 46 deletions(-) create mode 100644 src/tables.py diff --git a/src/container_managment.py b/src/container_managment.py index 84d89cb..289ef82 100644 --- a/src/container_managment.py +++ b/src/container_managment.py @@ -1,5 +1,5 @@ import docker -from game_tables import GameTable +from game_tables import GameTable def build_container_routing_labels(prefix: str, strip_prefix=True): @@ -18,29 +18,27 @@ def build_container_routing_labels(prefix: str, strip_prefix=True): prefix=prefix ) if strip_prefix: - labels[middleware_key] = "/{prefix}".format(prefix=prefix) - labels[middleware_router_key] = "{prefix}-stripprefix".format( - prefix=prefix - ) + labels[middleware_key] = "/{prefix}".format(prefix=prefix) + labels[middleware_router_key] = "{prefix}-stripprefix".format(prefix=prefix) return labels def stop_container(docker_id): try: - client = docker.from_env() - container = client.containers.get(docker_id) - container.kill() - return True + client = docker.from_env() + container = client.containers.get(docker_id) + container.kill() + return True except docker.errors.NotFound, docker.errors.APIError: - return False + return False def start_foundry_container(table: GameTable, version=12): client = docker.from_env() container = None - if table.docker_id != None and table.docker_id != 0 and table.docker_id != Null: + if table.docker_id != None and table.docker_id != 0: try: container = client.containers.get(table.docker_id) except docker.errors.NotFound, docker.errors.NullResource: @@ -64,12 +62,15 @@ def start_foundry_container(table: GameTable, version=12): "FOUNDRY_ROUTE_PREFIX={prefix}".format(prefix=table.game_table_link) ] - ports = { - "30000": str(30000 + table.game_table_id) - } + ports = {"30000": str(30000 + table.game_table_id)} container = client.containers.run( - image=image, labels=labels, volumes=volumes, environment=environment, ports=ports, detach=True + image=image, + labels=labels, + volumes=volumes, + environment=environment, + ports=ports, + detach=True, ) return container.id diff --git a/src/database.py b/src/database.py index 3e7842d..9120963 100644 --- a/src/database.py +++ b/src/database.py @@ -1,9 +1,29 @@ from flask import g -import sqlite3 +from sqlite3 import connect, Connection, Cursor def get_db(): db = getattr(g, "_database", None) if db is None: - db = sqlite3.connect("/data/tables.db") + db = connect("/data/tables.db") return db + + +class SmartCursor: + connection: Connection + curosr: Cursor + autoClose: bool + + def __init__(self, cursor: Cursor = None, connection: Connectoin = None): + self.cursor = cursor + self.connection = connection + + def __enter__(self): + if self.cursor != None: + self.cursor = self.connection.cursor() + self.autoClose = True + return self.cursor + + def __exit__(self, exc_type, exc_value, traceback): + if self.autoClose: + self.cursor.close() diff --git a/src/key_tables.py b/src/key_tables.py index 53dd564..3b19b2b 100644 --- a/src/key_tables.py +++ b/src/key_tables.py @@ -1,3 +1,7 @@ +from sqlite3 import Connection, Cursor +from database import SmartCursor + + class KeyTable: key: str game_table_id: number @@ -50,32 +54,41 @@ class KeyTable: 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() +class KeyService: - if row == None: - return None + connection: Connection - return KeyTable(row[0], row[1]) + def __init__(self, connection: Connection): + self.connection = connection + def get_key_for_table(self, game_table_id, cursor=None) -> KeyTable: + with SmartCursor(cursor, self.connection) as smartCursor: + smartCursor.execute( + """SELECT key, game_table_id + FROM game_keys + WHERE game_table_id = ?""", + (game_table_id,), + ) + row = smartCursor.fetchone() -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] + if row == None: + return None + return KeyTable(row[0], row[1]) -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] + def get_reserved_keys(self, cursor=None) -> KeyTable: + with SmartCursor(cursor, self.connection) as smartCursor: + smartCurosr.execute("""SELECT key, game_table_id + FROM game_keys + WHERE game_table_id IS NOT NULL""") + rows = smartCursor.fetchall() + + return [KeyTable(row[0], row[1]) for row in rows] + + def get_free_keys(self, cursor=None) -> KeyTable: + with SmartCursor(cursor, connection) as smartCursor: + smartCursor.execute("""SELECT key, game_table_id + FROM game_keys + WHERE game_table_id IS NULL""") + rows = smartCursor.fetchall() + return [KeyTable(row[0], row[1]) for row in rows] diff --git a/src/main.py b/src/main.py index 8fee2f5..bf2e3b2 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,7 @@ from flask import Flask, g, jsonify, request import sqlite3 import game_tables -import key_tables +from key_tables import KeyService, KeyTable import container_managment app = Flask(__name__) @@ -68,8 +68,9 @@ def get_table(table_id): def activate_table(table_id): db = get_db() cursor = db.cursor() + keyService = KeyService(db) - keys = key_tables.get_free_keys(cursor) + keys = keyService.get_free_keys(cursor) table = game_tables.read_by_id(table_id, cursor) if table == None: @@ -83,9 +84,7 @@ def activate_table(table_id): keys[0].reserve(table.game_table_id) table.active = True - table.docker_id = container_managment.start_foundry_container( - table - ) + table.docker_id = container_managment.start_foundry_container(table) app.logger.info(table.docker_id) keys[0].commit(cursor) table.commit(cursor) @@ -98,8 +97,10 @@ def activate_table(table_id): def deactivate_table(table_id): db = get_db() cursor = db.cursor() + keyService = KeyService(db) + table = game_tables.read_by_id(table_id, cursor) - key = key_tables.get_key_for_table(table_id, cursor) + key = keyService.get_key_for_table(table_id, cursor) if table == None: return "No such table", 404 diff --git a/src/tables.py b/src/tables.py new file mode 100644 index 0000000..4668262 --- /dev/null +++ b/src/tables.py @@ -0,0 +1,5 @@ +from flask import Blueprint, Flask, g, jsonify, request +import KeyTables +import GameTables + +tables_apt = Blueprint("tables_api", __name__)