🚑 Fixed live-API

This commit is contained in:
Mawoka
2022-10-04 20:59:13 +02:00
parent 228accea8e
commit b25d16c21e
2 changed files with 14 additions and 3 deletions
+12 -1
View File
@@ -5,7 +5,7 @@
import json import json
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from pydantic import BaseModel from pydantic import BaseModel, validator
from classquiz.config import settings, redis from classquiz.config import settings, redis
from classquiz.db.models import ( from classquiz.db.models import (
PlayGame, PlayGame,
@@ -38,6 +38,17 @@ class _ABCDQuizAnswer(ABCDQuizAnswer):
class _QuizQuestion(QuizQuestion): class _QuizQuestion(QuizQuestion):
answers: list[ABCDQuizAnswer] | RangeQuizAnswer | list[VotingQuizAnswer] 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): class _GetLivePlayGame(PlayGame):
questions: list[_QuizQuestion] questions: list[_QuizQuestion]
+2 -2
View File
@@ -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()) await redis.sadd(f"game_session:{data.game_pin}:players", GamePlayer(username=data.username, sid=sid).json())
if data.custom_field == "": if data.custom_field == "":
data.custom_field = None data.custom_field = None
print(data.custom_field)
if data.custom_field is not None: 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( # await redis.set(
# f"game_session:{data.game_pin}", # f"game_session:{data.game_pin}",
# GameSession(admin=redis_res.admin, game_id=redis_res.game_id, answers=[]).json(), # GameSession(admin=redis_res.admin, game_id=redis_res.game_id, answers=[]).json(),