2026-02-05 08:42:38 +01:00
|
|
|
from __future__ import annotations
|
2026-02-06 09:59:54 +01:00
|
|
|
from typing import Any, Dict
|
2026-02-05 08:42:38 +01:00
|
|
|
from uuid import uuid4
|
2026-02-11 19:22:43 +01:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from warchron.model.war import War
|
|
|
|
|
from warchron.model.war_event import WarEvent, InfluenceSpent, InfluenceGained
|
|
|
|
|
from warchron.model.score_service import ScoreService
|
2026-02-05 08:42:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class WarParticipant:
|
|
|
|
|
def __init__(self, *, player_id: str, faction: str):
|
|
|
|
|
self.id: str = str(uuid4())
|
2026-02-06 09:59:54 +01:00
|
|
|
self.player_id: str = player_id # ref to Model.players
|
2026-02-05 08:42:38 +01:00
|
|
|
self.faction: str = faction
|
2026-02-11 19:22:43 +01:00
|
|
|
self.events: list[WarEvent] = []
|
2026-02-05 08:42:38 +01:00
|
|
|
|
|
|
|
|
def set_id(self, new_id: str) -> None:
|
|
|
|
|
self.id = new_id
|
|
|
|
|
|
|
|
|
|
def set_player(self, new_player: str) -> None:
|
|
|
|
|
self.player_id = new_player
|
|
|
|
|
|
|
|
|
|
def set_faction(self, new_faction: str) -> None:
|
|
|
|
|
self.faction = new_faction
|
2026-02-06 09:59:54 +01:00
|
|
|
|
|
|
|
|
def toDict(self) -> Dict[str, Any]:
|
|
|
|
|
return {
|
|
|
|
|
"id": self.id,
|
|
|
|
|
"player_id": self.player_id,
|
|
|
|
|
"faction": self.faction,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def fromDict(data: Dict[str, Any]) -> WarParticipant:
|
|
|
|
|
part = WarParticipant(
|
|
|
|
|
player_id=data["player_id"],
|
|
|
|
|
faction=data["faction"],
|
|
|
|
|
)
|
|
|
|
|
part.set_id(data["id"])
|
|
|
|
|
return part
|
2026-02-11 19:22:43 +01:00
|
|
|
|
|
|
|
|
# Computed properties
|
|
|
|
|
|
|
|
|
|
def influence_tokens(self) -> int:
|
|
|
|
|
gained = sum(e.amount for e in self.events if isinstance(e, InfluenceGained))
|
|
|
|
|
spent = sum(e.amount for e in self.events if isinstance(e, InfluenceSpent))
|
|
|
|
|
return gained - spent
|
|
|
|
|
|
|
|
|
|
def victory_points(self, war: "War") -> int:
|
|
|
|
|
return ScoreService.compute_victory_points_for_participant(war, self.id)
|
|
|
|
|
|
|
|
|
|
def narrative_points(self, war: "War") -> Dict[str, int]:
|
|
|
|
|
return ScoreService.compute_narrative_points_for_participant(war, self.id)
|