import os import socket import getpass from datetime import datetime from models.server_config import ServerConfig class ServerTUI: """Interaktive Konsolen-Oberfläche (TUI) für die Server-Konfiguration und Live-Logs.""" @staticmethod def clear_screen(): os.system("cls" if os.name == "nt" else "clear") @staticmethod def get_local_lan_ip() -> str: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(("10.255.255.255", 1)) ip = s.getsockname()[0] except Exception: ip = "127.0.0.1" finally: s.close() return ip @classmethod def print_banner(cls): cls.clear_screen() print("============================================================") print(" 🦇 Y A H T Z E E - D E D I C A T E D S E R V E R ") print("============================================================") @classmethod def run_setup_wizard(cls, config: ServerConfig) -> ServerConfig: cls.print_banner() lan_ip = cls.get_local_lan_ip() if config.load(): print(f"\n[i] Gespeicherte Konfiguration gefunden:") print(f" - Server-Name : {config.server_name}") print(f" - Modus : {'Lokales Netzwerk (LAN)' if config.is_lan_only else 'Alle Netzwerke / Internet (0.0.0.0)'}") print(f" - Bind-IP:Port: {config.bind_ip}:{config.port} (LAN-IP: {lan_ip})") print(f" - Max. Spieler: {config.max_players_per_room} | Bots erlaubt: {'Ja' if config.allow_bots else 'Nein'}") use_existing = input("\nMöchtest du mit diesen Einstellungen starten? [J/n]: ").strip().lower() if use_existing in ("", "j", "y", "ja", "yes"): return config print("\n--- SERVER KONFIGURATION ---") # 1. Server-Name name_in = input(f"1. Server-Name [{config.server_name}]: ").strip() if name_in: config.server_name = name_in # 2. Netzwerk-Modus (LAN vs. Alle Interfaces) print(f"\n2. Netzwerk-Modus (Deine erkannte LAN-IP ist: {lan_ip})") print(" [1] Lokales Netzwerk (LAN) - Binde direkt an LAN-IP") print(" [2] Alle Interfaces (0.0.0.0) - Für LAN, VPN & Port-Forwarding") net_choice = input(" Auswahl [1/2, Standard=2]: ").strip() if net_choice == "1": config.is_lan_only = True config.bind_ip = lan_ip else: config.is_lan_only = False config.bind_ip = "0.0.0.0" # 3. Port-Abfrage while True: port_in = input(f"\n3. Server-Port (1024-65535) [{config.port}]: ").strip() if not port_in: break if port_in.isdigit() and 1024 <= int(port_in) <= 65535: config.port = int(port_in) break print(" [!] Bitte gib einen gültigen Port zwischen 1024 und 65535 ein.") # 4. Spiel-Optionen (Was ist möglich?) while True: max_p = input(f"\n4. Maximale Spieler pro Match (2-4) [{config.max_players_per_room}]: ").strip() if not max_p: break if max_p.isdigit() and 2 <= int(max_p) <= 4: config.max_players_per_room = int(max_p) break print(" [!] Bitte eine Zahl zwischen 2 und 4 eingeben.") bots_in = input("5. KI-Bots zum Auffüllen von Matches erlauben? [J/n]: ").strip().lower() config.allow_bots = bots_in not in ("n", "no", "nein") # 6. Server-Passwort print("\n6. Server-Passwort (muss von Clients vor dem Account-Login eingegeben werden)") while True: try: srv_pw = getpass.getpass(" Neues Server-Passwort eingeben (unsichtbar): ").strip() except Exception: srv_pw = input(" Neues Server-Passwort eingeben: ").strip() if len(srv_pw) >= 3: config.set_server_password(srv_pw) break print(" [!] Das Server-Passwort muss mindestens 3 Zeichen lang sein!") config.save() print("\n[✔] Konfiguration gespeichert in 'server_config.json'!") return config @classmethod def show_running_header(cls, config: ServerConfig, total_accounts: int): cls.print_banner() lan_ip = cls.get_local_lan_ip() print(f" Server-Name : {config.server_name}") print(f" Lausche auf : {config.bind_ip}:{config.port}") print(f" Erreichbar (LAN) : {lan_ip}:{config.port}") print(f" Optionen : Max. {config.max_players_per_room} Spieler | Bots: {'Aktiv' if config.allow_bots else 'Aus'}") print(f" Server-Passwort : {'Geschützt (Aktiv)' if config.server_pw_hash else 'Keins'}") print(f" Reg. Accounts : {total_accounts}") print("------------------------------------------------------------") print(" Befehle im laufenden Betrieb: 'status', 'users', 'stop'") print("============================================================\n") @staticmethod def log(tag: str, message: str): ts = datetime.now().strftime("%H:%M:%S") print(f"[{ts}] [{tag}] {message}", flush=True)