yahtzee-py/models/stats.py

22 lines
620 B
Python
Raw Normal View History

2026-11-02 18:32:36 +00:00
import json
import os
class StatsModel:
STATS_FILE = "yahtzee_stats.json"
@classmethod
def load_stats(cls):
if os.path.exists(cls.STATS_FILE):
with open(cls.STATS_FILE, "r") as f:
return json.load(f)
return {"highest_score": 0, "wins": 0, "losses": 0}
@classmethod
def record_game(cls, won, score):
stats = cls.load_stats()
if score > stats["highest_score"]: stats["highest_score"] = score
if won: stats["wins"] += 1
else: stats["losses"] += 1
with open(cls.STATS_FILE, "w") as f:
json.dump(stats, f)