SQL DB Setup
This commit is contained in:
parent
9062d39a17
commit
bfdbb9b99a
|
|
@ -1 +1 @@
|
|||
{"authenticated":true,"discord_id":"277898241750859776","discord_name":"mrteels","db_id":1}
|
||||
{}
|
||||
76
database.py
76
database.py
|
|
@ -1,33 +1,63 @@
|
|||
import sqlite3
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
|
||||
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)
|
||||
# 1. Tabelle: players (Stammdaten)
|
||||
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
|
||||
avatar_url TEXT
|
||||
)
|
||||
''')
|
||||
|
||||
# Tabelle: matches (ehemals spiele)
|
||||
# 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,
|
||||
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,
|
||||
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,
|
||||
FOREIGN KEY (gamesystem_id) REFERENCES gamesystems (id),
|
||||
FOREIGN KEY (player1_id) REFERENCES players (id),
|
||||
FOREIGN KEY (player2_id) REFERENCES players (id)
|
||||
)
|
||||
|
|
@ -35,9 +65,27 @@ def init_db():
|
|||
|
||||
connection.commit()
|
||||
connection.close()
|
||||
print("Database structures created successfully!")
|
||||
seed_gamesystems()
|
||||
print("Standard Daten in die DB Geschrieben.")
|
||||
|
||||
print("Warhammer database successfully initialized!")
|
||||
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)
|
||||
]
|
||||
|
||||
# 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!")
|
||||
|
||||
|
||||
def get_or_create_player(discord_id, discord_name):
|
||||
# 1. Verbindung öffnen
|
||||
connection = sqlite3.connect("warhammer_league.db")
|
||||
|
|
@ -63,9 +111,6 @@ def get_or_create_player(discord_id, discord_name):
|
|||
# 5. Daten zurückgeben
|
||||
return player
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
|
||||
|
||||
def get_all_players():
|
||||
connection = sqlite3.connect("warhammer_league.db")
|
||||
|
|
@ -79,13 +124,8 @@ def get_all_players():
|
|||
|
||||
# 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
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -22,14 +22,15 @@ def setup_routes():
|
|||
def home_page():
|
||||
ui.dark_mode(True)
|
||||
|
||||
# --- Profil Block Oben ---
|
||||
with ui.card().classes('w-full items-center mt-10'):
|
||||
# --- LOGIN BEREICH ---
|
||||
if app.storage.user.get('authenticated', False):
|
||||
discord_name = app.storage.user.get('discord_name')
|
||||
db_id = app.storage.user.get('db_id')
|
||||
with ui.row():
|
||||
discord_name = app.storage.user.get('discord_name')
|
||||
db_id = app.storage.user.get('db_id')
|
||||
|
||||
ui.label(f'Welcome back, {discord_name}!').classes('text-green-500 font-bold')
|
||||
ui.button('Enter Match', on_click=lambda: ui.navigate.to('/add-match')).classes('mt-4 bg-blue-500 text-white')
|
||||
ui.label(f'Welcome back, {discord_name}!').classes('text-green-500 font-bold')
|
||||
ui.button('Enter Match', on_click=lambda: ui.navigate.to('/add-match')).classes('mt-4 bg-blue-500 text-white')
|
||||
|
||||
|
||||
|
||||
|
|
@ -58,28 +59,6 @@ def setup_routes():
|
|||
|
||||
|
||||
|
||||
|
||||
# --- LEADERBOARD BEREICH ---
|
||||
if app.storage.user.get('authenticated', False):
|
||||
with ui.card().classes('w-full items-center mt-6'):
|
||||
ui.label('Leaderboard').classes('text-xl font-bold mb-4')
|
||||
|
||||
# 1. Spalten (Columns) für die Tabelle definieren
|
||||
table_columns = [
|
||||
{'name': 'name', 'label': 'Player', 'field': 'name', 'align': 'left'},
|
||||
{'name': 'mmr', 'label': 'MMR', 'field': 'mmr', 'sortable': True},
|
||||
{'name': 'points', 'label': 'Points', 'field': 'points', 'sortable': True},
|
||||
{'name': 'games', 'label': 'Games Played', 'field': 'games', 'sortable': True},
|
||||
]
|
||||
|
||||
# 2. Daten (Rows) aus der Datenbank holen
|
||||
player_data = database.get_all_players()
|
||||
|
||||
# 3. Tabelle zeichnen
|
||||
ui.table(columns=table_columns, rows=player_data, row_key='name').classes('w-full max-w-4xl')
|
||||
|
||||
|
||||
|
||||
# --- DISCORD CALLBACK PAGE ---
|
||||
@ui.page('/login/discord')
|
||||
def discord_callback(code: str = None):
|
||||
|
|
|
|||
19
main.py
19
main.py
|
|
@ -3,10 +3,29 @@ from dotenv import load_dotenv
|
|||
from nicegui import ui
|
||||
from gui import main_gui
|
||||
from gui import match_gui
|
||||
import database
|
||||
|
||||
# 1. Lade die geheimen Variablen aus der .env Datei in den Speicher
|
||||
load_dotenv()
|
||||
|
||||
# --- DATENBANK CHECK ---
|
||||
# Prüfen, ob die Datei im aktuellen Ordner existiert
|
||||
db_file = "warhammer_league.db"
|
||||
|
||||
if not os.path.exists(db_file):
|
||||
print(f"WARNUNG: '{db_file}' nicht gefunden!")
|
||||
print("Starte Datenbank-Einrichtung...")
|
||||
|
||||
# 1. Erstellt die leere Datei und alle Tabellen-Strukturen
|
||||
database.init_db()
|
||||
database.seed_gamesystems()
|
||||
|
||||
print("Datenbank erfolgreich aufgebaut!")
|
||||
else:
|
||||
print(f"OK: Datenbank '{db_file}' gefunden. Lade System...")
|
||||
# -----------------------
|
||||
|
||||
|
||||
# 2. Variablen abrufen
|
||||
client_id = os.getenv("DISCORD_CLIENT_ID")
|
||||
client_secret = os.getenv("DISCORD_CLIENT_SECRET")
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user