fix mismatch part_id in choice event + fix revert events on sector/participant removal

This commit is contained in:
Maxime Réaux 2026-03-18 09:26:43 +01:00
parent 42ad708e77
commit db78c6dacc
6 changed files with 145 additions and 53 deletions

View file

@ -4,6 +4,7 @@ from typing import Any, Dict, List, TYPE_CHECKING
if TYPE_CHECKING:
from warchron.model.campaign import Campaign
from warchron.model.war import War
from warchron.model.exception import ForbiddenOperation
from warchron.model.choice import Choice
from warchron.model.battle import Battle
@ -17,6 +18,16 @@ class Round:
self.is_over: bool = False
self._campaign: Campaign | None = None # private link
@property
def campaign(self) -> "Campaign":
if self._campaign is None:
raise RuntimeError("Round is not linked to a Campaign")
return self._campaign
@property
def war(self) -> "War":
return self.campaign.war
def set_id(self, new_id: str) -> None:
self.id = new_id
@ -91,7 +102,6 @@ class Round:
choice.set_secondary(secondary_sector_id)
choice.set_comment(comment)
# FIXME remove corresponding InfluenceSpent and TieResolved
def clear_sector_references(self, sector_id: str) -> None:
for choice in self.choices.values():
trigger_revert_ties = False
@ -102,18 +112,19 @@ class Round:
choice.secondary_sector_id = None
trigger_revert_ties = True
if trigger_revert_ties:
if self._campaign and self._campaign._war:
self._campaign._war.revert_choice_ties(self.id, sector_id=sector_id)
self.war.revert_choice_ties(self.id, sector_id=sector_id)
def remove_choice(self, participant_id: str) -> None:
if participant_id not in self.choices:
return
if self.is_over:
# TODO catch me if you can (inner)
raise ForbiddenOperation("Can't remove choice in a closed round.")
# TODO prevent if battles already assigned
if self._campaign and self._campaign._war:
self._campaign._war.revert_choice_ties(
self.id, participants=[participant_id]
)
self.war.revert_choice_ties(
self.id,
participants=[self.campaign.campaign_to_war_part_id(participant_id)],
)
del self.choices[participant_id]
# Battle methods
@ -187,10 +198,16 @@ class Round:
if battle.winner_id == participant_id:
battle.winner_id = None
if trigger_revert_ties:
if self._campaign and self._campaign._war:
self._campaign._war.revert_battle_ties(battle.sector_id)
self.war.revert_battle_ties(
self.id,
participants=[
self.campaign.campaign_to_war_part_id(participant_id)
],
)
def remove_battle(self, sector_id: str) -> None:
if sector_id not in self.battles:
return
if self.is_over:
# TODO catch me if you can
raise ForbiddenOperation("Can't remove battle in a closed round.")
@ -198,6 +215,5 @@ class Round:
if bat and bat.is_finished():
# TODO catch me if you can
raise ForbiddenOperation("Can't remove finished battle.")
if self._campaign and self._campaign._war:
self._campaign._war.revert_battle_ties(sector_id)
self.war.revert_battle_ties(self.id, sector_id=sector_id)
del self.battles[sector_id]