Loading...
Loading...
Integrates the GameDistribution JavaScript SDK (GD SDK) into a JavaScript or HTML5 game, covering SDK initialization, interstitial and rewarded ad display, banner ads, ad callback handling, and game lifecycle events. Use when the user asks about GameDistribution, GD SDK, game ads, HTML5 game monetization, ad integration, or wiring up ads and lifecycle events in a JavaScript or HTML5 game.
npx skill4agent add hungify/playables-skills game-distribution-js-sdkwindow.gdsdkwindow.GD_OPTIONSonEventgameIdonEventgdsdk.showAd()gdsdk.preloadAd('rewarded')gdsdk.showAd('rewarded')gdsdk.showAd('display', { containerId })SDK_GAME_PAUSESDK_GAME_STARTwindow.GD_OPTIONSconst SDK_URL = 'https://html5.api.gamedistribution.com/main.min.js';
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}window.gdsdkSDK_READYwindow.GD_OPTIONSgameIdonEvent(event)const options = {
gameId: 'YOUR_GAME_ID'
};
let platformSdk = null;
let isInitialized = false;
let currentAdvertisementIsRewarded = false;
const listeners = {
interstitialStateChanged: [],
rewardedStateChanged: [],
bannerStateChanged: [],
pauseStateChanged: [],
audioStateChanged: []
};
function emit(name, payload) {
listeners[name].forEach((cb) => cb(payload));
}
function initialize() {
return new Promise((resolve, reject) => {
if (isInitialized) {
resolve();
return;
}
if (typeof options.gameId !== 'string') {
reject(new Error('Game params are not found'));
return;
}
window.GD_OPTIONS = {
gameId: options.gameId,
onEvent(event) {
switch (event.name) {
case 'SDK_READY':
platformSdk = window.gdsdk;
isInitialized = true;
// Optional: show an interstitial immediately once the SDK is ready
showInterstitial();
resolve();
break;
case 'SDK_GAME_START':
// Ad finished — game should resume
if (currentAdvertisementIsRewarded) {
emit('rewardedStateChanged', 'closed');
// Preload the next rewarded ad right away
platformSdk.preloadAd('rewarded');
} else {
emit('interstitialStateChanged', 'closed');
}
emit('pauseStateChanged', false);
emit('audioStateChanged', true);
break;
case 'SDK_GAME_PAUSE':
// Ad started — game should pause
if (currentAdvertisementIsRewarded) {
emit('rewardedStateChanged', 'opened');
} else {
emit('interstitialStateChanged', 'opened');
}
emit('pauseStateChanged', true);
emit('audioStateChanged', false);
break;
case 'SDK_REWARDED_WATCH_COMPLETE':
// Reward must be granted here
emit('rewardedStateChanged', 'rewarded');
break;
case 'SDK_GDPR_TRACKING':
case 'SDK_GDPR_TARGETING':
default:
break;
}
}
};
loadScript(SDK_URL).catch(reject);
});
}onEventSDK_READYwindow.gdsdkSDK_GAME_PAUSESDK_GAME_STARTSDK_REWARDED_WATCH_COMPLETESDK_GDPR_TRACKINGSDK_GDPR_TARGETINGgdsdkSDK_GAME_PAUSESDK_GAME_STARTfunction showInterstitial() {
currentAdvertisementIsRewarded = false;
if (!platformSdk) {
emit('interstitialStateChanged', 'failed');
return;
}
platformSdk.showAd().catch(() => {
emit('interstitialStateChanged', 'failed');
});
}showAd()SDK_GAME_STARTfunction preloadRewarded() {
if (platformSdk) {
platformSdk.preloadAd('rewarded');
}
}
function showRewarded() {
currentAdvertisementIsRewarded = true;
if (!platformSdk) {
emit('rewardedStateChanged', 'failed');
return;
}
platformSdk.showAd('rewarded').catch(() => {
emit('rewardedStateChanged', 'failed');
});
}gdsdk.showAd('display', { containerId })'banner-container'const BANNER_CONTAINER_ID = 'banner-container';
function createBannerContainer(position) {
const container = document.createElement('div');
container.id = BANNER_CONTAINER_ID;
container.style.position = 'fixed';
container.style.left = '0';
container.style.right = '0';
container.style.zIndex = '9999';
if (position === 'top') {
container.style.top = '0';
} else {
container.style.bottom = '0';
}
document.body.appendChild(container);
return container;
}
function showBanner(position) {
let container = document.getElementById(BANNER_CONTAINER_ID);
if (!container) {
container = createBannerContainer(position);
}
container.style.display = 'block';
platformSdk
.showAd('display', { containerId: BANNER_CONTAINER_ID })
.then(() => {
emit('bannerStateChanged', 'shown');
})
.catch(() => {
emit('bannerStateChanged', 'failed');
container.style.display = 'none';
});
}
function hideBanner() {
const container = document.getElementById(BANNER_CONTAINER_ID);
if (container) {
container.style.display = 'none';
}
emit('bannerStateChanged', 'hidden');
}position'top''bottom'SDK_GAME_PAUSESDK_GAME_STARTSDK_GAME_PAUSESDK_GAME_STARTSDK_READYdocument.addEventListener('visibilitychange', () => {
const isHidden = document.visibilityState === 'hidden';
emit('pauseStateChanged', isHidden);
emit('audioStateChanged', !isHidden);
});
window.addEventListener('blur', () => {
emit('pauseStateChanged', true);
emit('audioStateChanged', false);
});
window.addEventListener('focus', () => {
emit('pauseStateChanged', false);
emit('audioStateChanged', true);
});window.localStorage