from typing import List, Dict, TYPE_CHECKING from PyQt6.QtWidgets import QMessageBox, QDialog from PyQt6.QtGui import QIcon from warchron.constants import RefreshScope, ContextType, ItemType, Icons, IconName if TYPE_CHECKING: from warchron.controller.app_controller import AppController from warchron.controller.dtos import ( ParticipantOption, ObjectiveDTO, SectorDTO, RoundDTO, CampaignParticipantScoreDTO, ) from warchron.model.exception import ForbiddenOperation, DomainError from warchron.model.war import War from warchron.model.campaign import Campaign from warchron.model.campaign_participant import CampaignParticipant from warchron.model.sector import Sector from warchron.model.tie_manager import TieContext, TieResolver from warchron.model.score_service import ScoreService from warchron.model.result_checker import ResultChecker from warchron.controller.closure_workflow import CampaignClosureWorkflow from warchron.view.campaign_dialog import CampaignDialog from warchron.view.campaign_participant_dialog import CampaignParticipantDialog from warchron.view.sector_dialog import SectorDialog from warchron.view.tie_dialog import TieDialog class CampaignController: def __init__(self, app: "AppController"): self.app = app def _compute_campaign_ranking_icons( self, war: War, campaign: Campaign ) -> Dict[str, QIcon]: scores = ScoreService.compute_scores( war, ContextType.CAMPAIGN, campaign.id, ) ranking = ResultChecker.get_effective_ranking( war, ContextType.CAMPAIGN, campaign.id, scores ) icon_map = {} for rank, group in ranking: vp = scores[group[0]].victory_points tie_id = f"{campaign.id}:score:{vp}" is_tie = len(group) > 1 broken = TieResolver.was_tie_broken_by_tokens( war, ContextType.CAMPAIGN, tie_id, ) # choose icon name if rank == 1: base = IconName.VP1ST draw = IconName.VP1STDRAW tb = IconName.VP1STTIEBREAK elif rank == 2: base = IconName.VP2ND draw = IconName.VP2NDDRAW tb = IconName.VP2NDTIEBREAK elif rank == 3: base = IconName.VP3RD draw = IconName.VP3RDDRAW tb = IconName.VP3RDTIEBREAK else: base = IconName.VPNTH draw = IconName.VPNTHDRAW tb = IconName.VPNTHTIEBREAK if not is_tie: icon = Icons.get(base) elif not broken: icon = QIcon(Icons.get_pixmap(draw)) else: icon = QIcon(Icons.get_pixmap(tb)) for pid in group: icon_map[pid] = icon return icon_map def _fill_campaign_details(self, campaign_id: str) -> None: camp = self.app.model.get_campaign(campaign_id) self.app.view.show_campaign_details(name=camp.name, month=camp.month) sectors = camp.get_all_sectors() war = self.app.model.get_war_by_campaign(camp.id) sectors_for_display: List[SectorDTO] = [ SectorDTO( id=sect.id, name=sect.name, round_index=camp.get_round_index(sect.round_id), major=war.get_objective_name(sect.major_objective_id), minor=war.get_objective_name(sect.minor_objective_id), influence=war.get_objective_name(sect.influence_objective_id), mission=sect.mission, description=sect.description, ) for sect in sectors ] self.app.view.display_campaign_sectors(sectors_for_display) scores = ScoreService.compute_scores(war, ContextType.CAMPAIGN, campaign_id) rows: List[CampaignParticipantScoreDTO] = [] icon_map = {} if camp.is_over: icon_map = self._compute_campaign_ranking_icons(war, camp) for camp_part in camp.get_all_campaign_participants(): war_part_id = camp_part.war_participant_id war_part = war.get_war_participant(war_part_id) player_name = self.app.model.get_player_name(war_part.player_id) score = scores[war_part_id] rows.append( CampaignParticipantScoreDTO( campaign_participant_id=camp_part.id, war_participant_id=war_part_id, player_name=player_name, leader=camp_part.leader or "", theme=camp_part.theme or "", victory_points=score.victory_points, narrative_points=dict(score.narrative_points), rank_icon=icon_map.get(war_part_id), ) ) objectives = [ ObjectiveDTO(o.id, o.name, o.description) for o in war.get_all_objectives() ] self.app.view.display_campaign_participants(rows, objectives) self.app.view.endCampaignBtn.setEnabled(not camp.is_over) def _validate_campaign_inputs(self, name: str, month: int) -> bool: if not name.strip(): QMessageBox.warning( self.app.view, "Invalid name", "Campaign name cannot be empty." ) return False if not (1 <= month <= 12): QMessageBox.warning( self.app.view, "Invalid month", "Month must be between 1 and 12." ) return False return True def create_campaign(self) -> Campaign | None: if not self.app.navigation.selected_war_id: return None dialog = CampaignDialog( self.app.view, default_month=self.app.model.get_default_campaign_values( self.app.navigation.selected_war_id )["month"], ) if dialog.exec() != QDialog.DialogCode.Accepted: return None name = dialog.get_campaign_name() month = dialog.get_campaign_month() if not self._validate_campaign_inputs(name, month): return None return self.app.model.add_campaign( self.app.navigation.selected_war_id, name, month ) def edit_campaign(self, campaign_id: str) -> None: camp = self.app.model.get_campaign(campaign_id) camp_dialog = CampaignDialog( self.app.view, default_name=camp.name, default_month=camp.month ) if camp_dialog.exec() == QDialog.DialogCode.Accepted: name = camp_dialog.get_campaign_name() month = camp_dialog.get_campaign_month() if not self._validate_campaign_inputs(name, month): return self.app.model.update_campaign(campaign_id, name=name, month=month) def close_campaign(self) -> None: campaign_id = self.app.navigation.selected_campaign_id if not campaign_id: return camp = self.app.model.get_campaign(campaign_id) war = self.app.model.get_war_by_campaign(campaign_id) workflow = CampaignClosureWorkflow(self.app) try: workflow.start(war, camp) except DomainError as e: QMessageBox.warning( self.app.view, "Deletion forbidden", str(e), ) self.app.is_dirty = True self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS) self.app.navigation.refresh_and_select( RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=campaign_id ) def resolve_ties( self, war: War, contexts: List[TieContext] ) -> Dict[str, Dict[str, bool]]: bids_map = {} for ctx in contexts: active = TieResolver.get_active_participants( war, ctx.context_type, ctx.context_id, ctx.participants ) players = [ ParticipantOption(id=pid, name=self.app.model.get_participant_name(pid)) for pid in active ] counters = [war.get_influence_tokens(pid) for pid in active] dialog = TieDialog( parent=self.app.view, players=players, counters=counters, context_type=ContextType.CAMPAIGN, context_id=ctx.context_id, ) if not dialog.exec(): raise ForbiddenOperation("Tie resolution cancelled") bids_map[ctx.context_id] = dialog.get_bids() return bids_map def create_campaign_participant(self) -> CampaignParticipant | None: if not self.app.navigation.selected_campaign_id: return None participants = self.app.model.get_available_war_participants( self.app.navigation.selected_campaign_id ) part_opts = [ ParticipantOption(id=p.id, name=self.app.model.get_player_name(p.player_id)) for p in participants ] dialog = CampaignParticipantDialog(self.app.view, participants=part_opts) if dialog.exec() != QDialog.DialogCode.Accepted: return None player_id = dialog.get_player_id() leader = dialog.get_participant_leader() theme = dialog.get_participant_theme() if not player_id: return None return self.app.model.add_campaign_participant( self.app.navigation.selected_campaign_id, player_id, leader, theme ) def edit_campaign_participant(self, participant_id: str) -> None: camp_part = self.app.model.get_campaign_participant(participant_id) war_part = self.app.model.get_war_participant(camp_part.war_participant_id) player = self.app.model.get_player(war_part.player_id) part_opt = [ParticipantOption(id=player.id, name=player.name)] camp_part_dialog = CampaignParticipantDialog( self.app.view, participants=part_opt, default_participant_id=camp_part.id, default_leader=camp_part.leader, default_theme=camp_part.theme, editable_player=False, ) if camp_part_dialog.exec() == QDialog.DialogCode.Accepted: leader = camp_part_dialog.get_participant_leader() theme = camp_part_dialog.get_participant_theme() self.app.model.update_campaign_participant( participant_id, leader=leader, theme=theme ) # Sector methods def _validate_sector_inputs( self, name: str, round_id: str | None, major_id: str | None, minor_id: str | None, influence_id: str | None, ) -> bool: if not name.strip(): QMessageBox.warning( self.app.view, "Invalid name", "Sector name cannot be empty." ) return False # TODO allow same objectives in different fields? return True def create_sector(self) -> Sector | None: if not self.app.navigation.selected_campaign_id: return None war = self.app.model.get_war_by_campaign( self.app.navigation.selected_campaign_id ) camp = self.app.model.get_campaign(self.app.navigation.selected_campaign_id) rounds = camp.get_all_rounds() rnd_objs: List[RoundDTO] = [ RoundDTO(id=rnd.id, index=camp.get_round_index(rnd.id), is_over=rnd.is_over) for rnd in rounds ] objectives = war.get_all_objectives() obj_dtos: List[ObjectiveDTO] = [ ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description) for obj in objectives ] dialog = SectorDialog( self.app.view, default_name="", rounds=rnd_objs, objectives=obj_dtos ) if dialog.exec() != QDialog.DialogCode.Accepted: return None name = dialog.get_sector_name() round_id = dialog.get_round_id() major_id = dialog.get_major_id() minor_id = dialog.get_minor_id() influence_id = dialog.get_influence_id() mission = dialog.get_mission() description = dialog.get_description() if not self._validate_sector_inputs( name, round_id, major_id, minor_id, influence_id ): return None return self.app.model.add_sector( self.app.navigation.selected_campaign_id, name, round_id, major_id, minor_id, influence_id, mission, description, ) def edit_sector(self, sector_id: str) -> None: sect = self.app.model.get_sector(sector_id) camp = self.app.model.get_campaign_by_sector(sector_id) war = self.app.model.get_war_by_campaign(camp.id) rounds = camp.get_all_rounds() rnd_dto: List[RoundDTO] = [ RoundDTO(id=rnd.id, index=i, is_over=rnd.is_over) for i, rnd in enumerate(rounds, start=1) ] objectives = war.get_all_objectives() obj_dto: List[ObjectiveDTO] = [ ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description) for obj in objectives ] sect_dialog = SectorDialog( self.app.view, default_name=sect.name, rounds=rnd_dto, default_round_id=sect.round_id, objectives=obj_dto, default_major_id=sect.major_objective_id, default_minor_id=sect.minor_objective_id, default_influence_id=sect.influence_objective_id, default_mission=sect.mission, default_description=sect.description, ) if sect_dialog.exec() == QDialog.DialogCode.Accepted: name = sect_dialog.get_sector_name() round_id = sect_dialog.get_round_id() major_id = sect_dialog.get_major_id() minor_id = sect_dialog.get_minor_id() influence_id = sect_dialog.get_influence_id() mission = sect_dialog.get_mission() description = sect_dialog.get_description() self.app.model.update_sector( sector_id, name=name, round_id=round_id, major_id=major_id, minor_id=minor_id, influence_id=influence_id, mission=mission, description=description, )