🚧 Some progress with ClassQuizController

This commit is contained in:
Mawoka
2023-04-15 13:24:36 +02:00
parent ac18621672
commit 9d685e25df
17 changed files with 2061 additions and 1496 deletions
@@ -7,6 +7,7 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import os
import uuid
from datetime import datetime
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
@@ -29,7 +30,7 @@ class JoinGameResponse(BaseModel):
@router.post("/join")
async def join_game(data: JoinGameInput):
async def join_game(data: JoinGameInput) -> JoinGameResponse:
controller = await Controllers.objects.get_or_none(id=data.id, secret_key=data.secret_key)
game_pin = await redis.get(f"game:cqc:code:{data.code}")
if game_pin is None:
@@ -65,4 +66,17 @@ async def register_with_code(data: RegisterWithCodeInput) -> RegisterWithCodeRes
await redis.delete(f"controller_setup:{data.code}")
c_id = uuid.UUID(c_id)
controller = await Controllers.objects.get(id=c_id)
controller.first_seen = datetime.now()
controller.last_seen = datetime.now()
await controller.update()
return RegisterWithCodeResponse(id=controller.id, secret_key=controller.secret_key)
@router.get("/ping")
async def ping_server(id: uuid.UUID, secret_key: str, version: str):
controller = await Controllers.objects.get_or_none(id=id, secret_key=secret_key)
if controller is None:
raise HTTPException(status_code=404, detail="Key and/or id invalid")
controller.last_seen = datetime.now()
controller.os_version = version
await controller.update()
@@ -78,16 +78,16 @@ class WebSocketRequest(BaseModel):
wss_clients = {}
@router.websocket("/{id}")
async def websocket_endpoint(ws: WebSocket, game_id: str, id: str):
@router.websocket("/{game_id}")
async def websocket_endpoint(ws: WebSocket, game_id: str):
try:
if id in wss_clients.keys():
if game_id in wss_clients.keys():
await ws.close(code=status.WS_1001_GOING_AWAY)
print("Client {} already exists.".format(id))
print("Client {} already exists.".format(game_id))
return
print("hI!")
await ws.accept()
wss_clients[id] = ws
wss_clients[game_id] = ws
player_id, game_pin = game_id.split(":")
if player_id is None or game_pin is None:
@@ -105,7 +105,10 @@ async def websocket_endpoint(ws: WebSocket, game_id: str, id: str):
raw_data = await ws.receive_text()
try:
data = WebSocketRequest.parse_raw(raw_data)
except ValidationError:
except ValidationError as e:
print("ValError")
print(e)
print(raw_data)
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="ValidationError").json())
continue
@@ -116,10 +119,10 @@ async def websocket_endpoint(ws: WebSocket, game_id: str, id: str):
except (KeyError, AttributeError):
await ws.send_text(WebSocketRequest(type=WebSocketTypes.Error, data="InvalidButton").json())
continue
print(data)
await submit_answer_fn(answer_index, game_pin, player_id, now)
print("Data from client {}: {}".format(id, data))
print("Data from client {}: {}".format(game_id, data))
except WebSocketDisconnect as ex:
print("Client {} is disconnected: {}".format(id, ex))
wss_clients.pop(id, None)
print("Client {} is disconnected: {}".format(game_id, ex))
wss_clients.pop(game_id, None)