from __future__ import annotations from uuid import uuid4 from typing import Any, Dict from warchron.model.choice import Choice from warchron.model.battle import Battle class Round: def __init__(self) -> None: self.id: str = str(uuid4()) self.choices: Dict[str, Choice] = {} self.battles: Dict[str, Battle] = {} self.is_over: bool = False def set_id(self, new_id: str) -> None: self.id = new_id def set_state(self, new_state: bool) -> None: self.is_over = new_state def toDict(self) -> Dict[str, Any]: return { "id": self.id, "choices": [c.toDict() for c in self.choices.values()], "battles": [b.toDict() for b in self.battles.values()], "is_over": self.is_over, } @staticmethod def fromDict(data: Dict[str, Any]) -> Round: rnd = Round() rnd.set_id(data["id"]) for c in data.get("choices", []): choice = Choice.fromDict(c) rnd.choices[choice.participant_id] = choice for b in data.get("battles", []): battle = Battle.fromDict(b) rnd.battles[battle.sector_id] = battle rnd.set_state(data.get("is_over", False)) return rnd # Choice methods def get_choice(self, participant_id: str) -> Choice | None: return self.choices.get(participant_id) def has_choice_with_sector(self, sector_id: str) -> bool: return any( choice.priority_sector_id == sector_id or choice.secondary_sector_id == sector_id for choice in self.choices.values() ) def has_choice_with_participant(self, participant_id: str) -> bool: return any( choice.participant_id == participant_id for choice in self.choices.values() ) def create_choice(self, participant_id: str) -> Choice: if participant_id not in self.choices: choice = Choice( participant_id=participant_id, priority_sector_id=None, secondary_sector_id=None, ) self.choices[participant_id] = choice return self.choices[participant_id] def update_choice( self, participant_id: str, priority_sector_id: str | None, secondary_sector_id: str | None, comment: str | None, ) -> None: choice = self.get_choice(participant_id) if choice: choice.set_priority(priority_sector_id) choice.set_secondary(secondary_sector_id) choice.set_comment(comment) def clear_sector_references(self, sector_id: str) -> None: for choice in self.choices.values(): if choice.priority_sector_id == sector_id: choice.priority_sector_id = None if choice.secondary_sector_id == sector_id: choice.secondary_sector_id = None def remove_choice(self, participant_id: str) -> None: del self.choices[participant_id] # Battle methods def get_battle(self, sector_id: str) -> Battle | None: return self.battles.get(sector_id) def has_battle_with_sector(self, sector_id: str) -> bool: return any(bat.sector_id == sector_id for bat in self.battles.values()) def has_battle_with_participant(self, participant_id: str) -> bool: return any( bat.player_1_id == participant_id or bat.player_2_id == participant_id for bat 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 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 return self.battles[sector_id] def update_battle( self, sector_id: str, player_1_id: str | None, player_2_id: str | None, winner_id: str | None, score: str | None, victory_condition: str | None, comment: str | None, ) -> None: bat = self.get_battle(sector_id) if bat: bat.set_player_1(player_1_id) bat.set_player_2(player_2_id) bat.set_winner(winner_id) bat.set_score(score) bat.set_victory_condition(victory_condition) bat.set_comment(comment) def clear_participant_references(self, participant_id: str) -> None: for battle in self.battles.values(): if battle.player_1_id == participant_id: battle.player_1_id = None if battle.player_2_id == participant_id: battle.player_2_id = None if battle.winner_id == participant_id: battle.winner_id = None def remove_battle(self, sector_id: str) -> None: del self.battles[sector_id]