fix re-enable token on closed camapign + refacto war_event attributes
This commit is contained in:
parent
5c124f9229
commit
6efd22527a
8 changed files with 108 additions and 57 deletions
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue