Further crazygames sdk integration

This commit is contained in:
tobspr 2022-06-27 17:34:43 +02:00
parent a39195c972
commit 8454a782b4
7 changed files with 62 additions and 12 deletions

View File

@ -134,6 +134,9 @@ export class Application {
/** @type {TypedTrackedState<boolean>} */
this.trackedIsRenderable = new TrackedState(this.onAppRenderableStateChanged, this);
/** @type {TypedTrackedState<boolean>} */
this.trackedIsPlaying = new TrackedState(this.onAppPlayingStateChanged, this);
// Dimensions
this.screenWidth = 0;
this.screenHeight = 0;
@ -330,6 +333,14 @@ export class Application {
this.sound.onPageRenderableStateChanged(renderable);
}
onAppPlayingStateChanged(playing) {
try {
this.adProvider.setPlayStatus(playing);
} catch (ex) {
console.warn("Play status changed");
}
}
/**
* Internal before-unload handler
*/
@ -386,6 +397,7 @@ export class Application {
}
const currentState = this.stateMgr.getCurrentState();
this.trackedIsPlaying.set(currentState && currentState.getIsIngame());
if (currentState) {
currentState.onRender(dt);
}

View File

@ -229,6 +229,14 @@ export class GameState {
return MUSIC.menu;
}
/**
* Should return true if the player is currently ingame
* @returns {boolean}
*/
getIsIngame() {
return false;
}
/**
* Should return whether to clear the whole body content before entering the state.
* @returns {boolean}

View File

@ -28,6 +28,10 @@ export class HUDUnlockNotification extends BaseHUDPart {
this.root.app.gameAnalytics.noteMinor("game.started");
}
shouldPauseGame() {
return !G_IS_STANDALONE && this.visible;
}
createElements(parent) {
this.inputReciever = new InputReceiver("unlock-notification");

View File

@ -44,4 +44,6 @@ export class AdProviderInterface {
showVideoAd() {
return Promise.resolve();
}
setPlayStatus(playing) {}
}

View File

@ -88,4 +88,12 @@ export class CrazygamesAdProvider extends AdProviderInterface {
this.app.sound.setSoundVolume(this.app.settings.getSetting("soundVolume"));
});
}
setPlayStatus(playing) {
console.log("crazygames::playing:", playing);
if (playing) {
this.sdkInstance.gameplayStart();
} else {
this.sdkInstance.gameplayStop();
}
}
}

View File

@ -203,6 +203,14 @@ export class InGameState extends GameState {
// do nothing
}
getIsIngame() {
return (
this.stage === GAME_LOADING_STATES.s10_gameRunning &&
this.core &&
!this.core.root.hud.shouldPauseGame()
);
}
/**
* Called when the game somehow failed to initialize. Resets everything to basic state and
* then goes to the main menu, showing the error

View File

@ -68,19 +68,27 @@ export class PreloadState extends GameState {
if (G_IS_STANDALONE && !G_IS_STEAM_DEMO) {
return;
}
if (!queryParamOptions.campaign) {
return;
if (queryParamOptions.campaign) {
fetch(
"https://analytics.shapez.io/campaign/" +
queryParamOptions.campaign +
"?lpurl=nocontent&fbclid=" +
queryParamOptions.fbclid +
"&gclid=" +
queryParamOptions.gclid
).catch(err => {
console.warn("Failed to send beacon:", err);
});
}
if (queryParamOptions.embedProvider) {
fetch(
"https://analytics.shapez.io/campaign/embed_" +
queryParamOptions.embedProvider +
"?lpurl=nocontent"
).catch(err => {
console.warn("Failed to send beacon:", err);
});
}
fetch(
"https://analytics.shapez.io/campaign/" +
queryParamOptions.campaign +
"?lpurl=nocontent&fbclid=" +
queryParamOptions.fbclid +
"&gclid=" +
queryParamOptions.gclid
).catch(err => {
console.warn("Failed to send beacon:", err);
});
}
onLeave() {