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,
|
||||
|
||||
@@ -5,7 +5,14 @@ from django.test import TestCase
|
||||
|
||||
from fupogfakta.models import Category, GameSession, Guess, LieAnswer, Player, Question, RoundConfig, RoundQuestion, ScoreEvent
|
||||
from fupogfakta.payloads import build_lie_started_payload, build_reveal_payload
|
||||
from fupogfakta.services import get_current_round_question, prepare_mixed_answers, resolve_scores, select_round_question
|
||||
from fupogfakta.services import (
|
||||
finish_game,
|
||||
get_current_round_question,
|
||||
prepare_mixed_answers,
|
||||
resolve_scores,
|
||||
select_round_question,
|
||||
start_next_round,
|
||||
)
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -63,6 +70,46 @@ class FupOgFaktaExtractionSliceTests(TestCase):
|
||||
round_question.refresh_from_db()
|
||||
self.assertEqual(round_question.mixed_answers, answers)
|
||||
|
||||
def test_start_next_round_moves_scoreboard_transition_into_service(self):
|
||||
self.session.status = GameSession.Status.SCOREBOARD
|
||||
self.session.save(update_fields=["status"])
|
||||
|
||||
result = start_next_round(self.session)
|
||||
|
||||
self.session.refresh_from_db()
|
||||
self.assertTrue(result.should_broadcast)
|
||||
self.assertEqual(result.session.status, GameSession.Status.LIE)
|
||||
self.assertEqual(result.session.current_round, 2)
|
||||
self.assertEqual(result.round_config.number, 2)
|
||||
self.assertTrue(result.round_config.started_from_scoreboard)
|
||||
self.assertEqual(result.round_question.round_number, 2)
|
||||
|
||||
def test_start_next_round_rejects_plain_lie_without_scoreboard_marker(self):
|
||||
self.session.status = GameSession.Status.LIE
|
||||
self.session.current_round = 2
|
||||
self.session.save(update_fields=["status", "current_round"])
|
||||
RoundConfig.objects.create(session=self.session, number=2, category=self.category, started_from_scoreboard=False)
|
||||
RoundQuestion.objects.create(
|
||||
session=self.session,
|
||||
round_number=2,
|
||||
question=self.question_two,
|
||||
correct_answer=self.question_two.correct_answer,
|
||||
)
|
||||
|
||||
with self.assertRaisesMessage(ValueError, "next_round_invalid_phase"):
|
||||
start_next_round(self.session)
|
||||
|
||||
def test_finish_game_moves_scoreboard_transition_into_service(self):
|
||||
self.session.status = GameSession.Status.SCOREBOARD
|
||||
self.session.save(update_fields=["status"])
|
||||
|
||||
result = finish_game(self.session)
|
||||
|
||||
self.session.refresh_from_db()
|
||||
self.assertTrue(result.should_broadcast)
|
||||
self.assertEqual(result.session.status, GameSession.Status.FINISHED)
|
||||
self.assertEqual(self.session.status, GameSession.Status.FINISHED)
|
||||
|
||||
def test_resolve_scores_applies_correct_and_bluff_points(self):
|
||||
round_question = RoundQuestion.objects.create(
|
||||
session=self.session,
|
||||
|
||||
Reference in New Issue
Block a user