2026-02-25 15:26:24 +01:00
|
|
|
import sqlite3
|
|
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
init_db()
|
|
|
|
|
|
2026-02-25 15:26:24 +01:00
|
|
|
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;')
|
|
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
# 1. Tabelle: players (Stammdaten)
|
2026-02-25 15:26:24 +01:00
|
|
|
cursor.execute('''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS players (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
discord_id TEXT UNIQUE,
|
|
|
|
|
name TEXT NOT NULL,
|
2026-02-27 10:26:40 +01:00
|
|
|
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,
|
2026-02-25 15:26:24 +01:00
|
|
|
mmr INTEGER DEFAULT 1000,
|
2026-02-27 10:26:40 +01:00
|
|
|
games_in_system INTEGER DEFAULT 0,
|
2026-02-25 15:26:24 +01:00
|
|
|
points INTEGER DEFAULT 0,
|
2026-02-27 10:26:40 +01:00
|
|
|
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)
|
2026-02-25 15:26:24 +01:00
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
# 4. Tabelle: matches (Wer hat wann gegen wen in welchem System gespielt?)
|
2026-02-25 15:26:24 +01:00
|
|
|
cursor.execute('''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS matches (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2026-02-27 10:26:40 +01:00
|
|
|
gamesystem_id INTEGER,
|
2026-02-25 15:26:24 +01:00
|
|
|
player1_id INTEGER,
|
|
|
|
|
score_player1 INTEGER,
|
|
|
|
|
player2_id INTEGER,
|
|
|
|
|
score_player2 INTEGER,
|
2026-02-27 10:26:40 +01:00
|
|
|
played_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
FOREIGN KEY (gamesystem_id) REFERENCES gamesystems (id),
|
2026-02-25 15:26:24 +01:00
|
|
|
FOREIGN KEY (player1_id) REFERENCES players (id),
|
|
|
|
|
FOREIGN KEY (player2_id) REFERENCES players (id)
|
|
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
|
|
|
|
|
connection.commit()
|
|
|
|
|
connection.close()
|
2026-02-27 10:26:40 +01:00
|
|
|
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)
|
|
|
|
|
]
|
2026-02-25 15:26:24 +01:00
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
# 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!")
|
2026-02-25 15:26:24 +01:00
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
|
2026-02-27 12:01:02 +01:00
|
|
|
|
|
|
|
|
def get_or_create_player(discord_id, discord_name, avatar_url):
|
2026-02-25 15:26:24 +01:00
|
|
|
connection = sqlite3.connect("warhammer_league.db")
|
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
2026-02-27 12:01:02 +01:00
|
|
|
# REPARIERT: Wir fragen nur noch id, name und avatar_url aus 'players' ab
|
|
|
|
|
cursor.execute("SELECT id, name, avatar_url FROM players WHERE discord_id = ?", (discord_id,))
|
2026-02-25 15:26:24 +01:00
|
|
|
player = cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
if player is None:
|
2026-02-27 12:01:02 +01:00
|
|
|
cursor.execute("INSERT INTO players (discord_id, name, avatar_url) VALUES (?, ?, ?)", (discord_id, discord_name, avatar_url))
|
2026-02-25 15:26:24 +01:00
|
|
|
connection.commit()
|
2026-02-27 12:01:02 +01:00
|
|
|
cursor.execute("SELECT id, name, avatar_url FROM players WHERE discord_id = ?", (discord_id,))
|
|
|
|
|
player = cursor.fetchone()
|
|
|
|
|
else:
|
|
|
|
|
# Falls sich Name oder Bild auf Discord geändert haben, machen wir ein Update
|
|
|
|
|
cursor.execute("UPDATE players SET name = ?, avatar_url = ? WHERE discord_id = ?", (discord_name, avatar_url, discord_id))
|
|
|
|
|
connection.commit()
|
|
|
|
|
cursor.execute("SELECT id, name, avatar_url FROM players WHERE discord_id = ?", (discord_id,))
|
2026-02-25 15:26:24 +01:00
|
|
|
player = cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
connection.close()
|
|
|
|
|
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
|
2026-02-27 10:26:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|