2026-03-06 15:02:53 +01:00
|
|
|
from typing import List, TYPE_CHECKING, Dict
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
from PyQt6.QtWidgets import QMessageBox, QDialog
|
2026-03-03 15:39:30 +01:00
|
|
|
from PyQt6.QtGui import QIcon
|
2026-02-10 09:53:49 +01:00
|
|
|
|
2026-02-23 21:18:27 +01:00
|
|
|
from warchron.constants import (
|
|
|
|
|
RefreshScope,
|
|
|
|
|
ItemType,
|
|
|
|
|
ContextType,
|
|
|
|
|
)
|
2026-02-24 15:40:24 +01:00
|
|
|
from warchron.model.exception import (
|
|
|
|
|
DomainError,
|
2026-03-12 16:28:20 +01:00
|
|
|
AbortedOperation,
|
2026-02-24 15:40:24 +01:00
|
|
|
RequiresConfirmation,
|
|
|
|
|
)
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from warchron.controller.app_controller import AppController
|
|
|
|
|
from warchron.controller.dtos import (
|
|
|
|
|
ParticipantOption,
|
2026-02-23 21:18:27 +01:00
|
|
|
WarParticipantScoreDTO,
|
2026-02-10 09:53:49 +01:00
|
|
|
ObjectiveDTO,
|
|
|
|
|
)
|
2026-02-13 15:44:28 +01:00
|
|
|
from warchron.model.war import War
|
|
|
|
|
from warchron.model.war_participant import WarParticipant
|
|
|
|
|
from warchron.model.objective import Objective
|
2026-02-23 21:18:27 +01:00
|
|
|
from warchron.model.tie_manager import TieContext, TieResolver
|
|
|
|
|
from warchron.model.score_service import ScoreService
|
|
|
|
|
from warchron.controller.closure_workflow import WarClosureWorkflow
|
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.war_dialog import WarDialog
|
|
|
|
|
from warchron.view.objective_dialog import ObjectiveDialog
|
|
|
|
|
from warchron.view.war_participant_dialog import WarParticipantDialog
|
2026-02-23 21:18:27 +01:00
|
|
|
from warchron.view.tie_dialog import TieDialog
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2026-03-06 16:39:22 +01:00
|
|
|
limited_objectives_for_display: List[ObjectiveDTO] = [
|
|
|
|
|
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
|
|
|
|
for obj in war.get_objectives_used_as_maj_or_min()
|
|
|
|
|
]
|
2026-02-23 21:18:27 +01:00
|
|
|
scores = ScoreService.compute_scores(war, ContextType.WAR, war.id)
|
|
|
|
|
rows: List[WarParticipantScoreDTO] = []
|
2026-03-03 15:39:30 +01:00
|
|
|
vp_icon_map: dict[str, QIcon] = {}
|
|
|
|
|
objective_icon_maps: Dict[str, Dict[str, QIcon]] = {}
|
2026-02-23 21:18:27 +01:00
|
|
|
if war.is_over:
|
2026-03-03 15:39:30 +01:00
|
|
|
vp_icon_map = RankingIcon.compute_icons(
|
|
|
|
|
war, ContextType.WAR, war_id, scores
|
|
|
|
|
)
|
2026-03-06 16:39:22 +01:00
|
|
|
for obj in war.get_objectives_used_as_maj_or_min():
|
2026-03-03 15:39:30 +01:00
|
|
|
objective_icon_maps[obj.id] = RankingIcon.compute_icons(
|
|
|
|
|
war,
|
2026-03-06 15:02:53 +01:00
|
|
|
ContextType.WAR,
|
|
|
|
|
war.id,
|
2026-03-03 15:39:30 +01:00
|
|
|
scores,
|
|
|
|
|
objective_id=obj.id,
|
|
|
|
|
)
|
2026-02-23 21:18:27 +01:00
|
|
|
for war_part in war.get_all_war_participants():
|
|
|
|
|
player_name = self.app.model.get_player_name(war_part.player_id)
|
|
|
|
|
score = scores[war_part.id]
|
2026-03-03 15:39:30 +01:00
|
|
|
objective_icons = {
|
|
|
|
|
obj_id: icon_map[war_part.id]
|
|
|
|
|
for obj_id, icon_map in objective_icon_maps.items()
|
|
|
|
|
if war_part.id in icon_map
|
|
|
|
|
}
|
2026-02-23 21:18:27 +01:00
|
|
|
rows.append(
|
|
|
|
|
WarParticipantScoreDTO(
|
|
|
|
|
war_participant_id=war_part.id,
|
|
|
|
|
player_id=war_part.player_id,
|
|
|
|
|
player_name=player_name,
|
|
|
|
|
faction=war_part.faction 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-03-03 15:39:30 +01:00
|
|
|
rank_icon=vp_icon_map.get(war_part.id),
|
|
|
|
|
objective_icons=objective_icons,
|
2026-02-23 21:18:27 +01:00
|
|
|
)
|
2026-02-10 09:53:49 +01:00
|
|
|
)
|
2026-03-06 16:39:22 +01:00
|
|
|
self.app.view.display_war_participants(rows, limited_objectives_for_display)
|
2026-02-24 09:11:37 +01:00
|
|
|
self.app.view.endWarBtn.setEnabled(not war.is_over)
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2026-02-13 15:44:28 +01:00
|
|
|
def create_war(self) -> War | None:
|
2026-02-10 09:53:49 +01:00
|
|
|
dialog = WarDialog(
|
|
|
|
|
self.app.view, default_year=self.app.model.get_default_war_values()["year"]
|
|
|
|
|
)
|
2026-02-13 15:44:28 +01:00
|
|
|
result = dialog.exec()
|
|
|
|
|
if result != QDialog.DialogCode.Accepted:
|
|
|
|
|
return None
|
|
|
|
|
name = dialog.get_war_name()
|
|
|
|
|
year = dialog.get_war_year()
|
|
|
|
|
if not self._validate_war_inputs(name, year):
|
|
|
|
|
return None
|
|
|
|
|
return self.app.model.add_war(name, year)
|
2026-02-10 09:53:49 +01:00
|
|
|
|
|
|
|
|
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-11 19:22:43 +01:00
|
|
|
def close_war(self) -> None:
|
|
|
|
|
war_id = self.app.navigation.selected_war_id
|
|
|
|
|
if not war_id:
|
|
|
|
|
return
|
|
|
|
|
war = self.app.model.get_war(war_id)
|
2026-02-23 21:18:27 +01:00
|
|
|
workflow = WarClosureWorkflow(self.app)
|
2026-02-11 19:22:43 +01:00
|
|
|
try:
|
2026-02-23 21:18:27 +01:00
|
|
|
workflow.start(war)
|
|
|
|
|
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-23 21:18:27 +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-23 21:18:27 +01:00
|
|
|
self.app.navigation.refresh_and_select(
|
|
|
|
|
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def resolve_ties(
|
|
|
|
|
self, war: War, contexts: List[TieContext]
|
2026-03-06 15:02:53 +01:00
|
|
|
) -> Dict[tuple[str, str, int | None], Dict[str, bool]]:
|
2026-02-23 21:18:27 +01:00
|
|
|
bids_map = {}
|
|
|
|
|
for ctx in contexts:
|
|
|
|
|
active = TieResolver.get_active_participants(
|
2026-02-25 16:54:21 +01:00
|
|
|
war,
|
2026-03-06 15:02:53 +01:00
|
|
|
ctx,
|
2026-02-25 16:54:21 +01:00
|
|
|
ctx.participants,
|
2026-02-23 21:18:27 +01:00
|
|
|
)
|
|
|
|
|
players = [
|
|
|
|
|
ParticipantOption(id=pid, name=self.app.model.get_participant_name(pid))
|
|
|
|
|
for pid in active
|
|
|
|
|
]
|
|
|
|
|
counters = [war.get_influence_tokens(pid) for pid in active]
|
|
|
|
|
dialog = TieDialog(
|
|
|
|
|
parent=self.app.view,
|
|
|
|
|
players=players,
|
|
|
|
|
counters=counters,
|
|
|
|
|
context_type=ContextType.WAR,
|
|
|
|
|
context_id=ctx.context_id,
|
2026-03-03 11:52:07 +01:00
|
|
|
context_name=None,
|
2026-02-23 21:18:27 +01:00
|
|
|
)
|
2026-03-06 15:02:53 +01:00
|
|
|
if ctx.objective_id:
|
|
|
|
|
objective = war.objectives[ctx.objective_id]
|
2026-03-03 11:52:07 +01:00
|
|
|
dialog = TieDialog(
|
|
|
|
|
parent=self.app.view,
|
|
|
|
|
players=players,
|
|
|
|
|
counters=counters,
|
|
|
|
|
context_type=ctx.context_type,
|
|
|
|
|
context_id=ctx.context_id,
|
2026-03-06 15:02:53 +01:00
|
|
|
context_name=f"Objective tie: {objective.name}",
|
2026-03-03 11:52:07 +01:00
|
|
|
)
|
2026-02-23 21:18:27 +01:00
|
|
|
if not dialog.exec():
|
2026-03-06 15:02:53 +01:00
|
|
|
TieResolver.cancel_tie_break(war, ctx)
|
2026-03-12 16:28:20 +01:00
|
|
|
raise AbortedOperation("Tie resolution cancelled")
|
2026-03-06 15:02:53 +01:00
|
|
|
bids_map[ctx.key()] = dialog.get_bids()
|
2026-02-23 21:18:27 +01:00
|
|
|
return bids_map
|
2026-02-11 19:22:43 +01:00
|
|
|
|
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
|
2026-02-13 16:12:43 +01:00
|
|
|
try:
|
|
|
|
|
self.app.model.set_major_value(war_id, value)
|
|
|
|
|
except DomainError as e:
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view,
|
|
|
|
|
"Setting forbidden",
|
|
|
|
|
str(e),
|
|
|
|
|
)
|
2026-02-24 15:40:24 +01:00
|
|
|
return
|
2026-02-10 16:26:49 +01:00
|
|
|
self.app.is_dirty = True
|
2026-02-13 16:12:43 +01:00
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
2026-02-10 16:26:49 +01:00
|
|
|
|
|
|
|
|
def set_minor_value(self, value: int) -> None:
|
|
|
|
|
war_id = self.app.navigation.selected_war_id
|
|
|
|
|
if not war_id:
|
|
|
|
|
return
|
2026-02-13 16:12:43 +01:00
|
|
|
try:
|
|
|
|
|
self.app.model.set_minor_value(war_id, value)
|
|
|
|
|
except DomainError as e:
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view,
|
|
|
|
|
"Setting forbidden",
|
|
|
|
|
str(e),
|
|
|
|
|
)
|
2026-02-24 15:40:24 +01:00
|
|
|
return
|
2026-02-10 16:26:49 +01:00
|
|
|
self.app.is_dirty = True
|
2026-02-13 16:12:43 +01:00
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
2026-02-10 16:26:49 +01:00
|
|
|
|
|
|
|
|
def set_influence_token(self, checked: bool) -> None:
|
|
|
|
|
war_id = self.app.navigation.selected_war_id
|
|
|
|
|
if not war_id:
|
|
|
|
|
return
|
2026-02-13 16:12:43 +01:00
|
|
|
try:
|
|
|
|
|
self.app.model.set_influence_token(war_id, checked)
|
|
|
|
|
except DomainError as e:
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view,
|
|
|
|
|
"Setting forbidden",
|
|
|
|
|
str(e),
|
|
|
|
|
)
|
2026-02-24 15:40:24 +01:00
|
|
|
return
|
|
|
|
|
except RequiresConfirmation as e:
|
|
|
|
|
reply = QMessageBox.question(
|
|
|
|
|
self.app.view,
|
|
|
|
|
"Confirm update",
|
|
|
|
|
str(e),
|
|
|
|
|
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
|
|
|
|
)
|
|
|
|
|
if reply == QMessageBox.StandardButton.Yes:
|
|
|
|
|
e.action()
|
|
|
|
|
else:
|
|
|
|
|
return
|
2026-02-10 16:26:49 +01:00
|
|
|
self.app.is_dirty = True
|
2026-02-13 16:12:43 +01:00
|
|
|
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
2026-02-24 15:40:24 +01:00
|
|
|
self.app.navigation.refresh_and_select(
|
|
|
|
|
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war_id
|
|
|
|
|
)
|
2026-02-10 16:26:49 +01:00
|
|
|
|
2026-02-10 09:53:49 +01:00
|
|
|
# Objective methods
|
|
|
|
|
|
2026-02-11 19:22:43 +01:00
|
|
|
def _validate_objective_inputs(self, name: str, description: str | None) -> bool:
|
2026-02-10 09:53:49 +01:00
|
|
|
if not name.strip():
|
|
|
|
|
QMessageBox.warning(
|
|
|
|
|
self.app.view, "Invalid name", "Objective name cannot be empty."
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
2026-02-13 15:44:28 +01:00
|
|
|
def create_objective(self) -> Objective | 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 = ObjectiveDialog(self.app.view)
|
|
|
|
|
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_objective_name()
|
|
|
|
|
description = dialog.get_objective_description()
|
|
|
|
|
if not self._validate_objective_inputs(name, description):
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
|
|
|
|
return self.app.model.add_objective(
|
2026-02-10 09:53:49 +01:00
|
|
|
self.app.navigation.selected_war_id, name, description
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2026-02-13 15:44:28 +01:00
|
|
|
def create_war_participant(self) -> WarParticipant | 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
|
|
|
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:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
2026-02-10 09:53:49 +01:00
|
|
|
player_id = dialog.get_player_id()
|
|
|
|
|
faction = dialog.get_participant_faction()
|
|
|
|
|
if not player_id:
|
2026-02-13 15:44:28 +01:00
|
|
|
return None
|
|
|
|
|
return self.app.model.add_war_participant(
|
2026-02-10 09:53:49 +01:00
|
|
|
self.app.navigation.selected_war_id, player_id, faction
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|