Merge branch 'main' into game_logic
This commit is contained in:
commit
59f9965925
2
app.py
2
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()
|
||||
|
@ -1,2 +1,3 @@
|
||||
Flask==2.2.2
|
||||
pyjwt==2.6.0
|
||||
Flask-SocketIO==5.3.2
|
||||
|
@ -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
|
||||
|
@ -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="/")
|
||||
|
@ -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
|
||||
|
||||
|
@ -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"])
|
||||
|
19
truthseeker/routes/routes_socketio.py
Normal file
19
truthseeker/routes/routes_socketio.py
Normal 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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user