> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/docs/sample-projects/web-games/claude-star-snatch.md).

# Claude Star Snatch

Make a Claude artifact game multiplayer with Edgegap: keep the rules between two marker lines, run them on a Node.js server near players, and add Find Match with the Edgegap matchmaker.

Learn how to take a browser game made as a [Claude](https://claude.ai/) artifact online on Edgegap.

![Claude Star Snatch, running online matches on Edgegap](https://raw.githubusercontent.com/edgegap/netcode-sample-claude-star-snatch/main/docs/images/card.jpg)

Star Snatch is an arena game for 4 ships: collect stars, and ram rivals at speed to knock their stars loose. First to 10 stars wins. Claude wrote the whole game, solo against 3 bots, as one HTML artifact from a single prompt.

* **Claude**: used while you build the game, to write the rules, the renderer and the bots.
* **Edgegap**: used while people play, for one dedicated server per match, near its players, and optional matchmaking.

This sample shows:

* **Rules between two marker lines**: the prompt asks Claude to keep every game rule between `/* RULES:BEGIN */` and `/* RULES:END */`, with no browser code inside. The artifact still plays in claude.ai, and you can keep changing it in the chat.
* **One script for the server**: `tools/split-artifact.mjs` copies that block into `client/rules.js`, which the Node.js server runs. It refuses browser-only code in the block, and a CI check keeps the server's rules in sync with the artifact.
* **Two ways to play online**: join one server by its address (`?server=`), or click **Find match** with the [Edgegap JavaScript SDK](https://www.npmjs.com/package/@edgegap/sdk), 2 to 4 players per match, with bots in the empty seats.
* **Claude's original output**: the tag `before` in the repository holds the untouched artifact, so every change for Edgegap is visible in the history.

{% hint style="info" %}
See the [sample repository](https://github.com/edgegap/netcode-sample-claude-star-snatch) for the game, the split script, the game server, the matchmaker config and the tests.
{% endhint %}

## ✔️ Preparation

Before we start, you will need:

* [Claude Star Snatch sample](https://github.com/edgegap/netcode-sample-claude-star-snatch), or your own game made as a Claude artifact.
* [Node.js](https://nodejs.org/) 22 or newer, to run the split script, the server and the tests.
* A GitHub repository, so GitHub Actions can build the server image. [Docker](https://docs.docker.com/get-docker/) works too.

☑️ Try the game locally first:

{% code collapsedlinecount="10" %}

```bash
npm install
npm run server
npm run client
```

{% endcode %}

Run the last two commands in separate terminals. Open `http://localhost:8080` in two browser tabs and click **Local server :7777** in both.

## 🤖 Build the Game with Claude

### 1. Ask for Rules Between Markers

☑️ In a new chat on claude.ai, describe your game and keep the architecture part of this prompt as it is. Star Snatch came from this prompt:

{% code overflow="wrap" collapsedlinecount="10" %}

```
Build a small browser game as a single HTML artifact called "Star Snatch" (plain Canvas 2D, no frameworks, no external libraries). Gameplay: 4 round ships in a square arena (800x800 units) move with WASD/arrow keys with drift (velocity + friction). Stars spawn at random spots; touching one picks it up. Ramming another ship at speed knocks one of its stars loose. First to 10 stars wins, then a results screen and restart. The human plays against 3 simple bots. VERY IMPORTANT architecture: put ALL game rules in one block between the exact lines /* RULES:BEGIN */ and /* RULES:END */, with NO window, document, canvas, audio, requestAnimationFrame or timers inside, and no export keyword. The world is plain data (objects, arrays, numbers, strings; no classes or Maps). The block defines createWorld(), addPlayer(world, id, name, isBot), removePlayer(world, id), setInput(world, id, {up,down,left,right}), step(world, dt) returning an array of events ({type:'pickup', id}, {type:'bump', a, b}, {type:'win', winner}), and botInput(world, id) using only positions. Everything else (rendering, keyboard input, calling step() at 60 Hz) stays outside the block.
```

{% endcode %}

Claude answered with a playable artifact. Play it in the preview, then download it or copy its code and save it unchanged as `artifact/star-snatch.html` in your repository.

{% hint style="warning" %}
**Pick a game that holds up with network delay.** Drifting movement, pushing, collecting, slow projectiles and turn-based play feel right online. Precise platforming, rhythm games and instant-hit aiming need more than this sample does.
{% endhint %}

✅ You may now proceed to the next step.

### 2. Split Out the Rules

☑️ Copy `tools/split-artifact.mjs` from the sample and run it once:

{% code overflow="wrap" collapsedlinecount="10" %}

```bash
node tools/split-artifact.mjs artifact/star-snatch.html --page
```

{% endcode %}

It writes `client/rules.js`, the block plus an `export` of every function in it, and `client/index.html`, the artifact's page loading those rules. If the block uses browser-only code, the script names the line: send that line back to Claude and ask to move it outside the block.

When you change the game later in claude.ai, save the new artifact over the old one and run `npm run split`.

✅ You may now proceed to the next step.

### 3. Add the Server and Online Play

☑️ Copy `server/`, `client/online.js`, `client/net.js`, `client/config.js` and `tests/` from the sample. The only server file written for this game is `server/game.js`: it runs the rules at 60 Hz, accepts the four direction buttons from each player, and sends the world back 20 times a second.

☑️ Add the online part of the sample's `client/index.html` to your page: **Play vs bots** and **Find match** buttons, drawing the server's world with Claude's own renderer, and the waiting, countdown and results screens.

☑️ Run `npm run test:local`: two headless players play a full match against a local server.

✅ You may now proceed to the next step.

## ⚡ Deploy and Connect

### 4. Build the Server Image

☑️ To get started, you'll need to [create a free account with Edgegap](https://app.edgegap.com/auth/register). No credit card required.

☑️ Find your [Container Registry](https://docs.edgegap.com/learn/advanced-features/edgegap-container-registry.md) project name and credentials. In your GitHub repository, add the variable `EDGEGAP_REGISTRY_PROJECT` and the secrets `EDGEGAP_REGISTRY_USERNAME` and `EDGEGAP_REGISTRY_TOKEN`, then copy the sample's `.github/workflows/server-image.yml`.

Every push to `main` checks that the server's rules match the artifact, plays a test match, and pushes `registry.edgegap.com/<project>/star-snatch:<tag>`.

{% hint style="warning" %}
Keep `provenance: false` and `sbom: false` in the workflow. With the defaults, the image is wrapped in an index that Edgegap cannot pull.
{% endhint %}

✅ You may now proceed to the next step.

### 5. Create the App Version

☑️ [Create an application](https://app.edgegap.com/application-management/applications/list) named `star-snatch`, with one version named after your tag:

* **Image**: registry `registry.edgegap.com`, repository `<project>/star-snatch`, tag `<tag>`,
* **Resources**: 0.25 vCPU and 512 MB memory,
* **Port**: `7777`, name `gameport`, protocol **WS**, with **TLS Upgrade** enabled,
* **Restart policy**: Never,
* **Max duration**: 30 minutes, as a safety net: the server stops itself,
* **Environment variables**: `EMPTY_TIMEOUT_S=15`, `FIRST_JOIN_TIMEOUT_S=60`, `RESULTS_SECONDS=8`.

{% hint style="warning" %}
**TLS Upgrade** lets browsers on `https` pages connect to `wss://<id>.pr.edgegap.net:<port>`. Edgegap terminates TLS and forwards plain WebSocket traffic to port `7777`. Connect with the host name, not the IP, so the certificate matches.
{% endhint %}

{% hint style="info" %}
Free Tier is limited to 2 Applications, 2 versions, and 10 GB of Container Registry storage.
{% endhint %}

☑️ Deploy one server from the dashboard, then open the page in two tabs with the deployment's address:

{% code overflow="wrap" collapsedlinecount="10" %}

```
http://localhost:8080/?server=wss://<fqdn>:<external port>
```

{% endcode %}

🙌 Both players are now in the same match, on a server placed near them, with bots in the empty seats. When the match ends, the deployment stops itself.

✅ You may now proceed to the next step.

### 6. Add Find Match (Optional)

☑️ Set `application.version` in the sample's `matchmaker/star-snatch.json` to your version, and add the address of your page to `allowed_cors_origins`.

☑️ Open the [Matchmaker](https://app.edgegap.com/matchmaker-management-v2/matchmakers/list) page in the dashboard, click **Create**, upload the config, and click **Create and Start**.

The `star-snatch` profile matches 2 to 4 players. After 20 seconds, a player waiting alone gets a server, and bots fill the other seats. Each game gets its own profile, so several games can share one matchmaker.

☑️ When the matchmaker shows **Online**, copy its **API URL** and **Auth Token** into `client/config.local.js`, a copy of `client/config.js`.

{% hint style="success" %}
The **matchmaker Auth Token is safe to ship in web pages**: it can only ask for matches. Never put your Edgegap API token in a page or in the server image.
{% endhint %}

☑️ Open your page in two browser tabs and click **Find match** in both within 20 seconds.

Each page:

1. joins the matchmaker and waits for `HOST_ASSIGNED`,
2. connects to `wss://<fqdn>:<port>`, retrying for a few seconds while the server starts,
3. plays: it sends its four buttons and draws the world the server sends back.

The game server:

1. waits for every matched player, then fills the empty seats with bots,
2. runs the match with the same rules as the artifact,
3. stops its own deployment after the results screen, or soon after everyone leaves.

## 📦 Ship Your Game

* Host the page anywhere: `client/` is static files with no build step. The sample includes a GitHub Pages workflow.
* Keep making the game in claude.ai: paste the artifact back into the chat, ask for the change while keeping every rule between the markers, then run `npm run split`.
* To ship new rules, push them, and point the app version at the new image tag. New matches use it, and running ones keep the old build.
* Turn `inspect` off in the matchmaker config before launch.

{% hint style="info" %}
Using a coding agent? The `edgegap-chat-canvas` skill in [Edgegap's agent skills](https://github.com/edgegap/llm-agent-skills), also served by the [Edgegap MCP server](https://docs.edgegap.com/docs/mcp-integration.md), holds these steps, the split script and the tests, and walks your agent through the same path for your own Claude artifact game.
{% endhint %}
