Save the match every second instead of waiting to be told

A force-quit deep into a turn came back at the clock's last-turn-switch value,
losing the whole interrupted turn. The tell was the delay counter returning at
a full 12: the newest saved state was the one tap() writes, and tap() sets
turnAt and calls saveGame() in the same breath, so it carries held ~ 0.
Restoring it reproduces the moment of the turn switch exactly.

The two triggers meant to save at quit time — visibilitychange and pagehide —
do not fire in Android's WebView. MainActivity's onPause() forwards to
web.onPause() under a comment claiming that is what makes visibilitychange
fire. It isn't: document visibility follows the WebView's view and window
visibility, not that call. The forwarding is still right for stopping timers
and audio, but the saved match should never have depended on it.

This is not new. It has almost certainly been broken since 1.0, and the release
check could not catch it, because for the first 12 seconds of a turn the delay
absorbs everything and render() never touches st.reserve. A clock that has lost
its place is indistinguishable from one that is correctly sitting still, so
every force-quit test that quit soon after a turn change passed. RELEASING.md
now says to let the turn run 40 seconds first, which is the only version of
that check worth running.

So the page stops waiting to be told. A timer writes the match once a second
while a clock is running — a timer rather than a hook in loop(), because
requestAnimationFrame stops when the page is hidden, which is exactly when this
matters. blur joins the listeners too: a different signal, and one that does
fire when the task switcher opens. At most a second can now be lost, which is
well inside the delay nobody is charged for anyway.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-15 15:07:18 +02:00
co-authored by Claude Opus 5
parent bf3f565dd0
commit 3a7eb8c612
3 changed files with 33 additions and 3 deletions
+16 -1
View File
@@ -1051,11 +1051,26 @@
if(st.phase === PAUSE && st.active < 0) st.phase = IDLE; // can't be mid-turn with no turn
}
// the only hooks iOS reliably fires before killing an app
// The hooks iOS fires before killing an app. Android's WebView is the problem
// child: neither of these is guaranteed there, whatever MainActivity's
// onPause() forwarding suggests, and when they don't fire the newest save is
// the one tap() wrote — which carries held ≈ 0, because tap() sets turnAt and
// saves in the same breath. Reopening then hands back the entire turn, and
// the bug hides in plain sight: quit inside the delay window and a clock that
// correctly hasn't moved looks exactly like a clock that lost its place.
document.addEventListener("visibilitychange", function(){
if(document.visibilityState === "hidden") saveGame();
});
window.addEventListener("pagehide", saveGame);
// a different signal from visibilitychange, and it does fire when Android's
// task switcher opens. saveGame() is idempotent, so an extra call is free
window.addEventListener("blur", saveGame);
// and the one that doesn't depend on being told anything: a second is the
// most a force-quit can now cost, which is well inside the delay nobody is
// charged for. A timer rather than a hook in loop(), because rAF stops when
// the page is hidden — exactly when this matters most.
setInterval(function(){ if(st.phase === RUN) saveGame(); }, 1000);
/* Reset is a hold, not a tap. It used to be two taps inside 2s, and a test
user found the button, tapped it once and gave up — which is exactly what