code quality (mypy+flake8)
This commit is contained in:
parent
7210ddc927
commit
55abdccc64
19 changed files with 778 additions and 497 deletions
|
|
@ -1,3 +1,4 @@
|
|||
from typing import Any, Dict, List
|
||||
from pathlib import Path
|
||||
import json
|
||||
import shutil
|
||||
|
|
@ -10,25 +11,25 @@ from warchron.model.round import Round, Choice, Battle
|
|||
|
||||
|
||||
class Model:
|
||||
def __init__(self):
|
||||
self.players: dict[str, Player] = {}
|
||||
self.wars: dict[str, War] = {}
|
||||
def __init__(self) -> None:
|
||||
self.players: Dict[str, Player] = {}
|
||||
self.wars: Dict[str, War] = {}
|
||||
|
||||
# File management methods
|
||||
|
||||
def new(self):
|
||||
def new(self) -> None:
|
||||
self.players.clear()
|
||||
self.wars.clear()
|
||||
|
||||
def load(self, path: Path):
|
||||
def load(self, path: Path) -> None:
|
||||
self.players.clear()
|
||||
self.wars.clear()
|
||||
self._load_data(path)
|
||||
|
||||
def save(self, path: Path):
|
||||
def save(self, path: Path) -> None:
|
||||
self._save_data(path)
|
||||
|
||||
def _load_data(self, path: Path):
|
||||
def _load_data(self, path: Path) -> None:
|
||||
if not path.exists() or path.stat().st_size == 0:
|
||||
return # Start empty
|
||||
try:
|
||||
|
|
@ -45,7 +46,7 @@ class Model:
|
|||
except json.JSONDecodeError:
|
||||
raise RuntimeError("Data file is corrupted")
|
||||
|
||||
def _save_data(self, path: Path):
|
||||
def _save_data(self, path: Path) -> None:
|
||||
if path.exists():
|
||||
shutil.copy(path, path.with_suffix(".json.bak"))
|
||||
data = {
|
||||
|
|
@ -58,31 +59,31 @@ class Model:
|
|||
|
||||
# Player methods
|
||||
|
||||
def add_player(self, name):
|
||||
def add_player(self, name: str) -> Player:
|
||||
player = Player(name)
|
||||
self.players[player.id] = player
|
||||
return player
|
||||
|
||||
def get_player(self, id):
|
||||
def get_player(self, id: str) -> Player:
|
||||
return self.players[id]
|
||||
|
||||
def get_player_name(self, player_id: str) -> str:
|
||||
return self.players[player_id].name
|
||||
|
||||
def update_player(self, player_id: str, *, name: str):
|
||||
def update_player(self, player_id: str, *, name: str) -> None:
|
||||
player = self.get_player(player_id)
|
||||
player.set_name(name)
|
||||
|
||||
def get_all_players(self) -> list[Player]:
|
||||
def get_all_players(self) -> List[Player]:
|
||||
return list(self.players.values())
|
||||
|
||||
def remove_player(self, player_id: str):
|
||||
def remove_player(self, player_id: str) -> None:
|
||||
# TODO manage war_participants referring to it
|
||||
del self.players[player_id]
|
||||
|
||||
# War methods
|
||||
|
||||
def get_default_war_values(self) -> dict:
|
||||
def get_default_war_values(self) -> Dict[str, Any]:
|
||||
return {"year": datetime.now().year}
|
||||
|
||||
def add_war(self, name: str, year: int) -> War:
|
||||
|
|
@ -90,7 +91,7 @@ class Model:
|
|||
self.wars[war.id] = war
|
||||
return war
|
||||
|
||||
def get_war(self, id) -> War:
|
||||
def get_war(self, id: str) -> War:
|
||||
return self.wars[id]
|
||||
|
||||
def get_war_by_campaign(self, campaign_id: str) -> War:
|
||||
|
|
@ -100,20 +101,20 @@ class Model:
|
|||
return war
|
||||
raise KeyError(f"Campaign {campaign_id} not found in any War")
|
||||
|
||||
def get_war_by_sector(self, sector_id: str) -> Campaign:
|
||||
def get_war_by_sector(self, sector_id: str) -> War:
|
||||
for war in self.wars.values():
|
||||
for camp in war.campaigns:
|
||||
for sect in camp.sectors.values():
|
||||
if sect.id == sector_id:
|
||||
return camp
|
||||
return war
|
||||
raise KeyError(f"Sector {sector_id} not found in any War")
|
||||
|
||||
def get_war_by_round(self, round_id: str) -> Campaign:
|
||||
def get_war_by_round(self, round_id: str) -> War:
|
||||
for war in self.wars.values():
|
||||
for camp in war.campaigns:
|
||||
for rnd in camp.rounds:
|
||||
if rnd.id == round_id:
|
||||
return camp
|
||||
return war
|
||||
raise KeyError(f"Round {round_id} not found in any War")
|
||||
|
||||
def get_war_by_objective(self, objective_id: str) -> War:
|
||||
|
|
@ -136,15 +137,15 @@ class Model:
|
|||
return war
|
||||
raise KeyError(f"Participant {participant_id} not found")
|
||||
|
||||
def update_war(self, war_id: str, *, name: str, year: int):
|
||||
def update_war(self, war_id: str, *, name: str, year: int) -> None:
|
||||
war = self.get_war(war_id)
|
||||
war.set_name(name)
|
||||
war.set_year(year)
|
||||
|
||||
def get_all_wars(self) -> list[War]:
|
||||
def get_all_wars(self) -> List[War]:
|
||||
return list(self.wars.values())
|
||||
|
||||
def remove_war(self, war_id: str):
|
||||
def remove_war(self, war_id: str) -> None:
|
||||
del self.wars[war_id]
|
||||
|
||||
# Objective methods
|
||||
|
|
@ -153,24 +154,26 @@ class Model:
|
|||
war = self.get_war(war_id)
|
||||
return war.add_objective(name, description)
|
||||
|
||||
def get_objective(self, objective_id) -> Objective:
|
||||
def get_objective(self, objective_id: str) -> Objective:
|
||||
for war in self.wars.values():
|
||||
for obj in war.objectives.values():
|
||||
if obj.id == objective_id:
|
||||
return obj
|
||||
raise KeyError("Objective not found")
|
||||
|
||||
def update_objective(self, objective_id: str, *, name: str, description: str):
|
||||
def update_objective(
|
||||
self, objective_id: str, *, name: str, description: str
|
||||
) -> None:
|
||||
war = self.get_war_by_objective(objective_id)
|
||||
war.update_objective(objective_id, name=name, description=description)
|
||||
|
||||
def remove_objective(self, objective_id: str):
|
||||
def remove_objective(self, objective_id: str) -> None:
|
||||
war = self.get_war_by_objective(objective_id)
|
||||
war.remove_objective(objective_id)
|
||||
|
||||
# War participant methods
|
||||
|
||||
def get_available_players(self, war_id: str) -> list[Player]:
|
||||
def get_available_players(self, war_id: str) -> List[Player]:
|
||||
war = self.get_war(war_id)
|
||||
return [
|
||||
player for player in self.players.values() if not war.has_player(player.id)
|
||||
|
|
@ -182,7 +185,7 @@ class Model:
|
|||
war = self.get_war(war_id)
|
||||
return war.add_war_participant(player_id, faction)
|
||||
|
||||
def get_war_participant(self, participant_id) -> WarParticipant:
|
||||
def get_war_participant(self, participant_id: str) -> WarParticipant:
|
||||
for war in self.wars.values():
|
||||
for part in war.participants.values():
|
||||
if part.id == participant_id:
|
||||
|
|
@ -192,17 +195,17 @@ class Model:
|
|||
def get_player_from_war_participant(self, war_part: WarParticipant) -> Player:
|
||||
return self.get_player(war_part.player_id)
|
||||
|
||||
def update_war_participant(self, participant_id: str, *, faction: str):
|
||||
def update_war_participant(self, participant_id: str, *, faction: str) -> None:
|
||||
war = self.get_war_by_war_participant(participant_id)
|
||||
war.update_war_participant(participant_id, faction=faction)
|
||||
|
||||
def remove_war_participant(self, participant_id: str):
|
||||
def remove_war_participant(self, participant_id: str) -> None:
|
||||
war = self.get_war_by_war_participant(participant_id)
|
||||
war.remove_war_participant(participant_id)
|
||||
|
||||
# Campaign methods
|
||||
|
||||
def get_default_campaign_values(self, war_id: str) -> dict:
|
||||
def get_default_campaign_values(self, war_id: str) -> Dict[str, Any]:
|
||||
war = self.get_war(war_id)
|
||||
return war.get_default_campaign_values()
|
||||
|
||||
|
|
@ -210,7 +213,7 @@ class Model:
|
|||
war = self.get_war(war_id)
|
||||
return war.add_campaign(name, month)
|
||||
|
||||
def get_campaign(self, campaign_id) -> Campaign:
|
||||
def get_campaign(self, campaign_id: str) -> Campaign:
|
||||
for war in self.wars.values():
|
||||
for campaign in war.campaigns:
|
||||
if campaign.id == campaign_id:
|
||||
|
|
@ -238,11 +241,11 @@ class Model:
|
|||
return camp
|
||||
raise KeyError(f"Sector {sector_id} not found")
|
||||
|
||||
def update_campaign(self, campaign_id: str, *, name: str, month: int):
|
||||
def update_campaign(self, campaign_id: str, *, name: str, month: int) -> None:
|
||||
war = self.get_war_by_campaign(campaign_id)
|
||||
war.update_campaign(campaign_id, name=name, month=month)
|
||||
|
||||
def remove_campaign(self, campaign_id: str):
|
||||
def remove_campaign(self, campaign_id: str) -> None:
|
||||
war = self.get_war_by_campaign(campaign_id)
|
||||
war.remove_campaign(campaign_id)
|
||||
|
||||
|
|
@ -260,7 +263,7 @@ class Model:
|
|||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_sector(name, round_id, major_id, minor_id, influence_id)
|
||||
|
||||
def get_sector(self, sector_id) -> Sector:
|
||||
def get_sector(self, sector_id: str) -> Sector:
|
||||
for war in self.wars.values():
|
||||
for camp in war.campaigns:
|
||||
for sect in camp.sectors.values():
|
||||
|
|
@ -277,7 +280,7 @@ class Model:
|
|||
major_id: str,
|
||||
minor_id: str,
|
||||
influence_id: str,
|
||||
):
|
||||
) -> None:
|
||||
war = self.get_war_by_sector(sector_id)
|
||||
war.update_sector(
|
||||
sector_id,
|
||||
|
|
@ -288,13 +291,13 @@ class Model:
|
|||
influence_id=influence_id,
|
||||
)
|
||||
|
||||
def remove_sector(self, sector_id: str):
|
||||
def remove_sector(self, sector_id: str) -> None:
|
||||
camp = self.get_campaign_by_sector(sector_id)
|
||||
camp.remove_sector(sector_id)
|
||||
|
||||
# Campaign participant methods
|
||||
|
||||
def get_available_war_participants(self, campaign_id: str) -> list[WarParticipant]:
|
||||
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)
|
||||
|
||||
|
|
@ -309,7 +312,7 @@ class Model:
|
|||
war_part = war.get_war_participant(participant_id)
|
||||
return self.players[war_part.player_id].name
|
||||
|
||||
def get_campaign_participant(self, participant_id) -> CampaignParticipant:
|
||||
def get_campaign_participant(self, participant_id: str) -> CampaignParticipant:
|
||||
for war in self.wars.values():
|
||||
for camp in war.campaigns:
|
||||
for part in camp.participants.values():
|
||||
|
|
@ -329,11 +332,11 @@ class Model:
|
|||
*,
|
||||
leader: str,
|
||||
theme: str,
|
||||
):
|
||||
) -> None:
|
||||
war = self.get_war_by_campaign_participant(participant_id)
|
||||
war.update_campaign_participant(participant_id, leader=leader, theme=theme)
|
||||
|
||||
def remove_campaign_participant(self, participant_id: str):
|
||||
def remove_campaign_participant(self, participant_id: str) -> None:
|
||||
war = self.get_war_by_campaign_participant(participant_id)
|
||||
war.remove_campaign_participant(participant_id)
|
||||
|
||||
|
|
@ -355,15 +358,15 @@ class Model:
|
|||
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]:
|
||||
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]
|
||||
|
||||
def get_round_participants(self, round_id: str) -> list[CampaignParticipant]:
|
||||
def get_round_participants(self, round_id: str) -> List[CampaignParticipant]:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
return list(camp.participants.values())
|
||||
|
||||
def remove_round(self, round_id: str):
|
||||
def remove_round(self, round_id: str) -> None:
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.remove_round(round_id)
|
||||
|
||||
|
|
@ -373,13 +376,6 @@ class Model:
|
|||
war = self.get_war_by_round(round_id)
|
||||
return war.create_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]
|
||||
return camp, rnd, participants, sectors
|
||||
|
||||
def update_choice(
|
||||
self,
|
||||
round_id: str,
|
||||
|
|
@ -387,13 +383,13 @@ class Model:
|
|||
priority_sector_id: str | None,
|
||||
secondary_sector_id: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> 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):
|
||||
def remove_choice(self, round_id: str, participant_id: str) -> None:
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.remove_choice(round_id, participant_id)
|
||||
|
||||
|
|
@ -413,7 +409,7 @@ class Model:
|
|||
score: str | None,
|
||||
victory_condition: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> None:
|
||||
war = self.get_war_by_round(round_id)
|
||||
war.update_battle(
|
||||
round_id,
|
||||
|
|
@ -426,6 +422,6 @@ class Model:
|
|||
comment,
|
||||
)
|
||||
|
||||
def remove_battle(self, round_id: str, sector_id: str):
|
||||
def remove_battle(self, round_id: str, sector_id: str) -> None:
|
||||
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