# Add visits between two Arena54 games to this project

Goal: Let a player of this game visit another Arena54 game and arrive signed in, or welcome a player who arrives from one.

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 the page keeps the player’s Arena54 token after sign-in, and where a link or door to another game would go.
- Find how the project reads configuration and environment variables, and which files git ignores.
- This is a plain web project: add no framework and no build step.
- If the project already has an Arena54 client or key variable, reuse it.
- 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`. Log `meta.request_id` with any failure.

- Every call here is made from the page, with the game’s PUBLISHABLE key (`a54_pub_…`) as `Authorization: Bearer …` and the player’s token as `X-Arena54-Player`.

Calls:

- 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 }]
- GET /play/entitlements: ?game={slug}: what this player owns in another game, if they chose to share it.
    data: [{ "game": "other-game-slug", "item": "gold-helmet", "name": "Gold helmet", "description": "…", "granted_at": "…", "look": { "picture": "https://…", "model": "https://…glb" | null, "kind": "vehicle" | null, "colour": "yellow" | null, "rarity": "rare" | null } | null }]   (empty when nothing is shared)

How it works:

- To send a player out, the page goes to `https://arena54.com/play/visit?from=<our game id>&to=<their game id>&redirect=<an address the other game registered>&state=<our own value>`. That is a page a person sees, not a call.
- The player reads what the other game will learn and agrees. They then land on the other game’s address with `#a54_token=…&a54_visit=…&from=…&state=…` after the hash.
- The game being visited reads the token from the hash exactly as it does for sign-in, then calls GET /play/visit. It answers the open visit or `null`.
- `brought_items: true` means the player agreed that this game may read what they own at home, with GET /play/entitlements?game=<from.slug>.
- Each game has its own id for the player and its own id for the visit. Never try to match them.
- GET /play/visits is for the home game: where this player went and whether each visit is over.
- With a TEST key GET /play/visit answers `null` and GET /play/visits answers an empty list. A real visit needs live keys in both games.

Refusals:

- 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.
- 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. Ask me which side this game is: it sends players out, it welcomes them, or both. Build only what I say.
2. Sending out: add one function `visitGame(toGameId, redirect)` that builds the arena54.com/play/visit address with our game id, a random `state` kept in sessionStorage, and goes there. Call it from the link or door I name.
3. Welcoming: where the page already reads `a54_token` from the hash, also read `a54_visit` and `from`. After storing the token, call GET /play/visit. If it answers a visit, greet the player as a visitor from `from.name`.
4. Welcoming, if `brought_items` is true and I have told you which of their items we honour: call GET /play/entitlements?game=<from.slug> and give our own matching thing for each. What we give is our own rule.
5. Welcoming: when the player leaves or the session ends, call POST /play/visit/end.
6. Sending out: where the player comes back, call GET /play/visits and show nothing more than whether the last visit is over.
7. Remove the hash from the address bar once it is read.

## 4. Prove it works

Set `BASE` and `ARENA54_PUBLISHABLE_KEY` (my TEST publishable key) in the shell. If I have not given you a key, ask me for a TEST publishable key once, and for nothing else. `BASE=https://developers.arena54.com/api/v1`.

- Get a sample token: `curl -X POST "$BASE/play/test-token" -H "Authorization: Bearer $ARENA54_PUBLISHABLE_KEY"`.
- Run: `curl "$BASE/play/visit" -H "Authorization: Bearer $ARENA54_PUBLISHABLE_KEY" -H "X-Arena54-Player: <the token>"`. Expect HTTP 200 and `"data": null`.
- Run the same for `/play/visits`. Expect HTTP 200 and an empty list.
- Open the page with `#a54_visit=test&from=some-game` added by hand and confirm it does not break when GET /play/visit answers null.
- A 503 `module_off` means Arena54 has not opened visits yet. Tell me, and leave the code in place.
- Say plainly that a real visit was not tried: it needs live keys in two games and a signed-in player.

Show me the output of each check.

## 5. Rules you must not break

- Never build the visit address with a `redirect` the other game did not give us.
- Never send our id for the player to another game, and never try to work out theirs.
- A visitor is a player like any other. Do not give them more than our own rules give.
- The token stays in the page. Do not put it in a link, a log or our server’s address.

## 6. Done when

- The visit code is in place for the side I named.
- GET /play/visit answered null and GET /play/visits an empty list with a test key.
- You have said that a real visit was not tried.

Then give me a short list of the files you changed and anything I must do by hand.

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