fix(ads): share one interstitial cooldown timer across kinds
CI / changes (pull_request) Successful in 1s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 25s
CI / ui (pull_request) Successful in 1m10s
CI / conformance (pull_request) Successful in 10s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m53s

Cooldowns were tracked per kind ({move,hint}), so a hint ad and a move ad
did not see each other: after a hinted move's ad the move timer was still
zero, and the next plain move fired an ad immediately — two ads within a
minute, under the cooldown. Track a single shared last-shown time; the kind
only selects the required gap (hint 1m, move 5m, vs_ai 30m) measured from the
last interstitial of any kind. A hint can still fire on its shorter gap
(D30's "independent" hint cooldown), but never stacks a second ad within a
cooldown.

Tests: a hint uses the shorter shared gap; a move does not stack onto a
just-shown hint ad.
This commit is contained in:
Ilia Denisov
2026-07-10 03:36:18 +02:00
parent 2ce80c241d
commit b5c8a04f0b
5 changed files with 53 additions and 31 deletions
+21 -16
View File
@@ -50,29 +50,33 @@ export async function showRewarded(): Promise<RewardedResult> {
}
// The post-move interstitial gate is client-mirrored: the server sends the cooldowns + suppressed in
// the profile (Profile.ads); the client tracks the last-shown time per kind in localStorage and
// self-gates. Last-shown is per-device (cleared with storage) — acceptable for a frequency gate.
// the profile (Profile.ads); the client tracks the last-shown time in localStorage and self-gates.
// Last-shown is per-device (cleared with storage) — acceptable for a frequency gate.
//
// It is a SINGLE shared timestamp across kinds, not one per kind: the kind only selects the required
// gap (hint's short cooldown vs the move / vs_ai one), while the wait is always measured from the
// last interstitial of ANY kind. A per-kind timer let a hint ad and a move ad stack — after a
// hint-move ad the move timer was still zero, so the next plain move fired an ad immediately.
const INTERSTITIAL_LAST_KEY = 'ads.interstitial.last';
// interstitialLast reads the last-shown epoch millis per kind (0 when absent/corrupt).
function interstitialLast(): { move: number; hint: number } {
// interstitialLast reads the last-shown epoch millis of any interstitial (0 when absent/corrupt, or
// when a pre-shared-timer value — an object — is still stored: it decodes to 0 and self-heals).
function interstitialLast(): number {
try {
const raw = localStorage.getItem(INTERSTITIAL_LAST_KEY);
if (raw) {
const j = JSON.parse(raw) as Partial<{ move: number; hint: number }>;
return { move: j.move ?? 0, hint: j.hint ?? 0 };
const n = Number(raw);
return Number.isFinite(n) ? n : 0;
}
} catch {
/* a corrupt or unavailable store simply resets the gate */
}
return { move: 0, hint: 0 };
return 0;
}
function recordInterstitial(kind: 'move' | 'hint', now: number): void {
function recordInterstitial(now: number): void {
try {
const l = interstitialLast();
l[kind] = now;
localStorage.setItem(INTERSTITIAL_LAST_KEY, JSON.stringify(l));
localStorage.setItem(INTERSTITIAL_LAST_KEY, String(now));
} catch {
/* ignore a write failure — the worst case is one extra ad next time */
}
@@ -81,9 +85,10 @@ function recordInterstitial(kind: 'move' | 'hint', now: number): void {
/**
* maybeShowInterstitial shows a post-move (`kind='move'`, global / vs_ai cooldown) or post-hint
* (`kind='hint'`, its own short cooldown) interstitial when the client-mirrored gate allows: not
* suppressed (no-ads), online, inside VK (the stub bypasses VK), and the mirrored cooldown elapsed.
* The stub (contour test) shows an "ad fired" toast. Returns whether an ad was shown. Fire-and-forget
* — the caller does not await a result to proceed.
* suppressed (no-ads), online, inside VK (the stub bypasses VK), and the required gap has elapsed
* since the last interstitial of any kind. The kind picks the gap; the timer is shared, so a hint ad
* and a move ad never fire within a cooldown of each other. The stub (contour test) shows an "ad
* fired" toast. Returns whether an ad was shown. Fire-and-forget — the caller does not await it.
*/
export async function maybeShowInterstitial(
ads: AdsConfig | undefined,
@@ -95,8 +100,8 @@ export async function maybeShowInterstitial(
if (!stub && executionContext() !== 'vk') return false;
const cooldownS = kind === 'hint' ? ads.cooldownHintS : opts.vsAi ? ads.cooldownVsAiS : ads.cooldownGlobalS;
const now = Date.now();
if (now - interstitialLast()[kind] < cooldownS * 1000) return false;
if (now - interstitialLast() < cooldownS * 1000) return false;
const shown = stub ? (showToast('ad fired'), true) : await vkShowInterstitial();
if (shown) recordInterstitial(kind, now);
if (shown) recordInterstitial(now);
return shown;
}