92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
|
|
import sqlite3
|
||
|
|
|
||
|
|
def init_db():
|
||
|
|
# Neue englische Datenbank-Datei
|
||
|
|
connection = sqlite3.connect("warhammer_league.db")
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
# Fremdschlüssel (Foreign Keys) in SQLite aktivieren
|
||
|
|
cursor.execute('PRAGMA foreign_keys = ON;')
|
||
|
|
|
||
|
|
# Tabelle: players (ehemals spieler)
|
||
|
|
cursor.execute('''
|
||
|
|
CREATE TABLE IF NOT EXISTS players (
|
||
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
|
discord_id TEXT UNIQUE,
|
||
|
|
name TEXT NOT NULL,
|
||
|
|
mmr INTEGER DEFAULT 1000,
|
||
|
|
points INTEGER DEFAULT 0,
|
||
|
|
games INTEGER DEFAULT 0
|
||
|
|
)
|
||
|
|
''')
|
||
|
|
|
||
|
|
# Tabelle: matches (ehemals spiele)
|
||
|
|
cursor.execute('''
|
||
|
|
CREATE TABLE IF NOT EXISTS matches (
|
||
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
|
player1_id INTEGER,
|
||
|
|
score_player1 INTEGER,
|
||
|
|
player2_id INTEGER,
|
||
|
|
score_player2 INTEGER,
|
||
|
|
FOREIGN KEY (player1_id) REFERENCES players (id),
|
||
|
|
FOREIGN KEY (player2_id) REFERENCES players (id)
|
||
|
|
)
|
||
|
|
''')
|
||
|
|
|
||
|
|
connection.commit()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
print("Warhammer database successfully initialized!")
|
||
|
|
|
||
|
|
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
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
init_db()
|
||
|
|
|
||
|
|
|
||
|
|
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 = []
|
||
|
|
for player in players:
|
||
|
|
result.append({
|
||
|
|
'id': player[0],
|
||
|
|
'name': player[1],
|
||
|
|
'mmr': player[2],
|
||
|
|
'points': player[3],
|
||
|
|
'games': player[4]
|
||
|
|
})
|
||
|
|
|
||
|
|
return result
|