refactor(gameplay): move scoreboard transitions into cartridge service
This commit is contained in:
@@ -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._start_next_round, gameplay_services.start_next_round)
|
||||
self.assertIs(lobby_views._finish_game, gameplay_services.finish_game)
|
||||
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)
|
||||
|
||||
|
||||
110
lobby/views.py
110
lobby/views.py
@@ -17,10 +17,12 @@ from fupogfakta.payloads import (
|
||||
build_start_next_round_response as _build_start_next_round_response,
|
||||
)
|
||||
from fupogfakta.services import (
|
||||
finish_game as _finish_game,
|
||||
get_current_round_question as _get_current_round_question,
|
||||
prepare_mixed_answers as _prepare_mixed_answers,
|
||||
resolve_scores as _resolve_scores,
|
||||
select_round_question as _select_round_question,
|
||||
start_next_round as _start_next_round,
|
||||
)
|
||||
from realtime.broadcast import sync_broadcast_phase_event
|
||||
|
||||
@@ -68,16 +70,6 @@ def _create_unique_session_code() -> str:
|
||||
|
||||
|
||||
|
||||
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 _maybe_promote_reveal_to_scoreboard(session: GameSession) -> GameSession:
|
||||
if session.status != GameSession.Status.REVEAL:
|
||||
return session
|
||||
@@ -930,71 +922,30 @@ def start_next_round(request: HttpRequest, code: str) -> JsonResponse:
|
||||
if session.host_id != request.user.id:
|
||||
return api_error(request, code="host_only_start_next_round", status=403)
|
||||
|
||||
should_broadcast = False
|
||||
with transaction.atomic():
|
||||
locked_session = GameSession.objects.select_for_update().select_related("host").get(pk=session.pk)
|
||||
next_round_config = None
|
||||
round_question = None
|
||||
try:
|
||||
transition = _start_next_round(session)
|
||||
except ValueError as exc:
|
||||
return api_error(request, code=str(exc), status=400)
|
||||
|
||||
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:
|
||||
return api_error(request, code="round_config_missing", status=400)
|
||||
|
||||
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
|
||||
|
||||
try:
|
||||
round_question = _reset_round_question_bootstrap_state(
|
||||
_select_round_question(locked_session, next_round_config)
|
||||
)
|
||||
except ValueError as exc:
|
||||
return api_error(request, code=str(exc), status=400)
|
||||
|
||||
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:
|
||||
return api_error(request, code="next_round_invalid_phase", status=400)
|
||||
|
||||
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
|
||||
):
|
||||
return api_error(request, code="next_round_invalid_phase", status=400)
|
||||
else:
|
||||
return api_error(request, code="next_round_invalid_phase", status=400)
|
||||
|
||||
if should_broadcast:
|
||||
lie_started_payload = _build_lie_started_payload(locked_session, next_round_config, round_question)
|
||||
if transition.should_broadcast:
|
||||
lie_started_payload = _build_lie_started_payload(
|
||||
transition.session,
|
||||
transition.round_config,
|
||||
transition.round_question,
|
||||
)
|
||||
sync_broadcast_phase_event(
|
||||
locked_session.code,
|
||||
transition.session.code,
|
||||
"phase.lie_started",
|
||||
lie_started_payload,
|
||||
)
|
||||
|
||||
return JsonResponse(_build_start_next_round_response(locked_session, next_round_config, round_question))
|
||||
return JsonResponse(
|
||||
_build_start_next_round_response(
|
||||
transition.session,
|
||||
transition.round_config,
|
||||
transition.round_question,
|
||||
)
|
||||
)
|
||||
|
||||
@require_POST
|
||||
@login_required
|
||||
@@ -1009,26 +960,21 @@ def finish_game(request: HttpRequest, code: str) -> JsonResponse:
|
||||
if session.host_id != request.user.id:
|
||||
return api_error(request, code="host_only_finish_game", status=403)
|
||||
|
||||
should_broadcast = False
|
||||
with transaction.atomic():
|
||||
locked_session = GameSession.objects.select_for_update().get(pk=session.pk)
|
||||
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:
|
||||
return api_error(request, code="finish_game_invalid_phase", status=400)
|
||||
try:
|
||||
transition = _finish_game(session)
|
||||
except ValueError as exc:
|
||||
return api_error(request, code=str(exc), status=400)
|
||||
|
||||
if should_broadcast:
|
||||
leaderboard = _build_leaderboard(locked_session)
|
||||
if transition.should_broadcast:
|
||||
leaderboard = _build_leaderboard(transition.session)
|
||||
winner = leaderboard[0] if leaderboard else None
|
||||
sync_broadcast_phase_event(
|
||||
locked_session.code,
|
||||
transition.session.code,
|
||||
"phase.game_over",
|
||||
{"winner": winner, "leaderboard": list(leaderboard)},
|
||||
)
|
||||
|
||||
return JsonResponse(_build_finish_game_response(locked_session))
|
||||
return JsonResponse(_build_finish_game_response(transition.session))
|
||||
|
||||
|
||||
@require_POST
|
||||
|
||||
Reference in New Issue
Block a user