save & load wars
This commit is contained in:
parent
1218f32752
commit
dc854b4065
5 changed files with 52 additions and 50 deletions
|
|
@ -29,20 +29,21 @@ class Campaign:
|
||||||
"id" : self.id,
|
"id" : self.id,
|
||||||
"name" : self.name,
|
"name" : self.name,
|
||||||
"month" : self.month,
|
"month" : self.month,
|
||||||
"entrants" : self.entrants,
|
# "entrants" : self.entrants,
|
||||||
"rounds" : self.rounds,
|
"rounds": [rnd.toDict() for rnd in self.rounds],
|
||||||
"is_over": self.is_over
|
"is_over": self.is_over
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fromDict(id, name, month, entrants, rounds, is_over):
|
def fromDict(data: dict):
|
||||||
tmp = Campaign(name=name)
|
camp = Campaign(name=data["name"])
|
||||||
tmp.set_id(id)
|
camp.set_id(data["id"])
|
||||||
tmp.set_month(month)
|
camp.set_month(data["month"])
|
||||||
## entrants placeholder
|
# camp.entrants = data.get("entrants", {})
|
||||||
## rounds placeholder
|
for rnd_data in data.get("rounds", []):
|
||||||
tmp.set_state(is_over)
|
camp.rounds.append(Round.fromDict(rnd_data))
|
||||||
return tmp
|
camp.set_state(data.get("is_over", False))
|
||||||
|
return camp
|
||||||
|
|
||||||
def add_round(self, number: int) -> Round:
|
def add_round(self, number: int) -> Round:
|
||||||
round = Round()
|
round = Round()
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@ class Model:
|
||||||
def new(self):
|
def new(self):
|
||||||
self.players.clear()
|
self.players.clear()
|
||||||
self.wars.clear()
|
self.wars.clear()
|
||||||
# self.campaigns.clear()
|
|
||||||
# self.rounds.clear()
|
|
||||||
|
|
||||||
def load(self, path: Path):
|
def load(self, path: Path):
|
||||||
self.players.clear()
|
self.players.clear()
|
||||||
|
|
@ -32,24 +30,25 @@ class Model:
|
||||||
try:
|
try:
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
for player in data["players"] :
|
self.players.clear()
|
||||||
saved_player = Player.fromDict(player["id"], player['name'])
|
self.wars.clear()
|
||||||
self.players[saved_player.id] = saved_player
|
for p in data.get("players", []):
|
||||||
for war in data["wars"]:
|
player = Player.fromDict(p)
|
||||||
# placeholder
|
self.players[player.id] = player
|
||||||
pass
|
for w in data.get("wars", []):
|
||||||
|
war = War.fromDict(w)
|
||||||
|
self.wars[war.id] = war
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
raise RuntimeError("Data file is corrupted")
|
raise RuntimeError("Data file is corrupted")
|
||||||
|
|
||||||
def _save_data(self, path: Path):
|
def _save_data(self, path: Path):
|
||||||
if path.exists():
|
if path.exists():
|
||||||
shutil.copy(path, path.with_suffix(".json.bak"))
|
shutil.copy(path, path.with_suffix(".json.bak"))
|
||||||
data = {}
|
data = {
|
||||||
data['version'] = "1.0"
|
"version": "1.0",
|
||||||
data['players'] = []
|
"players": [p.toDict() for p in self.players.values()],
|
||||||
data['wars'] = []
|
"wars": [w.toDict() for w in self.wars.values()]
|
||||||
for player in self.players.values():
|
}
|
||||||
data['players'].append(player.toDict())
|
|
||||||
with open(path, "w", encoding="utf-8") as f:
|
with open(path, "w", encoding="utf-8") as f:
|
||||||
json.dump(data, f, indent=2)
|
json.dump(data, f, indent=2)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class Player:
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fromDict(id, name):
|
def fromDict(data: dict):
|
||||||
tmp = Player(name=name)
|
play = Player(name=data["name"])
|
||||||
tmp.set_id(id)
|
play.set_id(data["id"])
|
||||||
return tmp
|
return play
|
||||||
|
|
@ -16,18 +16,19 @@ class Round:
|
||||||
|
|
||||||
def toDict(self):
|
def toDict(self):
|
||||||
return {
|
return {
|
||||||
"sectors" : self.sectors,
|
"id": self.id,
|
||||||
"choices" : self.choices,
|
# "sectors" : self.sectors,
|
||||||
"battles" : self.battles,
|
# "choices" : self.choices,
|
||||||
|
# "battles" : self.battles,
|
||||||
"is_over": self.is_over
|
"is_over": self.is_over
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fromDict(id, sectors, choices, battles, is_over):
|
def fromDict(data: dict):
|
||||||
tmp = Round()
|
rnd = Round()
|
||||||
tmp.set_id(id)
|
rnd.set_id(data["id"])
|
||||||
## sectors placeholder
|
# rnd.sectors = data.get("sectors", {})
|
||||||
## choices placeholder
|
# rnd.choices = data.get("choices", {})
|
||||||
## battles placeholder
|
# rnd.battles = data.get("battles", {})
|
||||||
tmp.set_state(is_over)
|
rnd.set_state(data.get("is_over", False))
|
||||||
return tmp
|
return rnd
|
||||||
|
|
@ -29,21 +29,22 @@ class War:
|
||||||
"id" : self.id,
|
"id" : self.id,
|
||||||
"name" : self.name,
|
"name" : self.name,
|
||||||
"year" : self.year,
|
"year" : self.year,
|
||||||
"entrants" : self.entrants,
|
# "entrants" : self.entrants,
|
||||||
"campaigns" : self.campaigns,
|
"campaigns": [camp.toDict() for camp in self.campaigns],
|
||||||
"is_over": self.is_over
|
"is_over": self.is_over
|
||||||
}
|
}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fromDict(id, name, year, entrants, campaigns, is_over):
|
def fromDict(data: dict):
|
||||||
tmp = War(name=name)
|
war = War(name=data["name"])
|
||||||
tmp.set_id(id)
|
war.set_id(data["id"])
|
||||||
tmp.set_year(year)
|
war.set_year(data["year"])
|
||||||
## entrants placeholder
|
# war.entrants = data.get("entrants", {})
|
||||||
## campaigns placeholder
|
for camp_data in data.get("campaigns", []):
|
||||||
tmp.set_state(is_over)
|
war.campaigns.append(Campaign.fromDict(camp_data))
|
||||||
return tmp
|
war.set_state(data.get("is_over", False))
|
||||||
|
return war
|
||||||
|
|
||||||
def add_campaign(self, name) -> Campaign:
|
def add_campaign(self, name) -> Campaign:
|
||||||
campaign = Campaign(name)
|
campaign = Campaign(name)
|
||||||
self.campaigns.append(campaign)
|
self.campaigns.append(campaign)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue