const state = {
me: null,
phase: null,
counts: null,
mySuggestions: [],
allSuggestions: [],
myVotes: [],
results: [],
adminKey: ""
};
const $ = (id) => document.getElementById(id);
const toastEl = $("toast");
function toast(msg, isError = false) {
toastEl.textContent = msg;
toastEl.classList.remove("hidden");
toastEl.classList.toggle("error", isError);
setTimeout(() => toastEl.classList.add("hidden"), 2400);
}
async function api(path, options = {}) {
const res = await fetch(path, {
headers: {
"Content-Type": "application/json",
...(options.adminKey ? { "X-Admin-Key": options.adminKey } : {})
},
...options
});
if (!res.ok) {
let msg = `${res.status}`;
try {
const body = await res.json();
msg = body.error || JSON.stringify(body);
} catch (_) { /* ignore */ }
throw new Error(msg);
}
return res.status === 204 ? null : res.json();
}
async function loadState() {
const [me, stateData] = await Promise.all([
api("/api/me"),
api("/api/state")
]);
state.me = me;
state.phase = stateData.currentPhase;
state.counts = stateData;
renderPhasePill();
renderCounts();
const nameInput = $("name-input");
if (!nameInput.dataset.userEditing) {
nameInput.value = me.displayName || "";
}
$("player-id").textContent = `Player ID: ${me.id}`;
}
async function loadSuggestData() {
if (state.phase !== "Suggest") return;
state.mySuggestions = await api("/api/suggestions/mine");
renderMySuggestions();
}
async function loadRevealData() {
if (state.phase === "Reveal" || state.phase === "Vote" || state.phase === "Results") {
state.allSuggestions = await api("/api/suggestions/all");
renderAllSuggestions();
}
}
async function loadVoteData() {
if (state.phase !== "Vote") return;
state.myVotes = await api("/api/votes/mine");
renderVotes();
}
async function loadResults() {
if (state.phase !== "Results") return;
state.results = await api("/api/results");
renderResults();
}
function renderPhasePill() {
$("phase-pill").textContent = state.phase || "Loading…";
document.querySelectorAll(".phase-view").forEach((el) => el.classList.add("hidden"));
const viewMap = {
Suggest: "suggest-view",
Reveal: "reveal-view",
Vote: "vote-view",
Results: "results-view"
};
const id = viewMap[state.phase];
if (id) $(id).classList.remove("hidden");
const phaseSelect = $("phase-select");
if (!phaseSelect.dataset.userEditing) {
phaseSelect.value = state.phase || "Suggest";
}
}
function renderCounts() {
if (!state.counts) return;
$("phase-description").textContent = `Phase: ${state.phase}`;
$("counts").textContent = `Players: ${state.counts.players} • Suggestions: ${state.counts.suggestions} • Votes: ${state.counts.votes}`;
}
function renderMySuggestions() {
const list = $("my-suggestions");
list.innerHTML = "";
state.mySuggestions.forEach((s) => {
const li = document.createElement("li");
li.innerHTML = `${s.name}${s.genre ? ` · ${s.genre}` : ""}
${s.description || ""}`;
list.appendChild(li);
});
}
function renderAllSuggestions() {
const list = $("all-suggestions");
list.innerHTML = "";
state.allSuggestions.forEach((s) => {
const li = document.createElement("li");
li.innerHTML = `${s.name} by ${s.author || "Anonymous"}${s.genre ? ` · ${s.genre}` : ""}
${s.description || ""}`;
list.appendChild(li);
});
}
function renderVotes() {
const list = $("vote-list");
list.innerHTML = "";
const votesMap = Object.fromEntries(state.myVotes.map((v) => [v.suggestionId, v.score]));
state.allSuggestions.forEach((s) => {
const li = document.createElement("li");
const current = votesMap[s.id] ?? 0;
li.innerHTML = `