You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
import { MiniGamePlatform } from '../platform/MiniGamePlatform';
|
|
|
|
export class InterstitialAd {
|
|
|
|
private static _keys: string[] = [];
|
|
|
|
static init(keys: string[]) {
|
|
this._keys = keys ?? [];
|
|
MiniGamePlatform.init();
|
|
}
|
|
|
|
static showInterstitialAd(onAdClose?: Function, onFail?: Function, onSuccess?: Function) {
|
|
MiniGamePlatform.init();
|
|
const pf = MiniGamePlatform.pf;
|
|
if (!pf?.createInterstitialAd) {
|
|
gg.sdk?.pfLogKey?.('AD:INTER_NO_API');
|
|
onAdClose?.(true);
|
|
return;
|
|
}
|
|
if (this._keys.length === 0) {
|
|
gg.sdk?.pfLogKey?.('AD:INTER_NO_UNIT');
|
|
onAdClose?.(false);
|
|
return;
|
|
}
|
|
const keyIndex = Math.floor(Math.random() * this._keys.length);
|
|
const interstitialAd = pf.createInterstitialAd({
|
|
adUnitId: this._keys[keyIndex],
|
|
});
|
|
if (!interstitialAd) {
|
|
gg.sdk?.pfLogKey?.('AD:INTER_CREATE_FAIL', this._keys[keyIndex]);
|
|
onFail?.();
|
|
return;
|
|
}
|
|
|
|
interstitialAd.onClose?.(() => {
|
|
console.log('[InterstitialAd] onClose');
|
|
try {
|
|
interstitialAd.destroy?.();
|
|
} catch { /* ignore */ }
|
|
onAdClose?.();
|
|
});
|
|
|
|
const loadResult = interstitialAd.load?.();
|
|
const afterLoad = () => {
|
|
const showResult = interstitialAd.show?.();
|
|
if (!showResult?.then) return;
|
|
showResult.then(() => {
|
|
console.log('[InterstitialAd] show ok');
|
|
onSuccess?.();
|
|
}).catch((err: unknown) => {
|
|
console.log('[InterstitialAd] show fail', err);
|
|
gg.sdk?.pfLogKey?.('AD:INTER_SHOW_FAIL', err);
|
|
onFail?.();
|
|
});
|
|
};
|
|
|
|
if (loadResult?.then) {
|
|
loadResult.then(afterLoad).catch((err: unknown) => {
|
|
console.log('[InterstitialAd] load fail', err);
|
|
gg.sdk?.pfLogKey?.('AD:INTER_LOAD_FAIL', err);
|
|
onFail?.();
|
|
});
|
|
} else {
|
|
afterLoad();
|
|
}
|
|
}
|
|
}
|
|
|