From b25d16c21e6d066681e12cf5a215eed69d0ddf37 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Tue, 4 Oct 2022 20:59:13 +0200 Subject: [PATCH] :ambulance: Fixed live-API --- classquiz/routers/live.py | 13 ++++++++++++- classquiz/socket_server/__init__.py | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/classquiz/routers/live.py b/classquiz/routers/live.py index 31295ca..e796f9c 100644 --- a/classquiz/routers/live.py +++ b/classquiz/routers/live.py @@ -5,7 +5,7 @@ import json from fastapi import APIRouter, HTTPException -from pydantic import BaseModel +from pydantic import BaseModel, validator from classquiz.config import settings, redis from classquiz.db.models import ( PlayGame, @@ -38,6 +38,17 @@ class _ABCDQuizAnswer(ABCDQuizAnswer): class _QuizQuestion(QuizQuestion): answers: list[ABCDQuizAnswer] | RangeQuizAnswer | list[VotingQuizAnswer] + @validator("answers") + def answers_not_none_if_abcd_type(cls, v, values): + if values["type"] == QuizQuestionType.ABCD and type(v[0]) != _ABCDQuizAnswer: + print(type(v[0])) + raise ValueError("Answers can't be none if type is ABCD") + if values["type"] == QuizQuestionType.RANGE and type(v) != RangeQuizAnswer: + raise ValueError("Answer must be from type RangeQuizAnswer if type is RANGE") + if values["type"] == QuizQuestionType.VOTING and type(v[0]) != VotingQuizAnswer: + raise ValueError("Answer must be from type VotingQuizAnswer if type is VOTING") + return v + class _GetLivePlayGame(PlayGame): questions: list[_QuizQuestion] diff --git a/classquiz/socket_server/__init__.py b/classquiz/socket_server/__init__.py index c73d21f..483a6d7 100644 --- a/classquiz/socket_server/__init__.py +++ b/classquiz/socket_server/__init__.py @@ -107,9 +107,9 @@ async def join_game(sid: str, data: dict): await redis.sadd(f"game_session:{data.game_pin}:players", GamePlayer(username=data.username, sid=sid).json()) if data.custom_field == "": data.custom_field = None - print(data.custom_field) if data.custom_field is not None: - await redis.hset(f"game:{data.game_pin}:player:custom_fields", data.username, data.custom_field) + await redis.hset(f"game:{data.game_pin}:players:custom_fields", data.username, data.custom_field) + # await redis.set( # f"game_session:{data.game_pin}", # GameSession(admin=redis_res.admin, game_id=redis_res.game_id, answers=[]).json(),