From c8402c697a3b8fe8da3e33f2c037e3d6544e0fb0 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Sun, 25 Sep 2022 21:04:38 +0200 Subject: [PATCH] :sparkles: Endpoint for live player-scores --- classquiz/routers/quiz.py | 6 ++++++ classquiz/socket_server/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/classquiz/routers/quiz.py b/classquiz/routers/quiz.py index 3d82447..8bde33c 100644 --- a/classquiz/routers/quiz.py +++ b/classquiz/routers/quiz.py @@ -348,3 +348,9 @@ async def set_next_question(game_pin: int, question_number: int, api_key: str): }, room=game_pin, ) + + +@router.get("/live/scores", tags=["live"]) +async def get_live_player_scores(game_pin: int): + res = await redis.hgetall(f"game_session:{game_pin}:player_scores") + return res diff --git a/classquiz/socket_server/__init__.py b/classquiz/socket_server/__init__.py index 8ac4665..f870bbf 100644 --- a/classquiz/socket_server/__init__.py +++ b/classquiz/socket_server/__init__.py @@ -272,7 +272,7 @@ async def submit_answer(sid: str, data: dict): score = 0 if answer_right: score = calculate_score(abs(diff) - latency, int(game_data.questions[int(data.question_index)].time)) - + await redis.hincrby(f"game_session:{session['game_pin']}:player_scores", session["username"], score) if answers is None: await redis.set( f"game_session:{session['game_pin']}:{data.question_index}",