refactor(gameplay): extract round start payload builders
This commit is contained in:
@@ -134,7 +134,7 @@ def build_phase_view_model(session: GameSession, *, players_count: int, has_roun
|
||||
}
|
||||
|
||||
|
||||
def build_start_next_round_response(
|
||||
def build_start_round_response(
|
||||
session: GameSession,
|
||||
round_config: RoundConfig,
|
||||
round_question: RoundQuestion,
|
||||
@@ -166,6 +166,39 @@ def build_start_next_round_response(
|
||||
}
|
||||
|
||||
|
||||
def build_question_shown_payload(round_question: RoundQuestion, lie_deadline_at: str, lie_seconds: int) -> dict:
|
||||
return {
|
||||
"round_question_id": round_question.id,
|
||||
"prompt": round_question.question.prompt,
|
||||
"shown_at": round_question.shown_at.isoformat(),
|
||||
"lie_deadline_at": lie_deadline_at,
|
||||
"lie_seconds": lie_seconds,
|
||||
}
|
||||
|
||||
|
||||
def build_question_shown_response(round_question: RoundQuestion, lie_deadline_at: str, lie_seconds: int) -> dict:
|
||||
return {
|
||||
"round_question": {
|
||||
"id": round_question.id,
|
||||
"prompt": round_question.question.prompt,
|
||||
"round_number": round_question.round_number,
|
||||
"shown_at": round_question.shown_at.isoformat(),
|
||||
"lie_deadline_at": lie_deadline_at,
|
||||
},
|
||||
"config": {
|
||||
"lie_seconds": lie_seconds,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def build_start_next_round_response(
|
||||
session: GameSession,
|
||||
round_config: RoundConfig,
|
||||
round_question: RoundQuestion,
|
||||
) -> dict:
|
||||
return build_start_round_response(session, round_config, round_question)
|
||||
|
||||
|
||||
def build_start_next_round_phase_event(
|
||||
session: GameSession,
|
||||
round_config: RoundConfig,
|
||||
|
||||
@@ -62,6 +62,24 @@ class LobbyGameplayExtractionTests(TestCase):
|
||||
self.assertIs(lobby_views._build_phase_view_model, gameplay_payloads.build_phase_view_model)
|
||||
self.assertIs(lobby_views._build_round_question_payload, gameplay_payloads.build_round_question_payload)
|
||||
self.assertIs(lobby_views._build_scoreboard_phase_event, gameplay_payloads.build_scoreboard_phase_event)
|
||||
self.assertIs(lobby_views._build_start_round_response, gameplay_payloads.build_start_round_response)
|
||||
self.assertIs(lobby_views._build_question_shown_payload, gameplay_payloads.build_question_shown_payload)
|
||||
self.assertIs(lobby_views._build_question_shown_response, gameplay_payloads.build_question_shown_response)
|
||||
|
||||
def test_start_round_view_source_stays_http_thin(self):
|
||||
source = inspect.getsource(inspect.unwrap(lobby_views.start_round))
|
||||
|
||||
self.assertIn("lie_started_payload = _build_lie_started_payload(session, round_config, round_question)", source)
|
||||
self.assertIn("_build_start_round_response(session, round_config, round_question)", source)
|
||||
self.assertNotIn('"round_question": {', source)
|
||||
|
||||
def test_show_question_view_source_stays_http_thin(self):
|
||||
source = inspect.getsource(inspect.unwrap(lobby_views.show_question))
|
||||
|
||||
self.assertIn("_build_question_shown_payload(round_question, lie_deadline_iso, round_config.lie_seconds)", source)
|
||||
self.assertIn("_build_question_shown_response(round_question, lie_deadline_iso, round_config.lie_seconds)", source)
|
||||
self.assertNotIn('"round_question": {', source)
|
||||
self.assertNotIn('"round_question_id": round_question.id', source)
|
||||
|
||||
def test_start_next_round_view_source_stays_http_thin(self):
|
||||
source = inspect.getsource(inspect.unwrap(lobby_views.start_next_round))
|
||||
|
||||
@@ -13,9 +13,12 @@ from fupogfakta.payloads import (
|
||||
build_leaderboard as _build_leaderboard,
|
||||
build_lie_started_payload as _build_lie_started_payload,
|
||||
build_phase_view_model as _build_phase_view_model,
|
||||
build_question_shown_payload as _build_question_shown_payload,
|
||||
build_question_shown_response as _build_question_shown_response,
|
||||
build_reveal_payload as _build_reveal_payload,
|
||||
build_round_question_payload as _build_round_question_payload,
|
||||
build_scoreboard_phase_event as _build_scoreboard_phase_event,
|
||||
build_start_round_response as _build_start_round_response,
|
||||
)
|
||||
from fupogfakta.services import (
|
||||
finish_game as _finish_game,
|
||||
@@ -315,30 +318,7 @@ def start_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
)
|
||||
|
||||
return JsonResponse(
|
||||
{
|
||||
"session": {
|
||||
"code": session.code,
|
||||
"status": session.status,
|
||||
"current_round": session.current_round,
|
||||
},
|
||||
"round": {
|
||||
"number": round_config.number,
|
||||
"category": {
|
||||
"slug": round_config.category.slug,
|
||||
"name": round_config.category.name,
|
||||
},
|
||||
},
|
||||
"round_question": {
|
||||
"id": round_question.id,
|
||||
"prompt": round_question.question.prompt,
|
||||
"round_number": round_question.round_number,
|
||||
"shown_at": round_question.shown_at.isoformat(),
|
||||
"lie_deadline_at": lie_started_payload["lie_deadline_at"],
|
||||
},
|
||||
"config": {
|
||||
"lie_seconds": round_config.lie_seconds,
|
||||
},
|
||||
},
|
||||
_build_start_round_response(session, round_config, round_question),
|
||||
status=201,
|
||||
)
|
||||
|
||||
@@ -391,31 +371,16 @@ def show_question(request: HttpRequest, code: str) -> JsonResponse:
|
||||
|
||||
lie_deadline_at = round_question.shown_at + timedelta(seconds=round_config.lie_seconds)
|
||||
|
||||
lie_deadline_iso = lie_deadline_at.isoformat()
|
||||
|
||||
sync_broadcast_phase_event(
|
||||
session.code,
|
||||
"phase.question_shown",
|
||||
{
|
||||
"round_question_id": round_question.id,
|
||||
"prompt": round_question.question.prompt,
|
||||
"shown_at": round_question.shown_at.isoformat(),
|
||||
"lie_deadline_at": lie_deadline_at.isoformat(),
|
||||
"lie_seconds": round_config.lie_seconds,
|
||||
},
|
||||
_build_question_shown_payload(round_question, lie_deadline_iso, round_config.lie_seconds),
|
||||
)
|
||||
|
||||
return JsonResponse(
|
||||
{
|
||||
"round_question": {
|
||||
"id": round_question.id,
|
||||
"prompt": round_question.question.prompt,
|
||||
"round_number": round_question.round_number,
|
||||
"shown_at": round_question.shown_at.isoformat(),
|
||||
"lie_deadline_at": lie_deadline_at.isoformat(),
|
||||
},
|
||||
"config": {
|
||||
"lie_seconds": round_config.lie_seconds,
|
||||
},
|
||||
},
|
||||
_build_question_shown_response(round_question, lie_deadline_iso, round_config.lie_seconds),
|
||||
status=201,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user