code quality (mypy+flake8)
This commit is contained in:
parent
7210ddc927
commit
55abdccc64
19 changed files with 778 additions and 497 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue