From 6fc98c48d9ec31c1eebba25b2628d538bde4e52d Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 28 Jun 2025 22:31:22 +0200 Subject: [PATCH] add show answer --- app.py | 28 +++++++++++++++++++++++++-- templates/host.html | 44 ++++++++++++++++++++++++++++--------------- templates/player.html | 35 ++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index bb6b953..b30638d 100644 --- a/app.py +++ b/app.py @@ -53,7 +53,8 @@ def host(): games[secret] = { 'players': {}, 'order': [], 'current_q': 0, 'answered': [], - 'started': False, 'finished': False + 'started': False, 'finished': False, + 'revealed': False } # build join URL + QR @@ -256,6 +257,8 @@ def on_submit_answer(data): if not secret or secret not in games or pid not in games[secret]['players']: return game = games[secret] + if game.get('revealed'): + return if any(pid == p for p,_ in game['answered']): return @@ -301,7 +304,22 @@ def on_rejoin_game(): join_room('player') join_room(pid) _send_sync_state(pid, game) - + + +@socketio.on('show_answer') +def on_show_answer(): + secret = session.get('secret') + if session.get('role') != 'host' or not secret or secret not in games: + return + game = games[secret] + game['revealed'] = True + # find the correct answer text for current question + correct = questions[game['current_q']]['correct'] + # emit to host so they can display it + emit('answer_revealed', {'correct': correct}, room='host') + # emit to all players so they can highlight it + socketio.emit('answer_revealed', {'correct': correct}, room='player') + # helpers @@ -337,6 +355,7 @@ def _send_sync_state(pid, game): def _send_question(secret): game = games[secret] + game['revealed'] = False q = questions[game['current_q']] # clear previous answers game['answered'].clear() @@ -403,4 +422,9 @@ def _score_and_emit(secret): if __name__ == '__main__': + # show own IP address + import socket + hostname = socket.gethostname() + ip_address = socket.gethostbyname(hostname) + print(f"Running on http://{ip_address}:5000/") socketio.run(app, host='0.0.0.0', port=5000) \ No newline at end of file diff --git a/templates/host.html b/templates/host.html index 114f523..1816bee 100644 --- a/templates/host.html +++ b/templates/host.html @@ -25,14 +25,15 @@ - -