Use session storage for client management

This commit is contained in:
Dane Johnson 2023-02-12 13:00:03 -06:00
parent 3723cfb595
commit 528c98587e

View File

@ -1,5 +1,5 @@
from flask import Flask, request from flask import Flask, request, session
from flask_socketio import SocketIO, emit, join_room, rooms from flask_socketio import SocketIO, emit, join_room
import requests import requests
from gencode import make_code from gencode import make_code
@ -10,13 +10,16 @@ socketio = SocketIO(app, cors_allowed_origins="*")
games = {} games = {}
## Display
@socketio.on('new-game') @socketio.on('new-game')
def on_newgame(): def on_newgame():
data = {} data = {}
code = make_code() session.room = make_code()
games[code] = Game(code) session.game = Game(session.room)
join_room(code) games[session.room] = session.game
emit('set-code', {'code': code}) join_room(session.room)
emit('set-code', {'code': session.room})
## Contestant ## Contestant
@ -24,27 +27,27 @@ def on_newgame():
def on_join_contestant(data): def on_join_contestant(data):
sid = request.sid sid = request.sid
signature = data['signature'] signature = data['signature']
room = data['room'] session.room = data['room']
join_room(room) join_room(session.room)
games[room].add_contestant(sid, signature) session.game = games[session.room]
emit('contestant-joined', {'sid': sid, 'signature': signature }, to=room) session.game.add_contestant(sid, signature)
emit('contestant-joined', {'sid': sid, 'signature': signature }, to=session.room)
## Host ## Host
@socketio.on('host-join') @socketio.on('host-join')
def on_join_host(data): def on_join_host(data):
sid = request.sid sid = request.sid
room = data['room'] session.room = data['room']
join_room(room) join_room(session.room)
games[room].add_host(sid) session.game = games[session.room]
emit('host-joined', {'sid': sid}, to=room) session.game.add_host(sid)
emit('host-joined', {'sid': sid}, to=session.room)
@socketio.on('host-start') @socketio.on('host-start')
def on_host_start(): def on_host_start():
room = rooms()[0] session.game.get_questions()
game = games[room] emit('categories', [c.name for c in session.game.categories[:6]], to=session.room)
game.get_questions()
emit('categories', [c.name for c in game.categories[:6]], to=room)
def main(): def main():
socketio.run(app, host='0.0.0.0', debug=True) socketio.run(app, host='0.0.0.0', debug=True)