detect campaign tie
This commit is contained in:
parent
7c9c941864
commit
60d8e6ca15
9 changed files with 203 additions and 116 deletions
|
|
@ -1,8 +1,8 @@
|
|||
from typing import List, TYPE_CHECKING
|
||||
from typing import List, Dict, TYPE_CHECKING
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox, QDialog
|
||||
|
||||
from warchron.constants import RefreshScope, ContextType
|
||||
from warchron.constants import RefreshScope, ContextType, ItemType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
|
|
@ -13,14 +13,18 @@ from warchron.controller.dtos import (
|
|||
RoundDTO,
|
||||
CampaignParticipantScoreDTO,
|
||||
)
|
||||
from warchron.model.exception import ForbiddenOperation, DomainError
|
||||
from warchron.model.war import War
|
||||
from warchron.model.campaign import Campaign
|
||||
from warchron.model.campaign_participant import CampaignParticipant
|
||||
from warchron.model.sector import Sector
|
||||
from warchron.model.closure_service import ClosureService
|
||||
from warchron.model.tie_manager import TieContext
|
||||
from warchron.model.score_service import ScoreService
|
||||
from warchron.view.campaign_dialog import CampaignDialog
|
||||
from warchron.view.campaign_participant_dialog import CampaignParticipantDialog
|
||||
from warchron.view.sector_dialog import SectorDialog
|
||||
from warchron.controller.closure_workflow import CampaignClosureWorkflow
|
||||
from warchron.view.tie_dialog import TieDialog
|
||||
|
||||
|
||||
class CampaignController:
|
||||
|
|
@ -101,10 +105,6 @@ class CampaignController:
|
|||
return 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)
|
||||
|
|
@ -123,25 +123,46 @@ class CampaignController:
|
|||
if not campaign_id:
|
||||
return
|
||||
camp = self.app.model.get_campaign(campaign_id)
|
||||
if camp.is_over:
|
||||
return
|
||||
war = self.app.model.get_war_by_campaign(campaign_id)
|
||||
workflow = CampaignClosureWorkflow(self.app)
|
||||
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(
|
||||
workflow.start(war, camp)
|
||||
except DomainError as e:
|
||||
QMessageBox.warning(
|
||||
self.app.view,
|
||||
"Tie detected",
|
||||
"Campaign 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.CAMPAIGN, item_id=campaign_id
|
||||
)
|
||||
|
||||
# Campaign participant methods
|
||||
def resolve_ties(
|
||||
self, war: War, contexts: List[TieContext]
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
bids_map = {}
|
||||
for ctx in contexts:
|
||||
players = [
|
||||
ParticipantOption(
|
||||
id=pid,
|
||||
name=self.app.model.get_participant_name(pid),
|
||||
)
|
||||
for pid in ctx.participants
|
||||
]
|
||||
counters = [war.get_influence_tokens(pid) for pid in ctx.participants]
|
||||
dialog = TieDialog(
|
||||
parent=self.app.view,
|
||||
players=players,
|
||||
counters=counters,
|
||||
context_type=ContextType.CAMPAIGN,
|
||||
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 create_campaign_participant(self) -> CampaignParticipant | None:
|
||||
if not self.app.navigation.selected_campaign_id:
|
||||
|
|
|
|||
|
|
@ -3,16 +3,12 @@ from typing import TYPE_CHECKING
|
|||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
|
||||
from warchron.constants import ContextType
|
||||
from warchron.model.exception import ForbiddenOperation
|
||||
from warchron.model.war_event import TieResolved
|
||||
from warchron.model.war import War
|
||||
from warchron.model.campaign import Campaign
|
||||
from warchron.model.battle import Battle
|
||||
from warchron.model.round import Round
|
||||
from warchron.model.closure_service import ClosureService
|
||||
from warchron.model.tie_manager import TieResolver
|
||||
from warchron.controller.dtos import TieContext
|
||||
|
||||
|
||||
class ClosureWorkflow:
|
||||
|
|
@ -25,53 +21,77 @@ class RoundClosureWorkflow(ClosureWorkflow):
|
|||
|
||||
def start(self, war: War, campaign: Campaign, round: Round) -> None:
|
||||
ClosureService.check_round_closable(round)
|
||||
ties = TieResolver.find_round_ties(round, war)
|
||||
ties = TieResolver.find_battle_ties(war, round.id)
|
||||
while ties:
|
||||
contexts = [
|
||||
RoundClosureWorkflow.build_battle_context(campaign, b) for b in ties
|
||||
]
|
||||
resolvable = []
|
||||
for ctx in contexts:
|
||||
if TieResolver.can_tie_be_resolved(war, ctx.participants):
|
||||
resolvable.append(ctx)
|
||||
for tie in ties:
|
||||
if TieResolver.can_tie_be_resolved(war, tie.participants):
|
||||
resolvable.append(tie)
|
||||
else:
|
||||
war.events.append(
|
||||
TieResolved(
|
||||
participant_id=None,
|
||||
context_type=ctx.context_type,
|
||||
context_id=ctx.context_id,
|
||||
participant_id=None, # draw confirmed
|
||||
context_type=tie.context_type,
|
||||
context_id=tie.context_id,
|
||||
)
|
||||
)
|
||||
if not resolvable:
|
||||
break
|
||||
bids_map = self.app.rounds.resolve_ties(war, contexts)
|
||||
for ctx in contexts:
|
||||
bids = bids_map[ctx.context_id]
|
||||
bids_map = self.app.rounds.resolve_ties(war, resolvable)
|
||||
for tie in resolvable:
|
||||
bids = bids_map[tie.context_id]
|
||||
TieResolver.apply_bids(
|
||||
war,
|
||||
ctx.context_type,
|
||||
ctx.context_id,
|
||||
tie.context_type,
|
||||
tie.context_id,
|
||||
bids,
|
||||
)
|
||||
TieResolver.try_tie_break(
|
||||
war,
|
||||
ctx.context_type,
|
||||
ctx.context_id,
|
||||
ctx.participants,
|
||||
tie.context_type,
|
||||
tie.context_id,
|
||||
tie.participants,
|
||||
)
|
||||
ties = TieResolver.find_round_ties(round, war)
|
||||
ties = TieResolver.find_battle_ties(war, round.id)
|
||||
for battle in round.battles.values():
|
||||
ClosureService.apply_battle_outcomes(war, campaign, battle)
|
||||
ClosureService.finalize_round(round)
|
||||
|
||||
@staticmethod
|
||||
def build_battle_context(campaign: Campaign, battle: Battle) -> TieContext:
|
||||
if battle.player_1_id is None or battle.player_2_id is None:
|
||||
raise ForbiddenOperation("Missing player(s) in this battle context.")
|
||||
p1 = campaign.participants[battle.player_1_id].war_participant_id
|
||||
p2 = campaign.participants[battle.player_2_id].war_participant_id
|
||||
return TieContext(
|
||||
context_type=ContextType.BATTLE,
|
||||
context_id=battle.sector_id,
|
||||
participants=[p1, p2],
|
||||
)
|
||||
|
||||
class CampaignClosureWorkflow(ClosureWorkflow):
|
||||
|
||||
def start(self, war: War, campaign: Campaign) -> None:
|
||||
ClosureService.check_campaign_closable(campaign)
|
||||
ties = TieResolver.find_campaign_ties(war, campaign.id)
|
||||
while ties:
|
||||
resolvable = []
|
||||
for tie in ties:
|
||||
if TieResolver.can_tie_be_resolved(war, tie.participants):
|
||||
resolvable.append(tie)
|
||||
else:
|
||||
war.events.append(
|
||||
TieResolved(
|
||||
participant_id=None,
|
||||
context_type=tie.context_type,
|
||||
context_id=tie.context_id,
|
||||
)
|
||||
)
|
||||
if not resolvable:
|
||||
break
|
||||
bids_map = self.app.campaigns.resolve_ties(war, resolvable)
|
||||
for tie in resolvable:
|
||||
bids = bids_map[tie.context_id]
|
||||
TieResolver.apply_bids(
|
||||
war,
|
||||
tie.context_type,
|
||||
tie.context_id,
|
||||
bids,
|
||||
)
|
||||
TieResolver.try_tie_break(
|
||||
war,
|
||||
tie.context_type,
|
||||
tie.context_id,
|
||||
tie.participants,
|
||||
)
|
||||
ties = TieResolver.find_campaign_ties(war, campaign.id)
|
||||
ClosureService.finalize_campaign(campaign)
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@ from dataclasses import dataclass
|
|||
|
||||
from PyQt6.QtGui import QIcon
|
||||
|
||||
from warchron.constants import ContextType
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ParticipantOption:
|
||||
|
|
@ -109,13 +107,6 @@ class BattleDTO:
|
|||
player2_tooltip: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class TieContext:
|
||||
context_type: ContextType
|
||||
context_id: str
|
||||
participants: List[str] # war_participant_ids
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ParticipantScoreDTO:
|
||||
participant_id: str
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ from PyQt6.QtGui import QIcon
|
|||
|
||||
from warchron.constants import ItemType, RefreshScope, Icons, IconName, ContextType
|
||||
from warchron.model.exception import ForbiddenOperation, DomainError
|
||||
from warchron.model.tie_manager import TieResolver
|
||||
from warchron.model.tie_manager import TieResolver, TieContext
|
||||
from warchron.model.result_checker import ResultChecker
|
||||
from warchron.model.round import Round
|
||||
from warchron.model.war import War
|
||||
|
||||
|
|
@ -18,7 +19,6 @@ from warchron.controller.dtos import (
|
|||
SectorDTO,
|
||||
ChoiceDTO,
|
||||
BattleDTO,
|
||||
TieContext,
|
||||
)
|
||||
from warchron.controller.closure_workflow import RoundClosureWorkflow
|
||||
from warchron.view.choice_dialog import ChoiceDialog
|
||||
|
|
@ -108,7 +108,7 @@ class RoundController:
|
|||
if TieResolver.was_tie_broken_by_tokens(
|
||||
war, ContextType.BATTLE, battle.sector_id
|
||||
):
|
||||
effective_winner = TieResolver.get_effective_winner_id(
|
||||
effective_winner = ResultChecker.get_effective_winner_id(
|
||||
war, ContextType.BATTLE, battle.sector_id, None
|
||||
)
|
||||
p1_war = None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue