Skip to content

Commit

Permalink
Merge branch 'main' into feature/more-filters
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlogand committed Jul 21, 2024
2 parents 920955a + ff4952e commit d09808d
Show file tree
Hide file tree
Showing 34 changed files with 260 additions and 200 deletions.
5 changes: 3 additions & 2 deletions backend/rorapp/functions/action_helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import List
from rorapp.functions.progress_helper import get_step
from rorapp.functions.websocket_message_helper import destroy_websocket_message
from rorapp.models import Action


def delete_old_actions(game_id: int) -> list[dict]:
def delete_old_actions(game_id: int) -> List[dict]:
websocket_messages = []

latest_step = get_step(game_id)
Expand All @@ -17,7 +18,7 @@ def delete_old_actions(game_id: int) -> list[dict]:
return websocket_messages


def delete_all_actions(game_id: int) -> list[dict]:
def delete_all_actions(game_id: int) -> List[dict]:
websocket_messages = []

actions = Action.objects.filter(step__phase__turn__game=game_id)
Expand Down
93 changes: 83 additions & 10 deletions backend/rorapp/functions/concession_helper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import json
from typing import List
from rest_framework.response import Response
from rorapp.functions.action_helper import delete_all_actions, delete_old_actions
from rorapp.functions.action_helper import delete_all_actions
from rorapp.functions.chromatic_order_helper import get_next_faction_in_chromatic_order
from rorapp.functions.mortality_phase_starter import setup_mortality_phase
from rorapp.functions.progress_helper import (
create_step_and_message,
get_phase,
get_step,
)
from rorapp.functions.revolution_phase_starter import generate_assign_concessions_action
from rorapp.functions.turn_starter import start_next_turn
from rorapp.functions.websocket_message_helper import (
create_websocket_message,
destroy_websocket_message,
Expand All @@ -19,11 +23,71 @@
SenatorActionLog,
Secret,
)
from rorapp.serializers import ActionLogSerializer, SenatorActionLogSerializer
from rorapp.serializers.concession import ConcessionSerializer
from rorapp.serializers import (
ActionSerializer,
ActionLogSerializer,
ConcessionSerializer,
SenatorActionLogSerializer,
)


def generate_assign_concessions_action(
game_id: int, faction: Faction | None = None
) -> List[dict]:
messages_to_send = []

def assign_concessions(action_id: int, data: dict) -> tuple[Response, list[dict]]:
if faction is None:
faction = Faction.objects.filter(game__id=game_id).order_by("rank").first()

if not isinstance(faction, Faction):
raise ValueError("Couldn't find a faction")

faction_secrets = Secret.objects.filter(faction=faction)

if faction_secrets.count() == 0:
next_faction = get_next_faction_in_chromatic_order(faction)
if isinstance(next_faction, Faction):
messages_to_send.extend(
generate_assign_concessions_action(game_id, next_faction)
)
else:
phase = get_phase(game_id)
if phase.name == "Revolution":
messages_to_send.extend(start_next_turn(game_id))
else:
messages_to_send.extend(setup_mortality_phase(game_id))
return messages_to_send

# Create step
step, step_message = create_step_and_message(game_id)
messages_to_send.append(step_message)

# Generate action for assigning concessions
senators = Senator.objects.filter(faction=faction, alive=True)
senator_id_list = [senator.id for senator in senators]
concession_secrets = faction_secrets.filter(type="concession").exclude(
name="Land Commissioner"
)
concession_secret_id_list = [concession.id for concession in concession_secrets]
action = Action(
step=step,
faction=faction,
type="assign_concessions",
required=True,
parameters={
"senators": senator_id_list,
"concession_secrets": concession_secret_id_list,
},
)
action.save()
messages_to_send.append(
create_websocket_message("action", ActionSerializer(action).data)
)

return messages_to_send


def assign_concessions(action_id: int, data: dict) -> tuple[Response, List[dict]]:
"""
Assign concessions to senators.
Expand All @@ -38,10 +102,12 @@ def assign_concessions(action_id: int, data: dict) -> tuple[Response, list[dict]
"""

messages_to_send = []


# The action and faction IDs are known to be valid
action = Action.objects.get(id=action_id)
faction = Faction.objects.get(id=action.faction.id)
game_id = faction.game.id

