Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

4.4 KB
Raw Blame
// The ad network a playable export is built for, and the three things every network wants from it in its own words:
// when the ad may start (and must pause), how the store opens, and that the game ended (docs/playables.feature PA2).
// The export names the network at build time (__RAEA_NETWORK__); a game's own code only calls install() and end().

export type Network = "applovin" | "unity" | "google" | "meta" | "moloco" | "mintegral" | "tiktok" | "liftoff" | "preview";

export interface StoreLinks {
  ios?: string;
  android?: string;
}

declare const __RAEA_NETWORK__: Network | undefined;
declare const __RAEA_STORE__: StoreLinks | undefined;

interface Mraid {
  getState(): string;
  isViewable(): boolean;
  addEventListener(event: string, f: (...a: unknown[]) => void): void;
  open(url?: string): void;
}

type Win = typeof globalThis & {
  mraid?: Mraid;
  ExitApi?: { exit(): void };
  FbPlayableAd?: { onCTAClick(): void };
  Liftoff?: { open(): void };
  install?: () => void;
  openAppStore?: () => void;
  gameReady?: () => void;
  gameEnd?: () => void;
  gameStart?: () => void;
  gameClose?: () => void;
};

const w = globalThis as Win;

/** The network this build is for: "preview" outside an export (a raea render, a browser tab) */
export const network: Network = typeof __RAEA_NETWORK__ === "string" ? __RAEA_NETWORK__ : "preview";
const store: StoreLinks = typeof __RAEA_STORE__ === "object" && __RAEA_STORE__ ? __RAEA_STORE__ : {};

const MRAID: readonly Network[] = ["applovin", "unity", "liftoff"];

/** The store link for this device: iOS's on iPhone/iPad, Android's otherwise */
export function storeUrl(): string | undefined {
  const ua = typeof navigator === "undefined" ? "" : navigator.userAgent;
  const ios = /iPhone|iPad|iPod|Macintosh/.test(ua) && !/Android/.test(ua);
  return (ios ? store.ios : store.android) ?? store.android ?? store.ios;
}

const listeners: ((e: { kind: "install" | "end" }) => void)[] = [];
/** Called on install() and end(), for the preview page's log and tests */
export function onAdEvent(f: (e: { kind: "install" | "end" }) => void): void {
  listeners.push(f);
}

let ended = false;

/** Open the store, the network's way. Call it only from a player's tap (every network rejects an automatic redirect). */
export function install(): void {
  for (const f of listeners) f({ kind: "install" });
  if (!ended) end();
  const url = storeUrl();
  switch (network) {
    case "applovin":
    case "unity":
      if (w.mraid) w.mraid.open(url);
      return;
    case "liftoff":
      if (w.Liftoff) w.Liftoff.open();
      else w.mraid?.open();
      return;
    case "google":
      w.ExitApi?.exit();
      return;
    case "meta":
    case "moloco":
      w.FbPlayableAd?.onCTAClick();
      return;
    case "mintegral":
      w.install?.();
      return;
    case "tiktok":
      w.openAppStore?.();
      return;
    case "preview":
      return;
  }
}

/** The game is over (won, lost or out of time): Mintegral's gameEnd; the host shows the end card. Called once. */
export function end(): void {
  if (ended) return;
  ended = true;
  for (const f of listeners) f({ kind: "end" });
  if (network === "mintegral") w.gameEnd?.();
}

/** Whether end() was called */
export function isEnded(): boolean {
  return ended;
}

/** Calls `start` once the ad may begin, then `visible(false|true)` as it is hidden and shown again (Unity, AppLovin:
 *  start only after MRAID's ready and while viewable, pause while not). Mintegral is told the game is ready and
 *  starts it with its own gameStart. */
export function whenReady(start: () => void, visible: (on: boolean) => void): void {
  let started = false;
  const go = () => {
    if (started) return;
    started = true;
    start();
  };
  if (MRAID.includes(network) && w.mraid) {
    const m = w.mraid;
    const onReady = () => {
      m.addEventListener("viewableChange", (on) => {
        if (on) go();
        visible(!!on);
      });
      if (m.isViewable()) go();
    };
    if (m.getState() === "loading") m.addEventListener("ready", onReady);
    else onReady();
    return;
  }
  if (network === "mintegral") {
    w.gameStart = go;
    w.gameClose = () => visible(false);
    w.gameReady?.();
    // Mintegral's preview tool calls gameStart; a page opened by hand starts on its own
    if (!w.gameReady) go();
    return;
  }
  if (typeof document !== "undefined") {
    document.addEventListener("visibilitychange", () => visible(document.visibilityState === "visible"));
  }
  go();
}