Files
plm-lottery/apps/web/static/web/home.js
T
davideandClaude Sonnet 5.5 42e5e33371 A7: player area - play, wallet, movements (WEB-03, WEB-05, WEB-20..27)
Gioca: the round, the player's funds and their bet; the bet is two steps
(a confirmation screen that sends nothing, then an explicit confirm) with a
fresh idempotency key per screen, and the server re-checks everything.
Portafoglio: spendable apart from in-confirmation, deposit address with a QR
of the player's own address, copy button, simulated test deposits.
Movimenti: the player's own ledger, paged, stacked as cards on a phone.
GET /app/stato/ feeds the page and its polling. Every view takes the user from
the session only. Checked end to end in a real browser (Playwright): status
moves from pending to confirmed without a reload, a double click is one bet.
Mutation checks: cross-account leak, non-idempotent POST and unrestricted
test-deposit amounts are all caught.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 14:41:27 +01:00

117 lines
4.7 KiB
JavaScript

/* The public home keeps itself current (WEB-05, WEB-10): the server already rendered the state, so this only
(1) runs the countdown against the *server's* clock, never the visitor's, and (2) polls /stato/ — every 5 s while
the page is visible, at once when it becomes visible again or when the countdown ends, slower after errors.
No dependencies, no inline code. */
function formatCountdown(totalSeconds) {
const s = Math.max(0, Math.floor(totalSeconds));
const hours = Math.floor(s / 3600), minutes = Math.floor((s % 3600) / 60), seconds = s % 60;
const two = (n) => String(n).padStart(2, "0");
return hours > 0 ? `${hours}:${two(minutes)}:${two(seconds)}` : `${two(minutes)}:${two(seconds)}`;
}
/* How far the visitor's clock is from the server's: add it to Date.now() to get the server's time. */
function serverOffsetMs(serverNowIso, clientNowMs) {
return Date.parse(serverNowIso) - clientNowMs;
}
function secondsLeft(closesAtIso, offsetMs, clientNowMs) {
return (Date.parse(closesAtIso) - (clientNowMs + offsetMs)) / 1000;
}
function pollDelayMs(failures) {
return Math.min(30000, 5000 * Math.pow(2, Math.max(0, failures)));
}
if (typeof module !== "undefined") {
module.exports = { formatCountdown, serverOffsetMs, secondsLeft, pollDelayMs };
}
if (typeof document !== "undefined") {
const card = document.getElementById("round-card");
if (card) {
const field = (name) => card.querySelector(`[data-field="${name}"]`);
let offsetMs = 0, closesAt = "", failures = 0, timer = null, polling = false;
const setText = (name, value) => { const el = field(name); if (el && el.textContent !== String(value)) el.textContent = value; };
const setHidden = (name, hidden) => { const el = field(name); if (el) el.hidden = hidden; };
const me = (name) => document.querySelector(`[data-me="${name}"]`);
const setMe = (name, value) => { const el = me(name); if (el && el.textContent !== String(value)) el.textContent = value; };
const hideMe = (name, hidden) => { const el = me(name); if (el) el.hidden = hidden; };
/* The player's own block (play screen only): funds, the bet's status, whether a bet can be placed. */
function renderMe(m) {
setMe("spendable_text", m.spendable_text);
setMe("incoming_text", m.incoming_text);
hideMe("incoming-line", !m.incoming_sats);
const p = m.participation;
hideMe("participation-box", !p);
setMe("participation_label", p ? p.label : "");
const badge = me("participation_label");
if (badge) badge.dataset.status = p ? p.status : "";
setMe("participation_message", p ? p.message : "");
hideMe("bet-form", !m.can_bet);
setMe("bet_blocker", m.bet_blocker);
hideMe("bet_blocker", !m.bet_blocker);
setMe("last_result", m.last_result);
hideMe("last_result", !m.last_result);
}
function render(s) {
if (s.me) renderMe(s.me);
card.dataset.state = s.state;
offsetMs = serverOffsetMs(s.server_now, Date.now());
closesAt = s.closes_at || "";
setText("label", s.label);
setText("message", s.message);
setText("bet_amount_text", s.bet_amount_text);
setText("jackpot_text", s.jackpot_text);
setText("confirmed_players", s.confirmed_players);
setText("pending_players", s.pending_players);
setHidden("pending", !s.pending_players);
setHidden("participate-line", !s.can_participate);
setHidden("countdown-line", !closesAt);
tickClock();
}
function tickClock() {
if (!closesAt) return;
const left = secondsLeft(closesAt, offsetMs, Date.now());
setText("countdown", formatCountdown(left));
if (left <= 0 && !polling) poll(); // the round has just closed: ask the server what happens next
}
async function poll() {
if (polling) return;
polling = true;
clearTimeout(timer);
try {
const response = await fetch(card.dataset.pollUrl, { headers: { Accept: "application/json" }, cache: "no-store" });
if (!response.ok) throw new Error(String(response.status));
render(await response.json());
failures = 0;
setHidden("connection", true);
} catch (error) {
failures += 1;
setHidden("connection", false);
} finally {
polling = false;
schedule();
}
}
function schedule() {
clearTimeout(timer);
if (!document.hidden) timer = setTimeout(poll, pollDelayMs(failures));
}
document.addEventListener("visibilitychange", () => { if (document.hidden) clearTimeout(timer); else poll(); });
setInterval(tickClock, 1000);
offsetMs = serverOffsetMs(card.dataset.serverNow, Date.now());
closesAt = card.dataset.closesAt || "";
tickClock();
schedule();
}
}