2026-03-11 11:44:57 +01:00
|
|
|
from __future__ import annotations
|
2026-03-12 16:28:20 +01:00
|
|
|
from typing import Dict, List, Callable, Tuple
|
2026-03-19 09:02:22 +01:00
|
|
|
from dataclasses import dataclass
|
2026-03-17 11:16:47 +01:00
|
|
|
from uuid import uuid4
|
|
|
|
|
|
2026-03-11 11:44:57 +01:00
|
|
|
import random
|
|
|
|
|
|
2026-03-19 09:02:22 +01:00
|
|
|
from warchron.constants import ContextType, ScoreKind, ChoiceStatus, AllocationType
|
2026-03-11 11:44:57 +01:00
|
|
|
from warchron.model.exception import (
|
|
|
|
|
DomainError,
|
|
|
|
|
ForbiddenOperation,
|
|
|
|
|
RequiresConfirmation,
|
|
|
|
|
)
|
|
|
|
|
from warchron.model.war import War
|
|
|
|
|
from warchron.model.round import Round
|
|
|
|
|
from warchron.model.battle import Battle
|
|
|
|
|
from warchron.model.score_service import ScoreService
|
2026-03-12 16:28:20 +01:00
|
|
|
from warchron.model.tie_manager import TieResolver, TieContext
|
2026-03-19 15:10:48 +01:00
|
|
|
from warchron.model.war_event import TieResolved
|
2026-03-12 16:28:20 +01:00
|
|
|
from warchron.model.score_service import ParticipantScore
|
|
|
|
|
|
|
|
|
|
ResolveTiesCallback = Callable[
|
|
|
|
|
["War", List["TieContext"]],
|
2026-03-17 11:16:47 +01:00
|
|
|
Dict[Tuple[str, str, int | None, str | None, str | None], Dict[str, bool]],
|
2026-03-12 16:28:20 +01:00
|
|
|
]
|
2026-03-11 11:44:57 +01:00
|
|
|
|
|
|
|
|
|
2026-03-19 09:02:22 +01:00
|
|
|
@dataclass(frozen=True, slots=True)
|
|
|
|
|
class AllocationResult:
|
|
|
|
|
priority: ChoiceStatus
|
|
|
|
|
secondary: ChoiceStatus
|
|
|
|
|
fallback: bool
|
|
|
|
|
|
|
|
|
|
|
2026-03-11 11:44:57 +01:00
|
|
|
class Pairing:
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def check_round_pairable(
|
2026-03-17 11:16:47 +01:00
|
|
|
war: War,
|
2026-03-11 11:44:57 +01:00
|
|
|
round: Round,
|
|
|
|
|
) -> None:
|
|
|
|
|
if round.is_over:
|
|
|
|
|
raise ForbiddenOperation("Can not resolve pairing on finished round")
|
|
|
|
|
if round.has_finished_battle():
|
|
|
|
|
raise ForbiddenOperation("Can not resolve pairing with finished battle(s)")
|
|
|
|
|
if len(round.battles) * 2 < len(round.choices):
|
|
|
|
|
raise DomainError(
|
|
|
|
|
"There are not enough sectors for all participants to battle"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def cleanup() -> None:
|
|
|
|
|
for bat in round.battles.values():
|
2026-03-17 11:16:47 +01:00
|
|
|
bat.clear_battle_players()
|
|
|
|
|
bat.set_winner(None)
|
|
|
|
|
war.revert_choice_ties(round.id)
|
2026-03-11 11:44:57 +01:00
|
|
|
|
|
|
|
|
if any(
|
|
|
|
|
bat.player_1_id is not None or bat.player_2_id is not None
|
|
|
|
|
for bat in round.battles.values()
|
|
|
|
|
):
|
|
|
|
|
raise RequiresConfirmation(
|
|
|
|
|
"Battle(s) already have player(s) assigned for this round.\n"
|
|
|
|
|
"Battle players will be cleared.\n"
|
2026-03-17 11:16:47 +01:00
|
|
|
"Choice tokens and tie-breaks will be deleted.\n"
|
2026-03-11 11:44:57 +01:00
|
|
|
"Do you want to continue?",
|
|
|
|
|
action=cleanup,
|
|
|
|
|
)
|
2026-03-19 11:25:40 +01:00
|
|
|
for pid, choice in round.choices.items():
|
|
|
|
|
if choice is not None and not choice.priority_sector_id:
|
|
|
|
|
raise ForbiddenOperation(
|
|
|
|
|
f"Missing priority choice for participant {pid}"
|
|
|
|
|
)
|
|
|
|
|
if choice is not None and not choice.secondary_sector_id:
|
|
|
|
|
raise ForbiddenOperation(
|
|
|
|
|
f"Missing secondary choice for participant {pid}"
|
|
|
|
|
)
|
2026-03-11 11:44:57 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def assign_battles_to_participants(
|
|
|
|
|
war: War,
|
|
|
|
|
round: Round,
|
2026-03-12 16:28:20 +01:00
|
|
|
resolve_ties_callback: ResolveTiesCallback,
|
2026-03-11 11:44:57 +01:00
|
|
|
) -> None:
|
|
|
|
|
campaign = war.get_campaign_by_round(round.id)
|
|
|
|
|
if campaign is None:
|
|
|
|
|
raise DomainError(f"Campaign for round {round.id} doesn't exist")
|
|
|
|
|
scores = ScoreService.compute_scores(
|
|
|
|
|
war,
|
|
|
|
|
ContextType.CAMPAIGN,
|
|
|
|
|
campaign.id,
|
|
|
|
|
)
|
2026-03-12 16:28:20 +01:00
|
|
|
|
|
|
|
|
def value_getter(score: ParticipantScore) -> int:
|
|
|
|
|
return score.victory_points
|
|
|
|
|
|
|
|
|
|
score_groups = ScoreService.group_participants_by_score(scores, value_getter)
|
2026-03-11 11:44:57 +01:00
|
|
|
sector_to_battle: Dict[str, Battle] = {
|
|
|
|
|
b.sector_id: b for b in round.battles.values()
|
|
|
|
|
}
|
|
|
|
|
for group in score_groups:
|
2026-03-12 16:28:20 +01:00
|
|
|
score_value = value_getter(scores[group[0]])
|
|
|
|
|
remaining: List[str] = [
|
|
|
|
|
campaign.war_to_campaign_part_id(pid) for pid in group
|
|
|
|
|
]
|
|
|
|
|
Pairing._run_phase(
|
2026-03-17 11:16:47 +01:00
|
|
|
war=war,
|
|
|
|
|
round=round,
|
|
|
|
|
remaining=remaining,
|
|
|
|
|
sector_to_battle=sector_to_battle,
|
|
|
|
|
resolve_ties_callback=resolve_ties_callback,
|
2026-03-12 16:28:20 +01:00
|
|
|
use_priority=True,
|
|
|
|
|
score_value=score_value,
|
|
|
|
|
)
|
|
|
|
|
Pairing._run_phase(
|
2026-03-17 11:16:47 +01:00
|
|
|
war=war,
|
|
|
|
|
round=round,
|
|
|
|
|
remaining=remaining,
|
|
|
|
|
sector_to_battle=sector_to_battle,
|
|
|
|
|
resolve_ties_callback=resolve_ties_callback,
|
2026-03-12 16:28:20 +01:00
|
|
|
use_priority=False,
|
|
|
|
|
score_value=score_value,
|
|
|
|
|
)
|
|
|
|
|
Pairing._assign_fallback(round, remaining)
|
2026-03-11 11:44:57 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2026-03-12 16:28:20 +01:00
|
|
|
def _run_phase(
|
|
|
|
|
war: War,
|
2026-03-11 11:44:57 +01:00
|
|
|
round: Round,
|
2026-03-12 16:28:20 +01:00
|
|
|
remaining: List[str],
|
2026-03-11 11:44:57 +01:00
|
|
|
sector_to_battle: Dict[str, Battle],
|
2026-03-12 16:28:20 +01:00
|
|
|
resolve_ties_callback: ResolveTiesCallback,
|
|
|
|
|
*,
|
|
|
|
|
use_priority: bool,
|
|
|
|
|
score_value: int,
|
2026-03-11 11:44:57 +01:00
|
|
|
) -> None:
|
2026-03-12 16:28:20 +01:00
|
|
|
demand = Pairing._build_sector_demand(
|
|
|
|
|
round,
|
|
|
|
|
remaining,
|
|
|
|
|
use_priority,
|
|
|
|
|
)
|
|
|
|
|
for sector_id, participants in demand.items():
|
|
|
|
|
battle = sector_to_battle.get(sector_id)
|
2026-03-11 11:44:57 +01:00
|
|
|
if not battle:
|
|
|
|
|
continue
|
2026-03-12 16:28:20 +01:00
|
|
|
places = len(battle.get_available_places())
|
|
|
|
|
if places <= 0:
|
|
|
|
|
continue
|
|
|
|
|
winners = Pairing._resolve_sector_allocation(
|
2026-03-17 11:16:47 +01:00
|
|
|
war=war,
|
|
|
|
|
round=round,
|
|
|
|
|
sector_id=sector_id,
|
|
|
|
|
participants=participants,
|
|
|
|
|
places=places,
|
|
|
|
|
resolve_ties_callback=resolve_ties_callback,
|
|
|
|
|
score_value=score_value,
|
2026-03-12 16:28:20 +01:00
|
|
|
)
|
|
|
|
|
for pid in winners:
|
|
|
|
|
battle.assign_participant(pid)
|
|
|
|
|
remaining.remove(pid)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _build_sector_demand(
|
|
|
|
|
round: Round,
|
|
|
|
|
participants: List[str],
|
|
|
|
|
use_priority: bool,
|
|
|
|
|
) -> Dict[str, List[str]]:
|
|
|
|
|
demand: Dict[str, List[str]] = {}
|
|
|
|
|
for pid in participants:
|
|
|
|
|
choice = round.choices.get(pid)
|
|
|
|
|
if not choice:
|
|
|
|
|
continue
|
|
|
|
|
sector_id = (
|
|
|
|
|
choice.priority_sector_id
|
|
|
|
|
if use_priority
|
|
|
|
|
else choice.secondary_sector_id
|
|
|
|
|
)
|
|
|
|
|
if not sector_id:
|
|
|
|
|
continue
|
|
|
|
|
demand.setdefault(sector_id, []).append(pid)
|
|
|
|
|
return demand
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _resolve_sector_allocation(
|
|
|
|
|
war: War,
|
|
|
|
|
round: Round,
|
|
|
|
|
sector_id: str,
|
|
|
|
|
participants: List[str],
|
|
|
|
|
places: int,
|
|
|
|
|
resolve_ties_callback: ResolveTiesCallback,
|
|
|
|
|
score_value: int,
|
|
|
|
|
) -> List[str]:
|
|
|
|
|
if len(participants) <= places:
|
|
|
|
|
return participants
|
|
|
|
|
campaign = war.get_campaign_by_round(round.id)
|
|
|
|
|
if campaign is None:
|
|
|
|
|
raise DomainError("Campaign not found for round {round.id}")
|
2026-03-18 09:26:43 +01:00
|
|
|
war_participants = [
|
|
|
|
|
campaign.campaign_to_war_part_id(pid) for pid in participants
|
|
|
|
|
]
|
2026-03-12 16:28:20 +01:00
|
|
|
context = TieContext(
|
|
|
|
|
context_type=ContextType.CHOICE,
|
|
|
|
|
context_id=round.id,
|
2026-03-18 09:26:43 +01:00
|
|
|
participants=war_participants,
|
2026-03-17 11:16:47 +01:00
|
|
|
score_value=score_value,
|
2026-03-12 16:28:20 +01:00
|
|
|
score_kind=ScoreKind.VP,
|
|
|
|
|
sector_id=sector_id,
|
|
|
|
|
)
|
|
|
|
|
# ---- resolve tie loop ----
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id = TieResolver.find_active_tie_id(war, context) or str(uuid4())
|
2026-03-12 16:28:20 +01:00
|
|
|
while not TieResolver.is_tie_resolved(war, context):
|
2026-03-17 11:16:47 +01:00
|
|
|
active = TieResolver.get_active_participants(
|
|
|
|
|
war, context, context.participants
|
|
|
|
|
)
|
|
|
|
|
current_context = TieContext(
|
|
|
|
|
context_type=context.context_type,
|
|
|
|
|
context_id=context.context_id,
|
|
|
|
|
participants=active,
|
|
|
|
|
score_value=context.score_value,
|
|
|
|
|
score_kind=context.score_kind,
|
|
|
|
|
sector_id=context.sector_id,
|
|
|
|
|
)
|
2026-03-18 10:48:29 +01:00
|
|
|
# natural, unbreakable or acceptable (enough places) draw
|
|
|
|
|
if (
|
|
|
|
|
not TieResolver.can_tie_be_resolved(
|
|
|
|
|
war, context, current_context.participants
|
|
|
|
|
)
|
|
|
|
|
or len(active) <= places
|
2026-03-17 11:16:47 +01:00
|
|
|
):
|
2026-03-12 16:28:20 +01:00
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(
|
|
|
|
|
None,
|
|
|
|
|
context.context_type,
|
|
|
|
|
context.context_id,
|
2026-03-18 09:26:43 +01:00
|
|
|
participants=context.participants,
|
2026-03-17 11:16:47 +01:00
|
|
|
tie_id=tie_id,
|
2026-03-12 16:28:20 +01:00
|
|
|
score_value=score_value,
|
|
|
|
|
sector_id=sector_id,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
break
|
2026-03-17 11:16:47 +01:00
|
|
|
bids_map = resolve_ties_callback(war, [current_context])
|
|
|
|
|
bids = bids_map[current_context.key()]
|
|
|
|
|
# confirmed draw if current bids are 0
|
|
|
|
|
if bids is not None and not any(bids.values()):
|
|
|
|
|
war.events.append(
|
|
|
|
|
TieResolved(
|
|
|
|
|
None,
|
|
|
|
|
context.context_type,
|
|
|
|
|
context.context_id,
|
|
|
|
|
participants=context.participants,
|
|
|
|
|
tie_id=tie_id,
|
|
|
|
|
score_value=context.score_value,
|
|
|
|
|
objective_id=context.objective_id,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
break
|
|
|
|
|
TieResolver.apply_bids(war, context, tie_id, bids)
|
|
|
|
|
TieResolver.resolve_tie_state(war, context, tie_id, bids)
|
2026-03-12 16:28:20 +01:00
|
|
|
ranked_groups = TieResolver.rank_by_tokens(
|
|
|
|
|
war,
|
|
|
|
|
context,
|
|
|
|
|
context.participants,
|
2026-03-11 11:44:57 +01:00
|
|
|
)
|
2026-03-12 16:28:20 +01:00
|
|
|
ordered: List[str] = []
|
|
|
|
|
for group in ranked_groups:
|
|
|
|
|
shuffled_group = list(group)
|
2026-03-17 11:16:47 +01:00
|
|
|
# TODO improve tie break with history parsing
|
2026-03-12 16:28:20 +01:00
|
|
|
random.shuffle(shuffled_group)
|
|
|
|
|
ordered.extend(
|
|
|
|
|
campaign.war_to_campaign_part_id(pid) for pid in shuffled_group
|
|
|
|
|
)
|
|
|
|
|
return ordered[:places]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _assign_fallback(
|
|
|
|
|
round: Round,
|
|
|
|
|
remaining: List[str],
|
|
|
|
|
) -> None:
|
2026-03-18 09:26:43 +01:00
|
|
|
# TODO avoid rematch
|
2026-03-12 16:28:20 +01:00
|
|
|
for pid in list(remaining):
|
|
|
|
|
available = round.get_battles_with_places()
|
|
|
|
|
if not available:
|
|
|
|
|
raise DomainError("No available battle remaining")
|
|
|
|
|
if len(available) == 1:
|
|
|
|
|
available[0].assign_participant(pid)
|
|
|
|
|
remaining.remove(pid)
|
|
|
|
|
continue
|
|
|
|
|
raise DomainError(f"Ambiguous fallback for participant {pid}")
|
2026-03-19 09:02:22 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_allocation_kind(
|
|
|
|
|
war: War,
|
|
|
|
|
round_id: str,
|
|
|
|
|
participant_id: str,
|
|
|
|
|
sector_id: str,
|
|
|
|
|
) -> AllocationType:
|
|
|
|
|
round = war.get_round(round_id)
|
|
|
|
|
choice = round.choices.get(participant_id)
|
|
|
|
|
if not choice:
|
|
|
|
|
raise DomainError(f"No choice found for participant {participant_id}")
|
|
|
|
|
if choice.priority_sector_id == sector_id:
|
|
|
|
|
return AllocationType.PRIORITY
|
|
|
|
|
if choice.secondary_sector_id == sector_id:
|
|
|
|
|
return AllocationType.SECONDARY
|
|
|
|
|
return AllocationType.FALLBACK
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_round_allocation(
|
|
|
|
|
war: War,
|
|
|
|
|
round: Round,
|
|
|
|
|
campaign_participant_id: str,
|
|
|
|
|
) -> AllocationResult:
|
|
|
|
|
choice = round.choices[campaign_participant_id]
|
|
|
|
|
campaign = war.get_campaign_by_round(round.id)
|
|
|
|
|
if campaign is None:
|
|
|
|
|
raise DomainError(f"No campaign found for round {round.id}")
|
|
|
|
|
war_pid = campaign.campaign_to_war_part_id(campaign_participant_id)
|
|
|
|
|
|
2026-03-19 15:10:48 +01:00
|
|
|
token_priority = TieResolver.participant_spent_token(
|
2026-03-19 09:02:22 +01:00
|
|
|
war,
|
2026-03-19 15:10:48 +01:00
|
|
|
ContextType.CHOICE,
|
2026-03-19 09:02:22 +01:00
|
|
|
round.id,
|
|
|
|
|
choice.priority_sector_id,
|
|
|
|
|
war_pid,
|
|
|
|
|
)
|
2026-03-19 15:10:48 +01:00
|
|
|
token_secondary = TieResolver.participant_spent_token(
|
2026-03-19 09:02:22 +01:00
|
|
|
war,
|
2026-03-19 15:10:48 +01:00
|
|
|
ContextType.CHOICE,
|
2026-03-19 09:02:22 +01:00
|
|
|
round.id,
|
|
|
|
|
choice.secondary_sector_id,
|
|
|
|
|
war_pid,
|
|
|
|
|
)
|
|
|
|
|
battle = round.get_battle_for_participant(campaign_participant_id)
|
|
|
|
|
allocation = AllocationType.FALLBACK
|
|
|
|
|
if battle:
|
|
|
|
|
allocation = Pairing.get_allocation_kind(
|
|
|
|
|
war,
|
|
|
|
|
round.id,
|
|
|
|
|
campaign_participant_id,
|
|
|
|
|
battle.sector_id,
|
|
|
|
|
)
|
|
|
|
|
priority_status = ChoiceStatus.NONE
|
|
|
|
|
secondary_status = ChoiceStatus.NONE
|
|
|
|
|
fallback = allocation == AllocationType.FALLBACK
|
|
|
|
|
if allocation == AllocationType.PRIORITY:
|
|
|
|
|
priority_status = (
|
|
|
|
|
ChoiceStatus.ALLOCATEDTOKEN
|
|
|
|
|
if token_priority
|
|
|
|
|
else ChoiceStatus.ALLOCATED
|
|
|
|
|
)
|
|
|
|
|
elif token_priority:
|
|
|
|
|
priority_status = ChoiceStatus.TOKEN
|
|
|
|
|
if allocation == AllocationType.SECONDARY:
|
|
|
|
|
secondary_status = (
|
|
|
|
|
ChoiceStatus.ALLOCATEDTOKEN
|
|
|
|
|
if token_secondary
|
|
|
|
|
else ChoiceStatus.ALLOCATED
|
|
|
|
|
)
|
|
|
|
|
elif token_secondary:
|
|
|
|
|
secondary_status = ChoiceStatus.TOKEN
|
|
|
|
|
return AllocationResult(
|
|
|
|
|
priority=priority_status,
|
|
|
|
|
secondary=secondary_status,
|
|
|
|
|
fallback=fallback,
|
|
|
|
|
)
|