🐛 Fixed Controller answering multiple times
This commit is contained in:
@@ -28,14 +28,15 @@ async def submit_answer_fn(data_answer: int, game_pin: str, player_id: str, now:
|
|||||||
game = PlayGame.parse_raw(redis_res_game)
|
game = PlayGame.parse_raw(redis_res_game)
|
||||||
if not game.question_show:
|
if not game.question_show:
|
||||||
return
|
return
|
||||||
if await redis.get(f"answer_given:{player_id}:{game.current_question}") is not None:
|
|
||||||
return
|
|
||||||
question = game.questions[game.current_question]
|
question = game.questions[game.current_question]
|
||||||
question_time = datetime.fromisoformat(await redis.get(f"game:{game_pin}:current_time"))
|
question_time = datetime.fromisoformat(await redis.get(f"game:{game_pin}:current_time"))
|
||||||
try:
|
try:
|
||||||
selected_answer = question.answers[data_answer].answer
|
selected_answer = question.answers[data_answer].answer
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
|
if await redis.get(f"answer_given:{player_id}:{game.current_question}") is not None:
|
||||||
|
return
|
||||||
answer_right = False
|
answer_right = False
|
||||||
if question.type == QuizQuestionType.ABCD:
|
if question.type == QuizQuestionType.ABCD:
|
||||||
for answer in question.answers:
|
for answer in question.answers:
|
||||||
@@ -50,6 +51,7 @@ async def submit_answer_fn(data_answer: int, game_pin: str, player_id: str, now:
|
|||||||
score = 0
|
score = 0
|
||||||
if answer_right:
|
if answer_right:
|
||||||
score = calculate_score(abs(diff), int(float(question.time)))
|
score = calculate_score(abs(diff), int(float(question.time)))
|
||||||
|
await redis.set(f"answer_given:{player_id}:{game.current_question}", "True", ex=600)
|
||||||
await redis.hincrby(f"game_session:{game_pin}:player_scores", username, score)
|
await redis.hincrby(f"game_session:{game_pin}:player_scores", username, score)
|
||||||
answer_data = AnswerData(
|
answer_data = AnswerData(
|
||||||
username=username,
|
username=username,
|
||||||
@@ -122,7 +124,6 @@ async def websocket_endpoint(ws: WebSocket, game_id: str):
|
|||||||
except (KeyError, AttributeError):
|
except (KeyError, AttributeError):
|
||||||
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").json())
|
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").json())
|
||||||
continue
|
continue
|
||||||
await redis.set(f"answer_given:{player_id}:{answer_index}", "True", ex=600)
|
|
||||||
await submit_answer_fn(answer_index, game_pin, player_id, now)
|
await submit_answer_fn(answer_index, game_pin, player_id, now)
|
||||||
|
|
||||||
except WebSocketDisconnect as ex:
|
except WebSocketDisconnect as ex:
|
||||||
|
|||||||
Reference in New Issue
Block a user