diff --git a/classquiz/routers/live.py b/classquiz/routers/live.py index e140bd7..976a75a 100644 --- a/classquiz/routers/live.py +++ b/classquiz/routers/live.py @@ -51,10 +51,13 @@ class GetLiveDataResponse(BaseModel): @router.get("/") async def get_live_game_data( - game_pin: int, api_key: str, player_count_as_a_string: bool = False, in_array: bool = False + game_pin: str, api_key: str, player_count_as_a_string: bool = False, in_array: bool = False ): user_id = await check_api_key(api_key) redis_res = await redis.get(f"game:{game_pin}") + if redis_res is None: + game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}") + redis_res = await redis.get(f"game:{game_pin}") if redis_res is None or user_id is None: raise HTTPException(status_code=404, detail="Game not found or API key not found") game = PlayGame.parse_raw(redis_res) @@ -96,9 +99,13 @@ async def get_live_game_data( @router.get("/user_count") -async def get_game_user_count(game_pin: int, as_string: bool = False): +async def get_game_user_count(game_pin: str, api_key: str, as_string: bool = False): # if redis_res is None: # raise HTTPException(status_code=404, detail="Game not found") + user_id = await check_api_key(api_key) + redis_res = await redis.get(f"game_session:{game_pin}") + if redis_res is None: + game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}") player_count = await redis.scard(f"game_session:{game_pin}:players") if as_string: return {"players": {"count": str(player_count)}} @@ -115,9 +122,12 @@ async def get_game_user_count(game_pin: int, as_string: bool = False): @router.get( "/players", ) -async def get_game_session(game_pin: int, api_key: str): +async def get_game_session(game_pin: str, api_key: str): user_id = await check_api_key(api_key) redis_res = await redis.get(f"game_session:{game_pin}") + if redis_res is None: + game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}") + redis_res = await redis.get(f"game_session:{game_pin}") if redis_res is None or user_id is None: raise HTTPException(status_code=404, detail="Game not found or API key not found") data = GameSession.parse_raw(redis_res) @@ -140,9 +150,12 @@ async def get_game_session(game_pin: int, api_key: str): @router.post("/set_question") -async def set_next_question(game_pin: int, question_number: int, api_key: str): +async def set_next_question(game_pin: str, question_number: int, api_key: str): user_id = await check_api_key(api_key) redis_res = await redis.get(f"game:{game_pin}") + if redis_res is None: + game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}") + redis_res = await redis.get(f"game:{game_pin}") if redis_res is None or user_id is None: raise HTTPException(status_code=404, detail="Game not found or API key not found") game_data = PlayGame.parse_raw(redis_res) @@ -161,7 +174,14 @@ async def set_next_question(game_pin: int, question_number: int, api_key: str): @router.get("/scores") -async def get_live_player_scores(game_pin: int): +async def get_live_player_scores(game_pin: str, api_key: str): + user_id = await check_api_key(api_key) + redis_res = await redis.get(f"game:{game_pin}") + if redis_res is None: + game_pin = await redis.get(f"game_pin:{user_id}:{game_pin}") + redis_res = await redis.get(f"game:{game_pin}") + if redis_res is None or user_id is None: + raise HTTPException(status_code=404, detail="Game not found or API key not found") res = await redis.hgetall(f"game_session:{game_pin}:player_scores") return_arr = [] for username in res: diff --git a/classquiz/routers/quiz.py b/classquiz/routers/quiz.py index 1d3c48f..d63044a 100644 --- a/classquiz/routers/quiz.py +++ b/classquiz/routers/quiz.py @@ -115,6 +115,7 @@ async def start_quiz( background_color=quiz.background_color, ) await redis.set(f"game:{str(game.game_pin)}", game.json(), ex=18000) + await redis.set(f"game_pin:{user.id}:{quiz_id}", game_pin, ex=18000) return {**quiz.dict(exclude={"id"}), **game.dict(exclude={"questions"})} diff --git a/classquiz/socket_server/__init__.py b/classquiz/socket_server/__init__.py index 29e3bfd..df3f810 100644 --- a/classquiz/socket_server/__init__.py +++ b/classquiz/socket_server/__init__.py @@ -194,7 +194,8 @@ class ReturnQuestion(QuizQuestion): if values["type"] == QuizQuestionType.RANGE and type(v) != RangeQuizAnswerWithoutSolution: raise ValueError("Answer must be from type RangeQuizAnswer if type is RANGE") if values["type"] == QuizQuestionType.VOTING and type(v[0]) != VotingQuizAnswer: - print("Answer must be from type VotingQuizAnswer if type is VOTING") + # print("Answer must be from type VotingQuizAnswer if type is VOTING") + pass return v @@ -208,12 +209,10 @@ async def set_question_number(sid, data: str): game_data.current_question = int(float(data)) await redis.set(f"game:{session['game_pin']}", game_data.json()) await redis.set(f"game:{session['game_pin']}:current_time", datetime.now().isoformat()) - # print(game_data.dict(include={"questions"})["questions"][int(float(data))]) temp_return = game_data.dict(include={"questions"})["questions"][int(float(data))] if game_data.questions[int(float(data))].type == QuizQuestionType.VOTING: for i in range(len(temp_return["answers"])): temp_return["answers"][i] = VotingQuizAnswer(**temp_return["answers"][i]) - print(temp_return) await sio.emit( "set_question_number", { diff --git a/frontend/src/routes/test/+page.svelte b/frontend/src/routes/test/+page.svelte deleted file mode 100644 index fc47f52..0000000 --- a/frontend/src/routes/test/+page.svelte +++ /dev/null @@ -1,129 +0,0 @@ - - - -
{answer}
-