Skip to content

Commit

Permalink
Move WebSocket message sending responsibility
Browse files Browse the repository at this point in the history
Shift responsibility for sending WebSocket messages from each action function to the submit action view, because it is common behavior so shouldn't be repeated. This will also make it easier to setup testing for WebSocket messages.
  • Loading branch information
iamlogand committed Nov 25, 2023
1 parent b3818bb commit 8964168
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
8 changes: 3 additions & 5 deletions backend/rorapp/functions/faction_leader_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
)
from rorapp.functions.mortality_phase_starter import setup_mortality_phase
from rorapp.functions.websocket_message_helper import (
send_websocket_messages,
create_websocket_message,
destroy_websocket_message,
)
Expand All @@ -30,7 +29,7 @@
)


def select_faction_leader(action_id, data) -> Response:
def select_faction_leader(action_id, data) -> (Response, dict):
"""
Select a faction leader.
Expand Down Expand Up @@ -58,7 +57,7 @@ def select_faction_leader(action_id, data) -> Response:
return set_faction_leader(senator.id)


def set_faction_leader(senator_id: int) -> Response:
def set_faction_leader(senator_id: int) -> (Response, dict):
senator = Senator.objects.get(id=senator_id)
game = Game.objects.get(id=senator.game.id)
faction = Faction.objects.get(id=senator.faction.id)
Expand Down Expand Up @@ -87,9 +86,8 @@ def set_faction_leader(senator_id: int) -> Response:
messages_to_send.extend(proceed_to_next_step_if_faction_phase(game.id, step))
messages_to_send.extend(proceed_to_next_step_if_forum_phase(game.id, step, faction))
messages_to_send.extend(delete_old_actions(game.id))
send_websocket_messages(game.id, messages_to_send)

return Response({"message": "Faction leader selected"}, status=200)
return Response({"message": "Faction leader selected"}, status=200), messages_to_send


def get_previous_title(faction) -> Optional[Title]:
Expand Down
7 changes: 4 additions & 3 deletions backend/rorapp/functions/mortality_phase_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
)


def face_mortality(action_id: int, chit_codes: List[int] | None = None) -> Response:
def face_mortality(
action_id: int, chit_codes: List[int] | None = None
) -> (Response, dict):
"""
Ready up for facing mortality.
Expand All @@ -60,8 +62,7 @@ def face_mortality(action_id: int, chit_codes: List[int] | None = None) -> Respo
if Action.objects.filter(step__id=action.step.id, completed=False).count() == 0:
messages_to_send.extend(resolve_mortality(game.id, chit_codes))

send_websocket_messages(game.id, messages_to_send)
return Response({"message": "Ready for mortality"}, status=200)
return Response({"message": "Ready for mortality"}, status=200), messages_to_send


def resolve_mortality(game_id: int, chit_codes: List[int] | None = None) -> dict:
Expand Down
17 changes: 13 additions & 4 deletions backend/rorapp/views/submit_action.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.db import transaction
from django.http import HttpRequest
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rorapp.functions import face_mortality, select_faction_leader
from rorapp.functions import send_websocket_messages
from rorapp.models import Game, Faction, Step, Action


Expand All @@ -13,7 +15,7 @@ class SubmitActionViewSet(viewsets.ViewSet):

@action(detail=True, methods=["post"])
@transaction.atomic
def submit_action(self, request, game_id, action_id=None):
def submit_action(self, request: HttpRequest, game_id: int, action_id: int | None =None):
# Try to get the game
try:
game = Game.objects.get(id=game_id)
Expand Down Expand Up @@ -51,11 +53,18 @@ def submit_action(self, request, game_id, action_id=None):
{"message": "Action is not related to the current step"}, status=403
)

# Action-specific logic
return self.perform_action(game.id, action, request)

def perform_action(self, game_id: int, action: Action, request: HttpRequest) -> Response:
response = None
messages = None
match action.type:
case "select_faction_leader":
return select_faction_leader(action.id, request.data)
response, messages = select_faction_leader(action.id, request.data)
case "face_mortality":
return face_mortality(action.id)
response, messages = face_mortality(action.id)
case _:
return Response({"message": "Action type is invalid"}, status=400)

send_websocket_messages(game_id, messages)
return response

0 comments on commit 8964168

Please sign in to comment.