/* Classifiche — logica pagina */ "use strict"; const $ = (id) => document.getElementById(id); const S = { type: "total", status: "all", q: "", from: 0, to: 0 }; const STATUS_LABEL = { "": "Attivo", i: "Inattivo", I: "Inattivo lungo", v: "In vacanza", vi: "Vacanza + inatt.", vI: "Vacanza + inatt.", a: "Admin" }; const stClass = (s) => "st-" + (s || ""); async function initHead() { try { const b = await api("/api/bootstrap"); $("rk-universe").textContent = `${b.server.name} — Classifiche`; $("rk-sub").textContent = `[s${b.server.number}-${b.server.community}] · aggiornamento: ${b.fetched.highscore_total ? b.fetched.highscore_total : "—"} UTC`; } catch (e) { /* header non bloccante */ } } function fmtScore(v) { if (v === null || v === undefined) return "—"; return Number(v).toLocaleString("it-IT"); } function posClass(rank) { if (!rank) return "pos na"; return "pos p" + rank; } async function load() { const msg = $("f-msg"); msg.textContent = "Caricamento…"; const p = new URLSearchParams({ type: S.type, status: S.status, q: S.q }); if (S.from > 0) p.set("start", S.from); if (S.to > 0) p.set("end", S.to); try { const d = await api("/api/rankings?" + p.toString()); render(d); msg.textContent = ""; } catch (e) { msg.textContent = e.message; } } function render(d) { const isFleet = d.type === "fleet"; const kindLabel = { total: "classifica generale", military: "classifica armamenti", fleet: "top flotte — navi dai rapporti", }[d.type] || d.type; $("rk-count").innerHTML = `Giocatori: ${d.count} — ` + `${kindLabel}`; const rows = d.rows.map((r) => { const st = r.status || ""; const stLab = STATUS_LABEL[st] || st; const jump = r.avatar ? `onclick="goGalaxy('${r.avatar}')"` : ""; const mine = r.mine ? ' ●' : ""; const empty = r.avatar ? `${artSVG(r.avatar, 26)}` : `${artSVG(r.name + r.id, 26)}`; const rep = r.reports || 0; const repCoords = r.report_coords || r.avatar; const repCell = rep ? `` + `${icon("badge_spy", 12)} ${rep}` : `—`; return `` + `${r.rank ? "#" + r.rank : "—"}` + `${empty}` + `${esc(r.name)}${mine}` + `${r.alliance_tag ? "[" + esc(r.alliance_tag) + "]" : ""}` + `${esc(stLab)}` + `${r.planets || 0}` + `${repCell}` + `${fmtScore(r.score)}${r.score ? (isFleet ? "navi" : "punti") : ""}` + ``; }).join(""); const head = `` + `Pos` + `GiocatoreAlleanzaStato` + `PianetiSpie` + `${isFleet ? "Navi" : "Punti"}` + ``; $("rk-results").innerHTML = rows ? `${head}${rows}
` : `
Nessun giocatore corrisponde ai filtri.
`; } function goGalaxy(coords) { const [g, s] = coords.split(":").map(Number); location.href = `/?g=${g}&s=${s}`; } /* ---------------- eventi ---------------- */ const TABS = { total: ["trophy", "Generale"], military: ["swords", "Armamenti"], fleet: ["cat_ships", "Top Flotte"] }; $("rk-tabs").querySelectorAll(".rk-tab").forEach((t) => { const [ic, label] = TABS[t.dataset.type] || ["trophy", t.dataset.type]; t.innerHTML = icon(ic, 15) + " " + label; t.onclick = () => { $("rk-tabs").querySelectorAll(".rk-tab").forEach((x) => x.classList.remove("on")); t.classList.add("on"); S.type = t.dataset.type; load(); }; }); function readFilters() { S.status = $("f-status").value; S.from = parseInt($("f-from").value, 10) || 0; S.to = parseInt($("f-to").value, 10) || 0; S.q = $("f-q").value.trim(); } $("f-apply").onclick = () => { readFilters(); load(); }; $("f-reset").onclick = () => { $("f-status").value = "all"; $("f-from").value = ""; $("f-to").value = ""; $("f-q").value = ""; readFilters(); load(); }; ["f-q", "f-from", "f-to"].forEach((id) => { $(id).addEventListener("keydown", (e) => { if (e.key === "Enter") { readFilters(); load(); } }); }); /* ---------------- init ---------------- */ initStars(); const lg = document.getElementById("rk-logo"); if (lg) lg.innerHTML = icon("trophy", 20); initHead(); load();