# Add Arena54 leaderboards and achievements to this project

Goal: Give this game a leaderboard and achievements, kept by Arena54.

You are a coding agent working inside my project. Do the work. Do not ask me anything you can find out by reading the project. Do not invent an endpoint, a field or an error code that is not written below.

## 1. Look at the project first

- Find the framework and language, how the project is run and tested, and where a round ends and its score is known, where the game could show a top-ten list, and which moments deserve a badge.
- Find where the project keeps its public configuration.
- This is a plain web project: use `fetch`, and add no framework and no build step.
- The page must be served from an address registered for the game (https, or http://localhost while building). Opening the file from disk will be refused with `origin_not_allowed`.
- Tell me in two or three lines what you found and what you will change, then carry on.

## 2. The contract

Base URL: `https://developers.arena54.com/api/v1` (called $BASE below).

Every answer is JSON in one envelope:

```json
{ "success": true, "data": …, "meta": { "request_id": "req_…", "mode": "test" } }
{ "success": false, "error": { "code": "…", "message": "…" }, "meta": { "request_id": "req_…", "mode": "test" } }
```

Branch on `error.code`. Show `error.message` to a person; it is written for them. Log `meta.request_id` with any failure.

Calls:

- POST /play/test-token: A sample player. Test keys only.
    data: { "token": "a54p_…", "player": { "id": "pl_test_player", "name": "Test Player" } }
- GET /play/me: Who the token is: id, name, when it expires.
    data: { "id": "pl_…", "name": "Ada", "game": { "slug": "…", "name": "…" }, "expires_at": "2026-10-12T21:00:00.000Z" }
- DELETE /play/me: The player erases themselves from this game: saves, scores, achievements, items and the connection.
    data: { "player": "pl_…", "removed": { "saves": 3, "scores": 1, "achievements": 2, "items": 0, "connection": 1, "ledger_accounts_unlinked": 0 } }   (the player's token as X-Arena54-Player; erases them from this game, so sign them out after)
- GET /play/visit: For the game being visited: the player’s open visit and where they came from, or null.
    data: { "visit": "uuid", "from": { "slug": "home-game", "name": "Home Game" }, "brought_items": true, "agreed_at": "…", "arrived_at": "…" } | null
- POST /play/visit/end: For the game being visited: say the visit is over.
    data: { "ended": true }   (no request body)
- GET /play/visits: For the home game: where this player went, and whether each visit is over.
    data: [{ "visit": "uuid", "to": "other-game", "state": "started" | "arrived" | "ended" | "expired", "started_at": "…", "arrived_at": "…" | null, "ended_at": "…" | null }]
- POST /play/beat: Say the player is still playing. Call it about once a minute. Arena54 counts the time by its own clock.
    data: { "counted": true, "seconds": 5400, "sessions": 3 }   (no request body; a test key answers { "counted": false, "test": true })
- GET /play/playtime: How long this player has played this game, and when they last played.
    data: { "seconds": 5400, "sessions": 3, "last_played_at": "…" | null }
- GET /play/boards: The game’s boards.
    data: [{ "slug": "high-score", "name": "High score", "order": "high" | "low", "max": 1000000, "writer": "page" | "server" | "replay", "reset": "week" (only on a weekly board: it starts empty every Monday 00:00 UTC) }]
- GET /play/boards/{id}: The top scores, 10 unless you send limit (more than 100 is treated as 100). No player needed.
    data: { "board": {…}, "top": [{ "rank": 1, "player": "pl_…", "name": "Ada", "score": 4200, "verified": false, "at": "…" }], "me": { "best": 4200, "rank": 2 } | null }   (me is null with no player token, and for a player with no score yet)
- POST /play/boards/{id}: Report {"score": n}: a whole number from 0 to the board’s largest.
    data: { "improved": true, "best": 4200, "rank": 2 }   (the request body is { "score": 4200 })
- GET /play/achievements: The game’s achievements, and which the player has. Needs a player token.
    data: [{ "slug": "first-win", "name": "First win", "description": "…", "writer": "page" | "server", "unlocked_at": "…" | null }]
- POST /play/achievements/{id}: Unlock one. Twice is the same as once.
    data: { "slug": "first-win", "name": "First win", "new": true }

Keys and headers:

- The game’s PUBLISHABLE key (starts `a54_pub_test_` or `a54_pub_live_`) may sit in the page. It works only from the addresses registered for the game.
- Send `Authorization: Bearer <publishable key>` on every call, and `X-Arena54-Player: <token>` on every call that concerns a player.
- Never put a secret key in a page or a game build. A secret key starts `a54_test_` or `a54_live_`, with no `pub_`.
- Test and live data are kept apart. A test key gets a sample player from POST /play/test-token; a live key needs a real sign-in.
- The key’s calls a minute are shared by every player of the game (60 on the free plan), and each player has 120 a minute. Call on load and when a round ends, never on every frame.
- A 429 carries `Retry-After` in seconds, and a page can read it.
- `error.message` is sometimes written for you, the developer. For `invalid_player`, `origin_not_allowed` and `module_not_enabled`, show the player your own words.

Refusals:

- 409 not_for_sale: The item has no price.
- 409 price_out_of_range: The item’s price is outside the range Arena54 sells at.
- 400 bad_redirect: A purchase was started without a return address inside one the game registered.
- 502 provider_unavailable: The payment provider could not open a checkout. Nothing was charged.
- 503 payments_not_open: A real purchase was asked for while real payments are closed. Nothing was charged. Test purchases still work.
- 429 too_soon: An economy report arrived sooner after the last one than is allowed. Retry-After says how long to wait.
- 400 bad_transfer: A transfer was sent without the other game’s id or without a reference of your own.
- 409 no_route: No route both makers agreed takes that item to that game.
- 409 cannot_transfer: The player cannot move that item there: they do not own it, already own its match there, do not play that game, or have not turned on sharing. One answer for all four, so a game cannot learn where its player plays.
- 409 ref_reused: The transfer reference was already used for a different move.
- 429 transfer_limit: The player has moved as many items out of this game as one day allows.
- 401 missing_key: No Authorization header, or what follows “Bearer” is not shaped like a game’s key.
- 401 invalid_key: The key is shaped like one but is unknown or revoked, or its game is gone.
- 403 origin_not_allowed: The page’s address is not one the game registered.
- 403 module_not_enabled: The module is not ticked for this game in the dashboard.
- 503 module_off: Arena54 has switched the module off for now.
- 401 invalid_player: No player token, an expired or altered one, or one for another game or mode.
- 401 player_revoked: The player disconnected the game. Send them to sign in again.
- 403 test_only: A sample player was asked for with a live key.
- 403 server_only: The board or achievement takes writes only from a secret key.
- 400 bad_score: The score is not a whole number from 0 to the board’s largest, or the body is not JSON.
- 404 not_found: No such save, board, achievement, item or address.
- 429 rate_limited: Too many calls this minute, for the key or for one player. Retry-After says how long to wait.

## 3. Do this, in order

1. If the project already has an Arena54 client and a player token, reuse them. Otherwise write a small client: it sends `Authorization: Bearer <publishable key>`, a JSON body with `Content-Type: application/json` when there is one, and `X-Arena54-Player: <token>` when there is one; it returns `data` on success and throws an error carrying `error.code`, `error.message` and the `Retry-After` header. With a TEST key, POST /play/test-token gives the sample player’s token.
2. Read the game’s boards with GET /play/boards and its achievements with GET /play/achievements. The `{id}` in a path is the `slug` they return; do not invent one. Pick a board whose `writer` is `page`, and keep in config which game moment unlocks which achievement. If either list is empty, stop and tell me to add boards and achievements for the game in the Arena54 dashboard.
3. When a round ends, POST /play/boards/{id} with `{ "score": n }`: a whole number from 0 to the board’s `max`. On a board whose `order` is `low`, the lower score wins. Show `best` and `rank` from the answer, and say so when `improved` is true.
4. Show the top ten with GET /play/boards/{id}?limit=10. It works before anyone signs in; with a player token `me` says where they stand, and is null until they have a score.
5. Unlock a badge with POST /play/achievements/{id} at the moment it is earned. Celebrate only when `new` is true.
6. On 403 `server_only`, that board or badge takes writes only from a server: do not retry from the page, and tell me.

## 4. Prove it works

Set `BASE=https://developers.arena54.com/api/v1` and `KEY` to the game’s test key. If I have not given you a key, ask me for a TEST key once, and for nothing else.

- Run: `curl "$BASE/play/boards" -H "Authorization: Bearer $KEY"`. Expect HTTP 200 and the game’s boards.
- With a test token as $TOKEN and a real board id: `curl -X POST "$BASE/play/boards/<id>" -H "Authorization: Bearer $KEY" -H "X-Arena54-Player: $TOKEN" -d '{"score":10}'` answers 200 with `improved`, `best` and `rank`. Sending `{"score":-1}` answers 400 `bad_score`.
- In the game: finish a round, and the score and rank appear; the top-ten list shows the test player. The sample player’s scores and badges stay between runs, so `improved` and `new` are true only the first time.

Show me the output of each check.

## 5. Rules you must not break

- Never trust the page for anything of value. A player can change what a page sends, so a save, a score or a badge reported by a page is a claim. Do not unlock paid content, currency or prizes from it.
- No secret key in a page or a game build.
- On 401 `invalid_player` or `player_revoked`, drop the token and offer sign-in again. Do not loop.
- On 403, show the message and stop; retrying will not help.
- On 429, wait for `Retry-After` seconds. Never retry in a tight loop.
- On 503 `module_off`, keep the game playable without the feature.
- Keep the token in memory, or in sessionStorage so a reload keeps the player signed in. Never in localStorage, a save, a URL you share, or a log.

## 6. Done when

- A finished round posts a score and shows best and rank.
- The top ten renders with and without a player.
- A badge unlocks once and is not celebrated twice.
- The ids come from the API, and `server_only` is handled without a retry loop.

Then give me a short list of the files you changed and anything I must do by hand (for example: register this page’s address for the game in the Arena54 dashboard).

For a person, not needed for this task: https://developers.arena54.com/docs/reference · This prompt: https://developers.arena54.com/prompts/leaderboards.web.md
