add/edit/delet objectives and participants in war

This commit is contained in:
Maxime Réaux 2026-01-28 16:25:40 +01:00
parent e33dec40be
commit 495a5adb98
14 changed files with 1088 additions and 322 deletions

View file

@ -1,27 +1,30 @@
from __future__ import annotations
from uuid import uuid4
from datetime import datetime
from warchron.model.campaign import Campaign
class War:
def __init__(self, name, year):
self.id = str(uuid4())
self.name = name
self.year = year
self.entrants = {}
self.campaigns = []
self.is_over = False
def set_id(self, new_id):
class War:
def __init__(self, name: str, year: int):
self.id: str = str(uuid4())
self.name: str = name
self.year: int = year
self.participants: dict[str, WarParticipant] = {}
self.objectives: dict[str, Objective] = {}
self.campaigns = []
self.is_over: bool = False
def set_id(self, new_id: str):
self.id = new_id
def set_name(self, new_name):
def set_name(self, new_name: str):
self.name = new_name
def set_year(self, new_year):
def set_year(self, new_year: int):
self.year = new_year
def set_state(self, new_state):
def set_state(self, new_state: bool):
self.is_over = new_state
def toDict(self):
@ -29,7 +32,7 @@ class War:
"id" : self.id,
"name" : self.name,
"year" : self.year,
# "entrants" : self.entrants,
# "participants" : self.participants,
"campaigns": [camp.toDict() for camp in self.campaigns],
"is_over": self.is_over
}
@ -38,12 +41,66 @@ class War:
def fromDict(data: dict):
war = War(name=data["name"], year=data["year"])
war.set_id(data["id"])
# war.entrants = data.get("entrants", {})
# war.participants = data.get("participants", {})
for camp_data in data.get("campaigns", []):
war.campaigns.append(Campaign.fromDict(camp_data))
war.set_state(data.get("is_over", False))
return war
# Objective methods
def add_objective(self, name: str, description: str) -> Objective:
objective = Objective(name, description)
self.objectives[objective.id] = objective
return objective
def get_objective(self, id: str) -> Objective:
return self.objectives[id]
def get_all_objectives(self) -> list[Objective]:
return list(self.objectives.values())
def update_objective(self, objective_id: str, *, name: str, description: str):
obj = self.get_objective(objective_id)
obj.set_name(name)
obj.set_description(description)
def remove_objective(self, objective_id: str):
del self.objectives[objective_id]
# War participant methods
def get_all_war_participants_ids(self) -> set[str]:
return set(self.participants.keys())
def has_participant(self, player_id: str) -> bool:
return player_id in self.participants
def add_war_participant(self, player_id: str, faction: str) -> WarParticipant:
if player_id in self.participants:
raise ValueError("Player already registered in this war")
participant = WarParticipant(player_id, faction)
self.participants[participant.id] = participant
return participant
def get_war_participant(self, id: str) -> WarParticipant:
return self.participants[id]
def get_all_war_participants(self) -> list[WarParticipant]:
return list(self.participants.values())
def update_war_participant(self, player_id: str, *, faction: str):
part = self.get_war_participant(player_id)
part.set_faction(faction)
def remove_war_participant(self, player_id: str):
del self.participants[player_id]
# Campaign methods
def has_campaign(self, campaign_id: str) -> bool:
return any(c.id == campaign_id for c in self.campaigns)
def get_default_campaign_values(self) -> dict:
return {
"month": datetime.now().month
@ -56,7 +113,7 @@ class War:
self.campaigns.append(campaign)
return campaign
def get_campaign(self, campaign_id) -> Campaign:
def get_campaign(self, campaign_id: str) -> Campaign:
for camp in self.campaigns:
if camp.id == campaign_id:
return camp
@ -79,4 +136,31 @@ class War:
def remove_campaign(self, campaign_id: str):
camp = self.get_campaign(campaign_id)
self.campaigns.remove(camp)
self.campaigns.remove(camp)
class Objective:
def __init__(self, name: str, description: str):
self.id: str = str(uuid4())
self.name: str = name
self.description: str = description
def set_id(self, new_id: str):
self.id = new_id
def set_name(self, new_name: str):
self.name = new_name
def set_description(self, new_description: str):
self.description = new_description
class WarParticipant:
def __init__(self, player_id: str, faction: str):
self.id: str = player_id # ref to Model.players
self.faction: str = faction
def set_id(self, new_id: str):
self.id = new_id
def set_faction(self, new_faction: str):
self.faction = new_faction