fix choice tiebreak loop and cancel tiebreak lost tokens

This commit is contained in:
Maxime Réaux 2026-03-17 11:16:47 +01:00
parent a3b9f5a943
commit 42ad708e77
13 changed files with 333 additions and 129 deletions

View file

@ -1,7 +1,9 @@
from __future__ import annotations
from uuid import uuid4
from typing import Any, Dict, List
from typing import Any, Dict, List, TYPE_CHECKING
if TYPE_CHECKING:
from warchron.model.campaign import Campaign
from warchron.model.exception import ForbiddenOperation
from warchron.model.choice import Choice
from warchron.model.battle import Battle
@ -13,6 +15,7 @@ class Round:
self.choices: Dict[str, Choice] = {}
self.battles: Dict[str, Battle] = {}
self.is_over: bool = False
self._campaign: Campaign | None = None # private link
def set_id(self, new_id: str) -> None:
self.id = new_id
@ -60,6 +63,7 @@ class Round:
def create_choice(self, participant_id: str) -> Choice:
if self.is_over:
# TODO catch me if you can
raise ForbiddenOperation("Can't create choice in a closed round.")
if participant_id not in self.choices:
choice = Choice(
@ -78,23 +82,38 @@ class Round:
comment: str | None,
) -> None:
if self.is_over:
# TODO catch me if you can
raise ForbiddenOperation("Can't update choice in a closed round.")
# TODO prevent if battles already assigned
choice = self.get_choice(participant_id)
if choice:
choice.set_priority(priority_sector_id)
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
if choice.priority_sector_id == sector_id:
choice.priority_sector_id = None
trigger_revert_ties = True
if choice.secondary_sector_id == sector_id:
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)
def remove_choice(self, participant_id: str) -> None:
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]
)
del self.choices[participant_id]
# Battle methods
@ -126,6 +145,7 @@ class Round:
def create_battle(self, sector_id: str) -> Battle:
if self.is_over:
# TODO catch me if you can
raise ForbiddenOperation("Can't create battle in a closed round.")
if sector_id not in self.battles:
battle = Battle(sector_id=sector_id, player_1_id=None, player_2_id=None)
@ -143,8 +163,10 @@ class Round:
comment: str | None,
) -> None:
if self.is_over:
# TODO catch me if you can
raise ForbiddenOperation("Can't update battle in a closed round.")
bat = self.get_battle(sector_id)
# TODO require confirmation if there was choice tie to clear it
if bat:
bat.set_player_1(player_1_id)
bat.set_player_2(player_2_id)
@ -155,17 +177,27 @@ class Round:
def clear_participant_references(self, participant_id: str) -> None:
for battle in self.battles.values():
trigger_revert_ties = False
if battle.player_1_id == participant_id:
battle.player_1_id = None
trigger_revert_ties = True
if battle.player_2_id == participant_id:
battle.player_2_id = None
trigger_revert_ties = True
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)
def remove_battle(self, sector_id: str) -> None:
if self.is_over:
# TODO catch me if you can
raise ForbiddenOperation("Can't remove battle in a closed round.")
bat = self.battles[sector_id]
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)
del self.battles[sector_id]