diff --git a/cmd/client/connection_wasm.go b/cmd/client/connection_wasm.go index 3ca4379..3f02464 100644 --- a/cmd/client/connection_wasm.go +++ b/cmd/client/connection_wasm.go @@ -190,6 +190,17 @@ func (g *Game) connectForLeaderboard() { return } + // Temporäre Daten für Leaderboard-Verbindung + if g.playerName == "" { + g.playerName = "LeaderboardViewer" + } + if g.roomID == "" { + g.roomID = "leaderboard_only" + } + if g.gameMode == "" { + g.gameMode = "solo" + } + // Neue Verbindung aufbauen g.connectToServer() @@ -200,6 +211,13 @@ func (g *Game) connectForLeaderboard() { // requestLeaderboard fordert das Leaderboard an func (g *Game) requestLeaderboard() { + // Verbindung aufbauen falls nicht vorhanden + if g.wsConn == nil || !g.wsConn.connected { + log.Println("📡 Keine Verbindung für Leaderboard, stelle Verbindung her...") + go g.connectForLeaderboard() + return + } + mode := "solo" if g.gameMode == "coop" { mode = "coop" diff --git a/cmd/client/gamestart_native.go b/cmd/client/gamestart_native.go new file mode 100644 index 0000000..2e25606 --- /dev/null +++ b/cmd/client/gamestart_native.go @@ -0,0 +1,9 @@ +//go:build !wasm +// +build !wasm + +package main + +// notifyGameStarted ist ein Stub für native Builds +func (g *Game) notifyGameStarted() { + // Native hat kein JavaScript-Interface +} diff --git a/cmd/client/gamestart_wasm.go b/cmd/client/gamestart_wasm.go new file mode 100644 index 0000000..f5687af --- /dev/null +++ b/cmd/client/gamestart_wasm.go @@ -0,0 +1,17 @@ +//go:build js && wasm +// +build js,wasm + +package main + +import ( + "log" + "syscall/js" +) + +// notifyGameStarted benachrichtigt JavaScript dass das Spiel startet +func (g *Game) notifyGameStarted() { + if startFunc := js.Global().Get("onGameStarted"); !startFunc.IsUndefined() { + startFunc.Invoke() + log.Println("🎮 Game Started - Benachrichtigung an JavaScript gesendet") + } +} diff --git a/cmd/client/lobby_native.go b/cmd/client/lobby_native.go new file mode 100644 index 0000000..adea0a9 --- /dev/null +++ b/cmd/client/lobby_native.go @@ -0,0 +1,9 @@ +//go:build !wasm +// +build !wasm + +package main + +// sendLobbyUpdateToJS ist ein Stub für native Builds (kein HTML-Interface) +func (g *Game) sendLobbyUpdateToJS() { + // Native hat kein HTML-Overlay, nichts zu tun +} diff --git a/cmd/client/lobby_wasm.go b/cmd/client/lobby_wasm.go new file mode 100644 index 0000000..01d1feb --- /dev/null +++ b/cmd/client/lobby_wasm.go @@ -0,0 +1,9 @@ +//go:build js && wasm +// +build js,wasm + +package main + +// sendLobbyUpdateToJS sendet Lobby-Updates an das HTML-Interface +func (g *Game) sendLobbyUpdateToJS() { + g.sendLobbyPlayersToJS() +} diff --git a/cmd/client/main.go b/cmd/client/main.go index bc771ce..3b5d541 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -367,7 +367,10 @@ func (g *Game) updateLobby() { // Spiel wurde gestartet? if g.gameState.Status == "COUNTDOWN" || g.gameState.Status == "RUNNING" { - g.appState = StateGame + if g.appState != StateGame { + g.appState = StateGame + g.notifyGameStarted() // Benachrichtige JavaScript dass Spiel startet + } } } diff --git a/cmd/client/web/game.js b/cmd/client/web/game.js index c46fed9..9cc397c 100644 --- a/cmd/client/web/game.js +++ b/cmd/client/web/game.js @@ -3,6 +3,83 @@ let wasmReady = false; let gameStarted = false; let audioMuted = false; +// WebSocket for Leaderboard (direct JS connection) +let leaderboardWS = null; +let leaderboardSessionID = 'lb_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); + +function connectLeaderboardWebSocket() { + if (leaderboardWS && leaderboardWS.readyState === WebSocket.OPEN) { + return; // Already connected + } + + const wsURL = 'ws://localhost:8080/ws'; + leaderboardWS = new WebSocket(wsURL); + + leaderboardWS.onopen = () => { + console.log('📡 Leaderboard WebSocket connected with session:', leaderboardSessionID); + + // Send JOIN message to register session + const joinMsg = JSON.stringify({ + type: 'join', + payload: { + name: leaderboardSessionID, + room_id: 'leaderboard_viewer', + game_mode: 'solo', + is_host: false, + team_name: '' + } + }); + leaderboardWS.send(joinMsg); + console.log('📝 Registered leaderboard session:', leaderboardSessionID); + }; + + leaderboardWS.onmessage = (event) => { + try { + const msg = JSON.parse(event.data); + + if (msg.type === 'leaderboard_response') { + console.log('📊 Received leaderboard:', msg.payload?.entries?.length || 0, 'entries'); + updateLeaderboard(msg.payload?.entries || []); + } + } catch (e) { + console.error('Failed to parse WebSocket message:', e); + } + }; + + leaderboardWS.onerror = (error) => { + console.error('❌ Leaderboard WebSocket error:', error); + }; + + leaderboardWS.onclose = () => { + console.log('📡 Leaderboard WebSocket closed'); + leaderboardWS = null; + }; +} + +function requestLeaderboardDirect() { + connectLeaderboardWebSocket(); + + // Wait for connection then request + const checkAndRequest = setInterval(() => { + if (leaderboardWS && leaderboardWS.readyState === WebSocket.OPEN) { + clearInterval(checkAndRequest); + + const msg = JSON.stringify({ + type: 'leaderboard_request', + payload: { + mode: 'solo', + limit: 10 + } + }); + leaderboardWS.send(msg); + console.log('📤 Requesting leaderboard via WebSocket (session:', leaderboardSessionID + ')'); + } + }, 100); + + // Timeout after 3 seconds + setTimeout(() => clearInterval(checkAndRequest), 3000); +} + // Initialize WASM async function initWASM() { const go = new Go(); @@ -17,11 +94,9 @@ async function initWASM() { console.log('✅ WASM loaded successfully'); - // Load initial leaderboard + // Load initial leaderboard via direct WebSocket setTimeout(() => { - if (window.requestLeaderboard) { - window.requestLeaderboard(); - } + requestLeaderboardDirect(); }, 500); } catch (err) { console.error('❌ Failed to load WASM:', err); @@ -187,23 +262,13 @@ function joinRoom() { // Lobby Functions function startGameFromLobby() { - // Host startet das Spiel - hideAllScreens(); - document.getElementById('menu').style.display = 'none'; - gameStarted = true; - - // Canvas sichtbar machen - const canvas = document.querySelector('canvas'); - if (canvas) { - canvas.classList.add('game-active'); - } - + // Host startet das Spiel - Lobby bleibt sichtbar bis Server COUNTDOWN/RUNNING sendet // Signal an WASM senden dass Spiel starten soll if (window.startGameFromLobby_WASM) { window.startGameFromLobby_WASM(); } - console.log('🎮 Host started game from lobby'); + console.log('🎮 Host requested game start - waiting for server...'); } function leaveLobby() { @@ -230,14 +295,12 @@ function loadLeaderboard() { const list = document.getElementById('leaderboardList'); list.innerHTML = '