close war and manage tie

This commit is contained in:
Maxime Réaux 2026-02-23 21:18:27 +01:00
parent d766befd31
commit 71e987304b
7 changed files with 185 additions and 55 deletions

View file

@ -1,5 +1,4 @@
from __future__ import annotations
from typing import List
from warchron.constants import ContextType
from warchron.model.exception import ForbiddenOperation
@ -78,24 +77,12 @@ class ClosureService:
# War methods
@staticmethod
def close_war(war: War) -> List[str]:
def check_war_closable(war: War) -> None:
if war.is_over:
raise ForbiddenOperation("War already closed")
if not war.all_campaigns_finished():
raise RuntimeError("All campaigns must be finished to close their war")
ties: List[str] = []
# for campaign in war.campaigns:
# # compute score
# # if participants have same score
# ties.append(
# ResolutionContext(
# context_type=ContextType.WAR,
# context_id=war.id,
# participant_ids=[
# war.participants[war_participant_id],
# war.participants[war_participant_id],
# ],
# )
# )
if ties:
return ties
raise ForbiddenOperation("All campaigns must be closed to close their war")
@staticmethod
def finalize_war(war: War) -> None:
war.is_over = True
return []

View file

@ -24,14 +24,12 @@ class ScoreService:
if not rnd.is_over:
continue
yield from rnd.battles.values()
elif context_type == ContextType.CAMPAIGN:
campaign = war.get_campaign(context_id)
for rnd in campaign.rounds:
if not rnd.is_over:
continue
yield from rnd.battles.values()
elif context_type == ContextType.BATTLE:
battle = war.get_battle(context_id)
campaign = war.get_campaign_by_sector(battle.sector_id)

View file

@ -82,7 +82,32 @@ class TieResolver:
@staticmethod
def find_war_ties(war: War) -> List[TieContext]:
return [] # TODO
if TieResolver.is_tie_resolved(war, ContextType.WAR, war.id):
return []
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
tie_id = f"{war.id}:score:{score_value}"
if TieResolver.is_tie_resolved(war, ContextType.WAR, tie_id):
continue
if not TieResolver.can_tie_be_resolved(
war, ContextType.WAR, tie_id, participants
):
war.events.append(TieResolved(None, ContextType.WAR, tie_id))
continue
ties.append(
TieContext(
context_type=ContextType.WAR,
context_id=tie_id,
participants=participants,
)
)
return ties
@staticmethod
def apply_bids(