refactor campaign participant ID

This commit is contained in:
Maxime Réaux 2026-02-02 14:33:31 +01:00
parent fbb1c913ba
commit ac01568c2f
4 changed files with 221 additions and 129 deletions

View file

@ -1,8 +1,9 @@
from __future__ import annotations
from uuid import uuid4
from uuid import uuid4
from warchron.model.round import Round, Choice, Battle
class Campaign:
def __init__(self, name: str, month: int):
self.id: str = str(uuid4())
@ -21,20 +22,20 @@ class Campaign:
def set_month(self, new_month: int):
self.month = new_month
def set_state(self, new_state: bool):
self.is_over = new_state
def toDict(self):
return {
"id" : self.id,
"name" : self.name,
"month" : self.month,
"id": self.id,
"name": self.name,
"month": self.month,
# "participants" : self.participants,
"rounds": [rnd.toDict() for rnd in self.rounds],
"is_over": self.is_over
"is_over": self.is_over,
}
@staticmethod
def fromDict(data: dict):
camp = Campaign(name=data["name"], month=data["month"])
@ -45,47 +46,61 @@ class Campaign:
camp.set_state(data.get("is_over", False))
return camp
# Campaign participant methods
# Campaign participant methods
def get_all_campaign_participants_ids(self) -> set[str]:
return set(self.participants.keys())
def has_participant(self, player_id: str) -> bool:
##TODO change lookup id target
return player_id in self.participants
def add_campaign_participant(self, player_id: str, leader: str, theme: str) -> CampaignParticipant:
if player_id in self.participants:
def add_campaign_participant(
self, war_participant_id: str, leader: str, theme: str
) -> CampaignParticipant:
## TODO change lookup id target
if war_participant_id in self.participants:
raise ValueError("Player already registered in this campaign")
participant = CampaignParticipant(player_id, leader, theme)
participant = CampaignParticipant(
war_participant_id=war_participant_id, leader=leader, theme=theme
)
self.participants[participant.id] = participant
return participant
def get_campaign_participant(self, id: str) -> CampaignParticipant:
return self.participants[id]
def get_campaign_participant(self, participant_id: str) -> CampaignParticipant:
try:
return self.participants[participant_id]
except KeyError:
raise KeyError(f"Participant {participant_id} not in campaign {self.id}")
def get_all_campaign_participants(self) -> list[CampaignParticipant]:
return list(self.participants.values())
def update_campaign_participant(self, player_id: str, *, leader: str, theme: str):
part = self.get_campaign_participant(player_id)
def update_campaign_participant(
self, participant_id: str, *, leader: str, theme: str
):
part = self.get_campaign_participant(participant_id)
# Can't change referred War.participant
part.set_leader(leader)
part.set_theme(theme)
def remove_campaign_participant(self, player_id: str):
def remove_campaign_participant(self, participant_id: str):
# TODO manage choices referring to it
# TODO manage battles referring to it
del self.participants[player_id]
del self.participants[participant_id]
# Sector methods
# Sector methods
def add_sector(self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str) -> Sector:
def add_sector(
self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str
) -> Sector:
sect = Sector(name, round_id, major_id, minor_id, influence_id)
self.sectors[sect.id] = sect
return sect
def get_sector(self, sector_id: str) -> Sector:
return self.sectors[sector_id]
def get_sector_name(self, sector_id: str) -> str:
if sector_id is None:
return ""
@ -95,31 +110,37 @@ class Campaign:
return list(self.sectors.values())
# TODO manage choices referring to it (round order!)
def update_sector(self, sector_id: str, *, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str):
def update_sector(
self,
sector_id: str,
*,
name: str,
round_id: str,
major_id: str,
minor_id: str,
influence_id: str,
):
sect = self.get_sector(sector_id)
sect.set_name(name)
sect.set_round(round_id)
sect.set_major(major_id)
sect.set_minor(minor_id)
sect.set_influence(influence_id)
def remove_sector(self, sector_id: str):
# TODO manage choices referring to it
# TODO manage battles referring to it
del self.sectors[sector_id]
def get_sectors_in_round(self, round_id: str) -> list[Sector]:
sectors = [
s for s in self.sectors.values()
if s.round_id == round_id
]
sectors = [s for s in self.sectors.values() if s.round_id == round_id]
return sectors
# Round methods
# Round methods
def has_round(self, round_id: str) -> bool:
return any(r.id == round_id for r in self.rounds)
def get_round(self, round_id: str) -> Round:
for rnd in self.rounds:
if rnd.id == round_id:
@ -128,12 +149,12 @@ class Campaign:
def get_all_rounds(self) -> list[Round]:
return list(self.rounds)
def add_round(self) -> Round:
round = Round()
self.rounds.append(round)
return round
def remove_round(self, round_id: str):
rnd = next((r for r in self.rounds if r.id == round_id), None)
if rnd:
@ -148,77 +169,99 @@ class Campaign:
raise KeyError("Round not found in campaign")
def get_round_name(self, round_id: str | None) -> str:
if round_id is None:
return ""
for rnd in self.rounds:
if rnd.id == round_id:
return rnd.name
if round_id is None:
return ""
for rnd in self.rounds:
if rnd.id == round_id:
return rnd.name
return ""
# Choice methods
# Choice methods
def create_choice(self, round_id: str, participant_id: str) -> Choice:
rnd = self.get_round(round_id)
return rnd.create_choice(participant_id)
def update_choice(self,
round_id: str,
participant_id: str,
priority_sector_id: str | None,
secondary_sector_id: str | None,
comment: str | None):
def update_choice(
self,
round_id: str,
participant_id: str,
priority_sector_id: str | None,
secondary_sector_id: str | None,
comment: str | None,
):
rnd = self.get_round(round_id)
rnd.update_choice(participant_id, priority_sector_id, secondary_sector_id, comment)
rnd.update_choice(
participant_id, priority_sector_id, secondary_sector_id, comment
)
def remove_choice(self, round_id: str, participant_id: str) -> Choice:
rnd = self.get_round(round_id)
rnd.remove_choice(participant_id)
# Battle methods
# Battle methods
def create_battle(self, round_id: str, sector_id: str) -> Battle:
rnd = self.get_round(round_id)
return rnd.create_battle(sector_id)
def update_battle(self,
round_id: str,
sector_id: str,
player_1_id: str | None,
player_2_id: str | None,
winner_id: str | None,
score: str | None,
victory_condition: str | None,
comment: str | None):
def update_battle(
self,
round_id: str,
sector_id: str,
player_1_id: str | None,
player_2_id: str | None,
winner_id: str | None,
score: str | None,
victory_condition: str | None,
comment: str | None,
):
rnd = self.get_round(round_id)
rnd.update_battle(sector_id, player_1_id, player_2_id, winner_id, score, victory_condition, comment)
rnd.update_battle(
sector_id,
player_1_id,
player_2_id,
winner_id,
score,
victory_condition,
comment,
)
def remove_battle(self, round_id: str, sector_id: str) -> Battle:
rnd = self.get_round(round_id)
rnd.remove_battle(sector_id)
class CampaignParticipant:
def __init__(self, player_id: str, leader: str, theme: str):
self.id: str = player_id # ref to War.participants
def __init__(self, *, war_participant_id: str, leader: str, theme: str):
self.id: str = str(uuid4())
self.war_participant_id: str = war_participant_id # ref to War.participants
self.leader: str = leader
self.theme: str = theme
def set_id(self, new_id: str):
self.id = new_id
def set_war_participant(self, new_participant: str):
self.war_participant_id = new_participant
def set_leader(self, new_faction: str):
self.leader = new_faction
def set_theme(self, new_theme: str):
self.theme = new_theme
class Sector:
def __init__(self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str):
def __init__(
self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str
):
self.id: str = str(uuid4())
self.name: str = name
self.round_id: str = round_id
self.major_objective_id: str | None = major_id # ref to War.objectives
self.minor_objective_id: str | None = minor_id # ref to War.objectives
self.influence_objective_id: str | None = influence_id # ref to War.objectives
self.major_objective_id: str | None = major_id # ref to War.objectives
self.minor_objective_id: str | None = minor_id # ref to War.objectives
self.influence_objective_id: str | None = influence_id # ref to War.objectives
self.mission: str | None = None
self.description: str | None = None