Liga-System/data/setup_database.py

140 lines
4.7 KiB
Python
Raw Normal View History

import sqlite3
import os
DB_PATH = "data/warhammer_league.db"
2026-02-27 10:26:40 +01:00
def init_db():
connection = sqlite3.connect(DB_PATH)
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,
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,
2026-03-02 16:17:50 +01:00
picture TEXT,
description TEXT,
2026-02-27 10:26:40 +01:00
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,
2026-02-27 10:26:40 +01:00
games_in_system INTEGER DEFAULT 0,
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-27 10:26:40 +01:00
# 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,
2026-02-27 10:26:40 +01:00
gamesystem_id INTEGER,
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),
FOREIGN KEY (player1_id) REFERENCES players (id),
FOREIGN KEY (player2_id) REFERENCES players (id)
)
''')
# 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)
)
''')
connection.commit()
connection.close()
print("Datenbank Struktur aufgebaut")
#nächster Schritt: Standard Spielsysteme eintragen
seed_gamesystems()
2026-02-27 10:26:40 +01:00
# --------------------
# --- Start Setup der DB Daten. Damit sie nicht GANZ leer sind.
# --------------------
2026-02-27 10:26:40 +01:00
def seed_gamesystems():
connection = sqlite3.connect(DB_PATH)
2026-02-27 10:26:40 +01:00
cursor = connection.cursor()
systems = [
2026-03-02 16:17:50 +01:00
("Warhammer 40k","/gui/pictures/wsdg.png","Die Schlacht um die Galaxie in einer entfernten Zukunft." , 0, 100),
("Age of Sigmar","/gui/pictures/wsdg.png","" , 0, 50),
("Spearhead","/gui/pictures/wsdg.png","" , 0, 50)
2026-02-27 10:26:40 +01:00
]
2026-02-27 10:26:40 +01:00
# executemany ist eine extrem schnelle SQL For-Schleife für Inserts
2026-03-02 16:17:50 +01:00
cursor.executemany("INSERT INTO gamesystems (name, picture, description, min_score, max_score) VALUES (?, ?, ?, ?, ?)", systems)
2026-02-27 10:26:40 +01:00
connection.commit()
connection.close()
print("Spielsysteme angelegt!")
#Nächster Schritt: Standard Achievments eintragen.
seed_achievements()
def seed_achievements():
connection = sqlite3.connect(DB_PATH)
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()
print("Achievements angelegt.")
2026-02-27 10:26:40 +01:00