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):
self.cursor = cursor
self.connection = connection
self.autoClose = False
def __enter__(self):
if self.cursor != None:
if self.cursor == None:
self.cursor = self.connection.cursor()
self.autoClose = True
return self.cursor

View File

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

View File

@@ -86,7 +86,7 @@ class KeyService:
return [KeyTable(row[0], row[1]) for row in rows]
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
FROM game_keys
WHERE game_table_id IS NULL""")

View File

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