A6: public home with the six states, confirmed prize, countdown and polling (WEB-05, WEB-10, WEB-11, WEB-14; LIM-24, WEB-55)
public_state() feeds both the server-rendered home (works without JavaScript) and GET /stato/, so they cannot disagree: none, open, closing, drawing, paying and maintenance, the round's own quota, the winner's 70% of the *confirmed* pool, bets in flight apart, and the closing time on the server's clock. home.js counts down against the server clock and polls every 5 s while visible. Checked in headless Chromium on mobile and desktop widths. Also records the profile reports (WEB-55, roadmap A9) and the name-lock trade-off (LIM-24). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
efb3e2e702
commit
939c113043
16 files changed
+627
-11
No files matched your search
@@ -0,0 +1,93 @@
|
||||
/* 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; };
|
||||
|
||||
function render(s) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -68,3 +68,22 @@ input[type=text], input[type=password] { width: 100%; min-height: 2.75rem; paddi
|
||||
|
||||
@media (min-width: 40rem) { h1 { font-size: 2rem; } .card { padding: 1.6rem; } }
|
||||
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
|
||||
|
||||
/* `hidden` must win over any `display` set above. */
|
||||
[hidden] { display: none !important; }
|
||||
|
||||
/* Public home: the state of the round (WEB-10). The colour is a cue, never the only one — the label says it too. */
|
||||
.lead { color: var(--muted); max-width: 40rem; }
|
||||
.round-card { --state: var(--muted); border-left: .4rem solid var(--state); }
|
||||
.round-card[data-state="open"] { --state: var(--ok); }
|
||||
.round-card[data-state="closing"], .round-card[data-state="drawing"], .round-card[data-state="paying"] { --state: var(--accent); }
|
||||
.round-card[data-state="maintenance"] { --state: var(--error); }
|
||||
.state-label { font-size: 1.35rem; font-weight: 700; margin: 0 0 .3rem; color: var(--state); }
|
||||
.state-message { margin: 0 0 1rem; }
|
||||
.countdown { font-size: 1.1rem; margin: 0 0 1rem; }
|
||||
.clock { font-size: 1.8rem; font-variant-numeric: tabular-nums; }
|
||||
.stats { display: grid; gap: .8rem 1.5rem; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); margin: 0 0 1.2rem; }
|
||||
.stats dt { color: var(--muted); font-size: .9rem; } .stats dd { margin: 0; font-size: 1.25rem; font-weight: 600; }
|
||||
.stats dd .hint { font-size: .85rem; font-weight: 400; }
|
||||
.actions { margin: 0; }
|
||||
.button.big { min-height: 3.25rem; padding-inline: 2.2rem; font-size: 1.15rem; }
|
||||
@@ -6,6 +6,7 @@
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<title>{% block title %}PLM Lottery{% endblock %}</title>
|
||||
<link rel="stylesheet" href="{% static 'web/site.css' %}">
|
||||
{% block head %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<a class="skip-link" href="#contenuto">Vai al contenuto</a>
|
||||
|
||||
@@ -1,5 +1,31 @@
|
||||
{% extends "web/base.html" %}
|
||||
{% load static %}
|
||||
{% block head %}<script src="{% static 'web/home.js' %}" defer></script>{% endblock %}
|
||||
{% block content %}
|
||||
<h1>PLM Lottery</h1>
|
||||
<p>Il sito è in costruzione.</p>
|
||||
<p class="lead">Una lotteria a round: ogni giocatore punta la stessa quota, si estrae un vincitore, il montepremi si divide 70% a lui e 30% alla piattaforma.</p>
|
||||
|
||||
<section id="round-card" class="card round-card" data-state="{{ s.state }}" data-poll-url="{% url 'web:state' %}"
|
||||
data-server-now="{{ s.server_now }}" data-closes-at="{{ s.closes_at|default:'' }}" aria-labelledby="round-state">
|
||||
<p class="state-label" id="round-state" data-field="label" aria-live="polite">{{ s.label }}</p>
|
||||
<p class="state-message" data-field="message">{{ s.message }}</p>
|
||||
|
||||
<p class="countdown"{% if s.seconds_left is None %} hidden{% endif %} data-field="countdown-line">
|
||||
Chiude fra <strong class="clock" data-field="countdown">{{ s.countdown_text|default:'00:00' }}</strong>
|
||||
<span class="hint">(ora del server)</span>
|
||||
</p>
|
||||
|
||||
<dl class="stats">
|
||||
<div><dt>Quota</dt><dd data-field="bet_amount_text">{{ s.bet_amount_text }}</dd></div>
|
||||
<div><dt>Montepremi confermato</dt><dd data-field="jackpot_text">{{ s.jackpot_text }}</dd></div>
|
||||
<div><dt>Giocatori</dt><dd><span data-field="confirmed_players">{{ s.confirmed_players }}</span>
|
||||
<span class="hint" data-field="pending"{% if not s.pending_players %} hidden{% endif %}>+<span data-field="pending_players">{{ s.pending_players }}</span> in conferma</span></dd></div>
|
||||
</dl>
|
||||
|
||||
<p class="actions"{% if not s.can_participate %} hidden{% endif %} data-field="participate-line">
|
||||
<a class="button big" href="{% url 'web:app-home' %}" data-field="participate">Partecipa</a>
|
||||
</p>
|
||||
<p class="hint" data-field="connection" role="status" hidden>Connessione persa: i dati potrebbero non essere aggiornati. Riprovo…</p>
|
||||
<noscript><p class="hint">Senza JavaScript la pagina non si aggiorna da sola: ricaricala per vedere lo stato più recente.</p></noscript>
|
||||
</section>
|
||||
{% endblock %}
|
||||
@@ -5,6 +5,7 @@ from . import views
|
||||
app_name = "web"
|
||||
urlpatterns = [
|
||||
path("", views.home, name="home"),
|
||||
path("stato/", views.state, name="state"),
|
||||
path("app/", views.app_home, name="app-home"),
|
||||
path("health/live", views.health_live, name="health-live"),
|
||||
]
|
||||
+12
-2
@@ -4,10 +4,20 @@ from django.http import HttpResponse, JsonResponse
|
||||
from django.shortcuts import render
|
||||
from django.views.decorators.cache import never_cache
|
||||
|
||||
from apps.lottery.status import public_state
|
||||
|
||||
|
||||
@never_cache
|
||||
def home(request):
|
||||
"""Placeholder landing page: the real home (WEB-10) arrives with A6."""
|
||||
return render(request, "web/home.html")
|
||||
"""The public home (WEB-10): the state is rendered by the server, so the page works without JavaScript;
|
||||
`home.js` then keeps the countdown and polls `state` (WEB-05)."""
|
||||
return render(request, "web/home.html", {"s": public_state()})
|
||||
|
||||
|
||||
@never_cache
|
||||
def state(request):
|
||||
"""The same state as JSON, public and never cached: what the home page polls (WEB-05, WEB-10)."""
|
||||
return JsonResponse(public_state())
|
||||
|
||||
|
||||
@never_cache
|
||||
|
||||
Reference in new issue
Block a user