prepare gui env
This commit is contained in:
parent
02e7221149
commit
d2bcf3bdd8
25 changed files with 291 additions and 198 deletions
55
src/wargame_campaign/model/model.py
Normal file
55
src/wargame_campaign/model/model.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
from pathlib import Path
|
||||
import json
|
||||
import shutil
|
||||
|
||||
from wargame_campaign.model.player import Player
|
||||
|
||||
class Model:
|
||||
def __init__(self):
|
||||
self.players = {}
|
||||
data_file_path = Path("data/warmachron.json")
|
||||
self.load_data(data_file_path)
|
||||
|
||||
def load_data(self, data_file_path):
|
||||
if not data_file_path.exists() or data_file_path.stat().st_size == 0:
|
||||
pass # Create empty json
|
||||
try:
|
||||
with open(data_file_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
for player in data["players"] :
|
||||
print(f"player {player}")
|
||||
saved_player = Player.fromDict(player["id"], player['name'])
|
||||
self.players[saved_player.id] = saved_player
|
||||
for war in data["wars"]:
|
||||
pass
|
||||
except json.JSONDecodeError:
|
||||
raise RuntimeError("Data file is corrupted")
|
||||
|
||||
def save_data(self, data_file_path):
|
||||
if data_file_path.exists():
|
||||
shutil.copy(data_file_path, data_file_path.with_suffix(".json.bak"))
|
||||
|
||||
data = {}
|
||||
data['verion'] = "1.0"
|
||||
data['players'] = []
|
||||
for player in self.players:
|
||||
data['players'].append(player.toDict())
|
||||
|
||||
with open(data_file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
def add_player(self, name):
|
||||
player = Player(name)
|
||||
self.players[player.id] = player
|
||||
return player
|
||||
|
||||
def get_player(self, id):
|
||||
return self.players[id]
|
||||
|
||||
def update_player(self, id, name):
|
||||
player = self.get_player(id)
|
||||
player.set_name(name)
|
||||
|
||||
def delete_player(self, id):
|
||||
del self.players[id]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue