Use session storage for client management
This commit is contained in:
parent
3723cfb595
commit
528c98587e
@ -1,5 +1,5 @@
|
||||
from flask import Flask, request
|
||||
from flask_socketio import SocketIO, emit, join_room, rooms
|
||||
from flask import Flask, request, session
|
||||
from flask_socketio import SocketIO, emit, join_room
|
||||
import requests
|
||||
|
||||
from gencode import make_code
|
||||
@ -10,13 +10,16 @@ socketio = SocketIO(app, cors_allowed_origins="*")
|
||||
|
||||
games = {}
|
||||
|
||||
## Display
|
||||
|
||||
@socketio.on('new-game')
|
||||
def on_newgame():
|
||||
data = {}
|
||||
code = make_code()
|
||||
games[code] = Game(code)
|
||||
join_room(code)
|
||||
emit('set-code', {'code': code})
|
||||
session.room = make_code()
|
||||
session.game = Game(session.room)
|
||||
games[session.room] = session.game
|
||||
join_room(session.room)
|
||||
emit('set-code', {'code': session.room})
|
||||
|
||||
## Contestant
|
||||
|
||||
@ -24,27 +27,27 @@ def on_newgame():
|
||||
def on_join_contestant(data):
|
||||
sid = request.sid
|
||||
signature = data['signature']
|
||||
room = data['room']
|
||||
join_room(room)
|
||||
games[room].add_contestant(sid, signature)
|
||||
emit('contestant-joined', {'sid': sid, 'signature': signature }, to=room)
|
||||
session.room = data['room']
|
||||
join_room(session.room)
|
||||
session.game = games[session.room]
|
||||
session.game.add_contestant(sid, signature)
|
||||
emit('contestant-joined', {'sid': sid, 'signature': signature }, to=session.room)
|
||||
|
||||
## Host
|
||||
|
||||
@socketio.on('host-join')
|
||||
def on_join_host(data):
|
||||
sid = request.sid
|
||||
room = data['room']
|
||||
join_room(room)
|
||||
games[room].add_host(sid)
|
||||
emit('host-joined', {'sid': sid}, to=room)
|
||||
session.room = data['room']
|
||||
join_room(session.room)
|
||||
session.game = games[session.room]
|
||||
session.game.add_host(sid)
|
||||
emit('host-joined', {'sid': sid}, to=session.room)
|
||||
|
||||
@socketio.on('host-start')
|
||||
def on_host_start():
|
||||
room = rooms()[0]
|
||||
game = games[room]
|
||||
game.get_questions()
|
||||
emit('categories', [c.name for c in game.categories[:6]], to=room)
|
||||
session.game.get_questions()
|
||||
emit('categories', [c.name for c in session.game.categories[:6]], to=session.room)
|
||||
|
||||
def main():
|
||||
socketio.run(app, host='0.0.0.0', debug=True)
|
||||
|
Loading…
Reference in New Issue
Block a user