> 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/chatgpt-turf-painters.md).

# ChatGPT Turf Painters

Make a ChatGPT 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 with [ChatGPT](https://chatgpt.com/) online on Edgegap.

![ChatGPT Turf Painters, running online matches on Edgegap](https://raw.githubusercontent.com/edgegap/netcode-sample-chatgpt-turf-painters/main/docs/images/card.jpg)

Turf Painters is an arena game for 4 painters: every tile you roll over takes your color, and bumping a rival at speed splashes your paint around them. After 60 seconds, the painter with the most tiles wins. ChatGPT wrote the whole game, solo against 3 bots, as one HTML file.

* **ChatGPT**: 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 ChatGPT to keep every game rule between `/* RULES:BEGIN */` and `/* RULES:END */`, with no browser code inside. The file still plays in ChatGPT's preview, 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 file.
* **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.
* **ChatGPT's own output**: the tag `before` in the repository holds the file as ChatGPT wrote it, so every change for Edgegap is visible in the history.

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

## ✔️ Preparation

Before we start, you will need:

* [ChatGPT Turf Painters sample](https://github.com/edgegap/netcode-sample-chatgpt-turf-painters), or your own game made with ChatGPT.
* [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 ChatGPT

### 1. Ask for Rules Between Markers

☑️ In a new chat on chatgpt.com, describe your game and keep the architecture part of this prompt as it is. Turf Painters came from this prompt:

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

```
Use canvas. Build a small browser game as a single HTML file called "Turf Painters" (plain Canvas 2D, no frameworks, no external libraries) and show it in canvas so I can play it in the preview. Gameplay: 4 round painters in a square arena (800x800 units, a 20x20 grid of floor tiles) move with WASD/arrow keys with drift (velocity + friction). Every tile a painter rolls over turns its color. Bumping another painter at speed splashes paint: the 3x3 tiles around the bumped painter turn to the bumper's color. A round lasts 60 seconds; the painter with the most tiles 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:'bump', a, b} and {type:'win', winner}), and botInput(world, id) using only positions and tiles. Everything else (rendering, keyboard input, calling step() at 60 Hz) stays outside the block.
```

{% endcode %}

☑️ Open the file ChatGPT returns and play it in the preview. Check that:

* the bots move,
* `createWorld()` returns a world with no players,
* `addPlayer()` gives each new player its own start spot.

The server adds its own players, so it needs all three. ChatGPT's first Turf Painters file had frozen bots and created the painters inside `createWorld()`. One follow-up fixed both:

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

```
Two fixes, keep everything else and the RULES markers the same. 1) The bots never move: setInput ignores bot players, but the game loop feeds bots through setInput. Let setInput work for every player. 2) createWorld() should return a world with no players; the page then adds the 4 painters with addPlayer, and addPlayer places each new painter at its own free start spot (one per corner area), so any code can add up to 4 players with any ids. Give me the full updated HTML file.
```

{% endcode %}

☑️ Ask ChatGPT to paste the complete HTML file in one code block, click **Copy code**, and save it unchanged as `artifact/turf-painters.html` in your repository.

{% hint style="warning" %}
**Pick a game that holds up with network delay.** Drifting movement, pushing, painting, 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/turf-painters.html --page
```

{% endcode %}

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

When you change the game later in ChatGPT, save the new file 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 feeds the bots, runs the rules at 60 Hz, accepts the four direction buttons from each player, counts tiles as scores, and sends the world back 20 times a second.

☑️ Add the online part of the sample's `client/index.html` to your page: **Find match** and **Local server** buttons, drawing the server's world with ChatGPT's own renderer, and the waiting, countdown and results text in the side panel.

☑️ 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 file, plays a test match, and pushes `registry.edgegap.com/<project>/turf-painters:<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 `turf-painters`, with one version named after your tag:

* **Image**: registry `registry.edgegap.com`, repository `<project>/turf-painters`, 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 round 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/turf-painters.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 `turf-painters` 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 round with the same rules as ChatGPT's file,
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 ChatGPT: upload the file back to the chat, ask for the change while keeping every rule between the markers and for the full file in one code block, 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 ChatGPT game.
{% endhint %}
