Untested Ormar and FastAPI update

This commit is contained in:
Mawoka
2025-05-20 12:12:06 +02:00
parent 2bfd73412d
commit 03e85f8586
31 changed files with 719 additions and 694 deletions
@@ -35,7 +35,7 @@ async def join_game(data: JoinGameInput) -> JoinGameResponse:
if game_pin is None:
raise HTTPException(status_code=404, detail="Game not found")
game = await redis.get(f"game:{game_pin}")
game = PlayGame.parse_raw(game)
game = PlayGame.model_validate_json(game)
# Check if game is already running
if game.started:
raise HTTPException(status_code=400, detail="Game started already")
@@ -25,7 +25,7 @@ async def submit_answer_fn(data_answer: int, game_pin: str, player_id: str, now:
username = await redis.get(f"game:cqc:player:{player_id}")
if redis_res_game is None or username is None:
raise HTTPException(status_code=404, detail="id not existent")
game = PlayGame.parse_raw(redis_res_game)
game = PlayGame.model_validate_json(redis_res_game)
if not game.question_show:
return
@@ -96,7 +96,7 @@ async def websocket_endpoint(ws: WebSocket, game_id: str):
player_id, game_pin = game_id.split(":")
if player_id is None or game_pin is None:
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="BadId").json())
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="BadId").model_dump_json())
await ws.close(code=status.WS_1003_UNSUPPORTED_DATA)
username = await redis.get(f"game:cqc:player:{player_id}")
await sio.emit(
@@ -104,17 +104,19 @@ async def websocket_endpoint(ws: WebSocket, game_id: str):
{"username": username, "sid": None},
room=f"admin:{game_pin}",
)
await redis.sadd(f"game_session:{game_pin}:players", GamePlayer(username=username, sid=None).json())
await redis.sadd(f"game_session:{game_pin}:players", GamePlayer(username=username, sid=None).model_dump_json())
while True:
raw_data = await ws.receive_text()
try:
data = WebSocketRequest.parse_raw(raw_data)
data = WebSocketRequest.model_validate_json(raw_data)
except ValidationError as e:
print("ValError")
print(e)
print(raw_data)
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="ValidationError").json())
await ws.send_text(
WebSocketRequest(type=WebSocketTypes.Error, data="ValidationError").model_dump_json()
)
continue
if data.type == WebSocketTypes.ButtonPress:
@@ -122,7 +124,9 @@ async def websocket_endpoint(ws: WebSocket, game_id: str):
try:
answer_index = button_to_index_map[data.data.lower()]
except (KeyError, AttributeError):
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").json())
await ws.send_text(
WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").model_dump_json()
)
continue
await submit_answer_fn(answer_index, game_pin, player_id, now)
+4 -4
View File
@@ -18,7 +18,7 @@ router = APIRouter()
class SetControllerUpInput(BaseModel):
player_name: str | None
player_name: str | None = None
name: str
@@ -57,7 +57,7 @@ async def get_controller(id: uuid.UUID, user: User = Depends(get_current_user))
controller = await Controller.objects.get_or_none(id=id, user=user.id)
if controller is None:
raise HTTPException(status_code=404, detail="Controller not found")
return GetControllerResponse(**controller.dict())
return GetControllerResponse(**controller.model_dump())
class ModifyControllerInput(BaseModel):
@@ -76,7 +76,7 @@ async def modify_controller(
controller.player_name = data.player_name
controller.name = data.name
await controller.update()
return GetControllerResponse(**controller.dict())
return GetControllerResponse(**controller.model_dump())
@router.get("/list")
@@ -86,7 +86,7 @@ async def get_all_controllers(user: User = Depends(get_current_user)) -> list[Ge
return []
return_list = []
for controller in controllers:
return_list.append(GetControllerResponse(**controller.dict()))
return_list.append(GetControllerResponse(**controller.model_dump()))
return return_list