choice tie-break and random fallback + exception cleanup + war<->camp pid
This commit is contained in:
parent
241d7f10f5
commit
241e76c937
15 changed files with 241 additions and 157 deletions
|
|
@ -1,8 +1,8 @@
|
|||
from __future__ import annotations
|
||||
from typing import Dict, List
|
||||
from typing import Dict, List, Callable, Tuple
|
||||
import random
|
||||
|
||||
from warchron.constants import ContextType
|
||||
from warchron.constants import ContextType, ScoreKind
|
||||
from warchron.model.exception import (
|
||||
DomainError,
|
||||
ForbiddenOperation,
|
||||
|
|
@ -12,6 +12,14 @@ 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
|
||||
from warchron.model.tie_manager import TieResolver, TieContext
|
||||
from warchron.model.war_event import TieResolved
|
||||
from warchron.model.score_service import ParticipantScore
|
||||
|
||||
ResolveTiesCallback = Callable[
|
||||
["War", List["TieContext"]],
|
||||
Dict[Tuple[str, str, int | None], Dict[str, bool]],
|
||||
]
|
||||
|
||||
|
||||
class Pairing:
|
||||
|
|
@ -30,13 +38,18 @@ class Pairing:
|
|||
)
|
||||
for pid, choice in round.choices.items():
|
||||
if choice is not None and not choice.priority_sector_id:
|
||||
raise DomainError(f"Missing priority choice for participant {pid}")
|
||||
raise ForbiddenOperation(
|
||||
f"Missing priority choice for participant {pid}"
|
||||
)
|
||||
if choice is not None and not choice.secondary_sector_id:
|
||||
raise DomainError(f"Missing secondary choice for participant {pid}")
|
||||
raise ForbiddenOperation(
|
||||
f"Missing secondary choice for participant {pid}"
|
||||
)
|
||||
|
||||
def cleanup() -> None:
|
||||
for bat in round.battles.values():
|
||||
bat.cleanup_battle_players()
|
||||
# FIXME cancel TieResolved + TokenSpent
|
||||
|
||||
if any(
|
||||
bat.player_1_id is not None or bat.player_2_id is not None
|
||||
|
|
@ -53,6 +66,7 @@ class Pairing:
|
|||
def assign_battles_to_participants(
|
||||
war: War,
|
||||
round: Round,
|
||||
resolve_ties_callback: ResolveTiesCallback,
|
||||
) -> None:
|
||||
campaign = war.get_campaign_by_round(round.id)
|
||||
if campaign is None:
|
||||
|
|
@ -62,57 +76,162 @@ class Pairing:
|
|||
ContextType.CAMPAIGN,
|
||||
campaign.id,
|
||||
)
|
||||
score_groups = ScoreService.group_participants_by_score(
|
||||
scores, lambda score: score.victory_points
|
||||
)
|
||||
|
||||
def value_getter(score: ParticipantScore) -> int:
|
||||
return score.victory_points
|
||||
|
||||
score_groups = ScoreService.group_participants_by_score(scores, value_getter)
|
||||
sector_to_battle: Dict[str, Battle] = {
|
||||
b.sector_id: b for b in round.battles.values()
|
||||
}
|
||||
for group in score_groups:
|
||||
# persistent equality → random order
|
||||
ordered_group = list(group)
|
||||
random.shuffle(ordered_group)
|
||||
for participant_id in ordered_group:
|
||||
camp_part = campaign.get_campaign_participant_by_war_participant_id(
|
||||
participant_id
|
||||
)
|
||||
if camp_part:
|
||||
Pairing._assign_single_participant(
|
||||
round,
|
||||
camp_part.id,
|
||||
sector_to_battle,
|
||||
)
|
||||
score_value = value_getter(scores[group[0]])
|
||||
remaining: List[str] = [
|
||||
campaign.war_to_campaign_part_id(pid) for pid in group
|
||||
]
|
||||
Pairing._run_phase(
|
||||
war,
|
||||
round,
|
||||
remaining,
|
||||
sector_to_battle,
|
||||
resolve_ties_callback,
|
||||
use_priority=True,
|
||||
score_value=score_value,
|
||||
)
|
||||
Pairing._run_phase(
|
||||
war,
|
||||
round,
|
||||
remaining,
|
||||
sector_to_battle,
|
||||
resolve_ties_callback,
|
||||
use_priority=False,
|
||||
score_value=score_value,
|
||||
)
|
||||
Pairing._assign_fallback(round, remaining)
|
||||
|
||||
@staticmethod
|
||||
def _assign_single_participant(
|
||||
def _run_phase(
|
||||
war: War,
|
||||
round: Round,
|
||||
participant_id: str,
|
||||
remaining: List[str],
|
||||
sector_to_battle: Dict[str, Battle],
|
||||
resolve_ties_callback: ResolveTiesCallback,
|
||||
*,
|
||||
use_priority: bool,
|
||||
score_value: int,
|
||||
) -> None:
|
||||
choice = round.choices.get(participant_id)
|
||||
preferred_sectors: List[str] = []
|
||||
if choice:
|
||||
if choice.priority_sector_id:
|
||||
preferred_sectors.append(choice.priority_sector_id)
|
||||
if choice.secondary_sector_id:
|
||||
preferred_sectors.append(choice.secondary_sector_id)
|
||||
# --- try preferred sectors ---
|
||||
for sect_id in preferred_sectors:
|
||||
battle = sector_to_battle.get(sect_id)
|
||||
demand = Pairing._build_sector_demand(
|
||||
round,
|
||||
remaining,
|
||||
use_priority,
|
||||
)
|
||||
for sector_id, participants in demand.items():
|
||||
battle = sector_to_battle.get(sector_id)
|
||||
if not battle:
|
||||
continue
|
||||
if battle.get_available_places():
|
||||
battle.assign_participant(participant_id)
|
||||
return
|
||||
# --- fallback rules ---
|
||||
available_battles = round.get_battles_with_places()
|
||||
if not available_battles:
|
||||
raise RuntimeError("No available battle remaining")
|
||||
if len(available_battles) == 1:
|
||||
available_battles[0].assign_participant(participant_id)
|
||||
return
|
||||
# multiple remaining battles → warning
|
||||
raise RuntimeError(
|
||||
f"Ambiguous fallback for participant {participant_id}: "
|
||||
"multiple battles still available"
|
||||
places = len(battle.get_available_places())
|
||||
if places <= 0:
|
||||
continue
|
||||
winners = Pairing._resolve_sector_allocation(
|
||||
war,
|
||||
round,
|
||||
sector_id,
|
||||
participants,
|
||||
places,
|
||||
resolve_ties_callback,
|
||||
score_value,
|
||||
)
|
||||
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}")
|
||||
context = TieContext(
|
||||
context_type=ContextType.CHOICE,
|
||||
context_id=round.id,
|
||||
participants=[
|
||||
campaign.campaign_to_war_part_id(pid) for pid in participants
|
||||
],
|
||||
score_kind=ScoreKind.VP,
|
||||
sector_id=sector_id,
|
||||
)
|
||||
# ---- resolve tie loop ----
|
||||
while not TieResolver.is_tie_resolved(war, context):
|
||||
if not TieResolver.can_tie_be_resolved(war, context, context.participants):
|
||||
war.events.append(
|
||||
TieResolved(
|
||||
None,
|
||||
context.context_type,
|
||||
context.context_id,
|
||||
score_value=score_value,
|
||||
sector_id=sector_id,
|
||||
)
|
||||
)
|
||||
break
|
||||
bids_map = resolve_ties_callback(war, [context])
|
||||
bids = bids_map[context.key()]
|
||||
TieResolver.apply_bids(war, context, bids)
|
||||
TieResolver.resolve_tie_state(war, context, bids)
|
||||
ranked_groups = TieResolver.rank_by_tokens(
|
||||
war,
|
||||
context,
|
||||
context.participants,
|
||||
)
|
||||
ordered: List[str] = []
|
||||
for group in ranked_groups:
|
||||
shuffled_group = list(group)
|
||||
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:
|
||||
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}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue