close war and manage tie
This commit is contained in:
parent
d766befd31
commit
71e987304b
7 changed files with 185 additions and 55 deletions
|
|
@ -50,3 +50,20 @@ class CampaignClosureWorkflow(ClosureWorkflow):
|
|||
)
|
||||
ties = TieResolver.find_campaign_ties(war, campaign.id)
|
||||
ClosureService.finalize_campaign(campaign)
|
||||
|
||||
|
||||
class WarClosureWorkflow(ClosureWorkflow):
|
||||
|
||||
def start(self, war: War) -> None:
|
||||
ClosureService.check_war_closable(war)
|
||||
ties = TieResolver.find_war_ties(war)
|
||||
while ties:
|
||||
bids_map = self.app.wars.resolve_ties(war, ties)
|
||||
for tie in ties:
|
||||
bids = bids_map[tie.context_id]
|
||||
TieResolver.apply_bids(war, tie.context_type, tie.context_id, bids)
|
||||
TieResolver.resolve_tie_state(
|
||||
war, tie.context_type, tie.context_id, tie.participants, bids
|
||||
)
|
||||
ties = TieResolver.find_war_ties(war)
|
||||
ClosureService.finalize_war(war)
|
||||
|
|
|
|||
|
|
@ -125,3 +125,14 @@ class CampaignParticipantScoreDTO:
|
|||
victory_points: int
|
||||
narrative_points: Dict[str, int]
|
||||
rank_icon: QIcon | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class WarParticipantScoreDTO:
|
||||
war_participant_id: str
|
||||
player_id: str
|
||||
player_name: str
|
||||
faction: str
|
||||
victory_points: int
|
||||
narrative_points: Dict[str, int]
|
||||
rank_icon: QIcon | None = None
|
||||
|
|
|
|||
|
|
@ -1,30 +1,71 @@
|
|||
from typing import List, TYPE_CHECKING
|
||||
from typing import List, TYPE_CHECKING, Dict
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox, QDialog
|
||||
from PyQt6.QtGui import QIcon
|
||||
|
||||
from warchron.constants import RefreshScope
|
||||
from warchron.model.exception import DomainError
|
||||
from warchron.constants import (
|
||||
RefreshScope,
|
||||
ItemType,
|
||||
ContextType,
|
||||
Icons,
|
||||
IconName,
|
||||
RANK_TO_ICON,
|
||||
)
|
||||
from warchron.model.exception import DomainError, ForbiddenOperation
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
from warchron.controller.dtos import (
|
||||
ParticipantOption,
|
||||
WarParticipantDTO,
|
||||
WarParticipantScoreDTO,
|
||||
ObjectiveDTO,
|
||||
)
|
||||
from warchron.model.war import War
|
||||
from warchron.model.war_participant import WarParticipant
|
||||
from warchron.model.objective import Objective
|
||||
from warchron.model.closure_service import ClosureService
|
||||
from warchron.model.tie_manager import TieContext, TieResolver
|
||||
from warchron.model.score_service import ScoreService
|
||||
from warchron.model.result_checker import ResultChecker
|
||||
from warchron.controller.closure_workflow import WarClosureWorkflow
|
||||
from warchron.view.war_dialog import WarDialog
|
||||
from warchron.view.objective_dialog import ObjectiveDialog
|
||||
from warchron.view.war_participant_dialog import WarParticipantDialog
|
||||
from warchron.view.tie_dialog import TieDialog
|
||||
|
||||
|
||||
class WarController:
|
||||
def __init__(self, app: "AppController"):
|
||||
self.app = app
|
||||
|
||||
def _compute_war_ranking_icons(self, war: War) -> Dict[str, QIcon]:
|
||||
scores = ScoreService.compute_scores(
|
||||
war,
|
||||
ContextType.WAR,
|
||||
war.id,
|
||||
)
|
||||
ranking = ResultChecker.get_effective_ranking(
|
||||
war, ContextType.WAR, war.id, scores
|
||||
)
|
||||
icon_map = {}
|
||||
for rank, group, token_map in ranking:
|
||||
base_icon = RANK_TO_ICON.get(rank, IconName.VPNTH)
|
||||
tie_id = f"{war.id}:score:{scores[group[0]].victory_points}"
|
||||
tie_resolved = TieResolver.is_tie_resolved(war, ContextType.WAR, tie_id)
|
||||
for pid in group:
|
||||
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
|
||||
|
||||
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)
|
||||
|
|
@ -39,17 +80,27 @@ class WarController:
|
|||
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,
|
||||
scores = ScoreService.compute_scores(war, ContextType.WAR, war.id)
|
||||
rows: List[WarParticipantScoreDTO] = []
|
||||
icon_map = {}
|
||||
if war.is_over:
|
||||
icon_map = self._compute_war_ranking_icons(war)
|
||||
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]
|
||||
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),
|
||||
rank_icon=icon_map.get(war_part.id),
|
||||
)
|
||||
)
|
||||
for p in war_parts
|
||||
]
|
||||
self.app.view.display_war_participants(participants_for_display)
|
||||
self.app.view.endWarBtn.setEnabled(not war.is_over)
|
||||
self.app.view.display_war_participants(rows, objectives_for_display)
|
||||
self.app.view.endCampaignBtn.setEnabled(not war.is_over)
|
||||
|
||||
def _validate_war_inputs(self, name: str, year: int) -> bool:
|
||||
if not name.strip():
|
||||
|
|
@ -94,23 +145,45 @@ class WarController:
|
|||
if not war_id:
|
||||
return
|
||||
war = self.app.model.get_war(war_id)
|
||||
if war.is_over:
|
||||
return
|
||||
workflow = WarClosureWorkflow(self.app)
|
||||
try:
|
||||
ties = ClosureService.close_war(war)
|
||||
except RuntimeError as e:
|
||||
QMessageBox.warning(self.app.view, "Cannot close war", str(e))
|
||||
return
|
||||
if ties:
|
||||
QMessageBox.information(
|
||||
workflow.start(war)
|
||||
except DomainError as e:
|
||||
QMessageBox.warning(
|
||||
self.app.view,
|
||||
"Tie detected",
|
||||
"War has unresolved ties.",
|
||||
"Deletion forbidden",
|
||||
str(e),
|
||||
)
|
||||
return
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh(RefreshScope.CURRENT_SELECTION_DETAILS)
|
||||
self.app.navigation.refresh(RefreshScope.WARS_TREE)
|
||||
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]
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
bids_map = {}
|
||||
for ctx in contexts:
|
||||
active = TieResolver.get_active_participants(
|
||||
war, ctx.context_type, ctx.context_id, ctx.participants
|
||||
)
|
||||
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,
|
||||
)
|
||||
if not dialog.exec():
|
||||
raise ForbiddenOperation("Tie resolution cancelled")
|
||||
bids_map[ctx.context_id] = dialog.get_bids()
|
||||
return bids_map
|
||||
|
||||
def set_major_value(self, value: int) -> None:
|
||||
war_id = self.app.navigation.selected_war_id
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue