215 lines
7.1 KiB
Python
215 lines
7.1 KiB
Python
|
|
import sqlite3
|
|
import os
|
|
import json
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "league_database.db")
|
|
|
|
|
|
dummy_is_in = True
|
|
|
|
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
|
|
)
|
|
''')
|
|
|
|
# 2. Tabelle: gamesystems (Globale Spielsysteme)
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS gamesystems (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL,
|
|
picture TEXT,
|
|
description TEXT,
|
|
active_players INTEGER DEFAULT 0,
|
|
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,
|
|
trend INTEGER DEFAULT 0,
|
|
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,
|
|
|
|
player1_base_change INTEGER,
|
|
player1_khorne INTEGER,
|
|
player1_slaanesh INTEGER,
|
|
player1_tzeentch INTEGER,
|
|
player1_mmr_change INTEGER,
|
|
|
|
player2_base_change INTEGER,
|
|
player2_khorne INTEGER,
|
|
player2_slaanesh INTEGER,
|
|
player2_tzeentch INTEGER,
|
|
player2_mmr_change INTEGER,
|
|
|
|
rust_factor REAL,
|
|
elo_factor REAL,
|
|
point_inflation REAL,
|
|
|
|
player2_check INTEGER DEFAULT 0,
|
|
match_is_counted INTEGER DEFAULT 0,
|
|
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
|
|
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()
|
|
|
|
|
|
# --------------------
|
|
# --- Start Setup der DB Daten. Damit sie nicht GANZ leer sind.
|
|
# --------------------
|
|
def seed_gamesystems():
|
|
connection = sqlite3.connect(DB_PATH)
|
|
cursor = connection.cursor()
|
|
systems = [
|
|
("Warhammer 40k", "40k_logo.png", "Die Schlacht um die Galaxie in einer entfernten Zukunft.", 0, 100),
|
|
("Warhammer Age of Sigmar", "aos_logo.png", "Der ewige Krieg um die Reiche der Sterblichen in einer epischen Fantasy-Welt.", 0, 50),
|
|
("Spearhead", "aos_logo.png", "Schnelle und taktische Scharmützel für actiongeladene Kämpfe.", 0, 50)
|
|
]
|
|
|
|
# executemany ist eine extrem schnelle SQL For-Schleife für Inserts
|
|
cursor.executemany("INSERT INTO gamesystems (name, picture, description, min_score, max_score) VALUES (?, ?, ?, ?, ?)", systems)
|
|
|
|
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.")
|
|
|
|
if dummy_is_in:
|
|
seed_dummy_player()
|
|
|
|
|
|
def seed_dummy_player():
|
|
connection = sqlite3.connect(DB_PATH)
|
|
cursor = connection.cursor()
|
|
|
|
# 1. Dummy-Spieler anlegen (falls noch nicht vorhanden)
|
|
query_player = """
|
|
INSERT OR IGNORE INTO players
|
|
(discord_id, discord_name, display_name, discord_avatar_url)
|
|
VALUES (?, ?, ?, ?)
|
|
"""
|
|
cursor.execute(query_player, ("dummy_001", "dummy_user", "Dummy Mc DummDumm", ""))
|
|
|
|
# 2. Wir holen uns die ID des Dummys (egal ob neu oder alt)
|
|
cursor.execute("SELECT id FROM players WHERE discord_id = 'dummy_001'")
|
|
dummy_row = cursor.fetchone()
|
|
|
|
if dummy_row:
|
|
dummy_id = dummy_row[0]
|
|
|
|
# 3. Wir holen alle IDs der aktuellen Spielsysteme
|
|
cursor.execute("SELECT id FROM gamesystems")
|
|
systems = cursor.fetchall()
|
|
|
|
# 4. Wir gehen jedes System durch und prüfen, ob er schon drin ist
|
|
for sys in systems:
|
|
sys_id = sys[0]
|
|
|
|
cursor.execute("SELECT id FROM player_game_statistic WHERE player_id = ? AND gamesystem_id = ?", (dummy_id, sys_id))
|
|
is_in_league = cursor.fetchone()
|
|
|
|
if not is_in_league:
|
|
# Er ist noch nicht drin -> Eintragen! (MMR startet durch DEFAULT automatisch bei 1000)
|
|
cursor.execute("""
|
|
INSERT INTO player_game_statistic (player_id, gamesystem_id)
|
|
VALUES (?, ?)
|
|
""", (dummy_id, sys_id))
|
|
|
|
connection.commit()
|
|
connection.close()
|
|
print("Test-Gegner 'Dummy Mc DummDumm' ist bereit und in allen Ligen angemeldet!")
|
|
|
|
|