split controller

This commit is contained in:
Maxime Réaux 2026-02-10 09:53:49 +01:00
parent 701f6b3292
commit 7792a76f8e
11 changed files with 1212 additions and 987 deletions

View file

@ -0,0 +1,239 @@
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,
)
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),
)
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)
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)
# 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
self.app.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
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, major_id: str, minor_id: str, influence_id: str
) -> 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] = [
RoundDTO(id=rnd.id, index=camp.get_round_index(rnd.id)) 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
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()
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,
)
self.app.is_dirty = True
self.app.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
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) 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,
)
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()
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,
)