add forbidden exceptions on closed elements
This commit is contained in:
parent
42eb625ef6
commit
88bd28e949
6 changed files with 151 additions and 71 deletions
|
|
@ -3,7 +3,7 @@ from uuid import uuid4
|
|||
from datetime import datetime
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from warchron.model.exception import DeletionForbidden
|
||||
from warchron.model.exception import ForbiddenOperation
|
||||
from warchron.model.war_participant import WarParticipant
|
||||
from warchron.model.objective import Objective
|
||||
from warchron.model.campaign_participant import CampaignParticipant
|
||||
|
|
@ -87,6 +87,8 @@ class War:
|
|||
# Objective methods
|
||||
|
||||
def add_objective(self, name: str, description: str | None) -> Objective:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't add objective in a closed war.")
|
||||
obj = Objective(name, description)
|
||||
self.objectives[obj.id] = obj
|
||||
return obj
|
||||
|
|
@ -106,18 +108,22 @@ class War:
|
|||
def update_objective(
|
||||
self, objective_id: str, *, name: str, description: str | None
|
||||
) -> None:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't update objective in a closed war.")
|
||||
obj = self.get_objective(objective_id)
|
||||
obj.set_name(name)
|
||||
obj.set_description(description)
|
||||
|
||||
def remove_objective(self, objective_id: str) -> None:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't remove objective in a closed war.")
|
||||
camp_using_obj: List[str] = []
|
||||
for camp in self.campaigns:
|
||||
if camp.has_sector_with_objective(objective_id):
|
||||
camp_using_obj.append(camp.name)
|
||||
if camp_using_obj:
|
||||
camps_str = ", ".join(camp_using_obj)
|
||||
raise DeletionForbidden(
|
||||
raise ForbiddenOperation(
|
||||
f"This objective is used in campaign(s) sector(s): {camps_str}.\n"
|
||||
"Remove it from campaign(s) first."
|
||||
)
|
||||
|
|
@ -135,6 +141,8 @@ class War:
|
|||
return any(part.player_id == player_id for part in self.participants.values())
|
||||
|
||||
def add_war_participant(self, player_id: str, faction: str) -> WarParticipant:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't add participant in a closed war.")
|
||||
if self.has_player(player_id):
|
||||
raise ValueError("Player already registered in this war")
|
||||
participant = WarParticipant(player_id=player_id, faction=faction)
|
||||
|
|
@ -148,18 +156,22 @@ class War:
|
|||
return list(self.participants.values())
|
||||
|
||||
def update_war_participant(self, participant_id: str, *, faction: str) -> None:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't update participant in a closed war.")
|
||||
part = self.get_war_participant(participant_id)
|
||||
# Can't change referred Model.players
|
||||
part.set_faction(faction)
|
||||
|
||||
def remove_war_participant(self, participant_id: str) -> None:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't remove participant in a closed war.")
|
||||
camp_using_part: List[str] = []
|
||||
for camp in self.campaigns:
|
||||
if camp.has_war_participant(participant_id):
|
||||
camp_using_part.append(camp.name)
|
||||
if camp_using_part:
|
||||
camps_str = ", ".join(camp_using_part)
|
||||
raise DeletionForbidden(
|
||||
raise ForbiddenOperation(
|
||||
f"This war participant is used in campaign(s): {camps_str}.\n"
|
||||
"Remove it from campaign(s) first."
|
||||
)
|
||||
|
|
@ -173,10 +185,21 @@ class War:
|
|||
def get_default_campaign_values(self) -> Dict[str, Any]:
|
||||
return {"month": datetime.now().month}
|
||||
|
||||
def has_finished_campaign(self) -> bool:
|
||||
return any(c.is_over for c in self.campaigns)
|
||||
|
||||
def has_finished_round(self) -> bool:
|
||||
return any(c.has_finished_round() for c in self.campaigns)
|
||||
|
||||
def has_finished_battle(self) -> bool:
|
||||
return any(c.has_finished_battle() for c in self.campaigns)
|
||||
|
||||
def all_campaigns_finished(self) -> bool:
|
||||
return all(c.is_over for c in self.campaigns)
|
||||
|
||||
def add_campaign(self, name: str, month: int | None = None) -> Campaign:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't add campaign in a closed war.")
|
||||
if month is None:
|
||||
month = self.get_default_campaign_values()["month"]
|
||||
campaign = Campaign(name, month)
|
||||
|
|
@ -212,6 +235,8 @@ class War:
|
|||
raise KeyError(f"Participant {participant_id} not found in any Campaign")
|
||||
|
||||
def update_campaign(self, campaign_id: str, *, name: str, month: int) -> None:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't update campaign in a closed war.")
|
||||
camp = self.get_campaign(campaign_id)
|
||||
camp.set_name(name)
|
||||
camp.set_month(month)
|
||||
|
|
@ -220,7 +245,20 @@ class War:
|
|||
return list(self.campaigns)
|
||||
|
||||
def remove_campaign(self, campaign_id: str) -> None:
|
||||
if self.is_over:
|
||||
raise ForbiddenOperation("Can't remove campaign in a closed war.")
|
||||
camp = self.get_campaign(campaign_id)
|
||||
if camp:
|
||||
if camp.is_over:
|
||||
raise ForbiddenOperation("Can't remove closed campaign.")
|
||||
if camp.has_finished_round():
|
||||
raise ForbiddenOperation(
|
||||
"Can't remove campaign with finished round(s)."
|
||||
)
|
||||
if camp.has_finished_battle():
|
||||
raise ForbiddenOperation(
|
||||
"Can't remove campaign with finished battle(s) in round(s)."
|
||||
)
|
||||
self.campaigns.remove(camp)
|
||||
|
||||
# Sector methods
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue