move stuff into tables.py.
This commit is contained in:
@@ -5,7 +5,7 @@ from sqlite3 import connect, Connection, Cursor
|
||||
def get_db():
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
db = connect("/data/tables.db")
|
||||
db = g._database = connect("/data/tables.db")
|
||||
return db
|
||||
|
||||
|
||||
|
||||
112
src/main.py
112
src/main.py
@@ -1,9 +1,8 @@
|
||||
from flask import Flask, g, jsonify, request
|
||||
import sqlite3
|
||||
from game_tables import GameService, GameTable
|
||||
from key_tables import KeyService, KeyTable
|
||||
import container_managment
|
||||
from database import SmartCursor
|
||||
import tables
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -16,11 +15,6 @@ def convertTable(tupple):
|
||||
"active": tupple[3] != 0,
|
||||
}
|
||||
|
||||
|
||||
def convertKey(tupple):
|
||||
return {"key": tupple[0], "table_id": tupple[1]}
|
||||
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
@@ -34,109 +28,7 @@ def close_connection(exception):
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
|
||||
@app.route("/api/active_tables")
|
||||
def get_active_tables():
|
||||
gameService = GameService(get_db())
|
||||
tables = gameService.read_active()
|
||||
return jsonify([table.toJson() for table in tables])
|
||||
|
||||
|
||||
@app.route("/api/tables")
|
||||
def get_tables():
|
||||
gameService = GameService(get_db())
|
||||
tables = gameService.read_all()
|
||||
return jsonify([table.toJson() for table in tables])
|
||||
|
||||
|
||||
@app.route("/api/tables", methods=["POST"])
|
||||
def create_table():
|
||||
app.logger.info(request.get_json())
|
||||
app.logger.info(request.get_json()["table_name"])
|
||||
db = get_db()
|
||||
with SmartCursor(connection=db) as smartCursor:
|
||||
table = GameTable(
|
||||
game_table_id=0,
|
||||
game_table_name=request.get_json()["table_name"],
|
||||
game_table_link=request.get_json()["table_link"],
|
||||
active=False,
|
||||
docker_id=None,
|
||||
)
|
||||
app.logger.info(table._created)
|
||||
app.logger.info(table._updated)
|
||||
table.commit(smartCursor)
|
||||
app.logger.info(table._created)
|
||||
app.logger.info(table._updated)
|
||||
db.commit()
|
||||
return jsonify({}), 200
|
||||
|
||||
|
||||
@app.route("/api/tables/<table_id>")
|
||||
def get_table(table_id):
|
||||
gameService = GameService(get_db())
|
||||
table = gameService.read_by_id(table_id)
|
||||
return jsonify(table.toJson())
|
||||
|
||||
|
||||
@app.route("/api/tables/<table_id>:start")
|
||||
def activate_table(table_id):
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
keyService = KeyService(db)
|
||||
gameService = GameService(db)
|
||||
|
||||
keys = keyService.get_free_keys(cursor)
|
||||
table = gameService.read_by_id(table_id, cursor)
|
||||
|
||||
if table == None:
|
||||
return "No such table", 404
|
||||
|
||||
if table.active:
|
||||
return "Table already active", 400
|
||||
|
||||
if len(keys) == 0:
|
||||
return "No more keys", 400
|
||||
|
||||
keys[0].reserve(table.game_table_id)
|
||||
table.active = True
|
||||
table.docker_id = container_managment.start_foundry_container(table)
|
||||
app.logger.info(table.docker_id)
|
||||
keys[0].commit(cursor)
|
||||
table.commit(cursor)
|
||||
cursor.close()
|
||||
db.commit()
|
||||
return jsonify(table.toJson())
|
||||
|
||||
|
||||
@app.route("/api/tables/<table_id>:stop")
|
||||
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
|
||||
|
||||
if not table.active:
|
||||
return "Table not active", 400
|
||||
|
||||
table.active = False
|
||||
if key != None:
|
||||
key.release()
|
||||
key.commit(cursor)
|
||||
|
||||
table.commit(cursor)
|
||||
app.logger.info(table.docker_id)
|
||||
container_managment.stop_container(table.docker_id)
|
||||
|
||||
cursor.close()
|
||||
db.commit()
|
||||
return jsonify(table.toJson())
|
||||
|
||||
app.register_blueprint(tables.tables, url_prefix="/api")
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
||||
112
src/tables.py
112
src/tables.py
@@ -1,5 +1,111 @@
|
||||
from flask import Blueprint, Flask, g, jsonify, request
|
||||
import KeyTables
|
||||
import GameTables
|
||||
from game_tables import GameService, GameTable
|
||||
from key_tables import KeyService, KeyTable
|
||||
import container_managment
|
||||
import random
|
||||
from database import get_db, SmartCursor
|
||||
|
||||
tables = Blueprint("tables_api", __name__)
|
||||
|
||||
@tables.route("/tables", methods=["POST"])
|
||||
def create_table():
|
||||
db = get_db()
|
||||
with SmartCursor(connection=db) as smartCursor:
|
||||
table = GameTable(
|
||||
game_table_id=random.randrange(1, 100000),
|
||||
game_table_name=request.get_json()["table_name"],
|
||||
game_table_link=request.get_json()["table_link"],
|
||||
active=False,
|
||||
docker_id=None,
|
||||
)
|
||||
table.commit(smartCursor)
|
||||
db.commit()
|
||||
return jsonify({}), 200
|
||||
|
||||
@tables.route("/tables/<table_id>")
|
||||
def get_table(table_id):
|
||||
gameService = GameService(get_db())
|
||||
table = gameService.read_by_id(table_id)
|
||||
return jsonify(table.toJson())
|
||||
|
||||
@tables.route("/tables/<int:table_id>", methods=["POST"])
|
||||
def update_table(table_id):
|
||||
return
|
||||
|
||||
@tables.route("tables/<table_id>", methods=["DELETE"])
|
||||
def delete_table(table_id):
|
||||
return
|
||||
|
||||
@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.route("/tables/all")
|
||||
def list_all_tables():
|
||||
gameService = GameService(get_db())
|
||||
tables = gameService.read_all()
|
||||
return jsonify([table.toJson() for table in tables])
|
||||
|
||||
|
||||
@tables.route("/tables/inactive")
|
||||
def list_inactive_tables():
|
||||
return
|
||||
|
||||
@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)
|
||||
|
||||
keys = keyService.get_free_keys(cursor)
|
||||
table = gameService.read_by_id(table_id, cursor)
|
||||
|
||||
if table == None:
|
||||
return "No such table", 404
|
||||
|
||||
if table.active:
|
||||
return "Table already active", 400
|
||||
|
||||
if len(keys) == 0:
|
||||
return "No more keys", 400
|
||||
|
||||
keys[0].reserve(table.game_table_id)
|
||||
table.active = True
|
||||
table.docker_id = container_managment.start_foundry_container(table)
|
||||
keys[0].commit(cursor)
|
||||
table.commit(cursor)
|
||||
cursor.close()
|
||||
db.commit()
|
||||
return jsonify(table.toJson())
|
||||
|
||||
@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
|
||||
|
||||
if not table.active:
|
||||
return "Table not active", 400
|
||||
|
||||
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())
|
||||
|
||||
tables_apt = Blueprint("tables_api", __name__)
|
||||
|
||||
Reference in New Issue
Block a user