detect and resolve battle tie with influence_token
This commit is contained in:
parent
115ddf8d50
commit
818d2886f4
23 changed files with 808 additions and 172 deletions
|
|
@ -1,30 +1,143 @@
|
|||
from __future__ import annotations
|
||||
from typing import Dict, Any, TypeVar, Type, cast
|
||||
from datetime import datetime
|
||||
from uuid import uuid4
|
||||
|
||||
T = TypeVar("T", bound="WarEvent")
|
||||
EVENT_REGISTRY: Dict[str, Type["WarEvent"]] = {}
|
||||
|
||||
|
||||
def register_event(cls: Type[T]) -> Type[T]:
|
||||
EVENT_REGISTRY[cls.TYPE] = cls
|
||||
return cls
|
||||
|
||||
|
||||
class WarEvent:
|
||||
def __init__(self, participant_id: str):
|
||||
TYPE = "WarEvent"
|
||||
|
||||
def __init__(self, participant_id: str | None = None):
|
||||
self.id: str = str(uuid4())
|
||||
self.participant_id: str = participant_id
|
||||
self.participant_id: str | None = participant_id
|
||||
self.timestamp: datetime = datetime.now()
|
||||
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_participant(self, new_war_part_id: str | None) -> None:
|
||||
self.participant_id = new_war_part_id
|
||||
|
||||
def set_timestamp(self, new_timestamp: datetime) -> None:
|
||||
self.timestamp = new_timestamp
|
||||
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"type": self.TYPE,
|
||||
"id": self.id,
|
||||
"participant_id": self.participant_id,
|
||||
"timestamp": self.timestamp.isoformat(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _base_fromDict(cls: Type[T], ev: T, data: Dict[str, Any]) -> T:
|
||||
ev.id = data["id"]
|
||||
ev.participant_id = data["participant_id"]
|
||||
ev.timestamp = datetime.fromisoformat(data["timestamp"])
|
||||
return ev
|
||||
|
||||
@staticmethod
|
||||
def fromDict(data: Dict[str, Any]) -> "WarEvent":
|
||||
ev_type = data["type"]
|
||||
cls = cast(Type[WarEvent], EVENT_REGISTRY.get(ev_type))
|
||||
if cls is None:
|
||||
raise ValueError(f"Unknown event type: {ev_type}")
|
||||
return cls.fromDict(data)
|
||||
|
||||
|
||||
@register_event
|
||||
class TieResolved(WarEvent):
|
||||
def __init__(self, participant_id: str, context_type: str, context_id: str):
|
||||
TYPE = "TieResolved"
|
||||
|
||||
def __init__(self, participant_id: str | None, context_type: str, context_id: str):
|
||||
super().__init__(participant_id)
|
||||
self.participant_id: str | None = (
|
||||
participant_id # winner or None (confirmed tie)
|
||||
)
|
||||
self.context_type = context_type # battle, round, campaign, war
|
||||
self.context_id = context_id
|
||||
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
d = super().toDict()
|
||||
d.update(
|
||||
{
|
||||
"context_type": self.context_type,
|
||||
"context_id": self.context_id,
|
||||
}
|
||||
)
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def fromDict(cls, data: Dict[str, Any]) -> TieResolved:
|
||||
ev = cls(
|
||||
data["participant_id"],
|
||||
data["context_type"],
|
||||
data["context_id"],
|
||||
)
|
||||
return cls._base_fromDict(ev, data)
|
||||
|
||||
|
||||
@register_event
|
||||
class InfluenceGained(WarEvent):
|
||||
TYPE = "InfluenceGained"
|
||||
|
||||
def __init__(self, participant_id: str, amount: int, source: str):
|
||||
super().__init__(participant_id)
|
||||
self.amount = amount
|
||||
self.source = source # "battle", "tie_resolution", etc.
|
||||
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
d = super().toDict()
|
||||
d.update(
|
||||
{
|
||||
"amount": self.amount,
|
||||
"source": self.source,
|
||||
}
|
||||
)
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def fromDict(cls, data: Dict[str, Any]) -> InfluenceGained:
|
||||
ev = cls(
|
||||
data["participant_id"],
|
||||
data["amount"],
|
||||
data["source"],
|
||||
)
|
||||
return cls._base_fromDict(ev, data)
|
||||
|
||||
|
||||
@register_event
|
||||
class InfluenceSpent(WarEvent):
|
||||
def __init__(self, participant_id: str, amount: int, context: str):
|
||||
TYPE = "InfluenceSpent"
|
||||
|
||||
def __init__(self, participant_id: str, amount: int, context_type: str):
|
||||
super().__init__(participant_id)
|
||||
self.amount = amount
|
||||
self.context = context # "battle_tie", "campaign_tie", etc.
|
||||
self.context_type = context_type # "battle_tie", "campaign_tie", etc.
|
||||
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
d = super().toDict()
|
||||
d.update(
|
||||
{
|
||||
"amount": self.amount,
|
||||
"context_type": self.context_type,
|
||||
}
|
||||
)
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
def fromDict(cls, data: Dict[str, Any]) -> InfluenceSpent:
|
||||
ev = cls(
|
||||
data["participant_id"],
|
||||
data["amount"],
|
||||
data["context_type"],
|
||||
)
|
||||
return cls._base_fromDict(ev, data)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue