refactor(gameplay): delegate host transition events from service
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import random
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import GameSession, Guess, LieAnswer, Player, Question, RoundConfig, RoundQuestion, ScoreEvent
|
||||
from .payloads import build_finish_game_phase_event, build_start_next_round_phase_event
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -13,12 +15,16 @@ class RoundTransitionResult:
|
||||
round_config: RoundConfig
|
||||
round_question: RoundQuestion
|
||||
should_broadcast: bool
|
||||
phase_event_name: str | None = None
|
||||
phase_event_payload: dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FinishGameResult:
|
||||
session: GameSession
|
||||
should_broadcast: bool
|
||||
phase_event_name: str | None = None
|
||||
phase_event_payload: dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -110,6 +116,9 @@ def start_next_round(session: GameSession) -> RoundTransitionResult:
|
||||
round_question = None
|
||||
should_broadcast = False
|
||||
|
||||
phase_event_name = None
|
||||
phase_event_payload = None
|
||||
|
||||
if locked_session.status == GameSession.Status.SCOREBOARD:
|
||||
previous_round_config = RoundConfig.objects.filter(
|
||||
session=locked_session,
|
||||
@@ -137,6 +146,9 @@ def start_next_round(session: GameSession) -> RoundTransitionResult:
|
||||
locked_session.status = GameSession.Status.LIE
|
||||
locked_session.save(update_fields=["current_round", "status"])
|
||||
should_broadcast = True
|
||||
phase_event = build_start_next_round_phase_event(locked_session, next_round_config, round_question)
|
||||
phase_event_name = phase_event["name"]
|
||||
phase_event_payload = phase_event["payload"]
|
||||
elif locked_session.status == GameSession.Status.LIE:
|
||||
if locked_session.current_round <= 1:
|
||||
raise ValueError("next_round_invalid_phase")
|
||||
@@ -160,6 +172,8 @@ def start_next_round(session: GameSession) -> RoundTransitionResult:
|
||||
round_config=next_round_config,
|
||||
round_question=round_question,
|
||||
should_broadcast=should_broadcast,
|
||||
phase_event_name=phase_event_name,
|
||||
phase_event_payload=phase_event_payload,
|
||||
)
|
||||
|
||||
|
||||
@@ -168,15 +182,25 @@ def finish_game(session: GameSession) -> FinishGameResult:
|
||||
with transaction.atomic():
|
||||
locked_session = GameSession.objects.select_for_update().get(pk=session.pk)
|
||||
should_broadcast = False
|
||||
phase_event_name = None
|
||||
phase_event_payload = None
|
||||
|
||||
if locked_session.status == GameSession.Status.SCOREBOARD:
|
||||
locked_session.status = GameSession.Status.FINISHED
|
||||
locked_session.save(update_fields=["status"])
|
||||
should_broadcast = True
|
||||
phase_event = build_finish_game_phase_event(locked_session)
|
||||
phase_event_name = phase_event["name"]
|
||||
phase_event_payload = phase_event["payload"]
|
||||
elif locked_session.status != GameSession.Status.FINISHED:
|
||||
raise ValueError("finish_game_invalid_phase")
|
||||
|
||||
return FinishGameResult(session=locked_session, should_broadcast=should_broadcast)
|
||||
return FinishGameResult(
|
||||
session=locked_session,
|
||||
should_broadcast=should_broadcast,
|
||||
phase_event_name=phase_event_name,
|
||||
phase_event_payload=phase_event_payload,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
158
lobby/tests.py
158
lobby/tests.py
@@ -29,6 +29,27 @@ User = get_user_model()
|
||||
|
||||
|
||||
class LobbyGameplayExtractionTests(TestCase):
|
||||
def setUp(self):
|
||||
self.host = User.objects.create_user(username="extract_host", password="secret123")
|
||||
self.client.login(username="extract_host", password="secret123")
|
||||
self.session = GameSession.objects.create(
|
||||
host=self.host,
|
||||
code="EXTR42",
|
||||
status=GameSession.Status.SCOREBOARD,
|
||||
)
|
||||
self.category = Category.objects.create(name="Historie", slug="historie-extract", is_active=True)
|
||||
self.round_config = RoundConfig.objects.create(
|
||||
session=self.session,
|
||||
number=1,
|
||||
category=self.category,
|
||||
)
|
||||
self.question = Question.objects.create(
|
||||
category=self.category,
|
||||
prompt="Hvornår faldt muren?",
|
||||
correct_answer="1989",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
def test_lobby_views_use_extracted_gameplay_helpers(self):
|
||||
self.assertIs(lobby_views._get_current_round_question, gameplay_services.get_current_round_question)
|
||||
self.assertIs(lobby_views._select_round_question, gameplay_services.select_round_question)
|
||||
@@ -38,11 +59,144 @@ class LobbyGameplayExtractionTests(TestCase):
|
||||
self.assertIs(lobby_views._start_next_round, gameplay_services.start_next_round)
|
||||
self.assertIs(lobby_views._finish_game, gameplay_services.finish_game)
|
||||
self.assertIs(lobby_views._build_scoreboard_phase_event, gameplay_payloads.build_scoreboard_phase_event)
|
||||
self.assertIs(lobby_views._build_start_next_round_phase_event, gameplay_payloads.build_start_next_round_phase_event)
|
||||
self.assertIs(lobby_views._build_start_next_round_response, gameplay_payloads.build_start_next_round_response)
|
||||
self.assertIs(lobby_views._build_finish_game_phase_event, gameplay_payloads.build_finish_game_phase_event)
|
||||
self.assertIs(lobby_views._build_finish_game_response, gameplay_payloads.build_finish_game_response)
|
||||
|
||||
@patch("lobby.views.sync_broadcast_phase_event")
|
||||
@patch("lobby.views._build_start_next_round_response", return_value={"ok": True})
|
||||
@patch("lobby.views._start_next_round")
|
||||
def test_start_next_round_view_delegates_transition_to_service(
|
||||
self,
|
||||
mock_start_next_round,
|
||||
mock_build_response,
|
||||
mock_sync_broadcast_phase_event,
|
||||
):
|
||||
next_round_config = RoundConfig.objects.create(
|
||||
session=self.session,
|
||||
number=2,
|
||||
category=self.category,
|
||||
started_from_scoreboard=True,
|
||||
)
|
||||
round_question = RoundQuestion.objects.create(
|
||||
session=self.session,
|
||||
round_number=2,
|
||||
question=self.question,
|
||||
correct_answer=self.question.correct_answer,
|
||||
)
|
||||
transition = gameplay_services.RoundTransitionResult(
|
||||
session=self.session,
|
||||
round_config=next_round_config,
|
||||
round_question=round_question,
|
||||
should_broadcast=True,
|
||||
phase_event_name="phase.lie_started",
|
||||
phase_event_payload={"round_question_id": round_question.id},
|
||||
)
|
||||
mock_start_next_round.return_value = transition
|
||||
|
||||
response = self.client.post(reverse("lobby:start_next_round", kwargs={"code": self.session.code}))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), {"ok": True})
|
||||
mock_start_next_round.assert_called_once_with(self.session)
|
||||
mock_build_response.assert_called_once_with(self.session, next_round_config, round_question)
|
||||
mock_sync_broadcast_phase_event.assert_called_once_with(
|
||||
self.session.code,
|
||||
"phase.lie_started",
|
||||
{"round_question_id": round_question.id},
|
||||
)
|
||||
|
||||
@patch("lobby.views.sync_broadcast_phase_event")
|
||||
@patch("lobby.views._build_finish_game_response", return_value={"ok": True})
|
||||
@patch("lobby.views._finish_game")
|
||||
def test_finish_game_view_delegates_transition_to_service(
|
||||
self,
|
||||
mock_finish_game,
|
||||
mock_build_response,
|
||||
mock_sync_broadcast_phase_event,
|
||||
):
|
||||
finished_session = GameSession.objects.get(pk=self.session.pk)
|
||||
finished_session.status = GameSession.Status.FINISHED
|
||||
transition = gameplay_services.FinishGameResult(
|
||||
session=finished_session,
|
||||
should_broadcast=True,
|
||||
phase_event_name="phase.game_over",
|
||||
phase_event_payload={"winner": None, "leaderboard": []},
|
||||
)
|
||||
mock_finish_game.return_value = transition
|
||||
|
||||
response = self.client.post(reverse("lobby:finish_game", kwargs={"code": self.session.code}))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), {"ok": True})
|
||||
mock_finish_game.assert_called_once_with(self.session)
|
||||
mock_build_response.assert_called_once_with(finished_session)
|
||||
mock_sync_broadcast_phase_event.assert_called_once_with(
|
||||
self.session.code,
|
||||
"phase.game_over",
|
||||
{"winner": None, "leaderboard": []},
|
||||
)
|
||||
|
||||
@patch("lobby.views.sync_broadcast_phase_event")
|
||||
@patch("lobby.views._build_start_next_round_response", return_value={"ok": True})
|
||||
@patch("lobby.views._start_next_round")
|
||||
def test_start_next_round_view_skips_broadcast_on_service_replay(
|
||||
self,
|
||||
mock_start_next_round,
|
||||
mock_build_response,
|
||||
mock_sync_broadcast_phase_event,
|
||||
):
|
||||
replay_round_config = RoundConfig.objects.create(
|
||||
session=self.session,
|
||||
number=2,
|
||||
category=self.category,
|
||||
started_from_scoreboard=True,
|
||||
)
|
||||
round_question = RoundQuestion.objects.create(
|
||||
session=self.session,
|
||||
round_number=2,
|
||||
question=self.question,
|
||||
correct_answer=self.question.correct_answer,
|
||||
)
|
||||
replay_session = GameSession.objects.get(pk=self.session.pk)
|
||||
replay_session.status = GameSession.Status.LIE
|
||||
replay_session.current_round = 2
|
||||
transition = gameplay_services.RoundTransitionResult(
|
||||
session=replay_session,
|
||||
round_config=replay_round_config,
|
||||
round_question=round_question,
|
||||
should_broadcast=False,
|
||||
)
|
||||
mock_start_next_round.return_value = transition
|
||||
|
||||
response = self.client.post(reverse("lobby:start_next_round", kwargs={"code": self.session.code}))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), {"ok": True})
|
||||
mock_start_next_round.assert_called_once_with(self.session)
|
||||
mock_build_response.assert_called_once_with(replay_session, replay_round_config, round_question)
|
||||
mock_sync_broadcast_phase_event.assert_not_called()
|
||||
|
||||
@patch("lobby.views.sync_broadcast_phase_event")
|
||||
@patch("lobby.views._build_finish_game_response", return_value={"ok": True})
|
||||
@patch("lobby.views._finish_game")
|
||||
def test_finish_game_view_skips_broadcast_on_service_replay(
|
||||
self,
|
||||
mock_finish_game,
|
||||
mock_build_response,
|
||||
mock_sync_broadcast_phase_event,
|
||||
):
|
||||
finished_session = GameSession.objects.get(pk=self.session.pk)
|
||||
finished_session.status = GameSession.Status.FINISHED
|
||||
transition = gameplay_services.FinishGameResult(session=finished_session, should_broadcast=False)
|
||||
mock_finish_game.return_value = transition
|
||||
|
||||
response = self.client.post(reverse("lobby:finish_game", kwargs={"code": self.session.code}))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json(), {"ok": True})
|
||||
mock_finish_game.assert_called_once_with(self.session)
|
||||
mock_build_response.assert_called_once_with(finished_session)
|
||||
mock_sync_broadcast_phase_event.assert_not_called()
|
||||
|
||||
class LobbyFlowTests(TestCase):
|
||||
def setUp(self):
|
||||
|
||||
@@ -10,13 +10,11 @@ from django.views.decorators.http import require_GET, require_POST
|
||||
|
||||
from fupogfakta.models import Category, GameSession, Guess, LieAnswer, Player, Question, RoundConfig, RoundQuestion, ScoreEvent
|
||||
from fupogfakta.payloads import (
|
||||
build_finish_game_phase_event as _build_finish_game_phase_event,
|
||||
build_finish_game_response as _build_finish_game_response,
|
||||
build_leaderboard as _build_leaderboard,
|
||||
build_lie_started_payload as _build_lie_started_payload,
|
||||
build_reveal_payload as _build_reveal_payload,
|
||||
build_scoreboard_phase_event as _build_scoreboard_phase_event,
|
||||
build_start_next_round_phase_event as _build_start_next_round_phase_event,
|
||||
build_start_next_round_response as _build_start_next_round_response,
|
||||
)
|
||||
from fupogfakta.services import (
|
||||
@@ -917,15 +915,10 @@ def start_next_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
return api_error(request, code=str(exc), status=400)
|
||||
|
||||
if transition.should_broadcast:
|
||||
phase_event = _build_start_next_round_phase_event(
|
||||
transition.session,
|
||||
transition.round_config,
|
||||
transition.round_question,
|
||||
)
|
||||
sync_broadcast_phase_event(
|
||||
transition.session.code,
|
||||
phase_event["name"],
|
||||
phase_event["payload"],
|
||||
transition.phase_event_name,
|
||||
transition.phase_event_payload,
|
||||
)
|
||||
|
||||
return JsonResponse(
|
||||
@@ -955,11 +948,10 @@ def finish_game(request: HttpRequest, code: str) -> JsonResponse:
|
||||
return api_error(request, code=str(exc), status=400)
|
||||
|
||||
if transition.should_broadcast:
|
||||
phase_event = _build_finish_game_phase_event(transition.session)
|
||||
sync_broadcast_phase_event(
|
||||
transition.session.code,
|
||||
phase_event["name"],
|
||||
phase_event["payload"],
|
||||
transition.phase_event_name,
|
||||
transition.phase_event_payload,
|
||||
)
|
||||
|
||||
return JsonResponse(_build_finish_game_response(transition.session))
|
||||
|
||||
Reference in New Issue
Block a user