feat(hint): unify the vs_ai idle hint online + offline (server-enforced, monotonic)
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 1m28s
CI / conformance (pull_request) Successful in 9s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m43s
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 1m28s
CI / conformance (pull_request) Successful in 9s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m43s
Online vs_ai hints were broken: #207 made the vs_ai hint button always-enabled and wallet-free (assuming all vs_ai = the offline idle-gate), but the backend still served online vs_ai from the allowance/wallet, so over-clicking hit ErrNoHintsLeft -> a generic error toast. Now online vs_ai uses the SAME idle-gate model as offline. Backend: Hint() for a vs_ai game skips the allowance/wallet and increments no hints_used (owner: vs_ai counts toward no hint statistic), and is idle-gated from the SERVER clock -- it returns ErrHintLocked (code hint_locked) until the robot's last move + 30 min, else serves the top move. GameState/StateView expose hint_unlock_left_seconds (server-computed seconds remaining; 0 for a human game / first move / not-your-turn). Pure helper hintUnlockLeftSeconds unit-tested. Wire: StateView gains hint_unlock_left_seconds (FlatBuffers, additive); pkg/wire + gateway transcode carry it (round-trip test). Client: the gate counts down from a MONOTONIC clock (performance.now()) anchored to the source's seconds-left when it lands (on load from the view; to the full window when the robot moves), so a client clock change cannot skew it and a relaunch re-reads a fresh value. The vs_ai hint button (online + offline) shows the lock + toast; doHint handles the hint_locked backstop by re-syncing. Replaces #207's absolute hintUnlockAtMs on the wire/model/delta (the offline record keeps the absolute for persistence; the view exposes seconds-left). Docs FUNCTIONAL(+_ru)/ARCHITECTURE updated. Verified: go build/vet + game/server/transcode tests (+ new hintUnlockLeftSeconds); ui check 0 / unit 490 / e2e 198 (one pre-existing webkit offline flake) / app entry 114.2/115.
This commit is contained in:
+36
-13
@@ -23,7 +23,7 @@
|
||||
import { centre, premiumGrid } from '../lib/premiums';
|
||||
import { variantNameKey, usesStarBlank, BLANK_STAR } from '../lib/variants';
|
||||
import { alphabetLetters, hasAlphabet } from '../lib/alphabet';
|
||||
import { hintsLeft, hintGateRemainingMs, hintLockMinutes } from '../lib/hints';
|
||||
import { hintsLeft, hintGateRemainingMs, hintLockMinutes, HINT_GATE_MS } from '../lib/hints';
|
||||
import { downloadUrl, shareOrDownloadGcg, shareUrlAsFile } from '../lib/share';
|
||||
import { insideVK, vkAndroidWebView, vkCopyText, vkDownloadFile, vkPlatform, vkShowImages } from '../lib/vk';
|
||||
import { getCachedGame, setCachedGame, setCachedDraft, type CachedGame } from '../lib/gamecache';
|
||||
@@ -168,19 +168,30 @@
|
||||
// hint was spent in another game (see lib/hints).
|
||||
const hintCount = $derived(hintsLeft(view, app.profile?.hintBalance ?? 0));
|
||||
// A vs_ai game's hint is unlimited and wallet-free, but idle-gated (an anti-frustration aid: it
|
||||
// unlocks only after the player has been stuck ~30 min on a turn). The unlock is a persisted
|
||||
// wall-clock instant carried on the game view (`hintUnlockAtMs`, stamped by the source off the
|
||||
// robot's reply and kept fresh by the move delta), so the wait survives leaving and reopening the
|
||||
// app. It is sanitised on read (source + lib/hints cap it at now + the window) so a device clock
|
||||
// set BACK cannot freeze the gate; a clock set forward just opens the hint early, harmless for a
|
||||
// solo game. `now` ticks so the 🔒 lifts live at the mark. 0 = open (the human's first move).
|
||||
let now = $state(Date.now());
|
||||
const hintUnlockAt = $derived(view?.game.vsAi ? (view.hintUnlockAtMs ?? 0) : 0);
|
||||
const hintRemaining = $derived(hintGateRemainingMs(hintUnlockAt, now));
|
||||
// unlocks only after the player has been stuck ~30 min on a turn). The source (the SERVER clock
|
||||
// online, the device clock offline) tells us the SECONDS LEFT (view.hintUnlockLeftSeconds); we
|
||||
// anchor a MONOTONIC countdown (performance.now()) to it, so a client clock change cannot skew it.
|
||||
// A fresh value arrives on load (armHintGate below); when the robot moves the wait is the full
|
||||
// window. `monoNow` ticks so the 🔒 lifts live at the mark.
|
||||
let hintGateStart = $state<number | null>(null); // performance.now() at the last anchor; null = open
|
||||
let gateLeftMs = $state(0);
|
||||
let monoNow = $state(performance.now());
|
||||
const hintRemaining = $derived(hintGateRemainingMs(hintGateStart, gateLeftMs, monoNow));
|
||||
const hintGated = $derived(hintRemaining > 0);
|
||||
// Anchor the countdown to a freshly received seconds-left (or open the gate). Called on every state
|
||||
// load from the view, and on the robot's move to the full window (the robot just moved).
|
||||
function armHintGate(leftSeconds: number): void {
|
||||
if (leftSeconds > 0) {
|
||||
hintGateStart = performance.now();
|
||||
gateLeftMs = leftSeconds * 1000;
|
||||
} else {
|
||||
hintGateStart = null;
|
||||
gateLeftMs = 0;
|
||||
}
|
||||
}
|
||||
$effect(() => {
|
||||
if (!view?.game.vsAi) return;
|
||||
const iv = setInterval(() => (now = Date.now()), 10_000);
|
||||
const iv = setInterval(() => (monoNow = performance.now()), 10_000);
|
||||
return () => clearInterval(iv);
|
||||
});
|
||||
// RACK_SIZE mirrors the engine's rules.RackSize (7 for every current variant). The exchange
|
||||
@@ -225,6 +236,8 @@
|
||||
source.draftGet(id).catch(() => ''),
|
||||
]);
|
||||
view = st;
|
||||
// Anchor the vs_ai idle-hint countdown to the freshly fetched seconds-left (0 = open / non-vs_ai).
|
||||
armHintGate(st.game.vsAi ? (st.hintUnlockLeftSeconds ?? 0) : 0);
|
||||
syncWallet(st.walletBalance);
|
||||
// Seed the unread flag from the authoritative state (the live stream only raises it).
|
||||
seedChatUnread(id, st.game.unreadChat, st.game.unreadMessages);
|
||||
@@ -345,7 +358,11 @@
|
||||
// While composing, reload so a draft overlapping the new move is reconciled; otherwise apply
|
||||
// the move as a delta with no fetch.
|
||||
if (placement.pending.length > 0) void load();
|
||||
else applyDelta(applyMoveDelta(cacheSnapshot(), { move: e.move, game: e.game, bagLen: e.bagLen, hintUnlockAtMs: e.hintUnlockAtMs }));
|
||||
else {
|
||||
applyDelta(applyMoveDelta(cacheSnapshot(), { move: e.move, game: e.game, bagLen: e.bagLen }));
|
||||
// The robot just moved (vs_ai) → the human's turn begins with the full idle window.
|
||||
if (view?.game.vsAi) armHintGate(HINT_GATE_MS / 1000);
|
||||
}
|
||||
} else if (e.kind === 'your_turn' && e.gameId === id) {
|
||||
// The opponent_moved delta carries the new state; your_turn only confirms the turn. Refetch
|
||||
// only if we missed the move (our cached count trails the event's).
|
||||
@@ -873,7 +890,7 @@
|
||||
// vs_ai: the idle gate replaces the wallet. While it is still closed, a tap only explains when the
|
||||
// hint unlocks (no wallet is ever spent) — matching the 🔒 badge. Measured fresh at tap time.
|
||||
if (view?.game.vsAi) {
|
||||
const remaining = hintGateRemainingMs(hintUnlockAt, Date.now());
|
||||
const remaining = hintGateRemainingMs(hintGateStart, gateLeftMs, performance.now());
|
||||
if (remaining > 0) {
|
||||
showToast(t('game.hintLockedIn', { n: hintLockMinutes(remaining) }), 'info');
|
||||
return;
|
||||
@@ -915,6 +932,12 @@
|
||||
// The backend does not spend a hint when there is no move.
|
||||
if (e instanceof GatewayError && e.code === 'no_hint_available') {
|
||||
showToast(t('game.noHintOptions'), 'info');
|
||||
} else if (e instanceof GatewayError && e.code === 'hint_locked') {
|
||||
// The server-clock backstop refused a vs_ai hint the client thought was open (a rare client
|
||||
// clock desync): re-sync the countdown from a fresh fetch and explain the gate.
|
||||
await load();
|
||||
const left = hintGateRemainingMs(hintGateStart, gateLeftMs, performance.now());
|
||||
showToast(t('game.hintLockedIn', { n: hintLockMinutes(left || 60_000) }), 'info');
|
||||
} else {
|
||||
handleError(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user