22 lines
626 B
Python
22 lines
626 B
Python
import json
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
DATA_FILE = Path("data/warmachron.json")
|
|
|
|
def load_data():
|
|
if not DATA_FILE.exists() or DATA_FILE.stat().st_size == 0:
|
|
return {"version": 1, "players": {}, "wars": []}
|
|
|
|
try:
|
|
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except json.JSONDecodeError:
|
|
raise RuntimeError("Data file is corrupted")
|
|
|
|
def save_data(data):
|
|
if DATA_FILE.exists():
|
|
shutil.copy(DATA_FILE, DATA_FILE.with_suffix(".json.bak"))
|
|
|
|
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, indent=2)
|