Skip to content

Commit

Permalink
Remove the initiate situation action (#521)
Browse files Browse the repository at this point in the history
* Remove the initiate situation action

* Clean up remaining traces of initiate situation
  • Loading branch information
iamlogand authored Jul 23, 2024
1 parent b0d5600 commit 76d8dcc
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 249 deletions.
21 changes: 9 additions & 12 deletions backend/rorapp/functions/faction_leader_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
from rorapp.functions.action_helper import delete_old_actions
from rorapp.functions.concession_helper import generate_assign_concessions_action
from rorapp.functions.forum_phase_helper import (
generate_initiate_situation_action,
generate_select_faction_leader_action,
initiate_situation,
)
from rorapp.functions.progress_helper import (
create_step_and_message,
get_phase,
get_step,
)
from rorapp.functions.progress_helper import get_phase, get_step
from rorapp.functions.revolution_phase_starter import start_revolution_phase
from rorapp.functions.websocket_message_helper import (
create_websocket_message,
Expand Down Expand Up @@ -192,20 +196,13 @@ def proceed_to_next_step_if_forum_phase(game_id, step, faction) -> List[dict]:

if next_faction is not None:
if step.phase.name.startswith("Final"):
latest_step = get_step(faction.game.id)
latest_phase = get_phase(faction.game.id)
new_step = Step(index=latest_step.index + 1, phase=latest_phase)
new_step.save()
messages_to_send.append(
create_websocket_message("step", StepSerializer(new_step).data)
)
new_step, message = create_step_and_message(faction.game.id)
messages_to_send.append(message)
messages_to_send.append(
generate_select_faction_leader_action(next_faction, new_step)
)
else:
messages_to_send.extend(
generate_initiate_situation_action(next_faction)
)
messages_to_send.extend(initiate_situation(next_faction.id))
else:
if step.phase.name.startswith("Final"):
messages_to_send.extend(end_game_with_influence_victory(game_id))
Expand Down
87 changes: 21 additions & 66 deletions backend/rorapp/functions/forum_phase_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
)


def generate_select_faction_leader_action(
faction: Faction, step: Step | None = None
) -> dict:
def generate_select_faction_leader_action(faction: Faction, step: Step) -> dict:
senators = Senator.objects.filter(faction=faction, alive=True)
senator_id_list = [senator.id for senator in senators]
action = Action(
Expand All @@ -45,94 +43,57 @@ def generate_select_faction_leader_action(
return create_websocket_message("action", ActionSerializer(action).data)


def generate_initiate_situation_action(faction: Faction) -> list[dict]:
messages_to_send = []

# Create new step
latest_step = get_step(faction.game.id)
# Need to get latest phase because the latest step might not be from the current forum phase
latest_phase = get_phase(faction.game.id)
new_step = Step(index=latest_step.index + 1, phase=latest_phase)
new_step.save()
messages_to_send.append(
create_websocket_message("step", StepSerializer(new_step).data)
)

# Create new action
action = Action(
step=new_step,
faction=faction,
type="initiate_situation",
required=True,
)
action.save()
messages_to_send.append(
create_websocket_message("action", ActionSerializer(action).data)
)
return messages_to_send


def initiate_situation(action_id: int) -> tuple[Response, list[dict]]:
def initiate_situation(faction_id: int) -> list[dict]:
"""
Initiate a random situation.
This function is called when a player initiates a situation during the forum phase.
Args:
action_id (int): The action ID.
Returns:
dict: The response with a message and a status code.
dict: The WebSocket messages to send.
"""
messages_to_send = []

# Mark the action as complete
action = Action.objects.get(id=action_id)
action.completed = True
action.save()
messages_to_send.append(destroy_websocket_message("action", action_id))
faction = Faction.objects.get(id=faction_id)
assert isinstance(faction, Faction)

# Get situation
situation = (
Situation.objects.filter(game=action.step.phase.turn.game)
.order_by("index")
.last()
)
situation = Situation.objects.filter(game=faction.game).order_by("index").last()
assert isinstance(situation, Situation)

_, message = create_step_and_message(faction.game.id)
messages_to_send.append(message)

if situation.secret:
messages_to_send.extend(create_new_secret(action.faction.id, situation.name))
messages_to_send.extend(create_new_secret(faction.id, situation.name))
situation.delete()
else:
match situation.type:
case "war":
messages_to_send.extend(
create_new_war(action.faction.id, situation.name)
)
messages_to_send.extend(create_new_war(faction.id, situation.name))
situation.delete()
case "senator":
messages_to_send.extend(
create_new_family(action.faction.id, situation.name)
)
messages_to_send.extend(create_new_family(faction.id, situation.name))
situation.delete()
case "leader":
messages_to_send.extend(
create_new_enemy_leader(action.faction.id, situation.name)
create_new_enemy_leader(faction.id, situation.name)
)
situation.delete()

# If no more situations remain then rename the current phase to "Final Forum Phase",
# triggering the end of the game once the phase is completed
if Situation.objects.filter(game=action.step.phase.turn.game).count() == 0:
current_phase = get_phase(action.faction.game.id)
if Situation.objects.filter(game=faction.game).count() == 0:
current_phase = get_phase(faction.game.id)
current_phase.name = "Final Forum"
current_phase.save()
messages_to_send.append(
create_websocket_message("phase", PhaseSerializer(current_phase).data)
)
latest_step = get_step(action.faction.game.id)
latest_step = get_step(faction.game.id)
new_action_log_index = (
ActionLog.objects.filter(step__phase__turn__game=action.faction.game.id)
ActionLog.objects.filter(step__phase__turn__game=faction.game.id)
.order_by("-index")[0]
.index
+ 1
Expand All @@ -149,14 +110,8 @@ def initiate_situation(action_id: int) -> tuple[Response, list[dict]]:
)
)

# Create new step
new_step = Step(index=action.step.index + 1, phase=action.step.phase)
new_step.save()
messages_to_send.append(
create_websocket_message("step", StepSerializer(new_step).data)
)
step, message = create_step_and_message(faction.game.id)
messages_to_send.append(message)

messages_to_send.append(
generate_select_faction_leader_action(action.faction, new_step)
)
return Response({"message": "Initiated"}, status=200), messages_to_send
messages_to_send.append(generate_select_faction_leader_action(faction, step))
return messages_to_send
4 changes: 2 additions & 2 deletions backend/rorapp/functions/forum_phase_starter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List
from rorapp.functions.action_helper import delete_old_actions
from rorapp.functions.forum_phase_helper import generate_initiate_situation_action
from rorapp.functions.forum_phase_helper import initiate_situation
from rorapp.functions.progress_helper import create_phase_and_message
from rorapp.models import Faction

Expand All @@ -24,6 +24,6 @@ def start_forum_phase(game_id: int) -> List[dict]:
# Create action and new step
first_faction = Faction.objects.filter(game__id=game_id).order_by("rank").first()
assert isinstance(first_faction, Faction)
messages_to_send.extend(generate_initiate_situation_action(first_faction))
messages_to_send.extend(initiate_situation(first_faction.id))
messages_to_send.extend(delete_old_actions(game_id))
return messages_to_send
Loading

0 comments on commit 76d8dcc

Please sign in to comment.