async function sendChunk() { const ticksToSend = currentTick - lastSentTick; if (ticksToSend <= 0) return; const payload = { sessionId: sessionID, inputs: [...inputLog], totalTicks: ticksToSend }; inputLog = []; lastSentTick = currentTick; try { const res = await fetch('/api/validate', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(payload) }); const data = await res.json(); if (data.serverObs) serverObstacles = data.serverObs; if (data.status === "dead") { console.error("SERVER TOT", data); gameOver("Vom Server gestoppt"); } else { const sScore = data.verifiedScore; if (Math.abs(score - sScore) > 200) score = sScore; } } catch (e) { console.error("Netzwerkfehler:", e); } } window.submitScore = async function() { const nameInput = document.getElementById('playerNameInput'); const name = nameInput.value; const btn = document.getElementById('submitBtn'); if (!name) return alert("Namen eingeben!"); btn.disabled = true; try { const res = await fetch('/api/submit-name', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ sessionId: sessionID, name: name }) }); const data = await res.json(); let myClaims = JSON.parse(localStorage.getItem('escape_claims') || '[]'); myClaims.push({ name: name, score: Math.floor(score / 10), code: data.claimCode, date: new Date().toLocaleString('de-DE'), sessionId: sessionID }); localStorage.setItem('escape_claims', JSON.stringify(myClaims)); document.getElementById('inputSection').style.display = 'none'; loadLeaderboard(); } catch (e) {} }; async function loadLeaderboard() { const res = await fetch(`/api/leaderboard?sessionId=${sessionID}`); const entries = await res.json(); let html = "

BESTENLISTE

"; entries.forEach(e => { const color = e.isMe ? "yellow" : "white"; html += `
#${e.rank} ${e.name}${Math.floor(e.score/10)}
`; }); document.getElementById('leaderboard').innerHTML = html; } async function loadStartScreenLeaderboard() { try { const listEl = document.getElementById('startLeaderboardList'); if (!listEl) return; const res = await fetch('/api/leaderboard'); const entries = await res.json(); if (entries.length === 0) { listEl.innerHTML = "
Noch keine Scores.
"; return; } let html = ""; entries.forEach(e => { let icon = "#" + e.rank; if (e.rank === 1) icon = "🥇"; if (e.rank === 2) icon = "🥈"; if (e.rank === 3) icon = "🥉"; html += `
${icon} ${e.name}${Math.floor(e.score / 10)}
`; }); listEl.innerHTML = html; } catch (e) {} }