refactor(gameplay): move scoreboard transitions into cartridge service
This commit is contained in:
@@ -1,6 +1,23 @@
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .models import GameSession, Player, Question, RoundConfig, RoundQuestion, ScoreEvent
|
||||
from django.db import transaction
|
||||
|
||||
from .models import GameSession, Guess, LieAnswer, Player, Question, RoundConfig, RoundQuestion, ScoreEvent
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RoundTransitionResult:
|
||||
session: GameSession
|
||||
round_config: RoundConfig
|
||||
round_question: RoundQuestion
|
||||
should_broadcast: bool
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FinishGameResult:
|
||||
session: GameSession
|
||||
should_broadcast: bool
|
||||
|
||||
|
||||
def get_current_round_question(session: GameSession) -> RoundQuestion | None:
|
||||
@@ -13,6 +30,16 @@ def get_current_round_question(session: GameSession) -> RoundQuestion | None:
|
||||
|
||||
|
||||
|
||||
def reset_round_question_bootstrap_state(round_question: RoundQuestion) -> RoundQuestion:
|
||||
Guess.objects.filter(round_question=round_question).delete()
|
||||
LieAnswer.objects.filter(round_question=round_question).delete()
|
||||
if round_question.mixed_answers:
|
||||
round_question.mixed_answers = []
|
||||
round_question.save(update_fields=["mixed_answers"])
|
||||
return round_question
|
||||
|
||||
|
||||
|
||||
def select_round_question(session: GameSession, round_config: RoundConfig) -> RoundQuestion:
|
||||
existing_round_question = get_current_round_question(session)
|
||||
if existing_round_question is not None:
|
||||
@@ -61,6 +88,83 @@ def prepare_mixed_answers(round_question: RoundQuestion) -> list[str]:
|
||||
|
||||
|
||||
|
||||
def start_next_round(session: GameSession) -> RoundTransitionResult:
|
||||
with transaction.atomic():
|
||||
locked_session = GameSession.objects.select_for_update().get(pk=session.pk)
|
||||
next_round_config = None
|
||||
round_question = None
|
||||
should_broadcast = False
|
||||
|
||||
if locked_session.status == GameSession.Status.SCOREBOARD:
|
||||
previous_round_config = RoundConfig.objects.filter(
|
||||
session=locked_session,
|
||||
number=locked_session.current_round,
|
||||
).select_related("category").first()
|
||||
if previous_round_config is None:
|
||||
raise ValueError("round_config_missing")
|
||||
|
||||
next_round_number = locked_session.current_round + 1
|
||||
next_round_config = RoundConfig(
|
||||
session=locked_session,
|
||||
number=next_round_number,
|
||||
category=previous_round_config.category,
|
||||
lie_seconds=previous_round_config.lie_seconds,
|
||||
guess_seconds=previous_round_config.guess_seconds,
|
||||
points_correct=previous_round_config.points_correct,
|
||||
points_bluff=previous_round_config.points_bluff,
|
||||
started_from_scoreboard=True,
|
||||
)
|
||||
locked_session.current_round = next_round_number
|
||||
|
||||
round_question = reset_round_question_bootstrap_state(select_round_question(locked_session, next_round_config))
|
||||
|
||||
next_round_config.save()
|
||||
locked_session.status = GameSession.Status.LIE
|
||||
locked_session.save(update_fields=["current_round", "status"])
|
||||
should_broadcast = True
|
||||
elif locked_session.status == GameSession.Status.LIE:
|
||||
if locked_session.current_round <= 1:
|
||||
raise ValueError("next_round_invalid_phase")
|
||||
|
||||
next_round_config = RoundConfig.objects.filter(
|
||||
session=locked_session,
|
||||
number=locked_session.current_round,
|
||||
).select_related("category").first()
|
||||
round_question = get_current_round_question(locked_session)
|
||||
if (
|
||||
next_round_config is None
|
||||
or not next_round_config.started_from_scoreboard
|
||||
or round_question is None
|
||||
):
|
||||
raise ValueError("next_round_invalid_phase")
|
||||
else:
|
||||
raise ValueError("next_round_invalid_phase")
|
||||
|
||||
return RoundTransitionResult(
|
||||
session=locked_session,
|
||||
round_config=next_round_config,
|
||||
round_question=round_question,
|
||||
should_broadcast=should_broadcast,
|
||||
)
|
||||
|
||||
|
||||
|
||||
def finish_game(session: GameSession) -> FinishGameResult:
|
||||
with transaction.atomic():
|
||||
locked_session = GameSession.objects.select_for_update().get(pk=session.pk)
|
||||
should_broadcast = False
|
||||
|
||||
if locked_session.status == GameSession.Status.SCOREBOARD:
|
||||
locked_session.status = GameSession.Status.FINISHED
|
||||
locked_session.save(update_fields=["status"])
|
||||
should_broadcast = True
|
||||
elif locked_session.status != GameSession.Status.FINISHED:
|
||||
raise ValueError("finish_game_invalid_phase")
|
||||
|
||||
return FinishGameResult(session=locked_session, should_broadcast=should_broadcast)
|
||||
|
||||
|
||||
|
||||
def resolve_scores(
|
||||
session: GameSession,
|
||||
round_question: RoundQuestion,
|
||||
|
||||
Reference in New Issue
Block a user