Smart curosr for Game Tables

This commit is contained in:
iamBadgers
2026-04-04 19:40:55 -07:00
parent d549f7d83d
commit 917870128d
4 changed files with 52 additions and 45 deletions

View File

@@ -17,9 +17,10 @@ class SmartCursor:
def __init__(self, cursor: Cursor = None, connection: Connectoin = None): def __init__(self, cursor: Cursor = None, connection: Connectoin = None):
self.cursor = cursor self.cursor = cursor
self.connection = connection self.connection = connection
self.autoClose = False
def __enter__(self): def __enter__(self):
if self.cursor != None: if self.cursor == None:
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
self.autoClose = True self.autoClose = True
return self.cursor return self.cursor

View File

@@ -1,3 +1,7 @@
from sqlite3 import Connection, Cursor
from database import SmartCursor
class GameTable: class GameTable:
_game_table_id: int _game_table_id: int
_game_table_name: str _game_table_name: str
@@ -112,33 +116,42 @@ class GameTable:
self._docker_id = docker_id self._docker_id = docker_id
def read_by_id(game_table_id, cursor): class GameService:
rows = cursor.execute( connection: Connection
"""SELECT game_table_id, game_table_name, game_table_link, active, docker_id
FROM game_tables
WHERE game_table_id = ?""",
(game_table_id,),
).fetchone()
if len(rows) == 0: def __init__(self, connection: Connection):
return None self.connection = connection
return GameTable(rows[0], rows[1], rows[2], rows[3], rows[4], True, True) def read_by_id(self, game_table_id, cursor=None):
with SmartCursor(cursor, self.connection) as smartCursor:
rows = smartCursor.execute(
"""SELECT game_table_id, game_table_name, game_table_link, active, docker_id
FROM game_tables
WHERE game_table_id = ?""",
(game_table_id,),
).fetchone()
if len(rows) == 0:
return None
def read_all(cursor): return GameTable(rows[0], rows[1], rows[2], rows[3], rows[4], True, True)
rows = cursor.execute(
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables"
).fetchall()
return [
GameTable(row[0], row[1], row[2], row[3], row[4], True, True) for row in rows
]
def read_all(self, cursor=None):
with SmartCursor(cursor, self.connection) as smartCursor:
rows = smartCursor.execute(
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables"
).fetchall()
return [
GameTable(row[0], row[1], row[2], row[3], row[4], True, True)
for row in rows
]
def read_active(cursor): def read_active(self, cursor=None):
rows = cursor.execute( with SmartCursor(cursor, self.connection) as smartCursor:
"SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables WHERE active != 0" rows = smartCursor.execute(
).fetchall() "SELECT game_table_id, game_table_name, game_table_link, active, docker_id FROM game_tables WHERE active != 0"
return [ ).fetchall()
GameTable(row[0], row[1], row[2], row[3], row[4], True, True) for row in rows return [
] GameTable(row[0], row[1], row[2], row[3], row[4], True, True)
for row in rows
]

View File

@@ -82,11 +82,11 @@ class KeyService:
FROM game_keys FROM game_keys
WHERE game_table_id IS NOT NULL""") WHERE game_table_id IS NOT NULL""")
rows = smartCursor.fetchall() rows = smartCursor.fetchall()
return [KeyTable(row[0], row[1]) for row in rows] return [KeyTable(row[0], row[1]) for row in rows]
def get_free_keys(self, cursor=None) -> KeyTable: def get_free_keys(self, cursor=None) -> KeyTable:
with SmartCursor(cursor, connection) as smartCursor: with SmartCursor(cursor, self.connection) as smartCursor:
smartCursor.execute("""SELECT key, game_table_id smartCursor.execute("""SELECT key, game_table_id
FROM game_keys FROM game_keys
WHERE game_table_id IS NULL""") WHERE game_table_id IS NULL""")

View File

@@ -1,6 +1,6 @@
from flask import Flask, g, jsonify, request from flask import Flask, g, jsonify, request
import sqlite3 import sqlite3
import game_tables from game_tables import GameService, GameTable
from key_tables import KeyService, KeyTable from key_tables import KeyService, KeyTable
import container_managment import container_managment
@@ -36,31 +36,22 @@ def close_connection(exception):
@app.route("/api/active_tables") @app.route("/api/active_tables")
def get_active_tables(): def get_active_tables():
db = get_db() gameService = GameService(get_db())
cursor = db.cursor() tables = gameService.read_active()
tables = game_tables.read_active(cursor)
cursor.close()
return jsonify([table.toJson() for table in tables]) return jsonify([table.toJson() for table in tables])
@app.route("/api/tables") @app.route("/api/tables")
def get_tables(): def get_tables():
db = get_db() gameService = GameService(get_db())
cursor = db.cursor() tables = gameService.read_all()
tables = game_tables.read_all(cursor)
cursor.close()
return jsonify([table.toJson() for table in tables]) return jsonify([table.toJson() for table in tables])
@app.route("/api/tables/<table_id>") @app.route("/api/tables/<table_id>")
def get_table(table_id): def get_table(table_id):
db = get_db() gameService = GameService(get_db())
cursor = db.cursor() table = gameService.read_by_id(table_id)
table = game_tables.read_by_id(table_id, cursor)
cursor.close()
return jsonify(table.toJson()) return jsonify(table.toJson())
@@ -69,9 +60,10 @@ def activate_table(table_id):
db = get_db() db = get_db()
cursor = db.cursor() cursor = db.cursor()
keyService = KeyService(db) keyService = KeyService(db)
gameService = GameService(db)
keys = keyService.get_free_keys(cursor) keys = keyService.get_free_keys(cursor)
table = game_tables.read_by_id(table_id, cursor) table = gameService.read_by_id(table_id, cursor)
if table == None: if table == None:
return "No such table", 404 return "No such table", 404
@@ -98,8 +90,9 @@ def deactivate_table(table_id):
db = get_db() db = get_db()
cursor = db.cursor() cursor = db.cursor()
keyService = KeyService(db) keyService = KeyService(db)
gameService = GameService(db)
table = game_tables.read_by_id(table_id, cursor) table = gameService.read_by_id(table_id, cursor)
key = keyService.get_key_for_table(table_id, cursor) key = keyService.get_key_for_table(table_id, cursor)
if table == None: if table == None: