add forbidden exceptions on closed elements

This commit is contained in:
Maxime Réaux 2026-02-13 11:38:59 +01:00
parent 42eb625ef6
commit 88bd28e949
6 changed files with 151 additions and 71 deletions

View file

@ -2,10 +2,7 @@ from __future__ import annotations
from uuid import uuid4
from typing import Any, Dict, List
from warchron.model.exception import (
DeletionRequiresConfirmation,
UpdateRequiresConfirmation,
)
from warchron.model.exception import ForbiddenOperation, RequiresConfirmation
from warchron.model.campaign_participant import CampaignParticipant
from warchron.model.sector import Sector
from warchron.model.round import Round
@ -78,6 +75,8 @@ class Campaign:
def add_campaign_participant(
self, war_participant_id: str, leader: str, theme: str
) -> CampaignParticipant:
if self.is_over:
raise ForbiddenOperation("Can't add participant in a closed campaign.")
if self.has_war_participant(war_participant_id):
raise ValueError("Player already registered in this campaign")
participant = CampaignParticipant(
@ -98,12 +97,16 @@ class Campaign:
def update_campaign_participant(
self, participant_id: str, *, leader: str, theme: str
) -> None:
if self.is_over:
raise ForbiddenOperation("Can't update participant in a closed campaign.")
part = self.get_campaign_participant(participant_id)
# Can't change referred War.participant
part.set_leader(leader)
part.set_theme(theme)
def remove_campaign_participant(self, participant_id: str) -> None:
if self.is_over:
raise ForbiddenOperation("Can't remove participant in a closed campaign.")
rounds_blocking: list[Round] = []
for rnd in self.rounds:
if rnd.has_choice_with_participant(
@ -123,13 +126,11 @@ class Campaign:
rounds_str = ", ".join(
str(self.get_round_index(rnd.id)) for rnd in rounds_blocking
)
raise DeletionRequiresConfirmation(
message=(
f"This participant is used in round(s): {rounds_str}.\n"
"Related choices and battles will be cleared.\n"
"Do you want to continue?"
),
cleanup_action=cleanup,
raise RequiresConfirmation(
f"This participant is used in round(s): {rounds_str}.\n"
"Related choices and battles will be cleared.\n"
"Do you want to continue?",
action=cleanup,
)
# Sector methods
@ -155,6 +156,8 @@ class Campaign:
mission: str | None,
description: str | None,
) -> Sector:
if self.is_over:
raise ForbiddenOperation("Can't add sector in a closed campaign.")
sect = Sector(
name, round_id, major_id, minor_id, influence_id, mission, description
)
@ -184,6 +187,8 @@ class Campaign:
mission: str | None,
description: str | None,
) -> None:
if self.is_over:
raise ForbiddenOperation("Can't update sector in a closed campaign.")
sect = self.get_sector(sector_id)
old_round_id = sect.round_id
@ -219,16 +224,16 @@ class Campaign:
rounds_str = ", ".join(
str(self.get_round_index(rnd.id)) for rnd in affected_rounds
)
raise UpdateRequiresConfirmation(
message=(
f"Changing the round of this sector will affect round(s): {rounds_str}."
"\nRelated battles and choices will be cleared.\n"
"Do you want to continue?"
),
apply_update=cleanup_and_update,
raise RequiresConfirmation(
f"Changing the round of this sector will affect round(s): {rounds_str}."
"\nRelated battles and choices will be cleared.\n"
"Do you want to continue?",
action=cleanup_and_update,
)
def remove_sector(self, sector_id: str) -> None:
if self.is_over:
raise ForbiddenOperation("Can't remove sector in a closed campaign.")
rounds_blocking: list[Round] = []
for rnd in self.rounds:
if rnd.has_battle_with_sector(sector_id) or rnd.has_choice_with_sector(
@ -248,13 +253,11 @@ class Campaign:
rounds_str = ", ".join(
str(self.get_round_index(rnd.id)) for rnd in rounds_blocking
)
raise DeletionRequiresConfirmation(
message=(
f"This sector is used in round(s): {rounds_str}.\n"
"Battles and choices using this sector will be cleared.\n"
"Do you want to continue?"
),
cleanup_action=cleanup,
raise RequiresConfirmation(
f"This sector is used in round(s): {rounds_str}.\n"
"Battles and choices using this sector will be cleared.\n"
"Do you want to continue?",
action=cleanup,
)
def get_sectors_in_round(self, round_id: str) -> List[Sector]:
@ -266,6 +269,15 @@ class Campaign:
def has_round(self, round_id: str) -> bool:
return any(r.id == round_id for r in self.rounds)
def has_finished_round(self) -> bool:
return any(r.is_over for r in self.rounds)
def has_finished_battle(self) -> bool:
return any(r.has_finished_battle() for r in self.rounds)
def all_rounds_finished(self) -> bool:
return all(r.is_over for r in self.rounds)
def get_round(self, round_id: str) -> Round:
for rnd in self.rounds:
if rnd.id == round_id:
@ -276,12 +288,21 @@ class Campaign:
return list(self.rounds)
def add_round(self) -> Round:
if self.is_over:
raise ForbiddenOperation("Can't add round in a closed campaign.")
round = Round()
self.rounds.append(round)
return round
def remove_round(self, round_id: str) -> None:
if self.is_over:
raise ForbiddenOperation("Can't remove round in a closed campaign.")
rnd = next((r for r in self.rounds if r.id == round_id), None)
if rnd:
if rnd.is_over:
raise ForbiddenOperation("Can't remove closed round.")
if rnd.has_finished_battle():
raise ForbiddenOperation("Can't remove round with finished battle(s).")
for sect in self.sectors.values():
if sect.round_id == round_id:
sect.round_id = None
@ -321,9 +342,6 @@ class Campaign:
# Battle methods
def all_rounds_finished(self) -> bool:
return all(r.is_over for r in self.rounds)
def create_battle(self, round_id: str, sector_id: str) -> Battle:
rnd = self.get_round(round_id)
return rnd.create_battle(sector_id)