2026-03-17 11:16:47 +01:00
|
|
|
from typing import List, Dict, DefaultDict, Tuple
|
|
|
|
|
from dataclasses import dataclass
|
2026-02-20 11:01:25 +01:00
|
|
|
from collections import defaultdict
|
2026-02-17 16:37:36 +01:00
|
|
|
|
2026-03-06 15:02:53 +01:00
|
|
|
from warchron.constants import ContextType, ScoreKind
|
2026-03-12 16:28:20 +01:00
|
|
|
from warchron.model.exception import ForbiddenOperation, DomainError
|
2026-02-11 19:22:43 +01:00
|
|
|
from warchron.model.war import War
|
2026-02-17 16:37:36 +01:00
|
|
|
from warchron.model.war_event import InfluenceSpent, TieResolved
|
2026-03-03 11:52:07 +01:00
|
|
|
from warchron.model.score_service import ScoreService, ParticipantScore
|
2026-02-20 11:01:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class TieContext:
|
|
|
|
|
context_type: ContextType
|
|
|
|
|
context_id: str
|
2026-03-17 11:16:47 +01:00
|
|
|
participants: List[str]
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value: int | None = None
|
2026-03-06 15:02:53 +01:00
|
|
|
score_kind: ScoreKind | None = None
|
|
|
|
|
objective_id: str | None = None
|
2026-03-12 16:28:20 +01:00
|
|
|
sector_id: str | None = None
|
2026-03-06 15:02:53 +01:00
|
|
|
|
2026-03-17 11:16:47 +01:00
|
|
|
def key(self) -> Tuple[str, str, int | None, str | None, str | None]:
|
|
|
|
|
return (
|
|
|
|
|
self.context_type,
|
|
|
|
|
self.context_id,
|
|
|
|
|
self.score_value,
|
|
|
|
|
self.objective_id,
|
|
|
|
|
self.sector_id,
|
|
|
|
|
)
|
2026-02-11 19:22:43 +01:00
|
|
|
|
|
|
|
|
|
2026-02-17 16:37:36 +01:00
|
|
|
class TieResolver:
|
2026-02-11 19:22:43 +01:00
|
|
|
|
2026-03-17 11:16:47 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def find_active_tie_id(
|
|
|
|
|
war: War,
|
|
|
|
|
context: TieContext,
|
|
|
|
|
) -> str | None:
|
|
|
|
|
for ev in reversed(war.events):
|
|
|
|
|
if (
|
|
|
|
|
isinstance(ev, InfluenceSpent)
|
|
|
|
|
and ev.context_type == context.context_type
|
|
|
|
|
and ev.context_id == context.context_id
|
|
|
|
|
and ev.objective_id == context.objective_id
|
|
|
|
|
and ev.sector_id == context.sector_id
|
|
|
|
|
and ev.participant_id in context.participants
|
|
|
|
|
):
|
|
|
|
|
return ev.tie_id
|
|
|
|
|
return None
|
|
|
|
|
|
2026-02-17 16:37:36 +01:00
|
|
|
@staticmethod
|
2026-02-20 11:01:25 +01:00
|
|
|
def find_battle_ties(war: War, round_id: str) -> List[TieContext]:
|
|
|
|
|
round = war.get_round(round_id)
|
|
|
|
|
campaign = war.get_campaign_by_round(round_id)
|
2026-02-17 16:37:36 +01:00
|
|
|
ties = []
|
|
|
|
|
for battle in round.battles.values():
|
2026-02-20 11:01:25 +01:00
|
|
|
if campaign is None:
|
2026-03-12 16:28:20 +01:00
|
|
|
raise DomainError("No campaign for this battle tie")
|
2026-02-20 11:01:25 +01:00
|
|
|
if battle.player_1_id is None or battle.player_2_id is None:
|
2026-03-12 16:28:20 +01:00
|
|
|
raise DomainError("Missing player(s) in this battle context.")
|
|
|
|
|
p1_id = campaign.campaign_to_war_part_id(battle.player_1_id)
|
|
|
|
|
p2_id = campaign.campaign_to_war_part_id(battle.player_2_id)
|
2026-03-17 11:16:47 +01:00
|
|
|
if not battle.is_draw():
|
|
|
|
|
continue
|
|
|
|
|
context: TieContext = TieContext(
|
|
|
|
|
ContextType.BATTLE, battle.sector_id, [p1_id, p2_id]
|
|
|
|
|
)
|
|
|
|
|
if TieResolver.is_tie_resolved(war, context):
|
|
|
|
|
continue
|
|
|
|
|
tie_id = TieResolver.find_active_tie_id(war, context)
|
2026-03-12 16:28:20 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(war, context, [p1_id, p2_id]):
|
2026-02-20 23:44:22 +01:00
|
|
|
war.events.append(
|
2026-03-17 11:16:47 +01:00
|
|
|
TieResolved(
|
|
|
|
|
participant_id=None,
|
|
|
|
|
context_type=ContextType.BATTLE,
|
|
|
|
|
context_id=battle.sector_id,
|
|
|
|
|
participants=[p1_id, p2_id],
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
)
|
2026-02-20 23:44:22 +01:00
|
|
|
)
|
|
|
|
|
continue
|
2026-02-20 11:01:25 +01:00
|
|
|
ties.append(
|
|
|
|
|
TieContext(
|
|
|
|
|
context_type=ContextType.BATTLE,
|
|
|
|
|
context_id=battle.sector_id,
|
2026-03-12 16:28:20 +01:00
|
|
|
participants=[p1_id, p2_id],
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value=None,
|
2026-03-06 15:02:53 +01:00
|
|
|
score_kind=None,
|
2026-02-20 11:01:25 +01:00
|
|
|
)
|
|
|
|
|
)
|
2026-02-17 16:37:36 +01:00
|
|
|
return ties
|
2026-02-11 19:22:43 +01:00
|
|
|
|
2026-02-20 11:01:25 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def find_campaign_ties(war: War, campaign_id: str) -> List[TieContext]:
|
|
|
|
|
scores = ScoreService.compute_scores(war, ContextType.CAMPAIGN, campaign_id)
|
|
|
|
|
buckets: DefaultDict[int, List[str]] = defaultdict(list)
|
|
|
|
|
for pid, score in scores.items():
|
|
|
|
|
buckets[score.victory_points].append(pid)
|
2026-02-23 11:37:50 +01:00
|
|
|
ties: List[TieContext] = []
|
|
|
|
|
for score_value, participants in buckets.items():
|
|
|
|
|
if len(participants) <= 1:
|
|
|
|
|
continue
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext = TieContext(
|
|
|
|
|
ContextType.CAMPAIGN,
|
|
|
|
|
campaign_id,
|
|
|
|
|
[],
|
|
|
|
|
score_value,
|
|
|
|
|
ScoreKind.VP,
|
|
|
|
|
)
|
|
|
|
|
if TieResolver.is_tie_resolved(war, context):
|
2026-02-23 11:37:50 +01:00
|
|
|
continue
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id = TieResolver.find_active_tie_id(war, context)
|
2026-03-06 15:02:53 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(war, context, participants):
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
2026-03-06 15:02:53 +01:00
|
|
|
TieResolved(
|
2026-03-17 11:16:47 +01:00
|
|
|
participant_id=None,
|
|
|
|
|
context_type=ContextType.CAMPAIGN,
|
|
|
|
|
context_id=campaign_id,
|
|
|
|
|
participants=participants,
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
score_value=score_value,
|
2026-03-06 15:02:53 +01:00
|
|
|
)
|
2026-02-25 16:54:21 +01:00
|
|
|
)
|
2026-02-23 11:37:50 +01:00
|
|
|
continue
|
|
|
|
|
ties.append(
|
|
|
|
|
TieContext(
|
|
|
|
|
context_type=ContextType.CAMPAIGN,
|
2026-02-25 16:54:21 +01:00
|
|
|
context_id=campaign_id,
|
2026-02-23 11:37:50 +01:00
|
|
|
participants=participants,
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value=score_value,
|
2026-03-06 15:02:53 +01:00
|
|
|
score_kind=ScoreKind.VP,
|
2026-02-20 11:01:25 +01:00
|
|
|
)
|
2026-02-23 11:37:50 +01:00
|
|
|
)
|
2026-02-20 11:01:25 +01:00
|
|
|
return ties
|
|
|
|
|
|
2026-03-03 11:52:07 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def find_campaign_objective_ties(
|
2026-03-17 11:16:47 +01:00
|
|
|
war: War, campaign_id: str, objective_id: str
|
2026-03-03 11:52:07 +01:00
|
|
|
) -> List[TieContext]:
|
2026-03-05 11:37:14 +01:00
|
|
|
scores = ScoreService.compute_scores(
|
2026-03-03 11:52:07 +01:00
|
|
|
war,
|
|
|
|
|
ContextType.CAMPAIGN,
|
|
|
|
|
campaign_id,
|
|
|
|
|
)
|
|
|
|
|
buckets: DefaultDict[int, List[str]] = defaultdict(list)
|
|
|
|
|
for pid, score in scores.items():
|
2026-03-05 11:37:14 +01:00
|
|
|
np_value = score.narrative_points.get(objective_id, 0)
|
|
|
|
|
buckets[np_value].append(pid)
|
2026-03-03 11:52:07 +01:00
|
|
|
ties: List[TieContext] = []
|
2026-03-06 15:02:53 +01:00
|
|
|
context_id = campaign_id
|
2026-03-05 11:37:14 +01:00
|
|
|
for np_value, participants in buckets.items():
|
2026-03-03 11:52:07 +01:00
|
|
|
if len(participants) <= 1:
|
|
|
|
|
continue
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext = TieContext(
|
|
|
|
|
ContextType.CAMPAIGN,
|
|
|
|
|
campaign_id,
|
|
|
|
|
[],
|
2026-03-05 11:37:14 +01:00
|
|
|
np_value,
|
2026-03-06 15:02:53 +01:00
|
|
|
ScoreKind.NP,
|
|
|
|
|
objective_id,
|
|
|
|
|
)
|
|
|
|
|
if TieResolver.is_tie_resolved(war, context):
|
2026-03-03 11:52:07 +01:00
|
|
|
continue
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id = TieResolver.find_active_tie_id(war, context)
|
2026-03-03 11:52:07 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(
|
|
|
|
|
war,
|
2026-03-06 15:02:53 +01:00
|
|
|
context,
|
2026-03-03 11:52:07 +01:00
|
|
|
participants,
|
|
|
|
|
):
|
|
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(
|
2026-03-17 11:16:47 +01:00
|
|
|
participant_id=None,
|
|
|
|
|
context_type=ContextType.CAMPAIGN,
|
|
|
|
|
context_id=context_id,
|
|
|
|
|
participants=participants,
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
score_value=np_value,
|
|
|
|
|
objective_id=objective_id,
|
2026-03-03 11:52:07 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
ties.append(
|
|
|
|
|
TieContext(
|
2026-03-06 15:02:53 +01:00
|
|
|
context_type=ContextType.CAMPAIGN,
|
2026-03-03 11:52:07 +01:00
|
|
|
context_id=context_id,
|
|
|
|
|
participants=participants,
|
2026-03-05 11:37:14 +01:00
|
|
|
score_value=np_value,
|
2026-03-06 15:02:53 +01:00
|
|
|
score_kind=ScoreKind.NP,
|
|
|
|
|
objective_id=objective_id,
|
2026-03-03 11:52:07 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return ties
|
|
|
|
|
|
2026-02-20 11:01:25 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def find_war_ties(war: War) -> List[TieContext]:
|
2026-02-26 15:14:44 +01:00
|
|
|
from warchron.model.result_checker import ResultChecker
|
|
|
|
|
|
2026-02-23 21:18:27 +01:00
|
|
|
scores = ScoreService.compute_scores(war, ContextType.WAR, war.id)
|
2026-02-26 15:14:44 +01:00
|
|
|
ranking = ResultChecker.get_effective_ranking(
|
2026-03-03 15:39:30 +01:00
|
|
|
war,
|
|
|
|
|
ContextType.WAR,
|
|
|
|
|
war.id,
|
2026-03-06 15:02:53 +01:00
|
|
|
ScoreKind.VP,
|
2026-03-03 15:39:30 +01:00
|
|
|
scores,
|
2026-03-06 15:02:53 +01:00
|
|
|
lambda s: s.victory_points,
|
2026-02-26 15:14:44 +01:00
|
|
|
)
|
2026-02-23 21:18:27 +01:00
|
|
|
ties: List[TieContext] = []
|
2026-02-26 15:14:44 +01:00
|
|
|
for _, group, _ in ranking:
|
|
|
|
|
if len(group) <= 1:
|
2026-02-23 21:18:27 +01:00
|
|
|
continue
|
2026-02-26 15:14:44 +01:00
|
|
|
score_value = scores[group[0]].victory_points
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext = TieContext(
|
|
|
|
|
ContextType.WAR,
|
|
|
|
|
war.id,
|
|
|
|
|
[],
|
2026-03-12 16:28:20 +01:00
|
|
|
score_value=score_value,
|
|
|
|
|
score_kind=ScoreKind.VP,
|
2026-03-06 15:02:53 +01:00
|
|
|
)
|
|
|
|
|
if TieResolver.is_tie_resolved(war, context):
|
2026-02-23 21:18:27 +01:00
|
|
|
continue
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id = TieResolver.find_active_tie_id(war, context)
|
2026-03-06 15:02:53 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(war, context, group):
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
2026-03-17 11:16:47 +01:00
|
|
|
TieResolved(
|
|
|
|
|
participant_id=None,
|
|
|
|
|
context_type=ContextType.WAR,
|
|
|
|
|
context_id=war.id,
|
|
|
|
|
participants=group,
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
score_value=score_value,
|
|
|
|
|
)
|
2026-02-25 16:54:21 +01:00
|
|
|
)
|
2026-02-23 21:18:27 +01:00
|
|
|
continue
|
|
|
|
|
ties.append(
|
|
|
|
|
TieContext(
|
|
|
|
|
context_type=ContextType.WAR,
|
2026-02-25 16:54:21 +01:00
|
|
|
context_id=war.id,
|
2026-02-26 15:14:44 +01:00
|
|
|
participants=group,
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value=score_value,
|
2026-03-06 15:02:53 +01:00
|
|
|
score_kind=ScoreKind.VP,
|
2026-02-23 21:18:27 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return ties
|
2026-02-20 11:01:25 +01:00
|
|
|
|
2026-03-03 11:52:07 +01:00
|
|
|
@staticmethod
|
2026-03-17 11:16:47 +01:00
|
|
|
def find_war_objective_ties(war: War, objective_id: str) -> List[TieContext]:
|
2026-03-03 11:52:07 +01:00
|
|
|
from warchron.model.result_checker import ResultChecker
|
|
|
|
|
|
2026-03-05 11:37:14 +01:00
|
|
|
scores = ScoreService.compute_scores(
|
2026-03-03 11:52:07 +01:00
|
|
|
war,
|
|
|
|
|
ContextType.WAR,
|
|
|
|
|
war.id,
|
|
|
|
|
)
|
2026-03-05 11:37:14 +01:00
|
|
|
|
|
|
|
|
def value_getter(score: ParticipantScore) -> int:
|
|
|
|
|
return score.narrative_points.get(objective_id, 0)
|
|
|
|
|
|
2026-03-03 11:52:07 +01:00
|
|
|
ranking = ResultChecker.get_effective_ranking(
|
|
|
|
|
war,
|
2026-03-06 15:02:53 +01:00
|
|
|
ContextType.WAR,
|
|
|
|
|
war.id,
|
|
|
|
|
ScoreKind.NP,
|
2026-03-03 11:52:07 +01:00
|
|
|
scores,
|
2026-03-06 15:02:53 +01:00
|
|
|
value_getter,
|
|
|
|
|
objective_id,
|
2026-03-03 11:52:07 +01:00
|
|
|
)
|
|
|
|
|
ties: List[TieContext] = []
|
|
|
|
|
for _, group, _ in ranking:
|
|
|
|
|
if len(group) <= 1:
|
|
|
|
|
continue
|
2026-03-05 11:37:14 +01:00
|
|
|
np_value = value_getter(scores[group[0]])
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext = TieContext(
|
|
|
|
|
ContextType.WAR, war.id, [], np_value, ScoreKind.NP, objective_id
|
|
|
|
|
)
|
2026-03-03 11:52:07 +01:00
|
|
|
if TieResolver.is_tie_resolved(
|
|
|
|
|
war,
|
2026-03-06 15:02:53 +01:00
|
|
|
context,
|
2026-03-03 11:52:07 +01:00
|
|
|
):
|
|
|
|
|
continue
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id = TieResolver.find_active_tie_id(war, context)
|
2026-03-03 11:52:07 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(
|
|
|
|
|
war,
|
2026-03-06 15:02:53 +01:00
|
|
|
context,
|
2026-03-03 11:52:07 +01:00
|
|
|
group,
|
|
|
|
|
):
|
|
|
|
|
war.events.append(
|
2026-03-17 11:16:47 +01:00
|
|
|
TieResolved(
|
|
|
|
|
participant_id=None,
|
|
|
|
|
context_type=ContextType.WAR,
|
|
|
|
|
context_id=war.id,
|
|
|
|
|
participants=group,
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
score_value=np_value,
|
|
|
|
|
objective_id=objective_id,
|
|
|
|
|
)
|
2026-03-03 11:52:07 +01:00
|
|
|
)
|
|
|
|
|
continue
|
|
|
|
|
ties.append(
|
|
|
|
|
TieContext(
|
2026-03-06 15:02:53 +01:00
|
|
|
context_type=ContextType.WAR,
|
|
|
|
|
context_id=war.id,
|
2026-03-03 11:52:07 +01:00
|
|
|
participants=group,
|
2026-03-05 11:37:14 +01:00
|
|
|
score_value=np_value,
|
2026-03-06 15:02:53 +01:00
|
|
|
score_kind=ScoreKind.NP,
|
|
|
|
|
objective_id=objective_id,
|
2026-03-03 11:52:07 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return ties
|
|
|
|
|
|
2026-02-17 16:37:36 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def apply_bids(
|
|
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id: str,
|
2026-02-17 16:37:36 +01:00
|
|
|
bids: Dict[str, bool], # war_participant_id -> spend?
|
|
|
|
|
) -> None:
|
|
|
|
|
for war_part_id, spend in bids.items():
|
|
|
|
|
if not spend:
|
|
|
|
|
continue
|
|
|
|
|
if war.get_influence_tokens(war_part_id) < 1:
|
|
|
|
|
raise ForbiddenOperation("Not enough tokens")
|
|
|
|
|
war.events.append(
|
|
|
|
|
InfluenceSpent(
|
|
|
|
|
participant_id=war_part_id,
|
|
|
|
|
amount=1,
|
2026-03-06 15:02:53 +01:00
|
|
|
context_type=context.context_type,
|
|
|
|
|
context_id=context.context_id,
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id=tie_id,
|
2026-03-06 15:02:53 +01:00
|
|
|
objective_id=context.objective_id,
|
2026-03-19 09:02:22 +01:00
|
|
|
sector_id=context.sector_id,
|
2026-02-17 16:37:36 +01:00
|
|
|
)
|
|
|
|
|
)
|
2026-02-11 19:22:43 +01:00
|
|
|
|
2026-02-24 10:04:16 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def cancel_tie_break(
|
|
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-02-24 10:04:16 +01:00
|
|
|
) -> None:
|
|
|
|
|
war.events = [
|
|
|
|
|
ev
|
|
|
|
|
for ev in war.events
|
|
|
|
|
if not (
|
|
|
|
|
(
|
2026-03-17 11:16:47 +01:00
|
|
|
(isinstance(ev, InfluenceSpent) or isinstance(ev, TieResolved))
|
2026-03-06 15:02:53 +01:00
|
|
|
and ev.context_type == context.context_type
|
|
|
|
|
and ev.context_id == context.context_id
|
2026-02-24 10:04:16 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
|
2026-02-17 16:37:36 +01:00
|
|
|
@staticmethod
|
2026-02-20 23:44:22 +01:00
|
|
|
def rank_by_tokens(
|
2026-02-17 16:37:36 +01:00
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-02-20 23:44:22 +01:00
|
|
|
participants: List[str],
|
|
|
|
|
) -> List[List[str]]:
|
|
|
|
|
spent = {pid: 0 for pid in participants}
|
|
|
|
|
for ev in war.events:
|
|
|
|
|
if (
|
|
|
|
|
isinstance(ev, InfluenceSpent)
|
2026-03-06 15:02:53 +01:00
|
|
|
and ev.context_type == context.context_type
|
|
|
|
|
and ev.context_id == context.context_id
|
|
|
|
|
and ev.objective_id == context.objective_id
|
2026-02-20 23:44:22 +01:00
|
|
|
and ev.participant_id in spent
|
|
|
|
|
):
|
|
|
|
|
spent[ev.participant_id] += ev.amount
|
|
|
|
|
sorted_items = sorted(spent.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
|
groups: List[List[str]] = []
|
|
|
|
|
current_score = None
|
|
|
|
|
for pid, score in sorted_items:
|
|
|
|
|
if score != current_score:
|
|
|
|
|
groups.append([])
|
|
|
|
|
current_score = score
|
|
|
|
|
groups[-1].append(pid)
|
|
|
|
|
return groups
|
|
|
|
|
|
2026-02-23 19:28:13 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def tokens_spent_map(
|
|
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-02-23 19:28:13 +01:00
|
|
|
participants: List[str],
|
|
|
|
|
) -> Dict[str, int]:
|
|
|
|
|
spent = {pid: 0 for pid in participants}
|
|
|
|
|
for ev in war.events:
|
|
|
|
|
if (
|
|
|
|
|
isinstance(ev, InfluenceSpent)
|
2026-03-06 15:02:53 +01:00
|
|
|
and ev.context_type == context.context_type
|
|
|
|
|
and ev.context_id == context.context_id
|
|
|
|
|
and ev.objective_id == context.objective_id
|
2026-02-23 19:28:13 +01:00
|
|
|
and ev.participant_id in spent
|
|
|
|
|
):
|
|
|
|
|
spent[ev.participant_id] += ev.amount
|
|
|
|
|
return spent
|
|
|
|
|
|
2026-02-23 13:16:45 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def get_active_participants(
|
|
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-02-23 13:16:45 +01:00
|
|
|
participants: List[str],
|
|
|
|
|
) -> List[str]:
|
2026-03-06 15:02:53 +01:00
|
|
|
groups = TieResolver.rank_by_tokens(war, context, participants)
|
2026-02-23 13:16:45 +01:00
|
|
|
return groups[0]
|
|
|
|
|
|
2026-02-20 23:44:22 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def resolve_tie_state(
|
|
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id: str,
|
|
|
|
|
bids: Dict[str, bool] | None = None,
|
2026-02-20 23:44:22 +01:00
|
|
|
) -> None:
|
2026-02-23 13:16:45 +01:00
|
|
|
# confirmed draw if current bids are 0
|
2026-02-20 23:44:22 +01:00
|
|
|
if bids is not None and not any(bids.values()):
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
2026-03-06 15:02:53 +01:00
|
|
|
TieResolved(
|
|
|
|
|
None,
|
|
|
|
|
context.context_type,
|
|
|
|
|
context.context_id,
|
2026-03-17 11:16:47 +01:00
|
|
|
participants=context.participants,
|
|
|
|
|
tie_id=tie_id,
|
2026-03-12 16:28:20 +01:00
|
|
|
score_value=context.score_value,
|
|
|
|
|
objective_id=context.objective_id,
|
2026-03-19 09:02:22 +01:00
|
|
|
sector_id=context.sector_id,
|
2026-03-06 15:02:53 +01:00
|
|
|
)
|
2026-02-25 16:54:21 +01:00
|
|
|
)
|
2026-02-20 23:44:22 +01:00
|
|
|
return
|
2026-03-06 15:02:53 +01:00
|
|
|
groups = TieResolver.rank_by_tokens(war, context, context.participants)
|
2026-02-20 23:44:22 +01:00
|
|
|
if len(groups[0]) == 1:
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(
|
2026-03-06 15:02:53 +01:00
|
|
|
groups[0][0],
|
|
|
|
|
context.context_type,
|
|
|
|
|
context.context_id,
|
2026-03-17 11:16:47 +01:00
|
|
|
participants=context.participants,
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
score_value=context.score_value,
|
|
|
|
|
objective_id=context.objective_id,
|
2026-03-19 09:02:22 +01:00
|
|
|
sector_id=context.sector_id,
|
2026-02-25 16:54:21 +01:00
|
|
|
)
|
|
|
|
|
)
|
2026-02-20 23:44:22 +01:00
|
|
|
return
|
|
|
|
|
# if tie persists, do nothing, workflow will call again
|
2026-02-11 19:22:43 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2026-02-23 13:16:45 +01:00
|
|
|
def can_tie_be_resolved(
|
2026-03-06 15:02:53 +01:00
|
|
|
war: War, context: TieContext, participants: List[str]
|
2026-02-23 13:16:45 +01:00
|
|
|
) -> bool:
|
2026-03-06 15:02:53 +01:00
|
|
|
active = TieResolver.get_active_participants(war, context, participants)
|
2026-02-23 13:16:45 +01:00
|
|
|
return any(war.get_influence_tokens(pid) > 0 for pid in active)
|
2026-02-17 16:37:36 +01:00
|
|
|
|
2026-02-18 11:15:53 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def was_tie_broken_by_tokens(
|
|
|
|
|
war: War,
|
2026-03-06 15:02:53 +01:00
|
|
|
context: TieContext,
|
2026-02-18 11:15:53 +01:00
|
|
|
) -> bool:
|
|
|
|
|
for ev in reversed(war.events):
|
|
|
|
|
if (
|
|
|
|
|
isinstance(ev, TieResolved)
|
2026-03-06 15:02:53 +01:00
|
|
|
and ev.context_type == context.context_type
|
|
|
|
|
and ev.context_id == context.context_id
|
|
|
|
|
and ev.score_value == context.score_value
|
|
|
|
|
and ev.objective_id == context.objective_id
|
2026-02-18 11:15:53 +01:00
|
|
|
):
|
|
|
|
|
return ev.participant_id is not None
|
|
|
|
|
return False
|
2026-02-20 23:44:22 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2026-03-06 15:02:53 +01:00
|
|
|
def is_tie_resolved(war: War, context: TieContext) -> bool:
|
2026-03-17 11:16:47 +01:00
|
|
|
for ev in war.events:
|
|
|
|
|
if not isinstance(ev, TieResolved):
|
|
|
|
|
continue
|
|
|
|
|
if ev.context_type != context.context_type:
|
|
|
|
|
continue
|
|
|
|
|
if ev.context_id != context.context_id:
|
|
|
|
|
continue
|
|
|
|
|
if (
|
|
|
|
|
context.score_value is not None
|
|
|
|
|
and ev.score_value != context.score_value
|
|
|
|
|
):
|
|
|
|
|
continue
|
|
|
|
|
if (
|
|
|
|
|
context.objective_id is not None
|
|
|
|
|
and ev.objective_id != context.objective_id
|
|
|
|
|
):
|
|
|
|
|
continue
|
|
|
|
|
if context.sector_id is not None and ev.sector_id != context.sector_id:
|
|
|
|
|
continue
|
|
|
|
|
return True
|
|
|
|
|
return False
|
2026-03-19 15:10:48 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def participant_spent_token(
|
|
|
|
|
war: War,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
|
|
|
|
sector_id: str | None,
|
|
|
|
|
war_participant_id: str,
|
|
|
|
|
) -> bool:
|
|
|
|
|
if context_type == ContextType.CHOICE and sector_id is None:
|
|
|
|
|
return False
|
|
|
|
|
for ev in war.events:
|
|
|
|
|
if not isinstance(ev, InfluenceSpent):
|
|
|
|
|
continue
|
|
|
|
|
if ev.context_type != context_type:
|
|
|
|
|
continue
|
|
|
|
|
if ev.context_id != context_id:
|
|
|
|
|
continue
|
|
|
|
|
if ev.sector_id != sector_id:
|
|
|
|
|
continue
|
|
|
|
|
if ev.participant_id == war_participant_id:
|
|
|
|
|
return True
|
|
|
|
|
return False
|