Sync round countdown across clients and enforce the bet cutoff on deadline, not scheduler tick
The round timer relied on each client's own wall clock, so two browsers with skewed local clocks showed different countdowns for the same round; the server now also returns server_time so the frontend can correct for clock skew. Also drop out-of-order /rounds/current responses (multiple independent triggers could resolve late and revert the UI to a stale drawing/result state) and prune per-round bookkeeping maps on round transitions. Separately, place_bet only checked status == "open", leaving a window (up to the scheduler's 5s tick interval) after a round's timer hit zero where a new bet could still be accepted. place_bet now checks the round's own deadline directly (round_accepts_bets), acting as an immediate "yellow light" for new entries while still letting already-broadcast bets confirm before the round closes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f27fe6243c
commit
7dcf6d2756
8 files changed
+87
-17
No files matched your search
+21
-2
@@ -544,6 +544,14 @@ function switchPanel(name) {
|
||||
}
|
||||
|
||||
let roundCloseAt = null;
|
||||
let serverTimeOffsetMs = 0; // serverNow - clientNow, so every client's countdown agrees regardless of local clock skew
|
||||
function serverNow() { return new Date(Date.now() + serverTimeOffsetMs); }
|
||||
// refreshRound() is triggered from several independent sources (poll timer, timer-hits-zero,
|
||||
// visibilitychange, placeBet, showDashboard) whose requests can resolve out of order over the
|
||||
// network. Track the latest applied response so a slow, stale one can never revert the UI to an
|
||||
// older round's state after a newer response has already moved it forward.
|
||||
let roundRequestSeq = 0;
|
||||
let roundAppliedSeq = 0;
|
||||
let roundTimerInterval = null;
|
||||
let roundPollTimeout = null;
|
||||
|
||||
@@ -649,7 +657,7 @@ let timerHitZero = false;
|
||||
function updateRoundTimer() {
|
||||
const el = document.getElementById('round-timer');
|
||||
if (!roundCloseAt) { el.textContent = '--:--'; timerHitZero = false; return; }
|
||||
const rawSec = Math.floor((roundCloseAt - new Date()) / 1000);
|
||||
const rawSec = Math.floor((roundCloseAt - serverNow()) / 1000);
|
||||
const totalSec = Math.max(0, rawSec);
|
||||
const mm = String(Math.floor(totalSec / 60)).padStart(2, '0');
|
||||
const ss = String(totalSec % 60).padStart(2, '0');
|
||||
@@ -691,8 +699,11 @@ function showNormalState() {
|
||||
}
|
||||
|
||||
async function refreshRound() {
|
||||
const seq = ++roundRequestSeq;
|
||||
try {
|
||||
const data = await call('GET', '/rounds/current');
|
||||
if (seq < roundAppliedSeq) return; // a newer refreshRound() call already applied its result
|
||||
roundAppliedSeq = seq;
|
||||
noteFetchOutcome(true);
|
||||
updateChainStatusBar(data);
|
||||
document.getElementById('round-title').textContent = data.round_id
|
||||
@@ -708,6 +719,7 @@ async function refreshRound() {
|
||||
jackpotEl.classList.add('jackpot-bump');
|
||||
}
|
||||
lastJackpotValue = jackpotValue;
|
||||
if (data.server_time) serverTimeOffsetMs = new Date(data.server_time) - new Date();
|
||||
roundCloseAt = data.closes_at ? new Date(data.closes_at) : null;
|
||||
updateRoundTimer();
|
||||
|
||||
@@ -735,6 +747,13 @@ async function refreshRound() {
|
||||
} else if (data.round_id && data.round_id !== activeResultRoundId) {
|
||||
// a genuinely new round is open — clear any previous result and go back to normal
|
||||
activeResultRoundId = null;
|
||||
// drop bookkeeping for old rounds so these maps don't grow for the life of the session
|
||||
for (const key of Object.keys(drawStartedAt)) {
|
||||
if (Number(key) !== data.round_id) delete drawStartedAt[key];
|
||||
}
|
||||
for (const id of revealedRounds) {
|
||||
if (id !== data.round_id) revealedRounds.delete(id);
|
||||
}
|
||||
showNormalState();
|
||||
} else if (!data.round_id && activeResultRoundId == null) {
|
||||
// nothing has ever been revealed and there's no active round — plain empty state
|
||||
@@ -747,7 +766,7 @@ async function refreshRound() {
|
||||
// The countdown reaching zero doesn't mean the server has actually closed the
|
||||
// round yet (it still waits for in-flight bets to confirm) — poll faster
|
||||
// through that gap too, not just once status flips to closing/drawing/paying_out.
|
||||
const pastDeadline = data.status === 'open' && roundCloseAt !== null && roundCloseAt - new Date() <= 0;
|
||||
const pastDeadline = data.status === 'open' && roundCloseAt !== null && roundCloseAt - serverNow() <= 0;
|
||||
scheduleNextRoundPoll(isDrawing || pastDeadline);
|
||||
} catch (e) {
|
||||
noteFetchOutcome(false);
|
||||
|
||||
Reference in new issue
Block a user