Started working on results-UI

This commit is contained in:
Mawoka
2023-01-31 17:26:19 +01:00
parent a980c70d85
commit bce22c43fc
11 changed files with 297 additions and 15 deletions
+2
View File
@@ -30,6 +30,7 @@ from classquiz.routers import (
remote,
community,
avatar,
results,
)
from classquiz.socket_server import sio
from classquiz.helpers import meilisearch_init, telemetry_ping, bg_tasks
@@ -83,6 +84,7 @@ async def auth_middleware_wrapper(request: Request, call_next):
return await rememberme_middleware(request, call_next)
app.include_router(results.router, tags=["results"], prefix="/api/v1/results", include_in_schema=True)
app.include_router(remote.router, tags=["remote"], prefix="/api/v1/remote", include_in_schema=True)
app.include_router(login.router, tags=["auth"], prefix="/api/v1/login", include_in_schema=True)
+3 -3
View File
@@ -300,12 +300,12 @@ class GameInLobby(BaseModel):
class GameResults(ormar.Model):
id: uuid.UUID = ormar.UUID(primary_key=True)
quiz_id: uuid.UUID = ormar.ForeignKey(Quiz)
user_id: uuid.UUID = ormar.ForeignKey(User)
quiz: uuid.UUID | Quiz = ormar.ForeignKey(Quiz)
user: uuid.UUID | User = ormar.ForeignKey(User)
timestamp: datetime = ormar.DateTime(default=datetime.now(), nullable=False)
player_count: int = ormar.Integer(nullable=False, default=0)
note: str | None = ormar.Text(nullable=True)
answers: Json[AnswerDataList] = ormar.JSON(True)
answers: Json[list[AnswerData]] = ormar.JSON(True)
player_scores: Json[dict[str, str]] = ormar.JSON(nullable=True)
custom_field_data: Json[dict[str, str]] | None = ormar.JSON(nullable=True)
+58
View File
@@ -0,0 +1,58 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from uuid import UUID
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from classquiz.auth import get_current_user
from classquiz.db.models import User, GameResults
router = APIRouter()
@router.get("/list", response_model=list[GameResults])
async def list_game_results(include_quiz: bool = False, user: User = Depends(get_current_user)):
if include_quiz is True:
results = await GameResults.objects.select_related("quiz").all(user=user.id)
else:
results = await GameResults.objects.all(user=user.id)
return results
@router.get("/list/{quiz_id}", response_model=list[GameResults])
async def get_results_by_quiz(quiz_id: UUID, include_quiz: bool = False, user: User = Depends(get_current_user)):
if include_quiz is True:
res = await GameResults.objects.select_related("quiz").all(user=user.id, quiz=quiz_id)
else:
res = await GameResults.objects.all(user=user.id, quiz=quiz_id)
if res is None:
raise HTTPException(status_code=404, detail="Game Result not found")
else:
return res
@router.get("/{game_id}", response_model=GameResults)
async def get_game_result(game_id: UUID, include_quiz: bool = False, user: User = Depends(get_current_user)):
if include_quiz:
res = await GameResults.objects.select_related("quiz").get_or_none(user=user.id, id=game_id)
else:
res = await GameResults.objects.get_or_none(user=user.id, id=game_id)
if res is None:
raise HTTPException(status_code=404, detail="Game Result not found")
else:
return res
class _SetNoteInput(BaseModel):
note: str
@router.post("/set_note", response_model=GameResults)
async def set_note(id: UUID, data: _SetNoteInput, user: User = Depends(get_current_user)):
res = await GameResults.objects.get_or_none(user=user.id, id=id)
if res is None:
raise HTTPException(status_code=404, detail="Game Result not found")
res.note = data.note
return await res.update()
+3 -5
View File
@@ -519,19 +519,17 @@ async def save_quiz(sid: str):
player_count = await redis.scard(f"game_session:{game_pin}:players")
answers = []
for i in range(len(game.questions)):
print("hiqq", i)
redis_res = await redis.get(f"game_session:{game_pin}:{i}")
try:
answers.append(AnswerDataList.parse_raw(redis_res).dict())
answers.append(json.loads(redis_res))
except ValidationError:
answers.append([])
player_scores = await redis.hgetall(f"game_session:{game_pin}:player_scores")
custom_field_data = await redis.hgetall(f"game:{game_pin}:players:custom_fields")
print(custom_field_data)
data = GameResults(
id=game.game_id,
quiz_id=game.quiz_id,
user_id=game.user_id,
quiz=game.quiz_id,
user=game.user_id,
timestamp=datetime.now(),
player_count=player_count,
answers=json.dumps(answers),