detect campaign tie
This commit is contained in:
parent
7c9c941864
commit
60d8e6ca15
9 changed files with 203 additions and 116 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue