yahtzee-py/models/save_manager.py

70 lines
2.9 KiB
Python
Raw Normal View History

2026-11-02 21:43:42 +00:00
import sqlite3
DB_FILE = "yahtzee_profiles.db"
class SaveManager:
"""Verwaltet gespeicherte Spielstände und benutzerdefinierte Bot-Konfigurationen."""
@staticmethod
def save_game(profile_id, game_state_json):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("REPLACE INTO saved_game (profile_id, game_data) VALUES (?, ?)", (profile_id, game_state_json))
conn.commit()
@staticmethod
def load_game(profile_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("SELECT game_data FROM saved_game WHERE profile_id=?", (profile_id,))
res = c.fetchone()
return res[0] if res else None
@staticmethod
def delete_save(profile_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("DELETE FROM saved_game WHERE profile_id=?", (profile_id,))
conn.commit()
@staticmethod
def save_bot_configs(profile_id, bot_configs_dict):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"CREATE TABLE IF NOT EXISTS bot_configs "
"(profile_id INTEGER, bot_index INTEGER, name TEXT, level INTEGER, PRIMARY KEY(profile_id, bot_index))"
)
for idx, (name, level) in bot_configs_dict.items():
c.execute(
"INSERT OR REPLACE INTO bot_configs (profile_id, bot_index, name, level) VALUES (?, ?, ?, ?)",
(profile_id, idx, name, level)
)
conn.commit()
@staticmethod
def load_bot_configs(profile_id):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute(
"CREATE TABLE IF NOT EXISTS bot_configs "
"(profile_id INTEGER, bot_index INTEGER, name TEXT, level INTEGER, PRIMARY KEY(profile_id, bot_index))"
)
c.execute("SELECT bot_index, name, level FROM bot_configs WHERE profile_id=?", (profile_id,))
return {row[0]: (row[1], row[2]) for row in c.fetchall()}
@staticmethod
def save_setting(key, value):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS app_settings (key TEXT PRIMARY KEY, value TEXT)")
c.execute("INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)", (key, str(value)))
conn.commit()
@staticmethod
def load_setting(key, default=None):
with sqlite3.connect(DB_FILE) as conn:
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS app_settings (key TEXT PRIMARY KEY, value TEXT)")
c.execute("SELECT value FROM app_settings WHERE key=?", (key,))
row = c.fetchone()
return row[0] if row else default