refacto update ; pretify code
This commit is contained in:
parent
6bd3ee31dc
commit
fbb1c913ba
11 changed files with 594 additions and 310 deletions
|
|
@ -8,12 +8,13 @@ from warchron.model.war import War, Objective, WarParticipant
|
|||
from warchron.model.campaign import Campaign, Sector, CampaignParticipant
|
||||
from warchron.model.round import Round, Choice, Battle
|
||||
|
||||
|
||||
class Model:
|
||||
def __init__(self):
|
||||
self.players: dict[str, Player] = {}
|
||||
self.players: dict[str, Player] = {}
|
||||
self.wars: dict[str, War] = {}
|
||||
|
||||
# File management methods
|
||||
# File management methods
|
||||
|
||||
def new(self):
|
||||
self.players.clear()
|
||||
|
|
@ -23,13 +24,13 @@ class Model:
|
|||
self.players.clear()
|
||||
self.wars.clear()
|
||||
self._load_data(path)
|
||||
|
||||
|
||||
def save(self, path: Path):
|
||||
self._save_data(path)
|
||||
|
||||
|
||||
def _load_data(self, path: Path):
|
||||
if not path.exists() or path.stat().st_size == 0:
|
||||
return # Start empty
|
||||
return # Start empty
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
|
@ -40,7 +41,7 @@ class Model:
|
|||
self.players[player.id] = player
|
||||
for w in data.get("wars", []):
|
||||
war = War.fromDict(w)
|
||||
self.wars[war.id] = war
|
||||
self.wars[war.id] = war
|
||||
except json.JSONDecodeError:
|
||||
raise RuntimeError("Data file is corrupted")
|
||||
|
||||
|
|
@ -50,12 +51,12 @@ class Model:
|
|||
data = {
|
||||
"version": "1.0",
|
||||
"players": [p.toDict() for p in self.players.values()],
|
||||
"wars": [w.toDict() for w in self.wars.values()]
|
||||
"wars": [w.toDict() for w in self.wars.values()],
|
||||
}
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
# Player methods
|
||||
# Player methods
|
||||
|
||||
def add_player(self, name):
|
||||
player = Player(name)
|
||||
|
|
@ -64,7 +65,7 @@ class Model:
|
|||
|
||||
def get_player(self, id):
|
||||
return self.players[id]
|
||||
|
||||
|
||||
def get_player_name(self, player_id: str) -> str:
|
||||
return self.players[player_id].name
|
||||
|
||||
|
|
@ -79,12 +80,10 @@ class Model:
|
|||
# TODO manage war_participants referring to it
|
||||
del self.players[player_id]
|
||||
|
||||
# War methods
|
||||
# War methods
|
||||
|
||||
def get_default_war_values(self) -> dict:
|
||||
return {
|
||||
"year": datetime.now().year
|
||||
}
|
||||
return {"year": datetime.now().year}
|
||||
|
||||
def add_war(self, name: str, year: int) -> War:
|
||||
war = War(name, year)
|
||||
|
|
@ -93,7 +92,7 @@ class Model:
|
|||
|
||||
def get_war(self, id) -> War:
|
||||
return self.wars[id]
|
||||
|
||||
|
||||
def get_war_by_campaign(self, campaign_id: str) -> War:
|
||||
for war in self.wars.values():
|
||||
for camp in war.campaigns:
|
||||
|
|
@ -143,7 +142,7 @@ class Model:
|
|||
def remove_war(self, war_id: str):
|
||||
del self.wars[war_id]
|
||||
|
||||
# Objective methods
|
||||
# Objective methods
|
||||
|
||||
def add_objective(self, war_id: str, name: str, description: str) -> Objective:
|
||||
war = self.get_war(war_id)
|
||||
|
|
@ -164,7 +163,7 @@ class Model:
|
|||
war = self.get_war_by_objective(objective_id)
|
||||
war.remove_objective(objective_id)
|
||||
|
||||
# War participant methods
|
||||
# War participant methods
|
||||
|
||||
def get_available_players(self, war_id: str) -> list[Player]:
|
||||
war = self.get_war(war_id)
|
||||
|
|
@ -174,7 +173,9 @@ class Model:
|
|||
if not war.has_participant(player.id)
|
||||
]
|
||||
|
||||
def add_war_participant(self, war_id: str, player_id: str, faction: str) -> WarParticipant:
|
||||
def add_war_participant(
|
||||
self, war_id: str, player_id: str, faction: str
|
||||
) -> WarParticipant:
|
||||
war = self.get_war(war_id)
|
||||
return war.add_war_participant(player_id, faction)
|
||||
|
||||
|
|
@ -193,7 +194,7 @@ class Model:
|
|||
war = self.get_war_by_war_participant(participant_id)
|
||||
war.remove_war_participant(participant_id)
|
||||
|
||||
# Campaign methods
|
||||
# Campaign methods
|
||||
|
||||
def get_default_campaign_values(self, war_id: str) -> dict:
|
||||
war = self.get_war(war_id)
|
||||
|
|
@ -240,9 +241,17 @@ class Model:
|
|||
war = self.get_war_by_campaign(campaign_id)
|
||||
war.remove_campaign(campaign_id)
|
||||
|
||||
# Sector methods
|
||||
# Sector methods
|
||||
|
||||
def add_sector(self, campaign_id: str, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str) -> Sector:
|
||||
def add_sector(
|
||||
self,
|
||||
campaign_id: str,
|
||||
name: str,
|
||||
round_id: str,
|
||||
major_id: str,
|
||||
minor_id: str,
|
||||
influence_id: str,
|
||||
) -> Sector:
|
||||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_sector(name, round_id, major_id, minor_id, influence_id)
|
||||
|
||||
|
|
@ -254,21 +263,39 @@ class Model:
|
|||
return sect
|
||||
raise KeyError("Sector not found")
|
||||
|
||||
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,
|
||||
):
|
||||
war = self.get_war_by_sector(sector_id)
|
||||
war.update_sector(sector_id, name=name, round_id=round_id, major_id=major_id, minor_id=minor_id, influence_id=influence_id)
|
||||
war.update_sector(
|
||||
sector_id,
|
||||
name=name,
|
||||
round_id=round_id,
|
||||
major_id=major_id,
|
||||
minor_id=minor_id,
|
||||
influence_id=influence_id,
|
||||
)
|
||||
|
||||
def remove_sector(self, sector_id: str):
|
||||
camp = self.get_campaign_by_sector(sector_id)
|
||||
camp.remove_sector(sector_id)
|
||||
|
||||
# Campaign participant methods
|
||||
# Campaign participant methods
|
||||
|
||||
def get_available_war_participants(self, campaign_id: str) -> list[WarParticipant]:
|
||||
war = self.get_war_by_campaign(campaign_id)
|
||||
return war.get_available_war_participants(campaign_id)
|
||||
|
||||
def add_campaign_participant(self, camp_id: str, player_id: str, leader: str, theme: str) -> CampaignParticipant:
|
||||
def add_campaign_participant(
|
||||
self, camp_id: str, player_id: str, leader: str, theme: str
|
||||
) -> CampaignParticipant:
|
||||
camp = self.get_campaign(camp_id)
|
||||
return camp.add_campaign_participant(player_id, leader, theme)
|
||||
|
||||
|
|
@ -280,7 +307,9 @@ class Model:
|
|||
return part
|
||||
raise KeyError("Participant not found")
|
||||
|
||||
def update_campaign_participant(self, participant_id: str, *, leader: str, theme: str):
|
||||
def update_campaign_participant(
|
||||
self, participant_id: str, *, leader: str, theme: str
|
||||
):
|
||||
camp = self.get_campaign_by_campaign_participant(participant_id)
|
||||
camp.update_campaign_participant(participant_id, leader=leader, theme=theme)
|
||||
|
||||
|
|
@ -288,12 +317,12 @@ class Model:
|
|||
camp = self.get_campaign_by_campaign_participant(participant_id)
|
||||
camp.remove_campaign_participant(participant_id)
|
||||
|
||||
# Round methods
|
||||
# Round methods
|
||||
|
||||
def add_round(self, campaign_id: str) -> Round:
|
||||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_round()
|
||||
|
||||
|
||||
def get_round(self, round_id: str) -> Round:
|
||||
for war in self.wars.values():
|
||||
for camp in war.campaigns:
|
||||
|
|
@ -305,7 +334,7 @@ class Model:
|
|||
def get_round_index(self, round_id: str) -> int:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
return camp.get_round_index(round_id)
|
||||
|
||||
|
||||
def get_round_sectors(self, round_id: str) -> list[Sector]:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
return [s for s in camp.sectors.values() if s.round_id == round_id]
|
||||
|
|
@ -318,33 +347,65 @@ class Model:
|
|||
war = self.get_war_by_round(round_id)
|
||||
war.remove_round(round_id)
|
||||
|
||||
# Choice methods
|
||||
# Choice methods
|
||||
|
||||
def create_choice(self, round_id: str, participant_id: str) -> Choice:
|
||||
war = self.get_war_by_round(round_id)
|
||||
return war.create_choice(round_id, participant_id)
|
||||
|
||||
def remove_choice(self, round_id: str, participant_id: str):
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.remove_choice(round_id, participant_id)
|
||||
|
||||
def get_round_choices_data(self, round_id: str):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
rnd = self.get_round(round_id)
|
||||
participants = camp.participants.values()
|
||||
sectors = [
|
||||
s for s in camp.sectors.values()
|
||||
if s.round_id == round_id
|
||||
]
|
||||
sectors = [s for s in camp.sectors.values() if s.round_id == round_id]
|
||||
return camp, rnd, participants, sectors
|
||||
|
||||
# Battle methods
|
||||
|
||||
def update_choice(
|
||||
self,
|
||||
round_id: str,
|
||||
participant_id: str,
|
||||
priority_sector_id: str | None,
|
||||
secondary_sector_id: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.update_choice(
|
||||
round_id, participant_id, priority_sector_id, secondary_sector_id, comment
|
||||
)
|
||||
|
||||
def remove_choice(self, round_id: str, participant_id: str):
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.remove_choice(round_id, participant_id)
|
||||
|
||||
# Battle methods
|
||||
|
||||
def create_battle(self, round_id: str, sector_id: str) -> Battle:
|
||||
war = self.get_war_by_round(round_id)
|
||||
return war.create_battle(round_id, 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,
|
||||
):
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.update_battle(
|
||||
round_id,
|
||||
sector_id,
|
||||
player_1_id,
|
||||
player_2_id,
|
||||
winner_id,
|
||||
score,
|
||||
victory_condition,
|
||||
comment,
|
||||
)
|
||||
|
||||
def remove_battle(self, round_id: str, sector_id: str):
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.remove_battle(round_id, sector_id)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue