Add smart cursor stuff

This commit is contained in:
iamBadgers
2026-04-04 16:48:34 -07:00
parent cb5c08b92c
commit d549f7d83d
5 changed files with 86 additions and 46 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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]

View File

@@ -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

5
src/tables.py Normal file
View File

@@ -0,0 +1,5 @@
from flask import Blueprint, Flask, g, jsonify, request
import KeyTables
import GameTables
tables_apt = Blueprint("tables_api", __name__)