fix re-enable token on closed camapign + refacto war_event attributes

This commit is contained in:
Maxime Réaux 2026-02-25 16:54:21 +01:00
parent 5c124f9229
commit 6efd22527a
8 changed files with 108 additions and 57 deletions

View file

@ -27,8 +27,7 @@ class ClosureService:
from warchron.model.result_checker import ResultChecker
already_granted = any(
isinstance(e, InfluenceGained)
and e.context_id == f"battle:{battle.sector_id}"
isinstance(e, InfluenceGained) and e.context_id == battle.sector_id
for e in war.events
)
if already_granted:
@ -51,7 +50,7 @@ class ClosureService:
participant_id=effective_winner,
amount=1,
context_type=ContextType.BATTLE,
context_id=f"battle:{battle.sector_id}",
context_id=battle.sector_id,
)
)

View file

@ -45,10 +45,9 @@ class ResultChecker:
current_rank = 1
for vp in sorted_vps:
participants = vp_buckets[vp]
tie_id = f"{context_id}:score:{vp}"
# no tie
if len(participants) == 1 or not TieResolver.is_tie_resolved(
war, context_type, tie_id
war, context_type, context_id, vp
):
ranking.append(
(current_rank, participants, {pid: 0 for pid in participants})
@ -59,11 +58,11 @@ class ResultChecker:
groups = TieResolver.rank_by_tokens(
war,
context_type,
tie_id,
context_id,
participants,
)
tokens_spent = TieResolver.tokens_spent_map(
war, context_type, tie_id, participants
war, context_type, context_id, participants
)
for group in groups:
group_tokens = {pid: tokens_spent[pid] for pid in group}

View file

@ -14,6 +14,7 @@ class TieContext:
context_type: ContextType
context_id: str
participants: List[str] # war_participant_ids
score_value: int | None = None
class TieResolver:
@ -47,6 +48,7 @@ class TieResolver:
context_type=ContextType.BATTLE,
context_id=battle.sector_id,
participants=[p1, p2],
score_value=None,
)
)
return ties
@ -63,19 +65,23 @@ class TieResolver:
for score_value, participants in buckets.items():
if len(participants) <= 1:
continue
tie_id = f"{campaign_id}:score:{score_value}"
if TieResolver.is_tie_resolved(war, ContextType.CAMPAIGN, tie_id):
if TieResolver.is_tie_resolved(
war, ContextType.CAMPAIGN, campaign_id, score_value
):
continue
if not TieResolver.can_tie_be_resolved(
war, ContextType.CAMPAIGN, tie_id, participants
war, ContextType.CAMPAIGN, campaign_id, participants
):
war.events.append(TieResolved(None, ContextType.CAMPAIGN, tie_id))
war.events.append(
TieResolved(None, ContextType.CAMPAIGN, campaign_id, score_value)
)
continue
ties.append(
TieContext(
context_type=ContextType.CAMPAIGN,
context_id=tie_id,
context_id=campaign_id,
participants=participants,
score_value=score_value,
)
)
return ties
@ -92,19 +98,21 @@ class TieResolver:
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):
if TieResolver.is_tie_resolved(war, ContextType.WAR, war.id, score_value):
continue
if not TieResolver.can_tie_be_resolved(
war, ContextType.WAR, tie_id, participants
war, ContextType.WAR, war.id, participants
):
war.events.append(TieResolved(None, ContextType.WAR, tie_id))
war.events.append(
TieResolved(None, ContextType.WAR, war.id, score_value)
)
continue
ties.append(
TieContext(
context_type=ContextType.WAR,
context_id=tie_id,
context_id=war.id,
participants=participants,
score_value=score_value,
)
)
return ties
@ -135,6 +143,7 @@ class TieResolver:
war: War,
context_type: ContextType,
context_id: str,
score_value: int | None = None,
) -> None:
war.events = [
ev
@ -149,6 +158,7 @@ class TieResolver:
isinstance(ev, TieResolved)
and ev.context_type == context_type
and ev.context_id == context_id
and ev.score_value == score_value
)
)
]
@ -210,26 +220,37 @@ class TieResolver:
@staticmethod
def resolve_tie_state(
war: War,
context_type: ContextType,
context_id: str,
participants: List[str],
ctx: TieContext,
bids: dict[str, bool] | None = None,
) -> None:
active = TieResolver.get_active_participants(
war, context_type, context_id, participants
war,
ctx.context_type,
ctx.context_id,
ctx.participants,
)
# confirmed draw if non had bid
if not active:
war.events.append(TieResolved(None, context_type, context_id))
war.events.append(
TieResolved(None, ctx.context_type, ctx.context_id, ctx.score_value)
)
return
# confirmed draw if current bids are 0
if bids is not None and not any(bids.values()):
war.events.append(TieResolved(None, context_type, context_id))
war.events.append(
TieResolved(None, ctx.context_type, ctx.context_id, ctx.score_value)
)
return
# else rank_by_tokens
groups = TieResolver.rank_by_tokens(war, context_type, context_id, participants)
groups = TieResolver.rank_by_tokens(
war, ctx.context_type, ctx.context_id, ctx.participants
)
if len(groups[0]) == 1:
war.events.append(TieResolved(groups[0][0], context_type, context_id))
war.events.append(
TieResolved(
groups[0][0], ctx.context_type, ctx.context_id, ctx.score_value
)
)
return
# if tie persists, do nothing, workflow will call again
@ -247,21 +268,29 @@ class TieResolver:
war: War,
context_type: ContextType,
context_id: str,
score_value: int | None = None,
) -> bool:
for ev in reversed(war.events):
if (
isinstance(ev, TieResolved)
and ev.context_type == context_type
and ev.context_id == context_id
and ev.score_value == score_value
):
return ev.participant_id is not None
return False
@staticmethod
def is_tie_resolved(war: War, context_type: ContextType, context_id: str) -> bool:
def is_tie_resolved(
war: War,
context_type: ContextType,
context_id: str,
score_value: int | None = None,
) -> bool:
return any(
isinstance(ev, TieResolved)
and ev.context_type == context_type
and ev.context_id == context_id
and ev.score_value == score_value
for ev in war.events
)

View file

@ -63,19 +63,32 @@ class WarEvent:
class TieResolved(WarEvent):
TYPE = "TieResolved"
def __init__(self, participant_id: str | None, context_type: str, context_id: str):
def __init__(
self,
participant_id: str | None,
context_type: str,
context_id: str,
score_value: int | None = None,
):
super().__init__(participant_id, context_type, context_id)
self.score_value = score_value
def toDict(self) -> Dict[str, Any]:
d = super().toDict()
d.update(
{
"score_value": self.score_value or None,
}
)
return d
@classmethod
def fromDict(cls, data: Dict[str, Any]) -> TieResolved:
ev = cls(
data["participant_id"],
data["participant_id"] or None,
data["context_type"],
data["context_id"],
data["score_value"] or None,
)
return cls._base_fromDict(ev, data)