code quality (mypy+flake8)
This commit is contained in:
parent
7210ddc927
commit
55abdccc64
19 changed files with 778 additions and 497 deletions
|
|
@ -1,32 +1,33 @@
|
|||
from __future__ import annotations
|
||||
from uuid import uuid4
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from warchron.model.round import Round, Choice, Battle
|
||||
|
||||
|
||||
class Campaign:
|
||||
def __init__(self, name: str, month: int):
|
||||
def __init__(self, name: str, month: int) -> None:
|
||||
self.id: str = str(uuid4())
|
||||
self.name: str = name
|
||||
self.month: int = month
|
||||
self.participants: dict[str, CampaignParticipant] = {}
|
||||
self.sectors: dict[str, Sector] = {}
|
||||
self.rounds: list[Round] = []
|
||||
self.participants: Dict[str, CampaignParticipant] = {}
|
||||
self.sectors: Dict[str, Sector] = {}
|
||||
self.rounds: List[Round] = []
|
||||
self.is_over = False
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name: str):
|
||||
def set_name(self, new_name: str) -> None:
|
||||
self.name = new_name
|
||||
|
||||
def set_month(self, new_month: int):
|
||||
def set_month(self, new_month: int) -> None:
|
||||
self.month = new_month
|
||||
|
||||
def set_state(self, new_state: bool):
|
||||
def set_state(self, new_state: bool) -> None:
|
||||
self.is_over = new_state
|
||||
|
||||
def toDict(self):
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
|
|
@ -37,7 +38,7 @@ class Campaign:
|
|||
}
|
||||
|
||||
@staticmethod
|
||||
def fromDict(data: dict):
|
||||
def fromDict(data: Dict[str, Any]) -> Campaign:
|
||||
camp = Campaign(name=data["name"], month=data["month"])
|
||||
camp.set_id(data["id"])
|
||||
# camp.participants = data.get("participants", {})
|
||||
|
|
@ -77,18 +78,18 @@ class Campaign:
|
|||
except KeyError:
|
||||
raise KeyError(f"Participant {participant_id} not in campaign {self.id}")
|
||||
|
||||
def get_all_campaign_participants(self) -> list[CampaignParticipant]:
|
||||
def get_all_campaign_participants(self) -> List[CampaignParticipant]:
|
||||
return list(self.participants.values())
|
||||
|
||||
def update_campaign_participant(
|
||||
self, participant_id: str, *, leader: str, theme: str
|
||||
):
|
||||
) -> None:
|
||||
part = self.get_campaign_participant(participant_id)
|
||||
# Can't change referred War.participant
|
||||
part.set_leader(leader)
|
||||
part.set_theme(theme)
|
||||
|
||||
def remove_campaign_participant(self, participant_id: str):
|
||||
def remove_campaign_participant(self, participant_id: str) -> None:
|
||||
# TODO manage choices referring to it
|
||||
# TODO manage battles referring to it
|
||||
del self.participants[participant_id]
|
||||
|
|
@ -110,7 +111,7 @@ class Campaign:
|
|||
return ""
|
||||
return self.sectors[sector_id].name
|
||||
|
||||
def get_all_sectors(self) -> list[Sector]:
|
||||
def get_all_sectors(self) -> List[Sector]:
|
||||
return list(self.sectors.values())
|
||||
|
||||
# TODO manage choices referring to it (round order!)
|
||||
|
|
@ -123,7 +124,7 @@ class Campaign:
|
|||
major_id: str,
|
||||
minor_id: str,
|
||||
influence_id: str,
|
||||
):
|
||||
) -> None:
|
||||
sect = self.get_sector(sector_id)
|
||||
sect.set_name(name)
|
||||
sect.set_round(round_id)
|
||||
|
|
@ -131,12 +132,12 @@ class Campaign:
|
|||
sect.set_minor(minor_id)
|
||||
sect.set_influence(influence_id)
|
||||
|
||||
def remove_sector(self, sector_id: str):
|
||||
def remove_sector(self, sector_id: str) -> None:
|
||||
# TODO manage choices referring to it
|
||||
# TODO manage battles referring to it
|
||||
del self.sectors[sector_id]
|
||||
|
||||
def get_sectors_in_round(self, round_id: str) -> list[Sector]:
|
||||
def get_sectors_in_round(self, round_id: str) -> List[Sector]:
|
||||
sectors = [s for s in self.sectors.values() if s.round_id == round_id]
|
||||
return sectors
|
||||
|
||||
|
|
@ -151,7 +152,7 @@ class Campaign:
|
|||
return rnd
|
||||
raise KeyError(f"Round {round_id} not found")
|
||||
|
||||
def get_all_rounds(self) -> list[Round]:
|
||||
def get_all_rounds(self) -> List[Round]:
|
||||
return list(self.rounds)
|
||||
|
||||
def add_round(self) -> Round:
|
||||
|
|
@ -159,7 +160,7 @@ class Campaign:
|
|||
self.rounds.append(round)
|
||||
return round
|
||||
|
||||
def remove_round(self, round_id: str):
|
||||
def remove_round(self, round_id: str) -> None:
|
||||
rnd = next((r for r in self.rounds if r.id == round_id), None)
|
||||
if rnd:
|
||||
self.rounds.remove(rnd)
|
||||
|
|
@ -172,14 +173,6 @@ class Campaign:
|
|||
return index
|
||||
raise KeyError("Round not found in campaign")
|
||||
|
||||
def get_round_name(self, round_id: str | None) -> str:
|
||||
if round_id is None:
|
||||
return ""
|
||||
for rnd in self.rounds:
|
||||
if rnd.id == round_id:
|
||||
return rnd.name
|
||||
return ""
|
||||
|
||||
# Choice methods
|
||||
|
||||
def create_choice(self, round_id: str, participant_id: str) -> Choice:
|
||||
|
|
@ -193,13 +186,13 @@ class Campaign:
|
|||
priority_sector_id: str | None,
|
||||
secondary_sector_id: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> 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:
|
||||
def remove_choice(self, round_id: str, participant_id: str) -> None:
|
||||
rnd = self.get_round(round_id)
|
||||
rnd.remove_choice(participant_id)
|
||||
|
||||
|
|
@ -219,7 +212,7 @@ class Campaign:
|
|||
score: str | None,
|
||||
victory_condition: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> None:
|
||||
rnd = self.get_round(round_id)
|
||||
rnd.update_battle(
|
||||
sector_id,
|
||||
|
|
@ -231,7 +224,7 @@ class Campaign:
|
|||
comment,
|
||||
)
|
||||
|
||||
def remove_battle(self, round_id: str, sector_id: str) -> Battle:
|
||||
def remove_battle(self, round_id: str, sector_id: str) -> None:
|
||||
rnd = self.get_round(round_id)
|
||||
rnd.remove_battle(sector_id)
|
||||
|
||||
|
|
@ -245,16 +238,16 @@ class CampaignParticipant:
|
|||
self.leader: str | None = leader
|
||||
self.theme: str | None = theme
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_war_participant(self, new_participant: str):
|
||||
def set_war_participant(self, new_participant: str) -> None:
|
||||
self.war_participant_id = new_participant
|
||||
|
||||
def set_leader(self, new_faction: str):
|
||||
def set_leader(self, new_faction: str) -> None:
|
||||
self.leader = new_faction
|
||||
|
||||
def set_theme(self, new_theme: str):
|
||||
def set_theme(self, new_theme: str) -> None:
|
||||
self.theme = new_theme
|
||||
|
||||
|
||||
|
|
@ -276,20 +269,20 @@ class Sector:
|
|||
self.mission: str | None = None
|
||||
self.description: str | None = None
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name: str):
|
||||
def set_name(self, new_name: str) -> None:
|
||||
self.name = new_name
|
||||
|
||||
def set_round(self, new_round_id: str):
|
||||
def set_round(self, new_round_id: str) -> None:
|
||||
self.round_id = new_round_id
|
||||
|
||||
def set_major(self, new_major_id: str):
|
||||
def set_major(self, new_major_id: str) -> None:
|
||||
self.major_objective_id = new_major_id
|
||||
|
||||
def set_minor(self, new_minor_id: str):
|
||||
def set_minor(self, new_minor_id: str) -> None:
|
||||
self.minor_objective_id = new_minor_id
|
||||
|
||||
def set_influence(self, new_influence_id: str):
|
||||
def set_influence(self, new_influence_id: str) -> None:
|
||||
self.influence_objective_id = new_influence_id
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
0
src/warchron/model/objective.py
Normal file
0
src/warchron/model/objective.py
Normal file
|
|
@ -1,22 +1,24 @@
|
|||
from __future__ import annotations
|
||||
from typing import Any, Dict
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, name):
|
||||
self.id = str(uuid4())
|
||||
self.name = name
|
||||
def __init__(self, name: str) -> None:
|
||||
self.id: str = str(uuid4())
|
||||
self.name: str = name
|
||||
|
||||
def set_id(self, new_id):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, name):
|
||||
def set_name(self, name: str) -> None:
|
||||
self.name = name
|
||||
|
||||
def toDict(self):
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
return {"id": self.id, "name": self.name}
|
||||
|
||||
@staticmethod
|
||||
def fromDict(data: dict):
|
||||
def fromDict(data: Dict[str, Any]) -> Player:
|
||||
play = Player(name=data["name"])
|
||||
play.set_id(data["id"])
|
||||
return play
|
||||
|
|
|
|||
|
|
@ -1,24 +0,0 @@
|
|||
import json
|
||||
import shutil
|
||||
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": []}
|
||||
|
||||
try:
|
||||
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
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"))
|
||||
|
||||
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
|
@ -1,21 +1,22 @@
|
|||
from __future__ import annotations
|
||||
from uuid import uuid4
|
||||
from typing import Any, Dict
|
||||
|
||||
|
||||
class Round:
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
self.id: str = str(uuid4())
|
||||
self.choices: dict[str, Choice] = {}
|
||||
self.battles: dict[str, Battle] = {}
|
||||
self.choices: Dict[str, Choice] = {}
|
||||
self.battles: Dict[str, Battle] = {}
|
||||
self.is_over: bool = False
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_state(self, new_state: bool):
|
||||
def set_state(self, new_state: bool) -> None:
|
||||
self.is_over = new_state
|
||||
|
||||
def toDict(self):
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"id": self.id,
|
||||
# "sectors" : self.sectors,
|
||||
|
|
@ -25,7 +26,7 @@ class Round:
|
|||
}
|
||||
|
||||
@staticmethod
|
||||
def fromDict(data: dict):
|
||||
def fromDict(data: Dict[str, Any]) -> Round:
|
||||
rnd = Round()
|
||||
rnd.set_id(data["id"])
|
||||
# rnd.sectors = data.get("sectors", {})
|
||||
|
|
@ -55,13 +56,14 @@ class Round:
|
|||
priority_sector_id: str | None,
|
||||
secondary_sector_id: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> None:
|
||||
choice = self.get_choice(participant_id)
|
||||
choice.set_priority(priority_sector_id)
|
||||
choice.set_secondary(secondary_sector_id)
|
||||
choice.set_comment(comment)
|
||||
if choice:
|
||||
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) -> None:
|
||||
del self.choices[participant_id]
|
||||
|
||||
# Battle methods
|
||||
|
|
@ -84,16 +86,17 @@ class Round:
|
|||
score: str | None,
|
||||
victory_condition: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> None:
|
||||
bat = self.get_battle(sector_id)
|
||||
bat.set_player_1(player_1_id)
|
||||
bat.set_player_2(player_2_id)
|
||||
bat.set_winner(winner_id)
|
||||
bat.set_score(score)
|
||||
bat.set_victory_condition(victory_condition)
|
||||
bat.set_comment(comment)
|
||||
if bat:
|
||||
bat.set_player_1(player_1_id)
|
||||
bat.set_player_2(player_2_id)
|
||||
bat.set_winner(winner_id)
|
||||
bat.set_score(score)
|
||||
bat.set_victory_condition(victory_condition)
|
||||
bat.set_comment(comment)
|
||||
|
||||
def remove_battle(self, sector_id: str):
|
||||
def remove_battle(self, sector_id: str) -> None:
|
||||
del self.battles[sector_id]
|
||||
|
||||
|
||||
|
|
@ -113,16 +116,16 @@ class Choice:
|
|||
)
|
||||
self.comment: str | None = None
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.participant_id = new_id
|
||||
|
||||
def set_priority(self, new_priority_id: str):
|
||||
def set_priority(self, new_priority_id: str | None) -> None:
|
||||
self.priority_sector_id = new_priority_id
|
||||
|
||||
def set_secondary(self, new_secondary_id: str):
|
||||
def set_secondary(self, new_secondary_id: str | None) -> None:
|
||||
self.secondary_sector_id = new_secondary_id
|
||||
|
||||
def set_comment(self, new_comment: str):
|
||||
def set_comment(self, new_comment: str | None) -> None:
|
||||
self.comment = new_comment
|
||||
|
||||
|
||||
|
|
@ -141,23 +144,23 @@ class Battle:
|
|||
self.victory_condition: str | None = None
|
||||
self.comment: str | None = None
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.sector_id = new_id
|
||||
|
||||
def set_player_1(self, new_player_id: str):
|
||||
def set_player_1(self, new_player_id: str | None) -> None:
|
||||
self.player_1_id = new_player_id
|
||||
|
||||
def set_player_2(self, new_player_id: str):
|
||||
def set_player_2(self, new_player_id: str | None) -> None:
|
||||
self.player_2_id = new_player_id
|
||||
|
||||
def set_winner(self, new_player_id: str):
|
||||
def set_winner(self, new_player_id: str | None) -> None:
|
||||
self.winner_id = new_player_id
|
||||
|
||||
def set_score(self, new_score: str):
|
||||
def set_score(self, new_score: str | None) -> None:
|
||||
self.score = new_score
|
||||
|
||||
def set_victory_condition(self, new_victory_condition: str):
|
||||
def set_victory_condition(self, new_victory_condition: str | None) -> None:
|
||||
self.victory_condition = new_victory_condition
|
||||
|
||||
def set_comment(self, new_comment: str):
|
||||
def set_comment(self, new_comment: str | None) -> None:
|
||||
self.comment = new_comment
|
||||
|
|
|
|||
|
|
@ -1,34 +1,35 @@
|
|||
from __future__ import annotations
|
||||
from uuid import uuid4
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from warchron.model.campaign import Campaign, Sector, CampaignParticipant
|
||||
from warchron.model.round import Round, Choice, Battle
|
||||
|
||||
|
||||
class War:
|
||||
def __init__(self, name: str, year: int):
|
||||
def __init__(self, name: str, year: int) -> None:
|
||||
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: list[Campaign] = []
|
||||
self.participants: Dict[str, WarParticipant] = {}
|
||||
self.objectives: Dict[str, Objective] = {}
|
||||
self.campaigns: List[Campaign] = []
|
||||
self.is_over: bool = False
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name: str):
|
||||
def set_name(self, new_name: str) -> None:
|
||||
self.name = new_name
|
||||
|
||||
def set_year(self, new_year: int):
|
||||
def set_year(self, new_year: int) -> None:
|
||||
self.year = new_year
|
||||
|
||||
def set_state(self, new_state: bool):
|
||||
def set_state(self, new_state: bool) -> None:
|
||||
self.is_over = new_state
|
||||
|
||||
def toDict(self):
|
||||
def toDict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
|
|
@ -39,7 +40,7 @@ class War:
|
|||
}
|
||||
|
||||
@staticmethod
|
||||
def fromDict(data: dict):
|
||||
def fromDict(data: Dict[str, Any]) -> War:
|
||||
war = War(name=data["name"], year=data["year"])
|
||||
war.set_id(data["id"])
|
||||
# war.participants = data.get("participants", {})
|
||||
|
|
@ -58,7 +59,7 @@ class War:
|
|||
def get_objective(self, id: str) -> Objective:
|
||||
return self.objectives[id]
|
||||
|
||||
def get_all_objectives(self) -> list[Objective]:
|
||||
def get_all_objectives(self) -> List[Objective]:
|
||||
return list(self.objectives.values())
|
||||
|
||||
def get_objective_name(self, objective_id: str | None) -> str:
|
||||
|
|
@ -67,12 +68,14 @@ class War:
|
|||
obj = self.objectives.get(objective_id)
|
||||
return obj.name if obj else ""
|
||||
|
||||
def update_objective(self, objective_id: str, *, name: str, description: str):
|
||||
def update_objective(
|
||||
self, objective_id: str, *, name: str, description: str
|
||||
) -> None:
|
||||
obj = self.get_objective(objective_id)
|
||||
obj.set_name(name)
|
||||
obj.set_description(description)
|
||||
|
||||
def remove_objective(self, objective_id: str):
|
||||
def remove_objective(self, objective_id: str) -> None:
|
||||
# TODO manage sectors referring to it
|
||||
del self.objectives[objective_id]
|
||||
|
||||
|
|
@ -97,15 +100,15 @@ class War:
|
|||
def get_war_participant(self, id: str) -> WarParticipant:
|
||||
return self.participants[id]
|
||||
|
||||
def get_all_war_participants(self) -> list[WarParticipant]:
|
||||
def get_all_war_participants(self) -> List[WarParticipant]:
|
||||
return list(self.participants.values())
|
||||
|
||||
def update_war_participant(self, player_id: str, *, faction: str):
|
||||
def update_war_participant(self, player_id: str, *, faction: str) -> None:
|
||||
part = self.get_war_participant(player_id)
|
||||
# Can't change referred Model.players
|
||||
part.set_faction(faction)
|
||||
|
||||
def remove_war_participant(self, player_id: str):
|
||||
def remove_war_participant(self, player_id: str) -> None:
|
||||
# TODO manage campaign_participants referring to it
|
||||
del self.participants[player_id]
|
||||
|
||||
|
|
@ -114,7 +117,7 @@ class War:
|
|||
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:
|
||||
def get_default_campaign_values(self) -> Dict[str, Any]:
|
||||
return {"month": datetime.now().month}
|
||||
|
||||
def add_campaign(self, name: str, month: int | None = None) -> Campaign:
|
||||
|
|
@ -150,15 +153,15 @@ class War:
|
|||
return camp
|
||||
raise KeyError(f"Participant {participant_id} not found in any Campaign")
|
||||
|
||||
def update_campaign(self, campaign_id: str, *, name: str, month: int):
|
||||
def update_campaign(self, campaign_id: str, *, name: str, month: int) -> None:
|
||||
camp = self.get_campaign(campaign_id)
|
||||
camp.set_name(name)
|
||||
camp.set_month(month)
|
||||
|
||||
def get_all_campaigns(self) -> list[Campaign]:
|
||||
def get_all_campaigns(self) -> List[Campaign]:
|
||||
return list(self.campaigns)
|
||||
|
||||
def remove_campaign(self, campaign_id: str):
|
||||
def remove_campaign(self, campaign_id: str) -> None:
|
||||
camp = self.get_campaign(campaign_id)
|
||||
self.campaigns.remove(camp)
|
||||
|
||||
|
|
@ -166,7 +169,7 @@ class War:
|
|||
|
||||
def add_sector(
|
||||
self,
|
||||
campaign_id,
|
||||
campaign_id: str,
|
||||
name: str,
|
||||
round_id: str,
|
||||
major_id: str,
|
||||
|
|
@ -176,8 +179,12 @@ class War:
|
|||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_sector(name, round_id, major_id, minor_id, influence_id)
|
||||
|
||||
def get_sector(self, id: str) -> Sector:
|
||||
return self.sectors[id]
|
||||
def get_sector(self, sector_id: str) -> Sector:
|
||||
for camp in self.campaigns:
|
||||
for sect in camp.sectors.values():
|
||||
if sect.id == sector_id:
|
||||
return sect
|
||||
raise KeyError("Sector not found")
|
||||
|
||||
def update_sector(
|
||||
self,
|
||||
|
|
@ -188,7 +195,7 @@ class War:
|
|||
major_id: str,
|
||||
minor_id: str,
|
||||
influence_id: str,
|
||||
):
|
||||
) -> None:
|
||||
camp = self.get_campaign_by_sector(sector_id)
|
||||
camp.update_sector(
|
||||
sector_id,
|
||||
|
|
@ -199,13 +206,13 @@ class War:
|
|||
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]:
|
||||
camp = self.get_campaign(campaign_id)
|
||||
return [
|
||||
part
|
||||
|
|
@ -219,8 +226,8 @@ class War:
|
|||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_campaign_participant(participant_id, leader, theme)
|
||||
|
||||
def get_campaign_participant(self, participant_id) -> CampaignParticipant:
|
||||
for camp in self.campaigns.values():
|
||||
def get_campaign_participant(self, participant_id: str) -> CampaignParticipant:
|
||||
for camp in self.campaigns:
|
||||
for part in camp.participants.values():
|
||||
if part.id == participant_id:
|
||||
return part
|
||||
|
|
@ -228,11 +235,11 @@ class War:
|
|||
|
||||
def update_campaign_participant(
|
||||
self, participant_id: str, *, leader: str, theme: str
|
||||
):
|
||||
) -> None:
|
||||
camp = self.get_campaign_by_campaign_participant(participant_id)
|
||||
camp.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:
|
||||
camp = self.get_campaign_by_campaign_participant(participant_id)
|
||||
camp.remove_campaign_participant(participant_id)
|
||||
|
||||
|
|
@ -246,7 +253,7 @@ class War:
|
|||
camp = self.get_campaign(campaign_id)
|
||||
return camp.add_round()
|
||||
|
||||
def remove_round(self, round_id: str):
|
||||
def remove_round(self, round_id: str) -> None:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.remove_round(round_id)
|
||||
|
||||
|
|
@ -263,13 +270,13 @@ class War:
|
|||
priority_sector_id: str | None,
|
||||
secondary_sector_id: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> None:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.update_choice(
|
||||
participant_id, priority_sector_id, secondary_sector_id, comment
|
||||
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:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.remove_choice(round_id, participant_id)
|
||||
|
||||
|
|
@ -289,9 +296,10 @@ class War:
|
|||
score: str | None,
|
||||
victory_condition: str | None,
|
||||
comment: str | None,
|
||||
):
|
||||
) -> None:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.update_battle(
|
||||
round_id,
|
||||
sector_id,
|
||||
player_1_id,
|
||||
player_2_id,
|
||||
|
|
@ -301,7 +309,7 @@ class War:
|
|||
comment,
|
||||
)
|
||||
|
||||
def remove_battle(self, round_id: str, sector_id: str):
|
||||
def remove_battle(self, round_id: str, sector_id: str) -> None:
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.remove_battle(round_id, sector_id)
|
||||
|
||||
|
|
@ -312,13 +320,13 @@ class Objective:
|
|||
self.name: str = name
|
||||
self.description: str = description
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name: str):
|
||||
def set_name(self, new_name: str) -> None:
|
||||
self.name = new_name
|
||||
|
||||
def set_description(self, new_description: str):
|
||||
def set_description(self, new_description: str) -> None:
|
||||
self.description = new_description
|
||||
|
||||
|
||||
|
|
@ -328,11 +336,11 @@ class WarParticipant:
|
|||
self.player_id: str = player_id # ref to WarModel.players
|
||||
self.faction: str = faction
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
def set_id(self, new_id: str) -> None:
|
||||
self.id = new_id
|
||||
|
||||
def set_player(self, new_player: str):
|
||||
def set_player(self, new_player: str) -> None:
|
||||
self.player_id = new_player
|
||||
|
||||
def set_faction(self, new_faction: str):
|
||||
def set_faction(self, new_faction: str) -> None:
|
||||
self.faction = new_faction
|
||||
|
|
|
|||
0
src/warchron/model/war_participant.py
Normal file
0
src/warchron/model/war_participant.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue