warchron_app/src/warchron/model/war.py

82 lines
2.5 KiB
Python
Raw Normal View History

2026-01-19 18:55:07 +01:00
from uuid import uuid4
from datetime import datetime
from warchron.model.campaign import Campaign
class War:
2026-01-22 23:42:47 +01:00
def __init__(self, name, year):
2026-01-19 18:55:07 +01:00
self.id = str(uuid4())
self.name = name
2026-01-22 23:42:47 +01:00
self.year = year
2026-01-19 18:55:07 +01:00
self.entrants = {}
2026-01-21 07:43:04 +01:00
self.campaigns = []
2026-01-19 18:55:07 +01:00
self.is_over = False
def set_id(self, new_id):
self.id = new_id
def set_name(self, new_name):
self.name = new_name
def set_year(self, new_year):
self.year = new_year
def set_state(self, new_state):
self.is_over = new_state
def toDict(self):
return {
"id" : self.id,
"name" : self.name,
"year" : self.year,
2026-01-21 08:31:48 +01:00
# "entrants" : self.entrants,
"campaigns": [camp.toDict() for camp in self.campaigns],
2026-01-19 18:55:07 +01:00
"is_over": self.is_over
}
@staticmethod
2026-01-21 08:31:48 +01:00
def fromDict(data: dict):
2026-01-22 23:42:47 +01:00
war = War(name=data["name"], year=data["year"])
2026-01-21 08:31:48 +01:00
war.set_id(data["id"])
# war.entrants = data.get("entrants", {})
for camp_data in data.get("campaigns", []):
war.campaigns.append(Campaign.fromDict(camp_data))
war.set_state(data.get("is_over", False))
return war
2026-01-22 23:42:47 +01:00
def get_default_campaign_values(self) -> dict:
return {
"month": datetime.now().month
}
def add_campaign(self, name: str, month: int | None = None) -> Campaign:
if month is None:
month = self.get_default_campaign_values()["month"]
campaign = Campaign(name, month)
2026-01-21 07:43:04 +01:00
self.campaigns.append(campaign)
return campaign
def get_campaign(self, campaign_id) -> Campaign:
2026-01-22 23:42:47 +01:00
for camp in self.campaigns:
if camp.id == campaign_id:
return camp
raise KeyError(f"Campaign {campaign_id} not found in War {self.id}")
2026-01-21 07:43:04 +01:00
2026-01-22 23:42:47 +01:00
def get_campaign_by_round(self, round_id: str) -> Campaign:
for camp in self.campaigns:
for rnd in camp.rounds:
if rnd.id == round_id:
return camp
raise KeyError(f"Round {round_id} not found in any Campaign")
def update_campaign(self, campaign_id: str, *, name: str, month: int):
camp = self.get_campaign(campaign_id)
camp.set_name(name)
camp.set_month(month)
2026-01-21 07:43:04 +01:00
def get_all_campaigns(self) -> list[Campaign]:
return list(self.campaigns)
2026-01-22 23:42:47 +01:00
def remove_campaign(self, campaign_id: str):
camp = self.get_campaign(campaign_id)
self.campaigns.remove(camp)