# Bring a Phaser or HTML5 game

For a Phaser game, or any game written in plain JavaScript or TypeScript (Three.js, PixiJS, Babylon.js, a bare canvas), that already runs on other web game portals. These games call the portal SDK from JavaScript, so they have the shortest port: change the script tag, then rename the calls or load the compatibility shim. The full references are the [Phaser page](https://docs.zokogames.com/engines/phaser) and [plain HTML5 and libraries](https://docs.zokogames.com/engines/html5-libraries). The overall route is in [bring your game](https://docs.zokogames.com/porting/overview).

## 1. Build settings

| Setting | On ZokoGames |
| --- | --- |
| SDK script tag | `<script src="https://sdk.zokogames.com/zokogames-sdk-v1.js"></script>` before your game script, in place of the other portal's tag. Keep the exact URL: the validator looks for it, and a bundled copy is flagged (`sdk_bundled`). |
| Paths | Relative only. With Vite set `base: "./"`; root-absolute paths raise `absolute_path`. See [relative paths](https://docs.zokogames.com/requirements/technical#relative-paths-only). |
| Pre-compression | None. Your page requests plain file names. |
| Archive | The build output zipped with `index.html` at the archive root. |
| Canvas | Sized from the window: `Phaser.Scale.FIT` with `autoCenter: CENTER_BOTH`, or the resize handler in [sizing](https://docs.zokogames.com/engines/html5-libraries#5-sizing). |

## 2. Rename the calls, or shim them

Most web game portal SDKs share the shapes ZokoGames uses: `init()`, `ad.requestAd(type, callbacks)` with started, finished and error callbacks, gameplay start and stop, user, cloud save. Pick one route.

**Rename (recommended).** Point your code at `window.ZokoGames.SDK` and check the differences listed in [migrating](https://docs.zokogames.com/guides/migrating#mapping-table). The usual shape after the rename:

```js
const SDK = window.ZokoGames?.SDK;                 // undefined if the script was blocked
let live = false;

async function startPlatform() {
  if (!SDK) return;
  await SDK.init();                                // never rejects
  live = SDK.environment !== "disabled";           // the game must also run when disabled
}

function showAd(type, pause, resume) {
  if (!live) return Promise.resolve(false);
  return new Promise((done) => {
    SDK.ad.requestAd(type, {
      adStarted: pause,
      adFinished: () => { resume(); done(true); },   // grant a reward only here
      adError: () => { resume(); done(false); },     // unfilled, adCooldown, adblock
    }).catch(() => { resume(); done(false); });
  });
}
```

**Every `SDK.game.*`, `SDK.user.*`, `SDK.ad.*` and `SDK.banner.*` call returns a promise, and any of them can reject** (for example `sdkNotInitialized`, `sdkDisabled`, `rateLimited` or `unexpectedError`; `SDK.data` is the exception: it is synchronous and throws). Code ported from a portal whose calls never failed often fires them and forgets, which leaves an unhandled rejection in the console, or a stuck pause when the call that should resume the game failed. Add `.catch(() => {})` (or `try`/`await`) to every call you do not read the result of, and resume the game in the error path.

**Shim (quick port).** Keep your call sites and add the shim after the SDK, naming the global your game already reads:

```html
<script src="https://sdk.zokogames.com/zokogames-sdk-v1.js"></script>
<script src="zokogames-compat-v1.js" data-global="YourOldGlobal"></script>
```

Download `zokogames-compat-v1.js` from `https://www.zokogames.com/sdk/zokogames-compat-v1.js` and ship it in your zip next to `index.html`. It forwards every call, translates the common differences (an old error spelling, callback-style calls, renamed loading calls) and runs `init()` for games that never call it. What it translates, and what it does not, is in [migrating](https://docs.zokogames.com/guides/migrating#two-ways-to-port). Plan to rename later: the shim is a bridge.

**Phaser plugin.** For Phaser, the [ZokoGames Phaser plugin](https://docs.zokogames.com/engines/phaser#what-the-plugin-gives-you) does the ad pause for you: `requestAd` sleeps the game loop and mutes sound on start and restores both on finish and on error. If your old code paused only one scene, switch to the plugin or to `game.loop.sleep()`.

| Your game does | With the SDK |
| --- | --- |
| Midgame ad (an interstitial or commercial break) or rewarded ad | `SDK.ad.requestAd("midgame", callbacks)` or `"rewarded"`, as in `showAd` above. Any other type string rejects with `invalidArgument`, and ads before the game starts (prerolls) are not shown. |
| Gameplay start and stop | `SDK.game.gameplayStart()` and `gameplayStop()`. Call `gameplayStart()` before the first midgame request: until then the portal refuses it with `adCooldown`. |
| Loading finished | `SDK.game.loadingStart()`, `loadingProgress(0..100)`, then `loadingStop()` once the first screen can be played; the Phaser plugin's `trackLoading(this.load)` does it for a loader. Nothing sends these for you in plain HTML5. |
| Player name, sign-in | `SDK.user.getUser()` resolves `null` for a guest (not an error); `SDK.user.showAuthPrompt()` opens the sign-in dialog. |
| Mute | Apply `SDK.game.settings.muteAudio` at start and in `addSettingsChangeListener`. |
| Cloud save | `SDK.data.setItem(key, value)` and `getItem(key)`, strings only. Moving old `localStorage` saves: [data](https://docs.zokogames.com/sdk/data#migrating-from-localstorage). |
| Leaderboard score | `SDK.user.submitScore({ encryptedScore, score })` on a board created in the console. The page can encrypt with Web Crypto, see [leaderboards](https://docs.zokogames.com/sdk/leaderboards#client-submission). |
| Purchases | `SDK.store.getCatalogItems()`, `buyItem(itemId)`, `consumeItem(sku, quantity)`. Purchases are test purchases for now ([purchases](https://docs.zokogames.com/sdk/purchases)). |

## 3. Build size and mobile

- Budget: 1,500 files, 250 MB unpacked, about 50 MB initial download (20 MB for mobile), estimated from uncompressed sizes.
- Minify and tree-shake; pack textures into atlases; WebP or KTX2 textures; modest audio bitrates.
- Load later levels after the first scene, not in one big `preload`.
- Cap `devicePixelRatio` on phones and start audio from a pointer or key event. See [mobile](https://docs.zokogames.com/guides/mobile).

## 4. Test before you submit

1. **Serve the game from `localhost`.** The SDK starts in local mode: a three-second overlay per ad, fake players, saves in `localStorage`. Add `?sdk_debug=true` to log every call, `?ad_response=unfilled` to force ad errors and `?user_response=logged_out` for guests. A second midgame within three minutes fails with `adCooldown`. See [local mode](https://docs.zokogames.com/getting-started/local-testing).
2. **Open the [Preview tool](https://www.zokogames.com/preview)** with your local address and watch the checklist tick.
3. **Upload the zip** and open the game's **Preview and QA** tab in the developer console to run it in the portal frame with ads on ([build preview](https://docs.zokogames.com/getting-started/local-testing#the-build-preview-in-the-console)).

## Checklist

- [ ] ZokoGames SDK script tag; the other portal's tag and any bundled SDK copy removed.
- [ ] Relative paths; `index.html` at the archive root.
- [ ] Calls renamed, or the shim loaded after the SDK with `data-global`.
- [ ] `init()` first; the game plays when the SDK is missing or `disabled`.
- [ ] The whole loop and audio pause on ad start and resume on finish and error; rewards only on finish.
- [ ] Saves through `SDK.data`; scores on a board created in the console.
- [ ] Other portal's logos, links and sitelock removed.
- [ ] Tested on `localhost`, in the Preview tool and in **Preview and QA**.

## Next

- Full references: [Phaser](https://docs.zokogames.com/engines/phaser), [plain HTML5 and libraries](https://docs.zokogames.com/engines/html5-libraries).
- Before upload: [build validation](https://docs.zokogames.com/requirements/build-validation) and the [launch checklist](https://docs.zokogames.com/requirements/checklist).
- Upload and publish: [submission walkthrough](https://docs.zokogames.com/getting-started/submission-walkthrough).
