diff --git a/index.html b/index.html index f5861d94e..ae29615c7 100644 --- a/index.html +++ b/index.html @@ -262,6 +262,7 @@ + diff --git a/src/client/graphics/GameRenderer.ts b/src/client/graphics/GameRenderer.ts index d501c095f..1e1db656c 100644 --- a/src/client/graphics/GameRenderer.ts +++ b/src/client/graphics/GameRenderer.ts @@ -34,6 +34,7 @@ import { ReplayPanel } from "./layers/ReplayPanel"; import { SAMRadiusLayer } from "./layers/SAMRadiusLayer"; import { SettingsModal } from "./layers/SettingsModal"; import { SpawnTimer } from "./layers/SpawnTimer"; +import { SpawnVideoAd } from "./layers/SpawnVideoAd"; import { StructureIconsLayer } from "./layers/StructureIconsLayer"; import { StructureLayer } from "./layers/StructureLayer"; import { TeamStats } from "./layers/TeamStats"; @@ -252,6 +253,12 @@ export function createRenderer( } inGameHeaderAd.game = game; + const spawnVideoAd = document.querySelector("spawn-video-ad") as SpawnVideoAd; + if (!(spawnVideoAd instanceof SpawnVideoAd)) { + console.error("spawn video ad not found"); + } + spawnVideoAd.game = game; + // When updating these layers please be mindful of the order. // Try to group layers by the return value of shouldTransform. // Not grouping the layers may cause excessive calls to context.save() and context.restore(). @@ -296,6 +303,7 @@ export function createRenderer( headsUpMessage, multiTabModal, inGameHeaderAd, + spawnVideoAd, alertFrame, performanceOverlay, ]; diff --git a/src/client/graphics/layers/SpawnVideoAd.ts b/src/client/graphics/layers/SpawnVideoAd.ts new file mode 100644 index 000000000..6d782d15d --- /dev/null +++ b/src/client/graphics/layers/SpawnVideoAd.ts @@ -0,0 +1,67 @@ +import { LitElement, html } from "lit"; +import { customElement, state } from "lit/decorators.js"; +import { crazyGamesSDK } from "src/client/CrazyGamesSDK"; +import { getGamesPlayed } from "src/client/Utils"; +import { GameType } from "src/core/game/Game"; +import { GameView } from "../../../core/game/GameView"; +import "../../components/VideoAd"; +import { Layer } from "./Layer"; + +@customElement("spawn-video-ad") +export class SpawnVideoAd extends LitElement implements Layer { + public game: GameView; + + @state() private shouldShow = false; + @state() private adComplete = false; + + createRenderRoot() { + return this; + } + + init() { + if ( + !window.adsEnabled || + window.innerWidth < 768 || + crazyGamesSDK.isOnCrazyGames() || + this.game.config().gameConfig().gameType === GameType.Singleplayer || + getGamesPlayed() < 3 || // Don't show to new players + getGamesPlayed() % 3 !== 0 // Only show 1 in 3 times + ) { + return; + } + this.shouldShow = true; + } + + tick() { + if (this.adComplete) return; + // Hide when spawn phase ends + if (this.shouldShow && !this.game.inSpawnPhase()) { + this.shouldShow = false; + this.requestUpdate(); + } + } + + private handleComplete = () => { + this.adComplete = true; + this.shouldShow = false; + }; + + shouldTransform(): boolean { + return false; + } + + render() { + if (!this.shouldShow || this.adComplete) { + return html``; + } + + return html` +
+ +
+ `; + } +}