refacto update ; pretify code
This commit is contained in:
parent
6bd3ee31dc
commit
fbb1c913ba
11 changed files with 594 additions and 310 deletions
|
|
@ -161,6 +161,15 @@ class Campaign:
|
|||
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):
|
||||
rnd = self.get_round(round_id)
|
||||
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)
|
||||
|
|
@ -170,6 +179,18 @@ class Campaign:
|
|||
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):
|
||||
rnd = self.get_round(round_id)
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
from uuid import uuid4
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, name ):
|
||||
def __init__(self, name):
|
||||
self.id = str(uuid4())
|
||||
self.name = name
|
||||
|
||||
|
||||
def set_id(self, new_id):
|
||||
self.id = new_id
|
||||
|
||||
|
|
@ -12,13 +13,10 @@ class Player:
|
|||
self.name = name
|
||||
|
||||
def toDict(self):
|
||||
return {
|
||||
"id" : self.id,
|
||||
"name" : self.name
|
||||
}
|
||||
|
||||
return {"id": self.id, "name": self.name}
|
||||
|
||||
@staticmethod
|
||||
def fromDict(data: dict):
|
||||
play = Player(name=data["name"])
|
||||
play.set_id(data["id"])
|
||||
return play
|
||||
return play
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from pathlib import Path
|
|||
|
||||
DATA_FILE = Path("data/warmachron.json")
|
||||
|
||||
|
||||
def load_data():
|
||||
if not DATA_FILE.exists() or DATA_FILE.stat().st_size == 0:
|
||||
return {"version": 1, "players": {}, "wars": []}
|
||||
|
|
@ -14,6 +15,7 @@ def load_data():
|
|||
except json.JSONDecodeError:
|
||||
raise RuntimeError("Data file is corrupted")
|
||||
|
||||
|
||||
def save_data(data):
|
||||
if DATA_FILE.exists():
|
||||
shutil.copy(DATA_FILE, DATA_FILE.with_suffix(".json.bak"))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import annotations
|
||||
from uuid import uuid4
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class Round:
|
||||
def __init__(self):
|
||||
|
|
@ -20,7 +21,7 @@ class Round:
|
|||
# "sectors" : self.sectors,
|
||||
# "choices" : self.choices,
|
||||
# "battles" : self.battles,
|
||||
"is_over": self.is_over
|
||||
"is_over": self.is_over,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -32,47 +33,58 @@ class Round:
|
|||
# rnd.battles = data.get("battles", {})
|
||||
rnd.set_state(data.get("is_over", False))
|
||||
return rnd
|
||||
|
||||
# Choice methods
|
||||
|
||||
# Choice methods
|
||||
|
||||
def get_choice(self, participant_id: str) -> Choice | None:
|
||||
return self.choices.get(participant_id)
|
||||
|
||||
|
||||
def create_choice(self, participant_id: str) -> Choice:
|
||||
if participant_id not in self.choices:
|
||||
choice = Choice(
|
||||
participant_id = participant_id,
|
||||
priority_sector_id = None,
|
||||
secondary_sector_id = None
|
||||
participant_id=participant_id,
|
||||
priority_sector_id=None,
|
||||
secondary_sector_id=None,
|
||||
)
|
||||
self.choices[participant_id] = choice
|
||||
return self.choices[participant_id]
|
||||
|
||||
def update_choice(self, participant_id: str, priority_sector_id: str | None, secondary_sector_id: str | None, comment: str | None):
|
||||
def update_choice(
|
||||
self,
|
||||
participant_id: str,
|
||||
priority_sector_id: str | None,
|
||||
secondary_sector_id: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
choice = self.get_choice(participant_id)
|
||||
choice.set_priority(priority_sector_id)
|
||||
choice.set_secondary(secondary_sector_id)
|
||||
choice.set_comment(comment)
|
||||
|
||||
def remove_choice(self,participant_id: str):
|
||||
def remove_choice(self, participant_id: str):
|
||||
del self.choices[participant_id]
|
||||
|
||||
# Battle methods
|
||||
# Battle methods
|
||||
|
||||
def get_battle(self, sector_id: str) -> Battle | None:
|
||||
return self.battles.get(sector_id)
|
||||
|
||||
|
||||
def create_battle(self, sector_id: str) -> Battle:
|
||||
if sector_id not in self.battles:
|
||||
battle = Battle(
|
||||
sector_id = sector_id,
|
||||
player_1_id = None,
|
||||
player_2_id = None
|
||||
)
|
||||
battle = Battle(sector_id=sector_id, player_1_id=None, player_2_id=None)
|
||||
self.battles[sector_id] = battle
|
||||
return self.battles[sector_id]
|
||||
|
||||
def update_battle(self, 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,
|
||||
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,
|
||||
):
|
||||
bat = self.get_battle(sector_id)
|
||||
bat.set_player_1(player_1_id)
|
||||
bat.set_player_2(player_2_id)
|
||||
|
|
@ -81,14 +93,24 @@ class Round:
|
|||
bat.set_victory_condition(victory_condition)
|
||||
bat.set_comment(comment)
|
||||
|
||||
def remove_battle(self,sector_id: str):
|
||||
def remove_battle(self, sector_id: str):
|
||||
del self.battles[sector_id]
|
||||
|
||||
|
||||
class Choice:
|
||||
def __init__(self, participant_id: str, priority_sector_id: str | None = None, secondary_sector_id: str | None = None):
|
||||
self.participant_id: str = participant_id # ref to Campaign.participants
|
||||
self.priority_sector_id: str | None = priority_sector_id # ref to Campaign.sectors
|
||||
self.secondary_sector_id: str | None = secondary_sector_id # ref to Campaign.sectors
|
||||
def __init__(
|
||||
self,
|
||||
participant_id: str,
|
||||
priority_sector_id: str | None = None,
|
||||
secondary_sector_id: str | None = None,
|
||||
):
|
||||
self.participant_id: str = participant_id # ref to Campaign.participants
|
||||
self.priority_sector_id: str | None = (
|
||||
priority_sector_id # ref to Campaign.sectors
|
||||
)
|
||||
self.secondary_sector_id: str | None = (
|
||||
secondary_sector_id # ref to Campaign.sectors
|
||||
)
|
||||
self.comment: str | None = None
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
|
|
@ -103,11 +125,17 @@ class Choice:
|
|||
def set_comment(self, new_comment: str):
|
||||
self.comment = new_comment
|
||||
|
||||
|
||||
class Battle:
|
||||
def __init__(self, sector_id: str, player_1_id: str | None = None, player_2_id: str | None = None):
|
||||
self.sector_id: str = sector_id # ref to Campaign.sector
|
||||
self.player_1_id: str | None = player_1_id # ref to Campaign.participants
|
||||
self.player_2_id: str | None = player_2_id # ref to Campaign.participants
|
||||
def __init__(
|
||||
self,
|
||||
sector_id: str,
|
||||
player_1_id: str | None = None,
|
||||
player_2_id: str | None = None,
|
||||
):
|
||||
self.sector_id: str = sector_id # ref to Campaign.sector
|
||||
self.player_1_id: str | None = player_1_id # ref to Campaign.participants
|
||||
self.player_2_id: str | None = player_2_id # ref to Campaign.participants
|
||||
self.winner_id: str | None = None
|
||||
self.score: str | None = None
|
||||
self.victory_condition: str | None = None
|
||||
|
|
@ -121,7 +149,7 @@ class Battle:
|
|||
|
||||
def set_player_2(self, new_player_id: str):
|
||||
self.player_2_id = new_player_id
|
||||
|
||||
|
||||
def set_winner(self, new_player_id: str):
|
||||
self.winner_id = new_player_id
|
||||
|
||||
|
|
@ -132,4 +160,4 @@ class Battle:
|
|||
self.victory_condition = new_victory_condition
|
||||
|
||||
def set_comment(self, new_comment: str):
|
||||
self.comment = new_comment
|
||||
self.comment = new_comment
|
||||
|
|
|
|||
|
|
@ -213,6 +213,10 @@ class War:
|
|||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_round()
|
||||
|
||||
def add_battle(self, campaign_id: str) -> Round:
|
||||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_round()
|
||||
|
||||
def remove_round(self, round_id: str):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.remove_round(round_id)
|
||||
|
|
@ -222,6 +226,15 @@ class War:
|
|||
def create_choice(self, round_id: str, participant_id: str) -> Choice:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
return camp.create_choice(round_id, 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):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.update_choice(participant_id, priority_sector_id, secondary_sector_id, comment)
|
||||
|
||||
def remove_choice(self, round_id: str, participant_id: str):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
|
|
@ -233,6 +246,18 @@ class War:
|
|||
camp = self.get_campaign_by_round(round_id)
|
||||
return camp.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):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.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):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.remove_battle(round_id, sector_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue