Download the exact captured file
---
name: edgegap-grok
description: 'Grok + Edgegap: make a browser game that Grok (grok.com, xAI) wrote as one HTML file online multiplayer, one authoritative server per match on Edgegap with an optional matchmaker, while the file still plays in Grok''s preview pane. Load this when someone made, or wants to make, a game with Grok ("Grok wrote me a game", "Grok code preview", "single HTML file from Grok") and wants online play, friends joining, Find Match or real servers, or asks how to prompt Grok so the game can go online later. Has the prompt that keeps every rule between /* RULES:BEGIN */ and /* RULES:END */ markers (Grok got it right first time), getting the exact file out with Copy code, the server game adapter, the online block for Grok''s page, a direct ?server= path, CI image build and tests. Verified sample: netcode-sample-grok-crown-keeper. Claude, ChatGPT, Gemini chat games: edgegap-chat-canvas; other AI tools: edgegap-vibe-coded.'
metadata:
version: "0.1.1"
---
# Edgegap for Grok games: Grok writes one HTML file, Edgegap runs the matches
On **grok.com**, Grok answers a game request with the whole game in one HTML code block. The block's **Preview** button opens a side pane (**Preview** / **Source**) where the game plays. That shape is the whole problem: the server needs the rules as a module of their own, and the preview needs everything in one file. The answer is to ask Grok for the rules between two exact marker lines and copy that block out with a script. The file keeps playing in Grok and can go back into the chat for more changes; the server runs the identical rules.
```
grok.com ──writes──> artifact/<game>.html (one file, solo vs bots, plays in Preview)
│ /* RULES:BEGIN */ ... /* RULES:END */
tools/split-artifact.mjs ──copies the block──> client/rules.js (ES module, generated, checked in CI)
│
page ──?server= or Find match──> Edgegap ──> one Node.js server per match: runs rules.js at 60 Hz,
<──────────── wss://<id>.pr.edgegap.net:<port> ── fills seats with bots, stops itself after the match
```
**Verified on Edgegap** with [netcode-sample-grok-crown-keeper](https://github.com/edgegap/netcode-sample-grok-crown-keeper) (2026-11-30; tag `before` = Grok's file unchanged, then the online work on `main`): Grok (free account, model picker on Fast, the default) wrote Crown Keeper from one prompt; it played in the preview with bots chasing the crown, and its first answer already met all four server requirements, so no fix prompt. The split script produced the server's rules with no edits to the block. The image was built by GitHub Actions; two browser tabs joined one Edgegap deployment in Toronto through `?server=` and played with two bots, and the server stopped itself; two headless players then played a full match on a second deployment (a bot held the crown for 20 s) and it stopped itself about 20 s after the results screen. Through a matchmaker shared with the other chat-game samples (one profile per game), two headless players were matched onto one deployment in 15 s, played a full match and it stopped itself 11 s after; two browser tabs clicking **Find match** landed on one deployment with two bots, and it stopped itself after both left. [references/crown-keeper.md](references/crown-keeper.md) has the run, file by file.
This skill shares the split script, server, client and test templates with `edgegap-chat-canvas` (the same workflow for Claude artifacts, ChatGPT and Gemini Canvas) and `edgegap-threejs`. Read the links below when a phase points to them.
## Who you are talking to
Often someone who made a game by chatting with Grok and has never run a server. Speak in outcomes ("each match gets its own server near its players and shuts down when the match ends, so you pay only while people play"), do the engineering yourself, and hand over only the account steps (signing in to Grok, registry secrets in GitHub, creating the matchmaker). Keep the prompts in `PROMPTS.md`: they are how the game gets changed later in Grok.
## Phase 0: Get a Grok game whose rules can leave the file
1. **Game fit** (`edgegap-vibe-coded` Phase 0 table): drift, pushing, grabbing, chasing, slow projectiles and turn-based play feel fine with no client prediction; precise platforming, rhythm and instant-hit aiming do not. Change the idea before Grok writes it.
2. **Sign in.** Grok asks for an account before it answers (the guest chat stops at "Sign up to continue"). A free account and the default model (Fast) are enough. If you drive grok.com from a browser tool, the developer signs in themselves.
3. **New game: send the prompt** in [references/prompts.md](references/prompts.md) §1 in a new chat. The gameplay sentences are the developer's; the architecture part stays word for word: every rule between the exact lines `/* RULES:BEGIN */` and `/* RULES:END */`, nothing from the browser inside, no `export`, a plain-data world, the six functions (`createWorld` with no players, `addPlayer` with its own free start spot, `removePlayer`, `setInput` for every player, `step` that drives the bots and returns events, `botInput` from positions only), and "Paste the complete HTML file in one code block".
4. **Existing Grok game without markers:** paste it back (or continue the chat) with the restructure prompt §2. Check the game still plays in **Preview** before going on.
5. **Play it, then check three things** before saving: the bots move in the preview; `createWorld()` returns a world with no players and `addPlayer()` gives each new player its own start spot; `step()` drives the bots itself (Grok's Crown Keeper did all three). If one fails, send fix prompt §3 rather than editing the block.
6. **Get the file out**: click **Copy code** on the HTML code block and save the clipboard unchanged as `artifact/<game>.html` in a new git repo (on Windows, `Get-Clipboard -Raw` written as UTF-8 without BOM keeps it exact; `.gitattributes` with `* text=auto eol=lf` normalizes the line endings). Commit, tag `before`. Record the model shown in the picker and every prompt verbatim in `PROMPTS.md`.
✅ Check: the file plays solo in Grok's preview with moving bots, and `grep -c "RULES:BEGIN\|RULES:END" artifact/<game>.html` prints `2`.
## Phase 1: Split, server, online page, local tests
1. **Split.** Copy [split-artifact.mjs](../edgegap-chat-canvas/assets/split-artifact.mjs) from `edgegap-chat-canvas` to `tools/`, then `node tools/split-artifact.mjs artifact/<game>.html --page`. It writes `client/rules.js` (the block + `export { ...every top-level name }`) and, once, `client/index.html` (Grok's page with the block removed and its `<script>` turned into a module importing the rules). It refuses missing or doubled markers, browser-only names in the block (with the line) and a missing function: send that message back to Grok as a fix request. Add `"split"` and `"check:rules"` (`--check`) scripts to `package.json`.
2. **Server.** `edgegap-threejs` [server templates](../edgegap-threejs/assets/server/) (`main.js`, `room.js`, `edgegap.js`, `util.js`, `Dockerfile` copying `server/` and only `client/rules.js`), plus `server/game.js` from [assets/server-game.js](assets/server-game.js): it wraps the rules, accepts only the four booleans, seats the room's players in join order, lets `step()` drive the bots, takes each player's score from the world (Crown Keeper: whole seconds holding the crown), ends on the rules' winner and sends the world itself as the snapshot. Adapt the score, `result()` and the snapshot's rounded fields to your rules; set `matchSeconds` above the game's natural length as a backstop.
3. **Online page.** Grok's Crown Keeper page draws everything inside one `loop()` and reads the global `world`, so the online block swaps in the server's world and Grok's own drawing code renders it. Paste [assets/page-online.js](assets/page-online.js) (about 90 lines) at the end of Grok's script; its header lists the four small edits to Grok's code. Add `client/online.js`, `client/net.js`, `client/config.js` from `edgegap-threejs` (`@edgegap/sdk` through an import map with the SRI hash) and set `PROFILE` in `online.js`. Check every time:
- The human's id is a constant (`'you'` in Crown Keeper): keep it for solo, and online use the server's player id (`HUMAN`) to label that player "(you)".
- Restart keys start a solo game from any screen: guard them to solo mode.
- The loop steps the world every frame: step only in solo mode; online, ease players toward each update (and anything attached to a player, like the crown on the holder's head).
- Names into `innerHTML` must be escaped online. Crown Keeper draws names on the canvas and its HUD uses `textContent`, so nothing needed escaping.
- Grok put each player's color in the world (`addPlayer` picks it), so colors work with server ids; if your page keys styles by solo ids, fall back to the start spot.
4. **Tests.** `edgegap-threejs` `tests/headless-player.mjs`, `local-match.mjs`, `e2e.mjs`; `playLikeABot()` feeds the snapshot straight to the rules' `botInput` (the snapshot is the world). Run `check:rules` before the local match in `test:local` and in CI.
✅ Check: `npm run test:local` prints `PASS`, and two tabs on `http://localhost:8080/?autoplay=local` (with `npm run server` running) see the same match with two bots.
## Phase 2: Image, app version, a direct match
1. **Image from GitHub Actions**: `edgegap-threejs` [`server-image.yml`](../edgegap-threejs/assets/ci/server-image.yml) with `IMAGE` set, `artifact/**` and `tools/**` in its `paths`, and a `npm run check:rules` step before the local match test. Keep `provenance: false` and `sbom: false`. The developer sets the registry secrets; `EDGEGAP_REGISTRY_PROJECT` is a variable, not a secret. Confirm the tag with `edgegap_list_registry_tags`.
2. **App version**: `edgegap_create_app_version` with port 7777 WS `gameport`, `tls_upgrade: true`, 256 vCPU units / 512 MB, restart `never`, max 30 minutes, env `EMPTY_TIMEOUT_S=15`, `FIRST_JOIN_TIMEOUT_S=60`, `RESULTS_SECONDS=8`.
3. **Direct match, no matchmaker**: `edgegap_deploy` near the players, `edgegap_wait_for_deployment`, then everyone opens `<page>?server=wss://<fqdn>:<external port>`. Enough for playing with friends. To check a deployment without a browser: `SERVER_URL=wss://<fqdn>:<external port> node tests/local-match.mjs` plays a full match with two headless players.
✅ Check: two windows on the `?server=` URL play one match; the deployment logs show both joins, `phase playing`, `match over`, `phase over`, and the deployment leaves the list by itself.
## Phase 3: Optional matchmaker, end to end, clean-up
Verified with Crown Keeper (profile `crown-keeper`); the steps are the ones of `edgegap-chat-canvas` Phase 3: `matchmaker/<game>.json` with one profile named after the game (2 to 4 players, a lone player matched after 20 s with bots, `ticket_expiration_period` 30s, the version name, every page origin in `allowed_cors_origins`); several games can share one matchmaker with a profile each. The developer creates and starts it in the dashboard (give them the file's full path); its API URL and Auth Token go in `client/config.local.js` (git-ignored) or `client/config.js` (client-safe; never the Edgegap API token). Then `MM_URL=... MM_TOKEN=... node tests/e2e.mjs <profile> 2` (Crown Keeper: HOST_ASSIGNED at 15 s, PASS), and two tabs click **Find match** within 20 s (`?autoplay=1` clicks it on load). Adding a profile to a shared matchmaker: copy its live configuration from the dashboard (**Configuration > Copy code**) and add to that, since others may have added profiles since your local copy; the dashboard lists every "image is not cached" as a warning and offers **Start Anyway**. Clean up: stop direct deployments nobody joined, stop or delete the matchmaker after testing, confirm no deployments of yours are running.
## Phase 4: Changing the game later
The file Grok wrote stays the source of the rules. Paste `artifact/<game>.html` back into Grok (or continue the same chat), ask for the change "keeping every rule between the /* RULES:BEGIN */ and /* RULES:END */ lines and the six functions' signatures, and paste the complete HTML file in one code block", copy it over `artifact/<game>.html`, run `npm run split`, commit both files. CI fails if `client/rules.js` no longer matches. Carry drawing or input changes into `client/index.html` by hand (diff Grok's script against the page's); the online block stays as it is. A new build means a new tag on the app version (`edgegap_update_app_version`); the matchmaker keeps working.
## Phase 5: Hand-off
Tell the developer: the prompt and model used, what the split script checks, what was added to the page, the app, version and matchmaker names, how to play without the matchmaker (`?server=`), how to change the game later (Phase 4), what was tested, and what keeps costing money (only a running matchmaker, and any deployment that didn't stop). If the repo becomes a public example, blank the matchmaker URL and token in `client/config.js`.
## Reference files
- [references/prompts.md](references/prompts.md): the Grok game prompt with markers, the restructure prompt, fix and follow-up prompts, the prompt that starts the Edgegap work.
- [references/crown-keeper.md](references/crown-keeper.md): Crown Keeper from grok.com to a verified Edgegap match: what Grok produced, the checks, the page and server changes, the Edgegap run.
- [assets/server-game.js](assets/server-game.js): `server/game.js` for a plain-data world whose `step()` drives the bots, with the score read from the world.
- [assets/page-online.js](assets/page-online.js): the online block for Grok's page, with the edits it needs.
- `edgegap-chat-canvas`: the split script, the other chats' prompts and fixes, the matchmaker phase. `edgegap-threejs`: server, client, CI and test templates, troubleshooting. `edgegap-vibe-coded`: game fit, public demo.