Liga-System/database.py
2026-02-27 09:26:40 +00:00

132 lines
4.1 KiB
Python

import sqlite3
if __name__ == "__main__":
init_db()
def init_db():
connection = sqlite3.connect("warhammer_league.db")
cursor = connection.cursor()
# Fremdschlüssel (Foreign Keys) in SQLite aktivieren
cursor.execute('PRAGMA foreign_keys = ON;')
# 1. Tabelle: players (Stammdaten)
cursor.execute('''
CREATE TABLE IF NOT EXISTS players (
id INTEGER PRIMARY KEY AUTOINCREMENT,
discord_id TEXT UNIQUE,
name TEXT NOT NULL,
avatar_url TEXT
)
''')
# 2. Tabelle: gamesystems (Globale Spielsysteme)
cursor.execute('''
CREATE TABLE IF NOT EXISTS gamesystems (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
min_score INTEGER DEFAULT 0,
max_score INTEGER DEFAULT 100
)
''')
# 3. Tabelle: player_game_statistic (Welcher Spieler hat in welchem System welche Stats?)
cursor.execute('''
CREATE TABLE IF NOT EXISTS player_game_statistic (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_id INTEGER,
gamesystem_id INTEGER,
mmr INTEGER DEFAULT 1000,
games_in_system INTEGER DEFAULT 0,
points INTEGER DEFAULT 0,
avv_points INTEGER DEFAULT 0,
last_played TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (player_id) REFERENCES players (id),
FOREIGN KEY (gamesystem_id) REFERENCES gamesystems (id)
)
''')
# 4. Tabelle: matches (Wer hat wann gegen wen in welchem System gespielt?)
cursor.execute('''
CREATE TABLE IF NOT EXISTS matches (
id INTEGER PRIMARY KEY AUTOINCREMENT,
gamesystem_id INTEGER,
player1_id INTEGER,
score_player1 INTEGER,
player2_id INTEGER,
score_player2 INTEGER,
played_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (gamesystem_id) REFERENCES gamesystems (id),
FOREIGN KEY (player1_id) REFERENCES players (id),
FOREIGN KEY (player2_id) REFERENCES players (id)
)
''')
connection.commit()
connection.close()
print("Database structures created successfully!")
seed_gamesystems()
print("Standard Daten in die DB Geschrieben.")
def seed_gamesystems():
connection = sqlite3.connect("warhammer_league.db")
cursor = connection.cursor()
systems = [
("Warhammer 40k", 0, 100),
("Age of Sigmar", 0, 50),
("Spearhead", 0, 50)
]
# executemany ist eine extrem schnelle SQL For-Schleife für Inserts
cursor.executemany("INSERT INTO gamesystems (name, min_score, max_score) VALUES (?, ?, ?)", systems)
connection.commit()
connection.close()
print("Spielsysteme wurden angelegt!")
def get_or_create_player(discord_id, discord_name):
# 1. Verbindung öffnen
connection = sqlite3.connect("warhammer_league.db")
cursor = connection.cursor()
# 2. Suchen, ob der Spieler schon existiert
cursor.execute("SELECT id, name, mmr, points, games FROM players WHERE discord_id = ?", (discord_id,))
player = cursor.fetchone()
# 3. Wenn der Spieler nicht gefunden wurde (ist "None")
if player is None:
# Neu anlegen
cursor.execute("INSERT INTO players (discord_id, name) VALUES (?, ?)", (discord_id, discord_name))
connection.commit()
# Den frisch angelegten Spieler direkt wieder auslesen, um seine neue ID zu bekommen
cursor.execute("SELECT id, name, mmr, points, games FROM players WHERE discord_id = ?", (discord_id,))
player = cursor.fetchone()
# 4. Verbindung schließen
connection.close()
# 5. Daten zurückgeben
return player
def get_all_players():
connection = sqlite3.connect("warhammer_league.db")
cursor = connection.cursor()
# Alle Spieler laden, absteigend sortiert nach MMR
cursor.execute("SELECT id, name, mmr, points, games FROM players ORDER BY mmr DESC")
players = cursor.fetchall()
connection.close()
# Die Daten für das Web-GUI in eine lesbare Form umwandeln (Liste von Dictionaries)
result = []
return result