refactor(gameplay): move transition payload builders to cartridge
This commit is contained in:
@@ -68,3 +68,49 @@ def build_lie_started_payload(session: GameSession, round_config: RoundConfig, r
|
||||
"lie_deadline_at": lie_deadline_at.isoformat(),
|
||||
"lie_seconds": round_config.lie_seconds,
|
||||
}
|
||||
|
||||
|
||||
def build_start_next_round_response(
|
||||
session: GameSession,
|
||||
round_config: RoundConfig,
|
||||
round_question: RoundQuestion,
|
||||
) -> dict:
|
||||
lie_started_payload = build_lie_started_payload(session, round_config, round_question)
|
||||
return {
|
||||
"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,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def build_finish_game_response(session: GameSession) -> dict:
|
||||
leaderboard = build_leaderboard(session)
|
||||
winner = leaderboard[0] if leaderboard else None
|
||||
return {
|
||||
"session": {
|
||||
"code": session.code,
|
||||
"status": GameSession.Status.FINISHED,
|
||||
"current_round": session.current_round,
|
||||
},
|
||||
"winner": winner,
|
||||
"leaderboard": leaderboard,
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from fupogfakta import services as gameplay_services
|
||||
from fupogfakta import payloads as gameplay_payloads, services as gameplay_services
|
||||
from fupogfakta.models import (
|
||||
Category,
|
||||
GameSession,
|
||||
@@ -34,6 +34,8 @@ class LobbyGameplayExtractionTests(TestCase):
|
||||
self.assertIs(lobby_views._select_round_question, gameplay_services.select_round_question)
|
||||
self.assertIs(lobby_views._prepare_mixed_answers, gameplay_services.prepare_mixed_answers)
|
||||
self.assertIs(lobby_views._resolve_scores, gameplay_services.resolve_scores)
|
||||
self.assertIs(lobby_views._build_start_next_round_response, gameplay_payloads.build_start_next_round_response)
|
||||
self.assertIs(lobby_views._build_finish_game_response, gameplay_payloads.build_finish_game_response)
|
||||
|
||||
|
||||
class LobbyFlowTests(TestCase):
|
||||
|
||||
@@ -10,9 +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_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_start_next_round_response as _build_start_next_round_response,
|
||||
)
|
||||
from fupogfakta.services import (
|
||||
get_current_round_question as _get_current_round_question,
|
||||
@@ -65,56 +67,6 @@ def _create_unique_session_code() -> str:
|
||||
raise RuntimeError("Could not generate unique session code")
|
||||
|
||||
|
||||
def _build_start_next_round_response(
|
||||
session: GameSession,
|
||||
round_config: RoundConfig,
|
||||
round_question: RoundQuestion,
|
||||
) -> JsonResponse:
|
||||
lie_started_payload = _build_lie_started_payload(session, round_config, round_question)
|
||||
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,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
def _build_finish_game_response(session: GameSession) -> JsonResponse:
|
||||
leaderboard = _build_leaderboard(session)
|
||||
winner = leaderboard[0] if leaderboard else None
|
||||
return JsonResponse(
|
||||
{
|
||||
"session": {
|
||||
"code": session.code,
|
||||
"status": GameSession.Status.FINISHED,
|
||||
"current_round": session.current_round,
|
||||
},
|
||||
"winner": winner,
|
||||
"leaderboard": leaderboard,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _reset_round_question_bootstrap_state(round_question: RoundQuestion) -> RoundQuestion:
|
||||
Guess.objects.filter(round_question=round_question).delete()
|
||||
@@ -1042,7 +994,7 @@ def start_next_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
lie_started_payload,
|
||||
)
|
||||
|
||||
return _build_start_next_round_response(locked_session, next_round_config, round_question)
|
||||
return JsonResponse(_build_start_next_round_response(locked_session, next_round_config, round_question))
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@@ -1076,7 +1028,7 @@ def finish_game(request: HttpRequest, code: str) -> JsonResponse:
|
||||
{"winner": winner, "leaderboard": list(leaderboard)},
|
||||
)
|
||||
|
||||
return _build_finish_game_response(locked_session)
|
||||
return JsonResponse(_build_finish_game_response(locked_session))
|
||||
|
||||
|
||||
@require_POST
|
||||
|
||||
Reference in New Issue
Block a user