2026-02-25 16:54:21 +01:00
|
|
|
from typing import List, Dict, Tuple, TYPE_CHECKING
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
from PyQt6.QtWidgets import QMessageBox, QDialog
|
|
|
|
|
|
2026-02-23 19:28:13 +01:00
|
|
|
from warchron.constants import (
|
|
|
|
|
RefreshScope,
|
|
|
|
|
ContextType,
|
|
|
|
|
ItemType,
|
|
|
|
|
)
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from warchron.controller.app_controller import AppController
|
|
|
|
|
from warchron.controller.dtos import (
|
|
|
|
|
ParticipantOption,
|
|
|
|
|
ObjectiveDTO,
|
|
|
|
|
SectorDTO,
|
|
|
|
|
RoundDTO,
|
2026-02-19 14:17:42 +01:00
|
|
|
CampaignParticipantScoreDTO,
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|
2026-02-20 11:01:25 +01:00
|
|
|
from warchron.model.exception import ForbiddenOperation, DomainError
|
|
|
|
|
from warchron.model.war import War
|
2026-02-13 15:44:28 +01:00
|
|
|
from warchron.model.campaign import Campaign
|
|
|
|
|
from warchron.model.campaign_participant import CampaignParticipant
|
|
|
|
|
from warchron.model.sector import Sector
|
2026-02-23 13:16:45 +01:00
|
|
|
from warchron.model.tie_manager import TieContext, TieResolver
|
2026-02-19 14:17:42 +01:00
|
|
|
from warchron.model.score_service import ScoreService
|
2026-02-23 17:36:28 +01:00
|
|
|
from warchron.controller.closure_workflow import CampaignClosureWorkflow
|
2026-02-26 11:28:29 +01:00
|
|
|
from warchron.controller.ranking_icon import RankingIcon
|
2026-02-10 09:53:49 +01:00
|
|
|
from warchron.view.campaign_dialog import CampaignDialog
|
|
|
|
|
from warchron.view.campaign_participant_dialog import CampaignParticipantDialog
|
|
|
|
|
from warchron.view.sector_dialog import SectorDialog
|
2026-02-20 11:01:25 +01:00
|
|
|
from warchron.view.tie_dialog import TieDialog
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CampaignController:
|
|
|
|
|
def __init__(self, app: "AppController"):
|
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
|
|
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),
|
2026-02-12 09:10:03 +01:00
|
|
|
mission=sect.mission,
|
|
|
|
|
description=sect.description,
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|
|
|
|
|
for sect in sectors
|
|
|
|
|
]
|
|
|
|
|
self.app.view.display_campaign_sectors(sectors_for_display)
|
2026-02-19 14:17:42 +01:00
|
|
|
scores = ScoreService.compute_scores(war, ContextType.CAMPAIGN, campaign_id)
|
|
|
|
|
rows: List[CampaignParticipantScoreDTO] = []
|
2026-02-23 17:36:28 +01:00
|
|
|
icon_map = {}
|
|
|
|
|
if camp.is_over:
|
2026-02-26 11:28:29 +01:00
|
|
|
icon_map = RankingIcon.compute_icons(
|
|
|
|
|
war, ContextType.CAMPAIGN, campaign_id, scores
|
|
|
|
|
)
|
2026-02-19 14:17:42 +01:00
|
|
|
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),
|
2026-02-24 09:06:13 +01:00
|
|
|
tokens=war.get_influence_tokens(war_part.id),
|
2026-02-23 17:36:28 +01:00
|
|
|
rank_icon=icon_map.get(war_part_id),
|
2026-02-19 14:17:42 +01:00
|
|
|
)
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|
2026-02-19 14:17:42 +01:00
|
|
|
objectives = [
|
|
|
|
|
ObjectiveDTO(o.id, o.name, o.description) for o in war.get_all_objectives()
|
2026-02-10 09:53:49 +01:00
|
|
|
]
|
2026-02-19 14:17:42 +01:00
|
|
|
self.app.view.display_campaign_participants(rows, objectives)
|
2026-02-11 19:22:43 +01:00
|
|
|
self.app.view.endCampaignBtn.setEnabled(not camp.is_over)
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2026-02-13 15:44:28 +01:00
|
|
|
def create_campaign(self) -> Campaign | None:
|
2026-02-10 09:53:49 +01:00
|
|
|
if not self.app.navigation.selected_war_id:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
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:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
name = dialog.get_campaign_name()
|
|
|
|
|
month = dialog.get_campaign_month()
|
|
|
|
|
if not self._validate_campaign_inputs(name, month):
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
|
|
|
|
return self.app.model.add_campaign(
|
2026-02-10 09:53:49 +01:00
|
|
|
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)
|
|
|
|
|
|
2026-02-11 19:22:43 +01:00
|
|
|
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)
|
2026-02-20 11:01:25 +01:00
|
|
|
war = self.app.model.get_war_by_campaign(campaign_id)
|
|
|
|
|
workflow = CampaignClosureWorkflow(self.app)
|
2026-02-11 19:22:43 +01:00
|
|
|
try:
|
2026-02-20 11:01:25 +01:00
|
|
|
workflow.start(war, camp)
|
|
|
|
|
except DomainError as e:
|
|
|
|
|
QMessageBox.warning(
|
2026-02-11 19:22:43 +01:00
|
|
|
self.app.view,
|
2026-02-24 15:40:24 +01:00
|
|
|
"Closure forbidden",
|
2026-02-20 11:01:25 +01:00
|
|
|
str(e),
|
2026-02-11 19:22:43 +01:00
|
|
|
)
|
2026-02-24 15:40:24 +01:00
|
|
|
return
|
2026-02-11 19:22:43 +01:00
|
|
|
self.app.is_dirty = True
|
|
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
2026-02-20 11:01:25 +01:00
|
|
|
self.app.navigation.refresh_and_select(
|
|
|
|
|
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=campaign_id
|
|
|
|
|
)
|
2026-02-11 19:22:43 +01:00
|
|
|
|
2026-02-20 11:01:25 +01:00
|
|
|
def resolve_ties(
|
|
|
|
|
self, war: War, contexts: List[TieContext]
|
2026-02-25 16:54:21 +01:00
|
|
|
) -> Dict[Tuple[ContextType, str, int | None], Dict[str, bool]]:
|
2026-02-20 11:01:25 +01:00
|
|
|
bids_map = {}
|
|
|
|
|
for ctx in contexts:
|
2026-02-23 13:16:45 +01:00
|
|
|
active = TieResolver.get_active_participants(
|
|
|
|
|
war, ctx.context_type, ctx.context_id, ctx.participants
|
|
|
|
|
)
|
2026-02-20 11:01:25 +01:00
|
|
|
players = [
|
2026-02-23 13:16:45 +01:00
|
|
|
ParticipantOption(id=pid, name=self.app.model.get_participant_name(pid))
|
|
|
|
|
for pid in active
|
2026-02-20 11:01:25 +01:00
|
|
|
]
|
2026-02-23 13:16:45 +01:00
|
|
|
counters = [war.get_influence_tokens(pid) for pid in active]
|
2026-02-20 11:01:25 +01:00
|
|
|
dialog = TieDialog(
|
|
|
|
|
parent=self.app.view,
|
|
|
|
|
players=players,
|
|
|
|
|
counters=counters,
|
|
|
|
|
context_type=ContextType.CAMPAIGN,
|
|
|
|
|
context_id=ctx.context_id,
|
|
|
|
|
)
|
|
|
|
|
if not dialog.exec():
|
2026-02-25 16:54:21 +01:00
|
|
|
TieResolver.cancel_tie_break(
|
|
|
|
|
war, ContextType.CAMPAIGN, ctx.context_id, ctx.score_value
|
|
|
|
|
)
|
2026-02-20 11:01:25 +01:00
|
|
|
raise ForbiddenOperation("Tie resolution cancelled")
|
2026-02-25 16:54:21 +01:00
|
|
|
bids_map[(ctx.context_type, ctx.context_id, ctx.score_value)] = (
|
|
|
|
|
dialog.get_bids()
|
|
|
|
|
)
|
2026-02-20 11:01:25 +01:00
|
|
|
return bids_map
|
2026-02-10 09:53:49 +01:00
|
|
|
|
2026-02-25 16:54:21 +01:00
|
|
|
# Campaign participant methods
|
|
|
|
|
|
2026-02-13 15:44:28 +01:00
|
|
|
def create_campaign_participant(self) -> CampaignParticipant | None:
|
2026-02-10 09:53:49 +01:00
|
|
|
if not self.app.navigation.selected_campaign_id:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
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:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
player_id = dialog.get_player_id()
|
|
|
|
|
leader = dialog.get_participant_leader()
|
|
|
|
|
theme = dialog.get_participant_theme()
|
|
|
|
|
if not player_id:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
|
|
|
|
return self.app.model.add_campaign_participant(
|
2026-02-10 09:53:49 +01:00
|
|
|
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(
|
2026-02-11 19:22:43 +01:00
|
|
|
self,
|
|
|
|
|
name: str,
|
|
|
|
|
round_id: str | None,
|
|
|
|
|
major_id: str | None,
|
|
|
|
|
minor_id: str | None,
|
|
|
|
|
influence_id: str | None,
|
2026-02-10 09:53:49 +01:00
|
|
|
) -> bool:
|
|
|
|
|
|
|
|
|
|
if not name.strip():
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view, "Invalid name", "Sector name cannot be empty."
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2026-02-13 15:44:28 +01:00
|
|
|
def create_sector(self) -> Sector | None:
|
2026-02-10 09:53:49 +01:00
|
|
|
if not self.app.navigation.selected_campaign_id:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
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] = [
|
2026-02-12 15:12:28 +01:00
|
|
|
RoundDTO(id=rnd.id, index=camp.get_round_index(rnd.id), is_over=rnd.is_over)
|
|
|
|
|
for rnd in rounds
|
2026-02-10 09:53:49 +01:00
|
|
|
]
|
|
|
|
|
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:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
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()
|
2026-02-12 09:10:03 +01:00
|
|
|
mission = dialog.get_mission()
|
|
|
|
|
description = dialog.get_description()
|
2026-02-10 09:53:49 +01:00
|
|
|
if not self._validate_sector_inputs(
|
|
|
|
|
name, round_id, major_id, minor_id, influence_id
|
|
|
|
|
):
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
|
|
|
|
return self.app.model.add_sector(
|
2026-02-10 09:53:49 +01:00
|
|
|
self.app.navigation.selected_campaign_id,
|
|
|
|
|
name,
|
|
|
|
|
round_id,
|
|
|
|
|
major_id,
|
|
|
|
|
minor_id,
|
|
|
|
|
influence_id,
|
2026-02-12 09:10:03 +01:00
|
|
|
mission,
|
|
|
|
|
description,
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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] = [
|
2026-02-12 15:12:28 +01:00
|
|
|
RoundDTO(id=rnd.id, index=i, is_over=rnd.is_over)
|
|
|
|
|
for i, rnd in enumerate(rounds, start=1)
|
2026-02-10 09:53:49 +01:00
|
|
|
]
|
|
|
|
|
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,
|
2026-02-12 09:10:03 +01:00
|
|
|
default_mission=sect.mission,
|
|
|
|
|
default_description=sect.description,
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|
|
|
|
|
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()
|
2026-02-12 09:10:03 +01:00
|
|
|
mission = sect_dialog.get_mission()
|
|
|
|
|
description = sect_dialog.get_description()
|
2026-02-10 09:53:49 +01:00
|
|
|
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,
|
2026-02-12 09:10:03 +01:00
|
|
|
mission=mission,
|
|
|
|
|
description=description,
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|