✨ Implemented live-API with quiz-id instead of game-pin
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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"})}
|
||||
|
||||
|
||||
|
||||
@@ -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",
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user