// Kit ads (v1) - AdMob rewarded ads with Google UMP consent (EEA/UK). // From _Tools\Library\mobile\kit (XP-Battleship/src/ads.ts). Needs @capacitor-community/admob. // // configureAds({ testing: false, rewarded: { revive: "ca-app-pub-xxx/yyy", double: "..." } }); // await initAds(); // at boot (native only, no-op on web) // if (await showRewarded("revive")) grant(); // if (needsPrivacyOptions()) show an "Ad settings" button -> showPrivacyOptions() // // Consent gathering is inert until a GDPR message AND a privacy-options form are published for // the app in the AdMob console. TESTING must be false in the Play-Store build. import { Capacitor } from "@capacitor/core"; import { resumeAudio, suspendAudio } from "./audio"; const TEST_REWARDED_ID = "ca-app-pub-3940256099942544/5224354917"; // Google's official test unit const cfg: { testing: boolean; rewarded: Record } = { testing: true, rewarded: {} }; export function configureAds(c: { testing: boolean; rewarded: Record }): void { cfg.testing = c.testing; cfg.rewarded = c.rewarded; } let initialized = false; let adShowing = false; let privacyOptionsRequired = false; /** Fails open on purpose (no network, no published message) - only an explicit * canRequestAds:false (user declined) disables ads. */ async function gatherConsent(): Promise { const { AdMob, AdmobConsentStatus } = await import("@capacitor-community/admob"); try { let info = await AdMob.requestConsentInfo(); if (info.isConsentFormAvailable && info.status === AdmobConsentStatus.REQUIRED) { info = await AdMob.showConsentForm(); } // enum not re-exported from the plugin entry point - compare the string value privacyOptionsRequired = String(info.privacyOptionsRequirementStatus) === "REQUIRED"; return info.canRequestAds !== false; } catch { return true; } } export function needsPrivacyOptions(): boolean { return privacyOptionsRequired; } /** Google requires this entry point to stay reachable after the first consent decision. */ export async function showPrivacyOptions(): Promise { if (!Capacitor.isNativePlatform()) return; const { AdMob } = await import("@capacitor-community/admob"); await AdMob.showPrivacyOptionsForm(); } /** True while an ad covers the game - pause the game loop. */ export function isAdShowing(): boolean { return adShowing; } export async function initAds(): Promise { if (!Capacitor.isNativePlatform() || initialized) return; if (!(await gatherConsent())) return; // consent BEFORE any ad request const { AdMob } = await import("@capacitor-community/admob"); await AdMob.initialize({ initializeForTesting: cfg.testing }); initialized = true; } /** Resolves true if the reward was earned. On web (dev) always grants after a short delay. */ export async function showRewarded(placement: string): Promise { if (!Capacitor.isNativePlatform()) { await new Promise((r) => setTimeout(r, 600)); return true; } if (!initialized) { // a transient failure at boot must not disable ads for the whole session await initAds().catch(() => {}); if (!initialized) return false; } const adId = cfg.testing ? TEST_REWARDED_ID : cfg.rewarded[placement]; if (!adId) return false; const { AdMob, RewardAdPluginEvents } = await import("@capacitor-community/admob"); // the ad is a native activity above the WebView: the page never sees visibilitychange adShowing = true; suspendAudio(); try { await AdMob.prepareRewardVideoAd({ adId }); return await new Promise((resolve) => { let rewarded = false; let settled = false; const subs: Array Promise }>> = []; // single exit, runs once - a path that never clears adShowing freezes the game for good const done = (result: boolean): void => { if (settled) return; settled = true; clearTimeout(watchdog); for (const s of subs) void s.then((h) => h.remove()).catch(() => {}); adShowing = false; resumeAudio(); resolve(result); }; subs.push(AdMob.addListener(RewardAdPluginEvents.Rewarded, () => { rewarded = true; })); subs.push(AdMob.addListener(RewardAdPluginEvents.Dismissed, () => done(rewarded))); subs.push(AdMob.addListener(RewardAdPluginEvents.FailedToShow, () => done(rewarded))); // backstop for a Dismissed event the SDK never delivers - un-freezes, never cuts an ad short const watchdog = setTimeout(() => done(rewarded), 300_000); AdMob.showRewardVideoAd().catch(() => done(false)); }); } catch { adShowing = false; resumeAudio(); return false; } }