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