warchron_app/src/warchron/controller/campaign_controller.py

370 lines
14 KiB
Python
Raw Normal View History

from typing import List, Dict, Tuple, TYPE_CHECKING
2026-02-10 09:53:49 +01:00
from PyQt6.QtWidgets import QMessageBox, QDialog
from PyQt6.QtGui import QIcon
2026-02-10 09:53:49 +01:00
2026-02-23 19:28:13 +01:00
from warchron.constants import (
RefreshScope,
ContextType,
ItemType,
Icons,
IconName,
RANK_TO_ICON,
)
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
from warchron.model.result_checker import ResultChecker
from warchron.controller.closure_workflow import CampaignClosureWorkflow
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 _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 = {}
2026-02-23 19:28:13 +01:00
for rank, group, token_map in ranking:
base_icon = RANK_TO_ICON.get(rank, IconName.VPNTH)
tie_resolved = TieResolver.is_tie_resolved(
war, ContextType.CAMPAIGN, campaign.id, scores[group[0]].victory_points
)
for pid in group:
2026-02-23 19:28:13 +01:00
spent = token_map.get(pid, 0)
if not tie_resolved and spent == 0:
icon_name = getattr(IconName, f"{base_icon.name}DRAW")
elif tie_resolved and spent == 0 and len(group) > 1:
icon_name = getattr(IconName, f"{base_icon.name}DRAW")
elif tie_resolved and spent > 0 and len(group) == 1:
icon_name = getattr(IconName, f"{base_icon.name}BREAK")
elif tie_resolved and spent > 0 and len(group) > 1:
icon_name = getattr(IconName, f"{base_icon.name}TIEDRAW")
else:
icon_name = base_icon
icon_map[pid] = QIcon(Icons.get_pixmap(icon_name))
return icon_map
2026-02-10 09:53:49 +01:00
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] = []
icon_map = {}
if camp.is_over:
icon_map = self._compute_campaign_ranking_icons(war, camp)
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),
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)
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)
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)
try:
2026-02-20 11:01:25 +01:00
workflow.start(war, camp)
except DomainError as e:
QMessageBox.warning(
self.app.view,
"Closure forbidden",
2026-02-20 11:01:25 +01:00
str(e),
)
return
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-20 11:01:25 +01:00
def resolve_ties(
self, war: War, contexts: List[TieContext]
) -> 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():
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")
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
# 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(
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
2026-02-19 14:17:42 +01:00
# TODO allow same objectives in different fields?
2026-02-10 09:53:49 +01:00
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] = [
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] = [
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
)