fix choice tiebreak loop and cancel tiebreak lost tokens

This commit is contained in:
Maxime Réaux 2026-03-17 11:16:47 +01:00
parent a3b9f5a943
commit 42ad708e77
13 changed files with 333 additions and 129 deletions

View file

@ -1,5 +1,7 @@
from __future__ import annotations
from typing import Dict, List, Callable, Tuple
from uuid import uuid4
import random
from warchron.constants import ContextType, ScoreKind
@ -18,7 +20,7 @@ from warchron.model.score_service import ParticipantScore
ResolveTiesCallback = Callable[
["War", List["TieContext"]],
Dict[Tuple[str, str, int | None], Dict[str, bool]],
Dict[Tuple[str, str, int | None, str | None, str | None], Dict[str, bool]],
]
@ -26,6 +28,7 @@ class Pairing:
@staticmethod
def check_round_pairable(
war: War,
round: Round,
) -> None:
if round.is_over:
@ -48,8 +51,9 @@ class Pairing:
def cleanup() -> None:
for bat in round.battles.values():
bat.cleanup_battle_players()
# FIXME cancel TieResolved + TokenSpent
bat.clear_battle_players()
bat.set_winner(None)
war.revert_choice_ties(round.id)
if any(
bat.player_1_id is not None or bat.player_2_id is not None
@ -58,6 +62,7 @@ class Pairing:
raise RequiresConfirmation(
"Battle(s) already have player(s) assigned for this round.\n"
"Battle players will be cleared.\n"
"Choice tokens and tie-breaks will be deleted.\n"
"Do you want to continue?",
action=cleanup,
)
@ -90,20 +95,20 @@ class Pairing:
campaign.war_to_campaign_part_id(pid) for pid in group
]
Pairing._run_phase(
war,
round,
remaining,
sector_to_battle,
resolve_ties_callback,
war=war,
round=round,
remaining=remaining,
sector_to_battle=sector_to_battle,
resolve_ties_callback=resolve_ties_callback,
use_priority=True,
score_value=score_value,
)
Pairing._run_phase(
war,
round,
remaining,
sector_to_battle,
resolve_ties_callback,
war=war,
round=round,
remaining=remaining,
sector_to_battle=sector_to_battle,
resolve_ties_callback=resolve_ties_callback,
use_priority=False,
score_value=score_value,
)
@ -133,13 +138,13 @@ class Pairing:
if places <= 0:
continue
winners = Pairing._resolve_sector_allocation(
war,
round,
sector_id,
participants,
places,
resolve_ties_callback,
score_value,
war=war,
round=round,
sector_id=sector_id,
participants=participants,
places=places,
resolve_ties_callback=resolve_ties_callback,
score_value=score_value,
)
for pid in winners:
battle.assign_participant(pid)
@ -187,26 +192,59 @@ class Pairing:
participants=[
campaign.campaign_to_war_part_id(pid) for pid in participants
],
score_value=score_value,
score_kind=ScoreKind.VP,
sector_id=sector_id,
)
# ---- resolve tie loop ----
tie_id = TieResolver.find_active_tie_id(war, context) or str(uuid4())
while not TieResolver.is_tie_resolved(war, context):
if not TieResolver.can_tie_be_resolved(war, context, context.participants):
active = TieResolver.get_active_participants(
war, context, context.participants
)
if len(active) <= 1:
break
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,
)
if not TieResolver.can_tie_be_resolved(
war, context, current_context.participants
):
war.events.append(
TieResolved(
None,
context.context_type,
context.context_id,
participants,
tie_id=tie_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)
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)
ranked_groups = TieResolver.rank_by_tokens(
war,
context,
@ -215,6 +253,8 @@ class Pairing:
ordered: List[str] = []
for group in ranked_groups:
shuffled_group = list(group)
# TODO improve tie break with history parsing
# TODO avoid rematch
random.shuffle(shuffled_group)
ordered.extend(
campaign.war_to_campaign_part_id(pid) for pid in shuffled_group