75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
|
|
import sqlite3
|
||
|
|
import os
|
||
|
|
|
||
|
|
from data.setup_database import DB_PATH
|
||
|
|
|
||
|
|
DB_FILE = "wood/log.db"
|
||
|
|
|
||
|
|
def log(action, details, player_id = None):
|
||
|
|
"""Schreibt ein Ereignis (Audit Trail) in das System-Log."""
|
||
|
|
connection = sqlite3.connect(DB_FILE)
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
cursor.execute(
|
||
|
|
"INSERT INTO system_log (player_id, action, details) VALUES (?, ?, ?)",
|
||
|
|
(player_id, action, details)
|
||
|
|
)
|
||
|
|
|
||
|
|
connection.commit()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
|
||
|
|
def setup_log_db():
|
||
|
|
# --- DATENBANK CHECK ---
|
||
|
|
# ACHTUNG: Großschreibung bei DB_FILE beachten!
|
||
|
|
if not os.path.exists(DB_FILE):
|
||
|
|
print(f"WARNUNG: '{DB_FILE}' nicht gefunden!")
|
||
|
|
print("Starte Log-Datenbank-Einrichtung...")
|
||
|
|
|
||
|
|
# Wir bauen die Datei direkt HIER auf, ohne setup_database aufzurufen!
|
||
|
|
connection = sqlite3.connect(DB_FILE)
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
cursor.execute('''
|
||
|
|
CREATE TABLE IF NOT EXISTS system_log (
|
||
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
|
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
|
|
player_id INTEGER,
|
||
|
|
action TEXT,
|
||
|
|
details TEXT
|
||
|
|
)
|
||
|
|
''')
|
||
|
|
connection.commit()
|
||
|
|
connection.close()
|
||
|
|
print("Log-Datenbank aufgebaut!")
|
||
|
|
else:
|
||
|
|
print(f"OK: Log-Datenbank '{DB_FILE}' gefunden. Lade System...")
|
||
|
|
|
||
|
|
|
||
|
|
def get_full_log():
|
||
|
|
"""Holt das komplette Log und übersetzt die Spieler-IDs in Namen."""
|
||
|
|
# 1. Alle Logs aus der log.db holen
|
||
|
|
log_conn = sqlite3.connect(DB_FILE)
|
||
|
|
log_conn.row_factory = sqlite3.Row
|
||
|
|
log_cursor = log_conn.cursor()
|
||
|
|
|
||
|
|
log_cursor.execute("SELECT * FROM system_log ORDER BY timestamp DESC")
|
||
|
|
logs = [dict(row) for row in log_cursor.fetchall()]
|
||
|
|
log_conn.close()
|
||
|
|
|
||
|
|
# 2. Spielernamen aus der Haupt-DB holen
|
||
|
|
main_conn = sqlite3.connect(DB_PATH)
|
||
|
|
main_cursor = main_conn.cursor()
|
||
|
|
main_cursor.execute("SELECT id, display_name, discord_name FROM players")
|
||
|
|
players_dict = {row[0]: f"{row[1]} ({row[2]})" for row in main_cursor.fetchall()}
|
||
|
|
main_conn.close()
|
||
|
|
|
||
|
|
# 3. Die IDs in den Logs durch Namen ersetzen
|
||
|
|
for log_entry in logs:
|
||
|
|
p_id = log_entry['player_id']
|
||
|
|
if p_id is not None and p_id in players_dict:
|
||
|
|
log_entry['player_name'] = players_dict[p_id]
|
||
|
|
else:
|
||
|
|
log_entry['player_name'] = "System / Unbekannt"
|
||
|
|
|
||
|
|
return logs
|