Remove basic sql guff and use sql alchemy models instead.

This commit is contained in:
iamBadgers
2026-04-23 17:03:15 -07:00
parent b6cef4f3c1
commit 497298afea
7 changed files with 35 additions and 383 deletions

View File

@@ -1,10 +1,10 @@
import docker
from game_tables import GameTable
from key_tables import KeyTable
import os
import socket
import inspect
from flask import current_app
from game_table_model import GameTable
from key_model import TableKey
def build_container_routing_labels(prefix: str, strip_prefix=True):
@@ -54,7 +54,7 @@ def stop_container(docker_id) -> bool:
return False
def start_foundry_container(table: GameTable, key: KeyTable):
def start_foundry_container(table: GameTable, key: TableKey):
client = docker.from_env()
container = None
networkName = client.containers.get(socket.gethostname()).attrs["HostConfig"][

View File

@@ -17,7 +17,7 @@ class GameTable(db.Model):
def to_dict(self):
return {
"table_id": self.game_table_id,
"id": self.game_table_id,
"table_name": self.game_table_name,
"table_link": self.game_table_link,
"active": self.active,

View File

@@ -1,231 +0,0 @@
from sqlite3 import Connection, Cursor
from database import SmartCursor
import random
class GameTableException(Exception):
def __init__(self, message):
super().__init__(self, message)
class GameTable:
_game_table_id: int
_game_table_name: str
_game_table_link: str
_active: bool
_version: int
_docker_id: str
_created: bool
_updated: bool
_delete: bool
def __init__(
self,
game_table_id: int,
game_table_name: str,
game_table_link: str,
active: bool,
version: int,
docker_id: int,
created: bool = False,
updated: bool = False,
delete: bool = False,
):
self._game_table_id = game_table_id
self._game_table_name = game_table_name
self._game_table_link = game_table_link
self._active = active
self._version = version
self._docker_id = docker_id
self._created = created
self._updated = updated
self._delete = delete
def toJson(self):
return {
"id": self._game_table_id,
"table_name": self._game_table_name,
"table_link": self._game_table_link,
"version": self._version,
"active": self._active,
}
def commit(self, cursor):
if not self._created:
self._game_table_id = cursor.execute(
"""SELECT MAX(game_table_id) + 1 FROM game_tables"""
).fetchone()[0]
cursor.execute(
"""INSERT INTO game_tables
(game_table_id, game_table_name, game_table_link, version, active, docker_id)
VALUES (?, ?, ?, ?, ?, ?)""",
(
self._game_table_id,
self._game_table_name,
self._game_table_link,
self._version,
self._active,
self._docker_id,
),
)
self._created = True
self._updated = True
return
if not self._updated:
cursor.execute(
"""UPDATE game_tables
SET game_table_name = ?, game_table_link = ?, version = ?, active = ?, docker_id = ?
WHERE game_table_id = ?""",
(
self._game_table_name,
self._game_table_link,
self._version,
self._active,
self._docker_id,
self._game_table_id,
),
)
self._updated = True
return
if self._delete:
cursor.execute(
"DELETE FROM game_tables WHERE game_table_id = ?",
(self._game_table_id,),
)
self._created = False
self._updated = True
self._delete = False
def delete(self):
if not self._created:
raise GameTableException("Cannot delete a table if it is not created.")
if not self._updated:
raise GameTableException("Cannot delete a table if it is dirty.")
self._delete = True
@property
def game_table_id(self):
return self._game_table_id
@game_table_id.setter
def set_game_table_id(self, game_table_id: int):
self._updated = False
self._game_table_id = game_table_id
@property
def game_table_name(self):
return self._game_table_name
@game_table_name.setter
def game_table_name(self, game_table_name: str):
self._updated = False
self._game_table_name = game_table_name
@property
def game_table_link(self):
return self._game_table_link
@game_table_link.setter
def game_table_link(self, game_table_link):
self._updated = False
self._game_table_link = game_table_link
@property
def version(self):
return self._version
@version.setter
def version(self, version: int):
self._updated = False
self._version = version
@property
def active(self):
return self._active
@active.setter
def active(self, active: bool):
self._updated = False
self._active = active
@property
def docker_id(self):
return self._docker_id
@docker_id.setter
def docker_id(self, docker_id: int):
self._upadted = False
self._docker_id = docker_id
class GameService:
connection: Connection
def __init__(self, connection: Connection):
self.connection = connection
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, version, docker_id
FROM game_tables
WHERE game_table_id = ?""",
(game_table_id,),
).fetchone()
if len(rows) == 0:
return None
return GameTable(
game_table_id=rows[0],
game_table_name=rows[1],
game_table_link=rows[2],
active=rows[3],
version=rows[4],
docker_id=rows[5],
created=True,
updated=True,
delete=False,
)
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, version, docker_id FROM game_tables"
).fetchall()
return [
GameTable(
game_table_id=row[0],
game_table_name=row[1],
game_table_link=row[2],
active=row[3],
version=row[4],
docker_id=row[5],
created=True,
updated=True,
delete=False,
)
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, version, docker_id FROM game_tables WHERE active != 0"
).fetchall()
return [
GameTable(
game_table_id=row[0],
game_table_name=row[1],
game_table_link=row[2],
active=row[3],
version=row[4],
docker_id=row[5],
created=True,
updated=True,
delete=False,
)
for row in rows
]

14
src/key_model.py Normal file
View File

@@ -0,0 +1,14 @@
from main import db
class TableKey(db.Model):
__tablename__ = "game_keys"
key = db.Column(db.String(100), primary_key=True)
game_table_id = db.Column(db.Integer)
def reserve(self, table_id: int):
self.game_table_id = table_id
def release(self):
self.game_table_id = None

View File

@@ -1,105 +0,0 @@
from sqlite3 import Connection, Cursor
from database import SmartCursor
class KeyTable:
key: str
game_table_id: int
key_file: str
created: bool
updated: bool
def __init__(
self,
key: str,
game_table_id: int,
key_file: str,
created: bool = True,
updated: bool = True,
):
self.key = key
self.game_table_id = game_table_id
self.key_file = key_file
self.created = created
self.updated = updated
def toJson(self):
return {
"key": self.key,
"table_id": self.game_table_id,
"key_file": self.key_file,
}
def reserve(self, game_table_id):
self.game_table_id = game_table_id
self.updated = False
def release(self):
self.game_table_id = None
self.updated = False
def commit(self, cursor):
if not self.created:
cursor.execute(
"""INSERT INTO game_keys
(key, game_table_id)
VALUES (?, ?)
""",
(
self.key,
self.game_table_id,
),
)
self.created = True
self.updated = True
if not self.updated:
cursor.execute(
"""UPDATE game_keys
SET game_table_id = ?
WHERE key = ?""",
(
self.game_table_id,
self.key,
),
)
self.updated = True
class KeyService:
connection: Connection
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, key_file
FROM game_keys
WHERE game_table_id = ?""",
(game_table_id,),
)
row = smartCursor.fetchone()
if row == None:
return None
return KeyTable(row[0], row[1], row[2])
def get_reserved_keys(self, cursor=None) -> KeyTable:
with SmartCursor(cursor, self.connection) as smartCursor:
smartCurosr.execute("""SELECT key, game_table_id, key_file
FROM game_keys
WHERE game_table_id IS NOT NULL""")
rows = smartCursor.fetchall()
return [KeyTable(row[0], row[1, row[2]]) for row in rows]
def get_free_keys(self, cursor=None) -> KeyTable:
with SmartCursor(cursor, self.connection) as smartCursor:
smartCursor.execute("""SELECT key, game_table_id, key_file
FROM game_keys
WHERE game_table_id IS NULL""")
rows = smartCursor.fetchall()
return [KeyTable(row[0], row[1], row[2]) for row in rows]

View File

@@ -1,8 +1,6 @@
from flask import Flask, g, jsonify, request
import sqlite3
import container_managment
from database import SmartCursor, get_db, init_db
import os
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

View File

@@ -1,11 +1,9 @@
from flask import Blueprint, Flask, g, jsonify, request, current_app
from game_tables import GameService, GameTable
from key_tables import KeyService, KeyTable
import container_managment
import random
from database import get_db, SmartCursor
from game_table_model import GameTable
import game_table_model
from key_model import TableKey
from main import db as potato
tables = Blueprint("tables_api", __name__)
@@ -47,41 +45,32 @@ def delete_table(table_id):
table = GameTable.query.get_or_404(table_id)
potato.session.delete(table)
potato.session.commit()
return jsonify({}), 200
@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 = GameTable.query.filter_by(active=True).all()
return jsonify([table.to_dict() 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 = GameTable.query.all()
return jsonify([table.to_dict() for table in tables])
@tables.route("/tables/inactive")
def list_inactive_tables():
return
tables = GameTable.query.filter_by(active=False).all()
return jsonify([table.to_dict() for table in tables])
@tables.route("/tables/<table_id>:start", methods=["POST"])
def activate_table(table_id):
db = get_db()
cursor = db.cursor()
keyService = KeyService(db)
gameService = GameService(db)
hard = bool(request.get_json()["hard"])
keys = keyService.get_free_keys(cursor)
table = gameService.read_by_id(table_id, cursor)
if table == None:
return "No such table", 404
keys = TableKey.query.filter_by(game_table_id=None).all()
table = GameTable.query.get_or_404(table_id)
if table.active:
return "Table already active", 400
@@ -97,25 +86,14 @@ def activate_table(table_id):
if hard:
container_managment.delete_container(table.docker_id)
table.docker_id = container_managment.start_foundry_container(table, key)
key.commit(cursor)
table.commit(cursor)
cursor.close()
db.commit()
return jsonify(table.toJson())
potato.session.commit()
return jsonify(table.to_dict())
@tables.route("/tables/<table_id>: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
table = GameTable.query.get_or_404(table_id)
key = TableKey.query.filter_by(game_table_id=table_id).first()
if not table.active:
return "Table not active", 400
@@ -123,11 +101,9 @@ def deactivate_table(table_id):
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())
potato.session.commit()
return jsonify(table.to_dict())