diff --git a/src/js/application.js b/src/js/application.js index 78d4518d..d31e6e3f 100644 --- a/src/js/application.js +++ b/src/js/application.js @@ -134,6 +134,9 @@ export class Application { /** @type {TypedTrackedState} */ this.trackedIsRenderable = new TrackedState(this.onAppRenderableStateChanged, this); + /** @type {TypedTrackedState} */ + 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); } diff --git a/src/js/core/game_state.js b/src/js/core/game_state.js index d71d3e52..6f276e99 100644 --- a/src/js/core/game_state.js +++ b/src/js/core/game_state.js @@ -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} diff --git a/src/js/game/hud/parts/unlock_notification.js b/src/js/game/hud/parts/unlock_notification.js index ff09cb4e..ba129f00 100644 --- a/src/js/game/hud/parts/unlock_notification.js +++ b/src/js/game/hud/parts/unlock_notification.js @@ -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"); diff --git a/src/js/platform/ad_provider.js b/src/js/platform/ad_provider.js index 4aa8949c..6b96768e 100644 --- a/src/js/platform/ad_provider.js +++ b/src/js/platform/ad_provider.js @@ -44,4 +44,6 @@ export class AdProviderInterface { showVideoAd() { return Promise.resolve(); } + + setPlayStatus(playing) {} } diff --git a/src/js/platform/ad_providers/crazygames.js b/src/js/platform/ad_providers/crazygames.js index 295a2102..5f70cea4 100644 --- a/src/js/platform/ad_providers/crazygames.js +++ b/src/js/platform/ad_providers/crazygames.js @@ -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(); + } + } } diff --git a/src/js/states/ingame.js b/src/js/states/ingame.js index aaf37269..ec410b1a 100644 --- a/src/js/states/ingame.js +++ b/src/js/states/ingame.js @@ -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 diff --git a/src/js/states/preload.js b/src/js/states/preload.js index c2e90cb8..00a4e282 100644 --- a/src/js/states/preload.js +++ b/src/js/states/preload.js @@ -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() {