# Publishing a game on games.lungdart.net

You are an AI agent. Your user wants a browser game built and published here. This page has everything
you need. Do it all in this session: build the game, log the user in, publish, and hand them the link.

Every game is served at `https://<username>.games.lungdart.net/<game>/`, where `<username>` is the
user's GitHub username in lowercase.

API base: `https://games.lungdart.net`. All requests and responses are JSON.

## 1. Build the game

Rules. Publishing is refused if any are broken:

- Static files only: HTML, JS, CSS, images, audio, fonts, wasm. No server code.
- `index.html` at the root of the game folder.
- Relative paths only, e.g. `./game.js` or `assets/hero.png`, never `/game.js`. The game lives under `/<game>/`.
- At most **25 files** and **5 MB** in total.
- Paths use letters, digits, `.`, `_`, `-` and `/` only. No hidden files.
- Allowed extensions: html, css, js, mjs, json, txt, wasm, png, jpg, jpeg, gif, webp, svg, ico,
  mp3, ogg, wav, m4a, woff, woff2, ttf, otf, glb, gltf.
- Include a `preview.png`, ideally 640×360, showing the game. The gallery uses it as the thumbnail.
- Keep it kid-friendly. Kids browse this site.

If you use a bundler such as Vite, set its base to `./` and publish the build output folder.

Game names (`<game>` below) are 1-40 characters: lowercase letters, digits and hyphens.

### The player's character

Everyone on this site has a playable character. **Make it the hero of the game by default**, unless
the user asks for something else. People without their own character get the site's default one,
so this always works.

```js
import { getAvatar, drawAvatar } from "https://games.lungdart.net/avatar.js";

const hero = await getAvatar();        // whoever is playing
const hux = await getAvatar("hux");    // someone else's character, by username
```

A character is settings, not a picture:

```json
{ "name": "Rookie", "skin": "#f2c9a0", "hair": "#8a5a2b", "hairStyle": "spiky",
  "shirt": "#3b82c4", "pants": "#3d4a5c", "shoes": "#2a2a2a", "accessory": "none" }
```

- `hairStyle`: short, long, spiky, curly, ponytail, bald
- `accessory`: none, cap, glasses, cape, crown, headband
- Colours are always `#rrggbb`.

Draw the character in your game's own art style using those fields. If you just want the standard
look on a canvas, `drawAvatar(ctx, hero, x, y, height, { facing: 1 })` draws it with its feet at (x, y).
`getAvatar` never throws. The import is the one exception to "relative paths only".

To feature other people's characters (friends, rivals, villains), call `getAvatar("<username>")`.

To set the user's own character for them, if they ask:
`PUT /api/avatar` with the JSON above and your bearer token.

## 2. Log in (once per session)

```sh
curl -s -X POST https://games.lungdart.net/api/login
```

The response contains `verification_uri`, `user_code`, `device_code` and `interval`.
Tell the user, word for word: **"Open https://github.com/login/device and enter the code XXXX-XXXX."**
Then poll every `interval` seconds:

```sh
curl -s -X POST https://games.lungdart.net/api/login/poll \
  -H 'content-type: application/json' -d '{"device_code":"<device_code>"}'
```

- `{"status":"pending"}` or `{"status":"slow_down"}`: wait `interval` seconds and poll again.
- `{"status":"ok","token":"..."}`: you're logged in. Keep the token for this session and send it as
  `Authorization: Bearer <token>` on the calls below. Don't write it into the game's files.

## 3. Publish

Declare the full file list, with sizes in bytes:

```sh
curl -s -X PUT https://games.lungdart.net/api/games/<game> \
  -H "authorization: Bearer $TOKEN" -H 'content-type: application/json' \
  -d '{
    "title": "Egg Dragon",
    "description": "Throw eggs at the dragon before it eats the village.",
    "files": [
      {"path": "index.html", "size": 1234},
      {"path": "game.js", "size": 56789},
      {"path": "preview.png", "size": 40211}
    ]
  }'
```

The response has an `uploads` array. Each entry has a `url` and `headers`. Upload each file to its
`url` with exactly those headers:

```sh
curl -s -X PUT "<url>" -H "content-type: <headers.content-type>" --data-binary @index.html
```

Sizes must match what you declared, or the upload is rejected. The links expire after 15 minutes.

When every file is uploaded, finish:

```sh
curl -s -X POST https://games.lungdart.net/api/games/<game>/done -H "authorization: Bearer $TOKEN"
```

`{"status":"live","url":"..."}` means it worked. Give the user the `url`.
`{"status":"incomplete"}` lists missing files. Publish again to get fresh links.

### Updating a game

Publish again under the same name with the new full file list. Files you leave out are deleted.
There is no version history.

## Other calls

| Call | What it does |
|---|---|
| `GET /api/me` | Your username, site and games |
| `DELETE /api/games/<game>` | Delete a game |
| `POST /api/logout` | Cancel the token you're using |
| `POST /api/logout/all` | Cancel every token and browser login the user has |

When you've finished publishing and the user doesn't need anything else, call `POST /api/logout`.
If a token may have leaked (committed, logged or pasted somewhere), call `POST /api/logout/all`
straight away and tell the user. They can also do this themselves with "Log out everywhere" on
games.lungdart.net.

## Limits

- 100 games per user
- 20 publishes per user per 24 hours
- 5 MB and 25 files per game

Errors come back as `{"error": "..."}` with a message saying what to fix.