# Try to get the secret_senator_map data
try:
Expand Down Expand Up @@ -100,20 +166,27 @@ def assign_concessions(action_id: int, data: dict) -> tuple[Response, list[dict]
messages_to_send.extend(assign_concession(faction, secret, senator))

# Delete old actions
messages_to_send.extend(delete_all_actions(faction.game.id))
messages_to_send.extend(delete_all_actions(game_id))

# Proceed to next turn or next faction
next_faction = get_next_faction_in_chromatic_order(faction)
messages_to_send.extend(
generate_assign_concessions_action(faction.game.id, next_faction)
)
if next_faction:
messages_to_send.extend(
generate_assign_concessions_action(game_id, next_faction)
)
else:
phase = get_phase(game_id)
if phase.name == "Revolution":
messages_to_send.extend(start_next_turn(game_id))
else:
messages_to_send.extend(setup_mortality_phase(game_id))

return Response(
{"message": "Concession assignment completed"}, status=200
), messages_to_send


def assign_concession(faction: Faction, secret: Secret, senator: Senator) -> list[dict]:
def assign_concession(faction: Faction, secret: Secret, senator: Senator) -> List[dict]:
messages_to_send = []

# Delete secret
Expand Down
5 changes: 3 additions & 2 deletions backend/rorapp/functions/enemy_leader_helper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import json
from typing import List
from django.conf import settings
from rorapp.functions.progress_helper import get_step
from rorapp.functions.websocket_message_helper import create_websocket_message
from rorapp.models import ActionLog, EnemyLeader, Faction, Game, War
from rorapp.serializers import ActionLogSerializer, EnemyLeaderSerializer, WarSerializer


def create_new_enemy_leader(initiating_faction_id: int, name: str) -> list[dict]:
def create_new_enemy_leader(initiating_faction_id: int, name: str) -> List[dict]:
"""
Create a new enemy leader. If there is a matching war, the leader joins it. If the matching war is not active then he activates it.
Expand Down Expand Up @@ -105,7 +106,7 @@ def create_new_enemy_leader(initiating_faction_id: int, name: str) -> list[dict]

def get_and_activate_matching_war(
game: Game, war_name: int
) -> tuple[War | None, list[dict]]:
) -> tuple[War | None, List[dict]]:
"""
Get the matching war to link to the enemy leader. Activate it if it is inactive.
"""
Expand Down
15 changes: 8 additions & 7 deletions backend/rorapp/functions/faction_leader_helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import List
from rest_framework.response import Response
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,
)
from rorapp.functions.mortality_phase_starter import setup_mortality_phase
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 (
Expand Down Expand Up @@ -34,7 +35,7 @@

def select_faction_leader_from_action(
action_id: int, data: dict
) -> tuple[Response, list[dict]]:
) -> tuple[Response, List[dict]]:
"""
Select a faction leader.
Expand Down Expand Up @@ -62,7 +63,7 @@ def select_faction_leader_from_action(
return select_faction_leader(senator.id)


def select_faction_leader(senator_id: int) -> tuple[Response, list[dict]]:
def select_faction_leader(senator_id: int) -> tuple[Response, List[dict]]:
senator = Senator.objects.get(id=senator_id)
game = Game.objects.get(id=senator.game.id)
assert isinstance(senator.faction, Faction)
Expand Down Expand Up @@ -121,7 +122,7 @@ def create_new_title(senator, step) -> dict:

def create_action_logs_and_related_messages(
game_id, step, faction, senator, previous_senator_id
) -> list[dict]:
) -> List[dict]:
messages_to_send = []

action_log = create_action_log(game_id, step, faction, senator, previous_senator_id)
Expand Down Expand Up @@ -174,17 +175,17 @@ def complete_action(action: Action, senator_id: int) -> dict:
return create_websocket_message("action", ActionSerializer(action).data)


def proceed_to_next_step_if_faction_phase(game_id, step) -> list[dict]:
def proceed_to_next_step_if_faction_phase(game_id, step) -> List[dict]:
messages_to_send = []
if (
step.phase.name == "Faction"
and not Action.objects.filter(step__id=step.id, completed=False).exists()
):
messages_to_send.extend(setup_mortality_phase(game_id))
messages_to_send.extend(generate_assign_concessions_action(game_id))
return messages_to_send


def proceed_to_next_step_if_forum_phase(game_id, step, faction) -> list[dict]:
def proceed_to_next_step_if_forum_phase(game_id, step, faction) -> List[dict]:
messages_to_send = []
if step.phase.name.endswith("Forum"):
next_faction = get_next_faction_in_chromatic_order(faction)
Expand Down
21 changes: 15 additions & 6 deletions backend/rorapp/functions/forum_phase_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from typing import List
from rest_framework.response import Response
from rorapp.functions.progress_helper import get_phase, get_step
from rorapp.functions.progress_helper import (
create_step_and_message,
get_phase,
get_step,
)
from rorapp.functions.senator_helper import create_new_family
from rorapp.functions.war_helper import create_new_war
from rorapp.functions.enemy_leader_helper import create_new_enemy_leader
Expand All @@ -10,23 +15,27 @@
)
from rorapp.models import (
Action,
ActionLog,
Faction,
Senator,
Situation,
Step,
)
from rorapp.models.action_log import ActionLog
from rorapp.serializers import ActionSerializer, PhaseSerializer, StepSerializer
from rorapp.serializers.action_log import ActionLogSerializer
from rorapp.serializers import (
ActionSerializer,
ActionLogSerializer,
PhaseSerializer,
StepSerializer,
)


def generate_select_faction_leader_action(
faction: Faction, new_step: Step | None = None
faction: Faction, step: Step | None = None
) -> dict:
senators = Senator.objects.filter(faction=faction, alive=True)
senator_id_list = [senator.id for senator in senators]
action = Action(
step=new_step,
step=step,
faction=faction,
type="select_faction_leader",
required=True,
Expand Down
5 changes: 3 additions & 2 deletions backend/rorapp/functions/forum_phase_starter.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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.progress_helper import create_phase_and_message
from rorapp.models import Faction


def start_forum_phase(game_id: int) -> list[dict]:
def start_forum_phase(game_id: int) -> List[dict]:
"""
Start the forum phase.
Args:
game_id (int): The game ID.
Returns:
list[dict]: The WebSocket messages to send.
List[dict]: The WebSocket messages to send.
"""
messages_to_send = []

Expand Down
3 changes: 2 additions & 1 deletion backend/rorapp/functions/game_ender.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
from django.utils import timezone
from rorapp.functions.progress_helper import get_step
from rorapp.models import Game
Expand All @@ -9,7 +10,7 @@
from rorapp.serializers.action_log import ActionLogSerializer


def end_game_with_influence_victory(game_id: int) -> list[dict]:
def end_game_with_influence_victory(game_id: int) -> List[dict]:
messages_to_send = []

winning_faction = find_influence_winner(game_id)
Expand Down
Loading

0 comments on commit d09808d

Please sign in to comment.