Files
Doom-Matrix/doom_matrix/engine/save.py
T
fedora_openclaw 71d9ee99b8 Rename Morpheus Hayden/Trinity VEGA to Morpheus/Trinity, add wallpaper assets
Shortens character names throughout dialogue, save.py loop-twist lines,
and README. Adds three wallpaper images to Wallpaper/ and embeds
Doom_Slyer.png as the README screenshot.
2026-07-06 20:18:28 +02:00

86 lines
2.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Speichersystem (JSON-Savegames) und persistente Meta-Statistiken fuer den
Loop-Twist: das Spiel 'erinnert' sich, wie oft es schon gespielt wurde."""
import json
import os
from doom_matrix.entities.player import Player
_SAVE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "saves")
_META_PATH = os.path.join(_SAVE_DIR, "meta.json")
_DEFAULT_META = {"run_count": 0, "completed_runs": 0, "endings_seen": []}
def _ensure_dir():
os.makedirs(_SAVE_DIR, exist_ok=True)
def save_game(player, slot="autosave"):
_ensure_dir()
path = os.path.join(_SAVE_DIR, f"{slot}.json")
with open(path, "w", encoding="utf-8") as f:
json.dump(player.to_dict(), f, ensure_ascii=False, indent=2)
return path
def load_game(slot="autosave"):
path = os.path.join(_SAVE_DIR, f"{slot}.json")
if not os.path.exists(path):
return None
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return Player.from_dict(data)
def save_exists(slot="autosave"):
return os.path.exists(os.path.join(_SAVE_DIR, f"{slot}.json"))
def load_meta():
if not os.path.exists(_META_PATH):
return dict(_DEFAULT_META)
try:
with open(_META_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
merged = dict(_DEFAULT_META)
merged.update(data)
return merged
except (json.JSONDecodeError, OSError):
return dict(_DEFAULT_META)
def save_meta(meta):
_ensure_dir()
with open(_META_PATH, "w", encoding="utf-8") as f:
json.dump(meta, f, ensure_ascii=False, indent=2)
def register_new_run():
meta = load_meta()
meta["run_count"] += 1
save_meta(meta)
return meta["run_count"]
def register_ending(ending_key):
meta = load_meta()
meta["completed_runs"] += 1
if ending_key not in meta["endings_seen"]:
meta["endings_seen"].append(ending_key)
save_meta(meta)
return meta
def loop_twist_line(run_count):
"""Gibt einen Dialogzeile zurueck, falls das Spiel schon oefter gelaufen ist -- oder None."""
if run_count <= 1:
return None
if run_count == 2:
return "Morpheus: 'Habe ich das hier nicht schon einmal gesagt?'"
if run_count < 5:
return f"Morpheus: 'Lauf Nummer {run_count}. Du wirst besser. Oder ich erinnere mich besser.'"
return f"Morpheus: 'Lauf Nummer {run_count}. Ehrlich gesagt -- ich weiss nicht mehr, welcher von uns beiden real ist.'"