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,
|
|
|
|
|
WarParticipantDTO,
|
|
|
|
|
ObjectiveDTO,
|
|
|
|
|
)
|
|
|
|
|
from warchron.view.war_dialog import WarDialog
|
|
|
|
|
from warchron.view.objective_dialog import ObjectiveDialog
|
|
|
|
|
from warchron.view.war_participant_dialog import WarParticipantDialog
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WarController:
|
|
|
|
|
def __init__(self, app: "AppController"):
|
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
|
|
def _fill_war_details(self, war_id: str) -> None:
|
|
|
|
|
war = self.app.model.get_war(war_id)
|
|
|
|
|
self.app.view.show_war_details(name=war.name, year=war.year)
|
2026-02-10 16:26:49 +01:00
|
|
|
self.app.view.set_war_objective_values(
|
|
|
|
|
major=war.major_value,
|
|
|
|
|
minor=war.minor_value,
|
|
|
|
|
influence=war.influence_token,
|
|
|
|
|
)
|
2026-02-10 09:53:49 +01:00
|
|
|
objectives = war.get_all_objectives()
|
|
|
|
|
objectives_for_display: List[ObjectiveDTO] = [
|
|
|
|
|
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
|
|
|
|
for obj in objectives
|
|
|
|
|
]
|
|
|
|
|
self.app.view.display_war_objectives(objectives_for_display)
|
|
|
|
|
war_parts = war.get_all_war_participants()
|
|
|
|
|
participants_for_display: List[WarParticipantDTO] = [
|
|
|
|
|
WarParticipantDTO(
|
|
|
|
|
id=p.id,
|
|
|
|
|
player_name=self.app.model.get_player_name(p.player_id),
|
|
|
|
|
faction=p.faction,
|
|
|
|
|
)
|
|
|
|
|
for p in war_parts
|
|
|
|
|
]
|
|
|
|
|
self.app.view.display_war_participants(participants_for_display)
|
|
|
|
|
|
|
|
|
|
def _validate_war_inputs(self, name: str, year: int) -> bool:
|
|
|
|
|
if not name.strip():
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view, "Invalid name", "War name cannot be empty."
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
if not (1970 <= year <= 3000):
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view, "Invalid year", "Year must be between 1970 and 3000."
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def add_war(self) -> None:
|
|
|
|
|
dialog = WarDialog(
|
|
|
|
|
self.app.view, default_year=self.app.model.get_default_war_values()["year"]
|
|
|
|
|
)
|
|
|
|
|
result = dialog.exec() # modal blocking dialog
|
|
|
|
|
if result == QDialog.DialogCode.Accepted:
|
|
|
|
|
name = dialog.get_war_name()
|
|
|
|
|
year = dialog.get_war_year()
|
|
|
|
|
if not self._validate_war_inputs(name, year):
|
|
|
|
|
return
|
|
|
|
|
war = self.app.model.add_war(name, year)
|
|
|
|
|
self.app.is_dirty = True
|
|
|
|
|
self.app.navigation.refresh_and_select(
|
|
|
|
|
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war.id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def edit_war(self, war_id: str) -> None:
|
|
|
|
|
war = self.app.model.get_war(war_id)
|
|
|
|
|
war_dialog = WarDialog(
|
|
|
|
|
self.app.view, default_name=war.name, default_year=war.year
|
|
|
|
|
)
|
|
|
|
|
if war_dialog.exec() == QDialog.DialogCode.Accepted:
|
|
|
|
|
name = war_dialog.get_war_name()
|
|
|
|
|
year = war_dialog.get_war_year()
|
|
|
|
|
if not self._validate_war_inputs(name, year):
|
|
|
|
|
return
|
|
|
|
|
self.app.model.update_war(war_id, name=name, year=year)
|
|
|
|
|
|
2026-02-10 16:26:49 +01:00
|
|
|
def set_major_value(self, value: int) -> None:
|
|
|
|
|
war_id = self.app.navigation.selected_war_id
|
|
|
|
|
if not war_id:
|
|
|
|
|
return
|
|
|
|
|
self.app.model.set_major_value(war_id, value)
|
|
|
|
|
self.app.is_dirty = True
|
|
|
|
|
|
|
|
|
|
def set_minor_value(self, value: int) -> None:
|
|
|
|
|
war_id = self.app.navigation.selected_war_id
|
|
|
|
|
if not war_id:
|
|
|
|
|
return
|
|
|
|
|
self.app.model.set_minor_value(war_id, value)
|
|
|
|
|
self.app.is_dirty = True
|
|
|
|
|
|
|
|
|
|
def set_influence_token(self, checked: bool) -> None:
|
|
|
|
|
war_id = self.app.navigation.selected_war_id
|
|
|
|
|
if not war_id:
|
|
|
|
|
return
|
|
|
|
|
self.app.model.set_influence_token(war_id, checked)
|
|
|
|
|
self.app.is_dirty = True
|
|
|
|
|
|
2026-02-10 09:53:49 +01:00
|
|
|
# Objective methods
|
|
|
|
|
|
|
|
|
|
def _validate_objective_inputs(self, name: str, description: str) -> bool:
|
|
|
|
|
if not name.strip():
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view, "Invalid name", "Objective name cannot be empty."
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def add_objective(self) -> None:
|
|
|
|
|
if not self.app.navigation.selected_war_id:
|
|
|
|
|
return
|
|
|
|
|
dialog = ObjectiveDialog(self.app.view)
|
|
|
|
|
if dialog.exec() != QDialog.DialogCode.Accepted:
|
|
|
|
|
return
|
|
|
|
|
name = dialog.get_objective_name()
|
|
|
|
|
description = dialog.get_objective_description()
|
|
|
|
|
if not self._validate_objective_inputs(name, description):
|
|
|
|
|
return
|
|
|
|
|
self.app.model.add_objective(
|
|
|
|
|
self.app.navigation.selected_war_id, name, description
|
|
|
|
|
)
|
|
|
|
|
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_objective(self, objective_id: str) -> None:
|
|
|
|
|
obj = self.app.model.get_objective(objective_id)
|
|
|
|
|
obj_dialog = ObjectiveDialog(
|
|
|
|
|
self.app.view, default_name=obj.name, default_description=obj.description
|
|
|
|
|
)
|
|
|
|
|
if obj_dialog.exec() == QDialog.DialogCode.Accepted:
|
|
|
|
|
name = obj_dialog.get_objective_name()
|
|
|
|
|
description = obj_dialog.get_objective_description()
|
|
|
|
|
if not self._validate_objective_inputs(name, description):
|
|
|
|
|
return
|
|
|
|
|
self.app.model.update_objective(
|
|
|
|
|
objective_id, name=name, description=description
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# War participant methods
|
|
|
|
|
|
|
|
|
|
def add_war_participant(self) -> None:
|
|
|
|
|
if not self.app.navigation.selected_war_id:
|
|
|
|
|
return
|
|
|
|
|
players = self.app.model.get_available_players(
|
|
|
|
|
self.app.navigation.selected_war_id
|
|
|
|
|
)
|
|
|
|
|
play_opts: List[ParticipantOption] = [
|
|
|
|
|
ParticipantOption(id=p.id, name=p.name) for p in players
|
|
|
|
|
]
|
|
|
|
|
dialog = WarParticipantDialog(self.app.view, players=play_opts)
|
|
|
|
|
if dialog.exec() != QDialog.DialogCode.Accepted:
|
|
|
|
|
return
|
|
|
|
|
player_id = dialog.get_player_id()
|
|
|
|
|
faction = dialog.get_participant_faction()
|
|
|
|
|
if not player_id:
|
|
|
|
|
return
|
|
|
|
|
self.app.model.add_war_participant(
|
|
|
|
|
self.app.navigation.selected_war_id, player_id, faction
|
|
|
|
|
)
|
|
|
|
|
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_war_participant(self, participant_id: str) -> None:
|
|
|
|
|
war_part = self.app.model.get_war_participant(participant_id)
|
|
|
|
|
player = self.app.model.get_player(war_part.player_id)
|
|
|
|
|
play_opt = ParticipantOption(id=player.id, name=player.name)
|
|
|
|
|
war_part_dialog = WarParticipantDialog(
|
|
|
|
|
self.app.view,
|
|
|
|
|
players=[play_opt],
|
|
|
|
|
default_player_id=war_part.id,
|
|
|
|
|
default_faction=war_part.faction,
|
|
|
|
|
editable_player=False,
|
|
|
|
|
)
|
|
|
|
|
if war_part_dialog.exec() == QDialog.DialogCode.Accepted:
|
|
|
|
|
faction = war_part_dialog.get_participant_faction()
|
|
|
|
|
self.app.model.update_war_participant(participant_id, faction=faction)
|