Add smart cursor stuff
This commit is contained in:
@@ -18,29 +18,27 @@ def build_container_routing_labels(prefix: str, strip_prefix=True):
|
|||||||
prefix=prefix
|
prefix=prefix
|
||||||
)
|
)
|
||||||
if strip_prefix:
|
if strip_prefix:
|
||||||
labels[middleware_key] = "/{prefix}".format(prefix=prefix)
|
labels[middleware_key] = "/{prefix}".format(prefix=prefix)
|
||||||
labels[middleware_router_key] = "{prefix}-stripprefix".format(
|
labels[middleware_router_key] = "{prefix}-stripprefix".format(prefix=prefix)
|
||||||
prefix=prefix
|
|
||||||
)
|
|
||||||
|
|
||||||
return labels
|
return labels
|
||||||
|
|
||||||
|
|
||||||
def stop_container(docker_id):
|
def stop_container(docker_id):
|
||||||
try:
|
try:
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container = client.containers.get(docker_id)
|
container = client.containers.get(docker_id)
|
||||||
container.kill()
|
container.kill()
|
||||||
return True
|
return True
|
||||||
except docker.errors.NotFound, docker.errors.APIError:
|
except docker.errors.NotFound, docker.errors.APIError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def start_foundry_container(table: GameTable, version=12):
|
def start_foundry_container(table: GameTable, version=12):
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
container = None
|
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:
|
try:
|
||||||
container = client.containers.get(table.docker_id)
|
container = client.containers.get(table.docker_id)
|
||||||
except docker.errors.NotFound, docker.errors.NullResource:
|
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)
|
"FOUNDRY_ROUTE_PREFIX={prefix}".format(prefix=table.game_table_link)
|
||||||
]
|
]
|
||||||
|
|
||||||
ports = {
|
ports = {"30000": str(30000 + table.game_table_id)}
|
||||||
"30000": str(30000 + table.game_table_id)
|
|
||||||
}
|
|
||||||
|
|
||||||
container = client.containers.run(
|
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
|
return container.id
|
||||||
|
|||||||
@@ -1,9 +1,29 @@
|
|||||||
from flask import g
|
from flask import g
|
||||||
import sqlite3
|
from sqlite3 import connect, Connection, Cursor
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = getattr(g, "_database", None)
|
db = getattr(g, "_database", None)
|
||||||
if db is None:
|
if db is None:
|
||||||
db = sqlite3.connect("/data/tables.db")
|
db = connect("/data/tables.db")
|
||||||
return 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()
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
from sqlite3 import Connection, Cursor
|
||||||
|
from database import SmartCursor
|
||||||
|
|
||||||
|
|
||||||
class KeyTable:
|
class KeyTable:
|
||||||
key: str
|
key: str
|
||||||
game_table_id: number
|
game_table_id: number
|
||||||
@@ -50,32 +54,41 @@ class KeyTable:
|
|||||||
self.updated = True
|
self.updated = True
|
||||||
|
|
||||||
|
|
||||||
def get_key_for_table(game_table_id, cursor):
|
class KeyService:
|
||||||
cursor.execute(
|
|
||||||
"""SELECT key, game_table_id
|
|
||||||
FROM game_keys
|
|
||||||
WHERE game_table_id = ?""",
|
|
||||||
(game_table_id,),
|
|
||||||
)
|
|
||||||
row = cursor.fetchone()
|
|
||||||
|
|
||||||
if row == None:
|
connection: Connection
|
||||||
return None
|
|
||||||
|
|
||||||
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):
|
if row == None:
|
||||||
curosr.execute("""SELECT key, game_table_id
|
return None
|
||||||
FROM game_keys
|
|
||||||
WHERE game_table_id IS NOT NULL""")
|
|
||||||
rows = cursor.fetchall()
|
|
||||||
return [KeyTable(row[0], row[1]) for row in rows]
|
|
||||||
|
|
||||||
|
return KeyTable(row[0], row[1])
|
||||||
|
|
||||||
def get_free_keys(cursor):
|
def get_reserved_keys(self, cursor=None) -> KeyTable:
|
||||||
cursor.execute("""SELECT key, game_table_id
|
with SmartCursor(cursor, self.connection) as smartCursor:
|
||||||
FROM game_keys
|
smartCurosr.execute("""SELECT key, game_table_id
|
||||||
WHERE game_table_id IS NULL""")
|
FROM game_keys
|
||||||
rows = cursor.fetchall()
|
WHERE game_table_id IS NOT NULL""")
|
||||||
return [KeyTable(row[0], row[1]) for row in rows]
|
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]
|
||||||
|
|||||||
13
src/main.py
13
src/main.py
@@ -1,7 +1,7 @@
|
|||||||
from flask import Flask, g, jsonify, request
|
from flask import Flask, g, jsonify, request
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import game_tables
|
import game_tables
|
||||||
import key_tables
|
from key_tables import KeyService, KeyTable
|
||||||
import container_managment
|
import container_managment
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -68,8 +68,9 @@ def get_table(table_id):
|
|||||||
def activate_table(table_id):
|
def activate_table(table_id):
|
||||||
db = get_db()
|
db = get_db()
|
||||||
cursor = db.cursor()
|
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)
|
table = game_tables.read_by_id(table_id, cursor)
|
||||||
|
|
||||||
if table == None:
|
if table == None:
|
||||||
@@ -83,9 +84,7 @@ def activate_table(table_id):
|
|||||||
|
|
||||||
keys[0].reserve(table.game_table_id)
|
keys[0].reserve(table.game_table_id)
|
||||||
table.active = True
|
table.active = True
|
||||||
table.docker_id = container_managment.start_foundry_container(
|
table.docker_id = container_managment.start_foundry_container(table)
|
||||||
table
|
|
||||||
)
|
|
||||||
app.logger.info(table.docker_id)
|
app.logger.info(table.docker_id)
|
||||||
keys[0].commit(cursor)
|
keys[0].commit(cursor)
|
||||||
table.commit(cursor)
|
table.commit(cursor)
|
||||||
@@ -98,8 +97,10 @@ def activate_table(table_id):
|
|||||||
def deactivate_table(table_id):
|
def deactivate_table(table_id):
|
||||||
db = get_db()
|
db = get_db()
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
keyService = KeyService(db)
|
||||||
|
|
||||||
table = game_tables.read_by_id(table_id, cursor)
|
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:
|
if table == None:
|
||||||
return "No such table", 404
|
return "No such table", 404
|
||||||
|
|||||||
5
src/tables.py
Normal file
5
src/tables.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from flask import Blueprint, Flask, g, jsonify, request
|
||||||
|
import KeyTables
|
||||||
|
import GameTables
|
||||||
|
|
||||||
|
tables_apt = Blueprint("tables_api", __name__)
|
||||||
Reference in New Issue
Block a user