2026-02-25 15:26:24 +01:00
|
|
|
import sqlite3
|
2026-03-02 10:16:30 +01:00
|
|
|
import os
|
2026-02-25 15:26:24 +01:00
|
|
|
|
2026-03-02 10:16:30 +01:00
|
|
|
DB_PATH = "data/warhammer_league.db"
|
2026-02-27 10:26:40 +01:00
|
|
|
|
2026-02-25 15:26:24 +01:00
|
|
|
def init_db():
|
2026-03-02 10:16:30 +01:00
|
|
|
connection = sqlite3.connect(DB_PATH)
|
2026-02-25 15:26:24 +01:00
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
|
|
# Fremdschlüssel (Foreign Keys) in SQLite aktivieren
|
|
|
|
|
cursor.execute('PRAGMA foreign_keys = ON;')
|
|
|
|
|
|
2026-03-02 10:16:30 +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,
|
2026-03-01 20:34:42 +01:00
|
|
|
discord_name TEXT NOT NULL,
|
|
|
|
|
discord_avatar_url TEXT,
|
|
|
|
|
display_name TEXT
|
2026-02-27 10:26:40 +01:00
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
|
2026-03-01 20:34:42 +01:00
|
|
|
# 5. Tabelle: achievements (Der globale Katalog aller möglichen Erfolge - Stammdaten)
|
|
|
|
|
cursor.execute('''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS achievements (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
|
description TEXT NOT NULL,
|
|
|
|
|
icon TEXT
|
|
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
|
|
|
|
|
# 6. Tabelle: player_achievements (Wer hat welchen Erfolg wann bekommen? - Bewegungsdaten)
|
|
|
|
|
cursor.execute('''
|
|
|
|
|
CREATE TABLE IF NOT EXISTS player_achievements (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
player_id INTEGER,
|
|
|
|
|
achievement_id INTEGER,
|
|
|
|
|
earned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
|
earned_in INTEGER,
|
|
|
|
|
FOREIGN KEY (earned_in) REFERENCES gamesystems (id),
|
|
|
|
|
FOREIGN KEY (player_id) REFERENCES players (id),
|
|
|
|
|
FOREIGN KEY (achievement_id) REFERENCES achievements (id)
|
|
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
|
2026-02-25 15:26:24 +01:00
|
|
|
connection.commit()
|
|
|
|
|
connection.close()
|
2026-03-02 10:16:30 +01:00
|
|
|
print("Datenbank Struktur aufgebaut")
|
|
|
|
|
|
|
|
|
|
#nächster Schritt: Standard Spielsysteme eintragen
|
|
|
|
|
seed_gamesystems()
|
|
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
|
2026-03-01 20:34:42 +01:00
|
|
|
# --------------------
|
|
|
|
|
# --- Start Setup der DB Daten. Damit sie nicht GANZ leer sind.
|
|
|
|
|
# --------------------
|
2026-02-27 10:26:40 +01:00
|
|
|
def seed_gamesystems():
|
2026-03-02 10:16:30 +01:00
|
|
|
connection = sqlite3.connect(DB_PATH)
|
2026-02-27 10:26:40 +01:00
|
|
|
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()
|
2026-03-02 10:16:30 +01:00
|
|
|
print("Spielsysteme angelegt!")
|
|
|
|
|
|
|
|
|
|
#Nächster Schritt: Standard Achievments eintragen.
|
|
|
|
|
seed_achievements()
|
2026-02-25 15:26:24 +01:00
|
|
|
|
2026-03-01 20:34:42 +01:00
|
|
|
def seed_achievements():
|
2026-03-02 10:16:30 +01:00
|
|
|
connection = sqlite3.connect(DB_PATH)
|
2026-03-01 20:34:42 +01:00
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
|
|
# Unsere Start-Stückliste an Erfolgen (Name, Beschreibung, Icon)
|
|
|
|
|
achievements = [
|
|
|
|
|
("Kingslayer", "Hat den Spieler auf Platz 1 besiegt!", "👑"),
|
|
|
|
|
("Unstoppable", "3 Spiele in Folge gewonnen.", "🔥"),
|
|
|
|
|
("First Blood", "Das allererste Ligaspiel absolviert.", "🩸")
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
cursor.executemany("INSERT INTO achievements (name, description, icon) VALUES (?, ?, ?)", achievements)
|
|
|
|
|
|
|
|
|
|
connection.commit()
|
|
|
|
|
connection.close()
|
2026-03-02 10:16:30 +01:00
|
|
|
print("Achievements angelegt.")
|
2026-03-01 20:34:42 +01:00
|
|
|
|
2026-02-27 10:26:40 +01:00
|
|
|
|