add/edit/delet objectives and participants in war
This commit is contained in:
parent
e33dec40be
commit
495a5adb98
14 changed files with 1088 additions and 322 deletions
|
|
@ -1,27 +1,29 @@
|
|||
from __future__ import annotations
|
||||
from uuid import uuid4
|
||||
from datetime import datetime
|
||||
|
||||
from warchron.model.round import Round
|
||||
|
||||
class Campaign:
|
||||
def __init__(self, name, month):
|
||||
self.id = str(uuid4())
|
||||
self.name = name
|
||||
self.month = month
|
||||
self.entrants = {}
|
||||
def __init__(self, name: str, month: int):
|
||||
self.id: str = str(uuid4())
|
||||
self.name: str = name
|
||||
self.month: int = month
|
||||
self.participants = {}
|
||||
self.sectors = {}
|
||||
self.rounds = []
|
||||
self.is_over = False
|
||||
|
||||
def set_id(self, new_id):
|
||||
def set_id(self, new_id: str):
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name):
|
||||
def set_name(self, new_name: str):
|
||||
self.name = new_name
|
||||
|
||||
def set_month(self, new_month):
|
||||
def set_month(self, new_month: int):
|
||||
self.month = new_month
|
||||
|
||||
def set_state(self, new_state):
|
||||
def set_state(self, new_state: bool):
|
||||
self.is_over = new_state
|
||||
|
||||
def toDict(self):
|
||||
|
|
@ -29,7 +31,7 @@ class Campaign:
|
|||
"id" : self.id,
|
||||
"name" : self.name,
|
||||
"month" : self.month,
|
||||
# "entrants" : self.entrants,
|
||||
# "participants" : self.participants,
|
||||
"rounds": [rnd.toDict() for rnd in self.rounds],
|
||||
"is_over": self.is_over
|
||||
}
|
||||
|
|
@ -38,13 +40,16 @@ class Campaign:
|
|||
def fromDict(data: dict):
|
||||
camp = Campaign(name=data["name"], month=data["month"])
|
||||
camp.set_id(data["id"])
|
||||
# camp.entrants = data.get("entrants", {})
|
||||
# camp.participants = data.get("participants", {})
|
||||
for rnd_data in data.get("rounds", []):
|
||||
camp.rounds.append(Round.fromDict(rnd_data))
|
||||
camp.set_state(data.get("is_over", False))
|
||||
return camp
|
||||
|
||||
def has_round(self, round_id: str) -> bool:
|
||||
return any(r.id == round_id for r in self.rounds)
|
||||
|
||||
def get_round(self, round_id) -> Round:
|
||||
def get_round(self, round_id: str) -> Round:
|
||||
return self.rounds[round_id]
|
||||
|
||||
def get_all_rounds(self) -> list[Round]:
|
||||
|
|
@ -65,3 +70,19 @@ class Campaign:
|
|||
if rnd.id == round_id:
|
||||
return index
|
||||
raise KeyError("Round not found in campaign")
|
||||
|
||||
class CampaignParticipant:
|
||||
def __init__(self,war_participant_id: str, leader: str):
|
||||
self.id: str = war_participant_id # ref to War.participants
|
||||
self.leader: str = leader
|
||||
self.victory_points = 0
|
||||
self.objective_points = {}
|
||||
|
||||
class Sector:
|
||||
def __init__(self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str):
|
||||
self.id: str = str(uuid4())
|
||||
self.name: str = name
|
||||
self.major_objective_id: str = major_id
|
||||
self.minor_objective_id: str = minor_id
|
||||
self.influence_objective_id: str = influence_id
|
||||
self.round_id: str = round_id
|
||||
|
|
|
|||
|
|
@ -4,14 +4,16 @@ import shutil
|
|||
from datetime import datetime
|
||||
|
||||
from warchron.model.player import Player
|
||||
from warchron.model.war import War
|
||||
from warchron.model.war import War, Objective, WarParticipant
|
||||
from warchron.model.campaign import Campaign
|
||||
from warchron.model.round import Round
|
||||
|
||||
class Model:
|
||||
def __init__(self):
|
||||
self.players = {}
|
||||
self.wars = {}
|
||||
self.players: dict[str, Player] = {}
|
||||
self.wars: dict[str, War] = {}
|
||||
|
||||
# File management methods
|
||||
|
||||
def new(self):
|
||||
self.players.clear()
|
||||
|
|
@ -53,6 +55,8 @@ class Model:
|
|||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
# Player methods
|
||||
|
||||
def add_player(self, name):
|
||||
player = Player(name)
|
||||
self.players[player.id] = player
|
||||
|
|
@ -60,17 +64,22 @@ 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
|
||||
|
||||
def update_player(self, player_id: str, *, name: str):
|
||||
player = self.get_player(player_id)
|
||||
player.set_name(name)
|
||||
|
||||
def delete_player(self, id):
|
||||
del self.players[id]
|
||||
|
||||
def get_all_players(self) -> list[Player]:
|
||||
return list(self.players.values())
|
||||
|
||||
def remove_player(self, player_id: str):
|
||||
del self.players[player_id]
|
||||
|
||||
# War methods
|
||||
|
||||
def get_default_war_values(self) -> dict:
|
||||
return {
|
||||
"year": datetime.now().year
|
||||
|
|
@ -91,6 +100,20 @@ class Model:
|
|||
return war
|
||||
raise KeyError(f"Campaign {campaign_id} not found in any War")
|
||||
|
||||
def get_war_by_objective(self, objective_id: str) -> War:
|
||||
for war in self.wars.values():
|
||||
for obj in war.objectives.values():
|
||||
if obj.id == objective_id:
|
||||
return war
|
||||
raise KeyError(f"Objective {objective_id} not found in any War")
|
||||
|
||||
def get_war_by_war_participant(self, participant_id: str) -> War:
|
||||
for war in self.wars.values():
|
||||
for part in war.participants.values():
|
||||
if part.id == participant_id:
|
||||
return war
|
||||
raise KeyError(f"Participant {participant_id} not found in any War")
|
||||
|
||||
def update_war(self, war_id: str, *, name: str, year: int):
|
||||
war = self.get_war(war_id)
|
||||
war.set_name(name)
|
||||
|
|
@ -99,6 +122,61 @@ class Model:
|
|||
def get_all_wars(self) -> list[War]:
|
||||
return list(self.wars.values())
|
||||
|
||||
def remove_war(self, war_id: str):
|
||||
del self.wars[war_id]
|
||||
|
||||
# Objective methods
|
||||
|
||||
def add_objective(self, war_id: str, name: str, description: str) -> Objective:
|
||||
war = self.get_war(war_id)
|
||||
return war.add_objective(name, description)
|
||||
|
||||
def get_objective(self, objective_id) -> 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):
|
||||
war = self.get_war_by_objective(objective_id)
|
||||
war.update_objective(objective_id, name=name, description=description)
|
||||
|
||||
def remove_objective(self, objective_id: str):
|
||||
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]:
|
||||
war = self.get_war(war_id)
|
||||
return [
|
||||
player
|
||||
for player in self.players.values()
|
||||
if not war.has_participant(player.id)
|
||||
]
|
||||
|
||||
def add_war_participant(self, war_id: str, player_id: str, faction: str) -> Objective:
|
||||
war = self.get_war(war_id)
|
||||
return war.add_war_participant(player_id, faction)
|
||||
|
||||
def get_war_participant(self, participant_id) -> WarParticipant:
|
||||
for war in self.wars.values():
|
||||
for part in war.participants.values():
|
||||
if part.id == participant_id:
|
||||
return part
|
||||
raise KeyError("Participant not found")
|
||||
|
||||
def update_war_participant(self, participant_id: str, *, faction: str):
|
||||
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):
|
||||
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:
|
||||
war = self.get_war(war_id)
|
||||
return war.get_default_campaign_values()
|
||||
|
|
@ -125,6 +203,12 @@ class Model:
|
|||
war = self.get_war_by_campaign(campaign_id)
|
||||
war.update_campaign(campaign_id, name=name, month=month)
|
||||
|
||||
def remove_campaign(self, campaign_id: str):
|
||||
war = self.get_war_by_campaign(campaign_id)
|
||||
war.remove_campaign(campaign_id)
|
||||
|
||||
# Round methods
|
||||
|
||||
def add_round(self, campaign_id: str) -> Round:
|
||||
campaign = self.get_campaign(campaign_id)
|
||||
return campaign.add_round()
|
||||
|
|
@ -141,16 +225,6 @@ class Model:
|
|||
camp = self.get_campaign_by_round(round_id)
|
||||
return camp.get_round_index(round_id)
|
||||
|
||||
def remove_player(self, player_id: str):
|
||||
del self.players[player_id]
|
||||
|
||||
def remove_war(self, war_id: str):
|
||||
del self.wars[war_id]
|
||||
|
||||
def remove_campaign(self, campaign_id: str):
|
||||
war = self.get_war_by_campaign(campaign_id)
|
||||
war.remove_campaign(campaign_id)
|
||||
|
||||
def remove_round(self, round_id: str):
|
||||
camp = self.get_campaign_by_round(round_id)
|
||||
camp.remove_round(round_id)
|
||||
camp.remove_round(round_id)
|
||||
|
|
|
|||
|
|
@ -2,16 +2,16 @@ from uuid import uuid4
|
|||
|
||||
class Round:
|
||||
def __init__(self):
|
||||
self.id = str(uuid4())
|
||||
self.id: str = str(uuid4())
|
||||
self.sectors = {}
|
||||
self.choices = {}
|
||||
self.battles = {}
|
||||
self.is_over = False
|
||||
self.is_over: bool = False
|
||||
|
||||
def set_id(self, new_id):
|
||||
def set_id(self, new_id: str):
|
||||
self.id = new_id
|
||||
|
||||
def set_state(self, new_state):
|
||||
def set_state(self, new_state: bool):
|
||||
self.is_over = new_state
|
||||
|
||||
def toDict(self):
|
||||
|
|
|
|||
|
|
@ -1,27 +1,30 @@
|
|||
from __future__ import annotations
|
||||
from uuid import uuid4
|
||||
from datetime import datetime
|
||||
|
||||
from warchron.model.campaign import Campaign
|
||||
|
||||
class War:
|
||||
def __init__(self, name, year):
|
||||
self.id = str(uuid4())
|
||||
self.name = name
|
||||
self.year = year
|
||||
self.entrants = {}
|
||||
self.campaigns = []
|
||||
self.is_over = False
|
||||
|
||||
def set_id(self, new_id):
|
||||
class War:
|
||||
def __init__(self, name: str, year: int):
|
||||
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 = []
|
||||
self.is_over: bool = False
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name):
|
||||
def set_name(self, new_name: str):
|
||||
self.name = new_name
|
||||
|
||||
def set_year(self, new_year):
|
||||
def set_year(self, new_year: int):
|
||||
self.year = new_year
|
||||
|
||||
def set_state(self, new_state):
|
||||
def set_state(self, new_state: bool):
|
||||
self.is_over = new_state
|
||||
|
||||
def toDict(self):
|
||||
|
|
@ -29,7 +32,7 @@ class War:
|
|||
"id" : self.id,
|
||||
"name" : self.name,
|
||||
"year" : self.year,
|
||||
# "entrants" : self.entrants,
|
||||
# "participants" : self.participants,
|
||||
"campaigns": [camp.toDict() for camp in self.campaigns],
|
||||
"is_over": self.is_over
|
||||
}
|
||||
|
|
@ -38,12 +41,66 @@ class War:
|
|||
def fromDict(data: dict):
|
||||
war = War(name=data["name"], year=data["year"])
|
||||
war.set_id(data["id"])
|
||||
# war.entrants = data.get("entrants", {})
|
||||
# war.participants = data.get("participants", {})
|
||||
for camp_data in data.get("campaigns", []):
|
||||
war.campaigns.append(Campaign.fromDict(camp_data))
|
||||
war.set_state(data.get("is_over", False))
|
||||
return war
|
||||
|
||||
# Objective methods
|
||||
|
||||
def add_objective(self, name: str, description: str) -> Objective:
|
||||
objective = Objective(name, description)
|
||||
self.objectives[objective.id] = objective
|
||||
return objective
|
||||
|
||||
def get_objective(self, id: str) -> Objective:
|
||||
return self.objectives[id]
|
||||
|
||||
def get_all_objectives(self) -> list[Objective]:
|
||||
return list(self.objectives.values())
|
||||
|
||||
def update_objective(self, objective_id: str, *, name: str, description: str):
|
||||
obj = self.get_objective(objective_id)
|
||||
obj.set_name(name)
|
||||
obj.set_description(description)
|
||||
|
||||
def remove_objective(self, objective_id: str):
|
||||
del self.objectives[objective_id]
|
||||
|
||||
# War participant methods
|
||||
|
||||
def get_all_war_participants_ids(self) -> set[str]:
|
||||
return set(self.participants.keys())
|
||||
|
||||
def has_participant(self, player_id: str) -> bool:
|
||||
return player_id in self.participants
|
||||
|
||||
def add_war_participant(self, player_id: str, faction: str) -> WarParticipant:
|
||||
if player_id in self.participants:
|
||||
raise ValueError("Player already registered in this war")
|
||||
participant = WarParticipant(player_id, faction)
|
||||
self.participants[participant.id] = participant
|
||||
return participant
|
||||
|
||||
def get_war_participant(self, id: str) -> WarParticipant:
|
||||
return self.participants[id]
|
||||
|
||||
def get_all_war_participants(self) -> list[WarParticipant]:
|
||||
return list(self.participants.values())
|
||||
|
||||
def update_war_participant(self, player_id: str, *, faction: str):
|
||||
part = self.get_war_participant(player_id)
|
||||
part.set_faction(faction)
|
||||
|
||||
def remove_war_participant(self, player_id: str):
|
||||
del self.participants[player_id]
|
||||
|
||||
# Campaign methods
|
||||
|
||||
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:
|
||||
return {
|
||||
"month": datetime.now().month
|
||||
|
|
@ -56,7 +113,7 @@ class War:
|
|||
self.campaigns.append(campaign)
|
||||
return campaign
|
||||
|
||||
def get_campaign(self, campaign_id) -> Campaign:
|
||||
def get_campaign(self, campaign_id: str) -> Campaign:
|
||||
for camp in self.campaigns:
|
||||
if camp.id == campaign_id:
|
||||
return camp
|
||||
|
|
@ -79,4 +136,31 @@ class War:
|
|||
|
||||
def remove_campaign(self, campaign_id: str):
|
||||
camp = self.get_campaign(campaign_id)
|
||||
self.campaigns.remove(camp)
|
||||
self.campaigns.remove(camp)
|
||||
|
||||
class Objective:
|
||||
def __init__(self, name: str, description: str):
|
||||
self.id: str = str(uuid4())
|
||||
self.name: str = name
|
||||
self.description: str = description
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
self.id = new_id
|
||||
|
||||
def set_name(self, new_name: str):
|
||||
self.name = new_name
|
||||
|
||||
def set_description(self, new_description: str):
|
||||
self.description = new_description
|
||||
|
||||
class WarParticipant:
|
||||
def __init__(self, player_id: str, faction: str):
|
||||
self.id: str = player_id # ref to Model.players
|
||||
self.faction: str = faction
|
||||
|
||||
def set_id(self, new_id: str):
|
||||
self.id = new_id
|
||||
|
||||
def set_faction(self, new_faction: str):
|
||||
self.faction = new_faction
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue