From 9af4670869537ddac95dd3f16f85d44a46fa5017 Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 25 Oct 2025 20:58:54 +0000 Subject: [PATCH] add countdown & improve --- app.py | 33 +++++++--------- templates/host.html | 91 +++++++++++++++++++++++++++++++------------ templates/player.html | 9 +++-- templates/upload.html | 2 +- 4 files changed, 87 insertions(+), 48 deletions(-) diff --git a/app.py b/app.py index de39ff5..ba2b5b0 100644 --- a/app.py +++ b/app.py @@ -127,9 +127,10 @@ def upload(): # build per-game question list questions = [] - for _, row in df.iterrows(): + for i, (_, row) in enumerate(df.iterrows()): col = row['correct'] questions.append({ + 'qid': i, 'question': str(row['question']), 'options': [str(row[f'answer{i}']) for i in range(1, 5)], 'correct': str(row[col]) @@ -342,17 +343,18 @@ def on_submit_answer(data): if not secret or secret not in games or pid not in games[secret]['players']: return game = games[secret] + + curr = game['questions'][game['current_q']] + if int(data.get('qid', -1)) != int(curr['qid']): + return + if game.get('revealed'): return if any(pid == t[0] for t in game['answered']): return - - # capture reaction time now = time.perf_counter() q_started = game.get('q_started', now) rt = max(0.0, now - q_started) - - # store (pid, answer, rt) game['answered'].append((pid, data.get('answer'), rt)) _score_and_emit(secret) @@ -363,12 +365,11 @@ def on_next_question(): if session.get('role') != 'host' or not secret or secret not in games: return game = games[secret] + game['revealed'] = True game['current_q'] += 1 - if game['current_q'] < len(game['questions']): _send_question(secret) else: - # mark finished & emit final boards game['finished'] = True board = sorted( [{'name':v['name'],'score':v['score']} for v in game['players'].values()], @@ -377,12 +378,7 @@ def on_next_question(): socketio.emit('game_over', {'board': board}, room='host') for psid,pdata in game['players'].items(): placement = next(i+1 for i,p in enumerate(board) if p['name']==pdata['name']) - socketio.emit( - 'game_over', - {'placement': placement, 'score': pdata['score']}, - room=psid - ) - + socketio.emit('game_over', {'placement': placement, 'score': pdata['score']}, room=psid) @socketio.on('rejoin_game') @@ -449,12 +445,13 @@ def _send_question(secret): game['revealed'] = False q = game['questions'][game['current_q']] game['answered'].clear() - - # NEW: start monotonic timer for this question game['q_started'] = time.perf_counter() - game['q_deadline_s'] = 20 # configurable per question if you want - - socketio.emit('new_question', {'question': q['question'], 'options': q['options']}, room='player') + game['q_deadline_s'] = 20 + socketio.emit('new_question', { + 'qid': q['qid'], + 'question': q['question'], + 'options': q['options'] + }, room='player') socketio.emit('new_question', {'question': q['question']}, room='host') diff --git a/templates/host.html b/templates/host.html index 864e3aa..6e891de 100644 --- a/templates/host.html +++ b/templates/host.html @@ -102,7 +102,7 @@