diff --git a/app.py b/app.py index 85345e1..0a00714 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from truthseeker import app # the variable 'app' is detected by `flask run` +from truthseeker import APP as app # the variable 'app' is detected by `flask run` if __name__ == "__main__": app.run() diff --git a/requirements.txt b/requirements.txt index d0f5f12..4882615 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ Flask==2.2.2 pyjwt==2.6.0 +Flask-SocketIO==5.3.2 diff --git a/tests/test_api.py b/tests/test_api.py index 4235864..f4924ae 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,8 +1,8 @@ import json import pytest -from truthseeker import app +from truthseeker import APP -test_app = app.test_client() +test_app = APP.test_client() class TestException(Exception): __test__ = False diff --git a/truthseeker/__init__.py b/truthseeker/__init__.py index 0500b4e..14cbd3a 100644 --- a/truthseeker/__init__.py +++ b/truthseeker/__init__.py @@ -1,28 +1,38 @@ import flask +from flask_socketio import SocketIO import os -from truthseeker.routes import routes_api, routes_ui +class TruthSeekerApp(flask.Flask): + def __init__(self): + super().__init__("truthseeker") -app = flask.Flask("truthseeker") + self.games_list = {} + + self.set_app_secret() + self.socketio_app = SocketIO(self) -def set_secret(app): - if os.path.isfile("instance/secret.txt"): - f = open("instance/secret.txt", "r") - app.config["SECRET_KEY"] = f.read() - f.close() - print("Read secret from secret.txt !") - else: - import secrets - app.config["SECRET_KEY"] = secrets.token_hex() - os.makedirs("instance", exist_ok=True) - f = open("instance/secret.txt", "w") - f.write(app.config["SECRET_KEY"]) - f.close() - print("Generated secret and wrote to secret.txt !") + def run_app(self): + self.socketio_app.run(self) -set_secret(app) + def set_app_secret(self): + if os.path.isfile("instance/secret.txt"): + f = open("instance/secret.txt", "r") + self.config["SECRET_KEY"] = f.read() + f.close() + print("Read secret from secret.txt !") + else: + import secrets + self.config["SECRET_KEY"] = secrets.token_hex() + os.makedirs("instance", exist_ok=True) + f = open("instance/secret.txt", "w") + f.write(self.config["SECRET_KEY"]) + f.close() + print("Generated secret and wrote to secret.txt !") +APP = TruthSeekerApp() -app.register_blueprint(routes_api.routes_api, url_prefix="/api/v1") -app.register_blueprint(routes_ui.routes_ui, url_prefix="/") +from truthseeker.routes import routes_api, routes_ui, routes_socketio + +APP.register_blueprint(routes_api.routes_api, url_prefix="/api/v1") +APP.register_blueprint(routes_ui.routes_ui, url_prefix="/") diff --git a/truthseeker/logic/game_logic.py b/truthseeker/logic/game_logic.py index 502a8d9..0b4a556 100644 --- a/truthseeker/logic/game_logic.py +++ b/truthseeker/logic/game_logic.py @@ -2,12 +2,11 @@ import string import random from truthseeker.logic.data_persistance.data_access import * from datetime import datetime, timedelta - +from truthseeker import APP # Map of all actively running games # games_list["game.game_id"]-> game info linked to that id -games_list = {} def random_string(length: int) ->str: """ @@ -92,12 +91,12 @@ def create_game(owner): game.owner = owner game.members.append(Member(owner)) game.game_id = random_string(6) - games_list[game.game_id] = game + APP.games_list[game.game_id] = game return game def get_game(game_id): - if game_id in games_list: - return games_list[game_id] + if game_id in APP.games_list: + return APP.games_list[game_id] else: return None @@ -111,8 +110,8 @@ def get_game_info(game_id): : return : The Game Object linked to the game_id : return type : Game """ - if game_id in games_list: - return games_list[game_id] + if game_id in APP.games_list: + return APP.games_list[game_id] else: return None diff --git a/truthseeker/routes/routes_api.py b/truthseeker/routes/routes_api.py index 13f6461..a22ef7c 100644 --- a/truthseeker/routes/routes_api.py +++ b/truthseeker/routes/routes_api.py @@ -1,6 +1,6 @@ import flask -import truthseeker +from truthseeker import APP from truthseeker.logic import game_logic from truthseeker.utils import check_username @@ -46,6 +46,8 @@ def join_game(): flask.session["is_owner"] = False flask.session["username"] = username + APP.socketio_app.emit("playersjoin", [flask.session["username"]], room="game."+game.game_id) + return {"error": 0} @routes_api.route("/startGame", methods=["GET", "POST"]) @@ -61,7 +63,7 @@ def start_game(): return {"error": 1, "msg": "this game is already started"} game.generate_data() game.has_started = True - + APP.socketio_app.emit("gamestart", {}, room="game."+game.game_id) return {"error": 0} @routes_api.route("/getGameData", methods=["GET", "POST"]) diff --git a/truthseeker/routes/routes_socketio.py b/truthseeker/routes/routes_socketio.py new file mode 100644 index 0000000..d94816e --- /dev/null +++ b/truthseeker/routes/routes_socketio.py @@ -0,0 +1,19 @@ +from flask_socketio import join_room +import socketio + +from truthseeker import APP +from truthseeker.logic import game_logic + +@APP.socketio_app.on('connect') +def connect(auth): + if not (auth and "game_id" in auth): + raise socketio.exceptions.ConnectionRefusedError("Invalid connection data passed") + + game = game_logic.get_game(auth["game_id"]) + if not game: + raise socketio.exceptions.ConnectionRefusedError("No game with this ID") + + room = join_room("game."+auth["game_id"]) + join_room(room) + +