__init__.py 2.61 KiB
import json
import os
from flask_socketio import SocketIO, send, emit
from utils import ERROR_LOG
from model import Team, Game
SOCKET = None
def init_websocket(app):
global SOCKET
SOCKET = SocketIO(app)
return SOCKET
CURRENT_FRONTEND_VIEW = "intro_screen"
def set_current_view(view):
global CURRENT_FRONTEND_VIEW
CURRENT_FRONTEND_VIEW = view
build_current_round_json()
LAST_SEND_REPLY = ""
def send_reply(html=None):
global LAST_SEND_REPLY
if html is not None:
LAST_SEND_REPLY = html
return LAST_SEND_REPLY
def verify_request_data(model, key, value, log_error=True):
if value is None:
if log_error:
ERROR_LOG.add("Request is incomplete. Missing %s.%s" % (model.__name__, key))
return None
obj = model.get_or_none(**{key: value})
if obj is None:
if log_error:
ERROR_LOG.add("Could not find %s by the identifier '%s':'%s'" % (model.__name__, key, value))
return None
return obj
CLEAN_ROUND = {
"state": "intro_screen",
"points": 0,
"current_teams":["", ""],
"current_team_logos": ["../static/teamlogos/0_no_logo_00.png", "../static/teamlogos/0_no_logo_00.png"],
"current_team_points": [0, 0],
"current_team_fails": [0, 0],
"answer_amount": 0,
"question": "",
"answers": []
}
def build_current_round_json(game=None, path="frontend/current_round.json"):
round_json = dict(CLEAN_ROUND)
round_json["state"] = CURRENT_FRONTEND_VIEW
if game is not None:
round_json["current_teams"] = [game.team_left.name, game.team_right.name]
round_json["current_team_points"] = [game.points_left, game.points_right]
round_json["current_team_logos"] = [game.team_left.logo_path, game.team_right.logo_path]
current_round = game.get_current_round()
if current_round is not None:
question = current_round.question
round_json["points"] = current_round.points
round_json["answer_amount"] = len(question.answers)
round_json["question"] = question.question
round_json["current_team_fails"] = [current_round.error_left, current_round.error_right]
anwser_array = []
for answer in question.answers:
answer_as_json = {
"answer": answer.answer,
"numberOfPeople": answer.points,
"visible": not answer.hidden
}
anwser_array.append(answer_as_json)
round_json["answers"] = anwser_array
SOCKET.emit("round", json.dumps(round_json))
from views.management import mgmt
from views.game import game