131 lines
5.0 KiB
JavaScript
131 lines
5.0 KiB
JavaScript
/* 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: <b>${d.count}</b> — ` +
|
|
`<span class="dim">${kindLabel}</span>`;
|
|
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 ? ' <span title="sei tu" class="mymark">●</span>' : "";
|
|
const empty = r.avatar
|
|
? `<span class="rk-avatar" ${jump} title="Vai al pianeta ${esc(r.avatar)}">${artSVG(r.avatar, 26)}</span>`
|
|
: `<span class="rk-avatar">${artSVG(r.name + r.id, 26)}</span>`;
|
|
const rep = r.reports || 0;
|
|
const repCoords = r.report_coords || r.avatar;
|
|
const repCell = rep
|
|
? `<span class="rep-badge"${repCoords ? ` onclick="goGalaxy('${esc(repCoords)}')"` : ""} ` +
|
|
`title="${rep} rapport${rep === 1 ? "o" : "i"} di spionaggio su questo giocatore">` +
|
|
`${icon("badge_spy", 12)} ${rep}</span>`
|
|
: `<span class="dim">—</span>`;
|
|
return `<tr class="${r.mine ? "mine" : ""}">` +
|
|
`<td class="${posClass(r.rank)}">${r.rank ? "#" + r.rank : "—"}</td>` +
|
|
`<td>${empty}</td>` +
|
|
`<td><span class="rk-name ${stClass(st)}" ${jump} title="Vai al pianeta">${esc(r.name)}</span>${mine}</td>` +
|
|
`<td class="rk-ally">${r.alliance_tag ? "[" + esc(r.alliance_tag) + "]" : ""}</td>` +
|
|
`<td><span class="pname-status ${stClass(st)}">${esc(stLab)}</span></td>` +
|
|
`<td class="planets-n">${r.planets || 0}</td>` +
|
|
`<td style="text-align:center">${repCell}</td>` +
|
|
`<td class="score">${fmtScore(r.score)}<small>${r.score ? (isFleet ? "navi" : "punti") : ""}</small></td>` +
|
|
`</tr>`;
|
|
}).join("");
|
|
const head =
|
|
`<thead><tr>` +
|
|
`<th style="width:56px">Pos</th><th style="width:40px"></th>` +
|
|
`<th>Giocatore</th><th>Alleanza</th><th>Stato</th>` +
|
|
`<th style="text-align:center">Pianeti</th><th style="text-align:center">Spie</th>` +
|
|
`<th style="text-align:right">${isFleet ? "Navi" : "Punti"}</th>` +
|
|
`</tr></thead>`;
|
|
$("rk-results").innerHTML =
|
|
rows
|
|
? `<table class="rank-table">${head}<tbody>${rows}</tbody></table>`
|
|
: `<div class="empty-note">Nessun giocatore corrisponde ai filtri.</div>`;
|
|
}
|
|
|
|
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();
|