✨ Added custom background-color
This commit is contained in:
@@ -111,6 +111,7 @@ class QuizInput(BaseModel):
|
||||
title: str
|
||||
description: str
|
||||
cover_image: str | None
|
||||
background_color: str | None
|
||||
questions: list[QuizQuestion]
|
||||
|
||||
|
||||
@@ -125,6 +126,7 @@ class Quiz(ormar.Model):
|
||||
questions: Json[list[QuizQuestion]] = ormar.JSON(nullable=False)
|
||||
imported_from_kahoot: Optional[bool] = ormar.Boolean(default=False, nullable=True)
|
||||
cover_image: Optional[str] = ormar.Text(nullable=True, unique=False)
|
||||
background_color: str | None = ormar.Text(nullable=True, unique=False)
|
||||
|
||||
class Meta:
|
||||
tablename = "quiz"
|
||||
@@ -171,6 +173,7 @@ class PlayGame(BaseModel):
|
||||
cover_image: str | None
|
||||
game_mode: str | None
|
||||
current_question: int = -1
|
||||
background_color: str | None
|
||||
|
||||
|
||||
class GamePlayer(BaseModel):
|
||||
|
||||
@@ -177,6 +177,7 @@ async def finish_edit(edit_id: str, quiz_input: QuizInput):
|
||||
quiz.updated_at = datetime.now()
|
||||
quiz.questions = quiz_input.dict()["questions"]
|
||||
quiz.cover_image = quiz_input.cover_image
|
||||
quiz.background_color = quiz_input.background_color
|
||||
for image in images_to_delete:
|
||||
if image is not None:
|
||||
try:
|
||||
|
||||
@@ -113,6 +113,7 @@ async def start_quiz(
|
||||
cover_image=quiz.cover_image,
|
||||
game_mode=game_mode,
|
||||
user_id=user.id,
|
||||
background_color=quiz.background_color,
|
||||
)
|
||||
await redis.set(f"game:{str(game.game_pin)}", game.json(), ex=18000)
|
||||
return {**quiz.dict(exclude={"id"}), **game.dict(exclude={"questions"})}
|
||||
|
||||
@@ -201,14 +201,16 @@ async def set_question_number(sid, data: str):
|
||||
if session["admin"]:
|
||||
game_pin = session["game_pin"]
|
||||
game_data = PlayGame.parse_raw(await redis.get(f"game:{session['game_pin']}"))
|
||||
game_data.current_question = int(data)
|
||||
game_data.current_question = int(float(data))
|
||||
await redis.set(f"game:{session['game_pin']}", game_data.json())
|
||||
await redis.set(f"game:{session['game_pin']}:current_time", datetime.now().isoformat())
|
||||
await sio.emit(
|
||||
"set_question_number",
|
||||
{
|
||||
"question_index": int(data),
|
||||
"question": ReturnQuestion(**game_data.dict(include={"questions"})["questions"][int(data)]).dict(),
|
||||
"question_index": int(float(data)),
|
||||
"question": ReturnQuestion(
|
||||
**game_data.dict(include={"questions"})["questions"][int(float(data))]
|
||||
).dict(),
|
||||
},
|
||||
room=game_pin,
|
||||
)
|
||||
@@ -244,21 +246,21 @@ async def submit_answer(sid: str, data: dict):
|
||||
session = await sio.get_session(sid)
|
||||
game_data = PlayGame.parse_raw(await redis.get(f"game:{session['game_pin']}"))
|
||||
answer_right = False
|
||||
if game_data.questions[int(data.question_index)].type == QuizQuestionType.ABCD:
|
||||
for answer in game_data.questions[int(data.question_index)].answers:
|
||||
if game_data.questions[int(float(data.question_index))].type == QuizQuestionType.ABCD:
|
||||
for answer in game_data.questions[int(float(data.question_index))].answers:
|
||||
if answer.answer == data.answer and answer.right:
|
||||
answer_right = True
|
||||
break
|
||||
elif game_data.questions[int(data.question_index)].type == QuizQuestionType.RANGE:
|
||||
elif game_data.questions[int(float(data.question_index))].type == QuizQuestionType.RANGE:
|
||||
if (
|
||||
game_data.questions[int(data.question_index)].answers.min_correct
|
||||
<= int(data.answer)
|
||||
<= game_data.questions[int(data.question_index)].answers.max_correct
|
||||
game_data.questions[int(float(data.question_index))].answers.min_correct
|
||||
<= int(float(data.answer))
|
||||
<= game_data.questions[int(float(data.question_index))].answers.max_correct
|
||||
):
|
||||
answer_right = True
|
||||
else:
|
||||
raise NotImplementedError
|
||||
latency = int((await sio.get_session(sid))["ping"])
|
||||
latency = int(float((await sio.get_session(sid))["ping"]))
|
||||
time_q_started = datetime.fromisoformat(await redis.get(f"game:{session['game_pin']}:current_time"))
|
||||
answers = await redis.get(f"game_session:{session['game_pin']}:{data.question_index}")
|
||||
diff = (time_q_started - now).total_seconds() * 1000 # - timedelta(milliseconds=latency)
|
||||
@@ -271,7 +273,9 @@ async def submit_answer(sid: str, data: dict):
|
||||
|
||||
score = 0
|
||||
if answer_right:
|
||||
score = calculate_score(abs(diff) - latency, int(game_data.questions[int(data.question_index)].time))
|
||||
score = calculate_score(
|
||||
abs(diff) - latency, int(float(game_data.questions[int(float(data.question_index))].time))
|
||||
)
|
||||
await redis.hincrby(f"game_session:{session['game_pin']}:player_scores", session["username"], score)
|
||||
if answers is None:
|
||||
await redis.set(
|
||||
|
||||
Reference in New Issue
Block a user