Merge branch 'main' into game_logic

This commit is contained in:
Djalim Simaila 2023-01-08 16:24:43 +01:00
commit 59f9965925
7 changed files with 62 additions and 31 deletions

2
app.py
View File

@ -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__": if __name__ == "__main__":
app.run() app.run()

View File

@ -1,2 +1,3 @@
Flask==2.2.2 Flask==2.2.2
pyjwt==2.6.0 pyjwt==2.6.0
Flask-SocketIO==5.3.2

View File

@ -1,8 +1,8 @@
import json import json
import pytest import pytest
from truthseeker import app from truthseeker import APP
test_app = app.test_client() test_app = APP.test_client()
class TestException(Exception): class TestException(Exception):
__test__ = False __test__ = False

View File

@ -1,28 +1,38 @@
import flask import flask
from flask_socketio import SocketIO
import os 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): def run_app(self):
if os.path.isfile("instance/secret.txt"): self.socketio_app.run(self)
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 !")
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") from truthseeker.routes import routes_api, routes_ui, routes_socketio
app.register_blueprint(routes_ui.routes_ui, url_prefix="/")
APP.register_blueprint(routes_api.routes_api, url_prefix="/api/v1")
APP.register_blueprint(routes_ui.routes_ui, url_prefix="/")

View File

@ -2,12 +2,11 @@ import string
import random import random
from truthseeker.logic.data_persistance.data_access import * from truthseeker.logic.data_persistance.data_access import *
from datetime import datetime, timedelta from datetime import datetime, timedelta
from truthseeker import APP
# Map of all actively running games # Map of all actively running games
# games_list["game.game_id"]-> game info linked to that id # games_list["game.game_id"]-> game info linked to that id
games_list = {}
def random_string(length: int) ->str: def random_string(length: int) ->str:
""" """
@ -92,12 +91,12 @@ def create_game(owner):
game.owner = owner game.owner = owner
game.members.append(Member(owner)) game.members.append(Member(owner))
game.game_id = random_string(6) game.game_id = random_string(6)
games_list[game.game_id] = game APP.games_list[game.game_id] = game
return game return game
def get_game(game_id): def get_game(game_id):
if game_id in games_list: if game_id in APP.games_list:
return games_list[game_id] return APP.games_list[game_id]
else: else:
return None return None
@ -111,8 +110,8 @@ def get_game_info(game_id):
: return : The Game Object linked to the game_id : return : The Game Object linked to the game_id
: return type : Game : return type : Game
""" """
if game_id in games_list: if game_id in APP.games_list:
return games_list[game_id] return APP.games_list[game_id]
else: else:
return None return None

View File

@ -1,6 +1,6 @@
import flask import flask
import truthseeker from truthseeker import APP
from truthseeker.logic import game_logic from truthseeker.logic import game_logic
from truthseeker.utils import check_username from truthseeker.utils import check_username
@ -46,6 +46,8 @@ def join_game():
flask.session["is_owner"] = False flask.session["is_owner"] = False
flask.session["username"] = username flask.session["username"] = username
APP.socketio_app.emit("playersjoin", [flask.session["username"]], room="game."+game.game_id)
return {"error": 0} return {"error": 0}
@routes_api.route("/startGame", methods=["GET", "POST"]) @routes_api.route("/startGame", methods=["GET", "POST"])
@ -61,7 +63,7 @@ def start_game():
return {"error": 1, "msg": "this game is already started"} return {"error": 1, "msg": "this game is already started"}
game.generate_data() game.generate_data()
game.has_started = True game.has_started = True
APP.socketio_app.emit("gamestart", {}, room="game."+game.game_id)
return {"error": 0} return {"error": 0}
@routes_api.route("/getGameData", methods=["GET", "POST"]) @routes_api.route("/getGameData", methods=["GET", "POST"])

View File

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