2026-02-20 11:01:25 +01:00
|
|
|
from typing import List, Dict, DefaultDict
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from collections import defaultdict
|
2026-02-17 16:37:36 +01:00
|
|
|
|
|
|
|
|
from warchron.constants import ContextType
|
|
|
|
|
from warchron.model.exception import ForbiddenOperation
|
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-02-20 11:01:25 +01:00
|
|
|
from warchron.model.score_service import ScoreService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class TieContext:
|
|
|
|
|
context_type: ContextType
|
|
|
|
|
context_id: str
|
|
|
|
|
participants: List[str] # war_participant_ids
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value: int | None = None
|
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-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():
|
|
|
|
|
if not battle.is_draw():
|
|
|
|
|
continue
|
2026-02-26 10:55:28 +01:00
|
|
|
if TieResolver.is_tie_resolved(
|
|
|
|
|
war, ContextType.BATTLE, battle.sector_id, None
|
|
|
|
|
):
|
2026-02-20 11:01:25 +01:00
|
|
|
continue
|
|
|
|
|
if campaign is None:
|
|
|
|
|
raise RuntimeError("No campaign for this battle tie")
|
|
|
|
|
if battle.player_1_id is None or battle.player_2_id is None:
|
|
|
|
|
raise RuntimeError("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
|
2026-02-23 13:16:45 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(
|
|
|
|
|
war, ContextType.BATTLE, battle.sector_id, [p1, p2]
|
|
|
|
|
):
|
2026-02-20 23:44:22 +01:00
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(None, ContextType.BATTLE, battle.sector_id)
|
|
|
|
|
)
|
|
|
|
|
continue
|
2026-02-20 11:01:25 +01:00
|
|
|
ties.append(
|
|
|
|
|
TieContext(
|
|
|
|
|
context_type=ContextType.BATTLE,
|
|
|
|
|
context_id=battle.sector_id,
|
|
|
|
|
participants=[p1, p2],
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value=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-02-25 16:54:21 +01:00
|
|
|
if TieResolver.is_tie_resolved(
|
|
|
|
|
war, ContextType.CAMPAIGN, campaign_id, score_value
|
|
|
|
|
):
|
2026-02-23 11:37:50 +01:00
|
|
|
continue
|
2026-02-23 13:16:45 +01:00
|
|
|
if not TieResolver.can_tie_be_resolved(
|
2026-02-25 16:54:21 +01:00
|
|
|
war, ContextType.CAMPAIGN, campaign_id, participants
|
2026-02-23 13:16:45 +01:00
|
|
|
):
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(None, ContextType.CAMPAIGN, campaign_id, score_value)
|
|
|
|
|
)
|
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-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
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def find_war_ties(war: War) -> List[TieContext]:
|
2026-02-23 21:18:27 +01:00
|
|
|
scores = ScoreService.compute_scores(war, ContextType.WAR, war.id)
|
|
|
|
|
buckets: DefaultDict[int, List[str]] = defaultdict(list)
|
|
|
|
|
for pid, score in scores.items():
|
|
|
|
|
buckets[score.victory_points].append(pid)
|
|
|
|
|
ties: List[TieContext] = []
|
|
|
|
|
for score_value, participants in buckets.items():
|
|
|
|
|
if len(participants) <= 1:
|
|
|
|
|
continue
|
2026-02-25 16:54:21 +01:00
|
|
|
if TieResolver.is_tie_resolved(war, ContextType.WAR, war.id, score_value):
|
2026-02-23 21:18:27 +01:00
|
|
|
continue
|
|
|
|
|
if not TieResolver.can_tie_be_resolved(
|
2026-02-25 16:54:21 +01:00
|
|
|
war, ContextType.WAR, war.id, participants
|
2026-02-23 21:18:27 +01:00
|
|
|
):
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(None, ContextType.WAR, war.id, score_value)
|
|
|
|
|
)
|
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-23 21:18:27 +01:00
|
|
|
participants=participants,
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value=score_value,
|
2026-02-23 21:18:27 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return ties
|
2026-02-20 11:01:25 +01:00
|
|
|
|
2026-02-17 16:37:36 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def apply_bids(
|
|
|
|
|
war: War,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
|
|
|
|
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,
|
|
|
|
|
context_type=context_type,
|
2026-02-20 23:44:22 +01:00
|
|
|
context_id=context_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,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value: int | None = None,
|
2026-02-24 10:04:16 +01:00
|
|
|
) -> None:
|
|
|
|
|
war.events = [
|
|
|
|
|
ev
|
|
|
|
|
for ev in war.events
|
|
|
|
|
if not (
|
|
|
|
|
(
|
|
|
|
|
isinstance(ev, InfluenceSpent)
|
|
|
|
|
and ev.context_type == context_type
|
|
|
|
|
and ev.context_id == context_id
|
|
|
|
|
)
|
|
|
|
|
or (
|
|
|
|
|
isinstance(ev, TieResolved)
|
|
|
|
|
and ev.context_type == context_type
|
|
|
|
|
and ev.context_id == context_id
|
2026-02-25 16:54:21 +01:00
|
|
|
and ev.score_value == score_value
|
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,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
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)
|
|
|
|
|
and ev.context_type == context_type
|
|
|
|
|
and ev.context_id == context_id
|
|
|
|
|
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,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
|
|
|
|
participants: List[str],
|
|
|
|
|
) -> Dict[str, int]:
|
|
|
|
|
spent = {pid: 0 for pid in participants}
|
|
|
|
|
for ev in war.events:
|
|
|
|
|
if (
|
|
|
|
|
isinstance(ev, InfluenceSpent)
|
|
|
|
|
and ev.context_type == context_type
|
|
|
|
|
and ev.context_id == context_id
|
|
|
|
|
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,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
|
|
|
|
participants: List[str],
|
|
|
|
|
) -> List[str]:
|
|
|
|
|
groups = TieResolver.rank_by_tokens(war, context_type, context_id, participants)
|
|
|
|
|
return groups[0]
|
|
|
|
|
|
2026-02-20 23:44:22 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def resolve_tie_state(
|
|
|
|
|
war: War,
|
2026-02-25 16:54:21 +01:00
|
|
|
ctx: TieContext,
|
2026-02-20 23:44:22 +01:00
|
|
|
bids: dict[str, bool] | None = None,
|
|
|
|
|
) -> None:
|
2026-02-23 13:16:45 +01:00
|
|
|
active = TieResolver.get_active_participants(
|
2026-02-25 16:54:21 +01:00
|
|
|
war,
|
|
|
|
|
ctx.context_type,
|
|
|
|
|
ctx.context_id,
|
|
|
|
|
ctx.participants,
|
2026-02-23 13:16:45 +01:00
|
|
|
)
|
|
|
|
|
# confirmed draw if non had bid
|
|
|
|
|
if not active:
|
2026-02-25 16:54:21 +01:00
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(None, ctx.context_type, ctx.context_id, ctx.score_value)
|
|
|
|
|
)
|
2026-02-23 13:16:45 +01:00
|
|
|
return
|
|
|
|
|
# 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(
|
|
|
|
|
TieResolved(None, ctx.context_type, ctx.context_id, ctx.score_value)
|
|
|
|
|
)
|
2026-02-20 23:44:22 +01:00
|
|
|
return
|
|
|
|
|
# else rank_by_tokens
|
2026-02-25 16:54:21 +01:00
|
|
|
groups = TieResolver.rank_by_tokens(
|
|
|
|
|
war, ctx.context_type, ctx.context_id, ctx.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(
|
|
|
|
|
groups[0][0], ctx.context_type, ctx.context_id, ctx.score_value
|
|
|
|
|
)
|
|
|
|
|
)
|
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(
|
|
|
|
|
war: War, context_type: ContextType, context_id: str, participants: List[str]
|
|
|
|
|
) -> bool:
|
|
|
|
|
active = TieResolver.get_active_participants(
|
|
|
|
|
war, context_type, context_id, participants
|
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
2026-02-25 16:54:21 +01:00
|
|
|
score_value: int | None = None,
|
2026-02-18 11:15:53 +01:00
|
|
|
) -> bool:
|
|
|
|
|
for ev in reversed(war.events):
|
|
|
|
|
if (
|
|
|
|
|
isinstance(ev, TieResolved)
|
|
|
|
|
and ev.context_type == context_type
|
|
|
|
|
and ev.context_id == context_id
|
2026-02-25 16:54:21 +01:00
|
|
|
and ev.score_value == score_value
|
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-02-25 16:54:21 +01:00
|
|
|
def is_tie_resolved(
|
|
|
|
|
war: War,
|
|
|
|
|
context_type: ContextType,
|
|
|
|
|
context_id: str,
|
|
|
|
|
score_value: int | None = None,
|
|
|
|
|
) -> bool:
|
2026-02-20 23:44:22 +01:00
|
|
|
return any(
|
|
|
|
|
isinstance(ev, TieResolved)
|
|
|
|
|
and ev.context_type == context_type
|
|
|
|
|
and ev.context_id == context_id
|
2026-02-25 16:54:21 +01:00
|
|
|
and ev.score_value == score_value
|
2026-02-20 23:44:22 +01:00
|
|
|
for ev in war.events
|
|
|
|
|
)
|