import { GAME_CONFIG, seedStr, updateTeamSegmented, updateResetCountdown, fetchConfig, fetchGridForSeed, fetchAndApplyScores, refreshFromServer, draw, } from "./game.js"; import { tryRestoreSession, showAuthOverlay, hideAuthOverlay, } from "./auth.js"; // ── DOM refs ────────────────────────────────────────────────────────────────── const refreshBtn = document.getElementById("refreshBtn"); const hint = document.getElementById("hint"); const cooldownEl = document.getElementById("cooldownConfig"); // ── Polling ─────────────────────────────────────────────────────────────────── let configPollTimer = 0; let scorePollTimer = 0; let resetTimer = null; function scheduleConfigPoll() { clearTimeout(configPollTimer); const ms = Math.max(5_000, GAME_CONFIG.configReloadIntervalSeconds * 1_000); configPollTimer = window.setTimeout(async () => { try { const changed = await fetchConfig(); if (changed) await refreshFromServer(); } catch { /* ignore */ } scheduleConfigPoll(); }, ms); } function scheduleScorePoll() { clearTimeout(scorePollTimer); scorePollTimer = window.setTimeout(async () => { await fetchAndApplyScores(); scheduleScorePoll(); }, 5_000); } // ── Boot ────────────────────────────────────────────────────────────────────── async function boot() { updateTeamSegmented(); const restored = await tryRestoreSession(); if (!restored) { showAuthOverlay(); } else { hideAuthOverlay(); } try { await fetchConfig(); await fetchGridForSeed(seedStr); await fetchAndApplyScores(); } catch { hint.textContent = "API unavailable — start the Node server (docker-compose up --build)."; cooldownEl.textContent = "?"; } draw(); if (resetTimer) clearInterval(resetTimer); resetTimer = setInterval(updateResetCountdown, 1_000); updateResetCountdown(); scheduleConfigPoll(); scheduleScorePoll(); } // ── Global event listeners ──────────────────────────────────────────────────── refreshBtn.addEventListener("click", () => refreshFromServer()); // ── Start ───────────────────────────────────────────────────────────────────── boot();