add user list and apend

This commit is contained in:
Maxime Réaux 2026-01-16 18:13:01 +01:00
parent d2bcf3bdd8
commit ee7a266e9d
24 changed files with 562 additions and 38 deletions

View file

@ -9,6 +9,7 @@ class Model:
self.players = {}
data_file_path = Path("data/warmachron.json")
self.load_data(data_file_path)
self.save_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:
@ -17,10 +18,10 @@ class Model:
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"]:
# placeholder
pass
except json.JSONDecodeError:
raise RuntimeError("Data file is corrupted")
@ -28,13 +29,12 @@ class Model:
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['version'] = "1.0"
data['players'] = []
for player in self.players:
data['wars'] = []
for player in self.players.values():
data['players'].append(player.toDict())
with open(data_file_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
@ -53,3 +53,6 @@ class Model:
def delete_player(self, id):
del self.players[id]
def get_all_players(self) -> list[Player]:
return list(self.players.values())