2026-02-10 09:53:49 +01:00
|
|
|
from typing import List, TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
from PyQt6.QtWidgets import QMessageBox, QDialog
|
|
|
|
|
|
|
|
|
|
from warchron.constants import ItemType, RefreshScope
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from warchron.controller.app_controller import AppController
|
|
|
|
|
from warchron.controller.dtos import (
|
|
|
|
|
ParticipantOption,
|
|
|
|
|
ObjectiveDTO,
|
|
|
|
|
CampaignParticipantDTO,
|
|
|
|
|
SectorDTO,
|
|
|
|
|
RoundDTO,
|
|
|
|
|
)
|
2026-02-11 19:22:43 +01:00
|
|
|
from warchron.model.closure_service import ClosureService
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
camp_parts = camp.get_all_campaign_participants()
|
|
|
|
|
participants_for_display: List[CampaignParticipantDTO] = [
|
|
|
|
|
CampaignParticipantDTO(
|
|
|
|
|
id=p.id,
|
|
|
|
|
player_name=self.app.model.get_participant_name(p.war_participant_id),
|
|
|
|
|
leader=p.leader or "",
|
|
|
|
|
theme=p.theme or "",
|
|
|
|
|
)
|
|
|
|
|
for p in camp_parts
|
|
|
|
|
]
|
|
|
|
|
self.app.view.display_campaign_participants(participants_for_display)
|
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
|
|
|
|
|
|
|
|
|
|
def add_campaign(self) -> None:
|
|
|
|
|
if not self.app.navigation.selected_war_id:
|
|
|
|
|
return
|
|
|
|
|
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
|
|
|
|
|
name = dialog.get_campaign_name()
|
|
|
|
|
month = dialog.get_campaign_month()
|
|
|
|
|
if not self._validate_campaign_inputs(name, month):
|
|
|
|
|
return
|
|
|
|
|
camp = self.app.model.add_campaign(
|
|
|
|
|
self.app.navigation.selected_war_id, name, month
|
|
|
|
|
)
|
|
|
|
|
self.app.is_dirty = True
|
|
|
|
|
self.app.navigation.refresh_and_select(
|
|
|
|
|
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=camp.id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
if camp.is_over:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
ties = ClosureService.close_campaign(camp)
|
|
|
|
|
except RuntimeError as e:
|
|
|
|
|
QMessageBox.warning(self.app.view, "Cannot close campaign", str(e))
|
|
|
|
|
return
|
|
|
|
|
if ties:
|
|
|
|
|
QMessageBox.information(
|
|
|
|
|
self.app.view,
|
|
|
|
|
"Tie detected",
|
|
|
|
|
"Campaign has unresolved ties.",
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
self.app.is_dirty = True
|
|
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
2026-02-12 15:12:28 +01:00
|
|
|
self.app.navigation.refresh(RefreshScope.WARS_TREE)
|
2026-02-11 19:22:43 +01:00
|
|
|
|
2026-02-10 09:53:49 +01:00
|
|
|
# Campaign participant methods
|
|
|
|
|
|
|
|
|
|
def add_campaign_participant(self) -> None:
|
|
|
|
|
if not self.app.navigation.selected_campaign_id:
|
|
|
|
|
return
|
|
|
|
|
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
|
|
|
|
|
player_id = dialog.get_player_id()
|
|
|
|
|
leader = dialog.get_participant_leader()
|
|
|
|
|
theme = dialog.get_participant_theme()
|
|
|
|
|
if not player_id:
|
|
|
|
|
return
|
|
|
|
|
self.app.model.add_campaign_participant(
|
|
|
|
|
self.app.navigation.selected_campaign_id, player_id, leader, theme
|
|
|
|
|
)
|
|
|
|
|
self.app.is_dirty = True
|
2026-02-10 11:09:58 +01:00
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
# allow same objectives in different fields?
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def add_sector(self) -> None:
|
|
|
|
|
if not self.app.navigation.selected_campaign_id:
|
|
|
|
|
return
|
|
|
|
|
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:
|
|
|
|
|
return
|
|
|
|
|
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
|
|
|
|
|
):
|
|
|
|
|
return
|
|
|
|
|
self.app.model.add_sector(
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
self.app.is_dirty = True
|
2026-02-10 11:09:58 +01:00
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
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
|
|
|
)
|