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,6 +2,7 @@ from __future__ import annotations
from uuid import uuid4
from typing import Any, Dict
from warchron.model.exception import ForbiddenOperation
from warchron.model.choice import Choice
from warchron.model.battle import Battle
@ -58,6 +59,8 @@ class Round:
)
def create_choice(self, participant_id: str) -> Choice:
if self.is_over:
raise ForbiddenOperation("Can't create choice in a closed round.")
if participant_id not in self.choices:
choice = Choice(
participant_id=participant_id,
@ -74,6 +77,8 @@ class Round:
secondary_sector_id: str | None,
comment: str | None,
) -> None:
if self.is_over:
raise ForbiddenOperation("Can't update choice in a closed round.")
choice = self.get_choice(participant_id)
if choice:
choice.set_priority(priority_sector_id)
@ -88,6 +93,8 @@ class Round:
choice.secondary_sector_id = None
def remove_choice(self, participant_id: str) -> None:
if self.is_over:
raise ForbiddenOperation("Can't remove choice in a closed round.")
del self.choices[participant_id]
# Battle methods
@ -104,12 +111,17 @@ class Round:
for bat in self.battles.values()
)
def has_finished_battle(self) -> bool:
return any(b.is_finished() for b in self.battles.values())
def all_battles_finished(self) -> bool:
return all(
b.winner_id is not None or b.is_draw() for b in self.battles.values()
)
def create_battle(self, sector_id: str) -> Battle:
if self.is_over:
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)
self.battles[sector_id] = battle
@ -125,6 +137,8 @@ class Round:
victory_condition: str | None,
comment: str | None,
) -> None:
if self.is_over:
raise ForbiddenOperation("Can't update battle in a closed round.")
bat = self.get_battle(sector_id)
if bat:
bat.set_player_1(player_1_id)
@ -144,4 +158,9 @@ class Round:
battle.winner_id = None
def remove_battle(self, sector_id: str) -> None:
if self.is_over:
raise ForbiddenOperation("Can't remove battle in a closed round.")
bat = self.battles[sector_id]
if bat and bat.is_finished():
raise ForbiddenOperation("Can't remove finished battle.")
del self.battles[sector_id]