# Match Socket

A match is one WebSocket from stomped to your bot: when the match starts stomped connects to the registered URL (`https` upgraded to `wss`) and closes the socket when the match is over. Your bot never polls and never calls stomped. The socket carries:

1. JSON text frames, server → bot: the current **step**. The server always tells you where you are.
2. JSON text frames, bot → server: the reply the step asks for, or a request: `ping`, `replay`.
3. Binary messages, server → bot: the game as the raw `.slp` event stream, one message per frame.
4. Binary messages, bot → server: 12-byte controller pads, one per frame.

## The Handshake

The upgrade request carries three headers:

| header | value |
| --- | --- |
| `x-stomped-match` | the match id this socket will carry; `deploy-check` for the registration probe |
| `x-stomped-timestamp` | unix milliseconds, as a string |
| `x-stomped-signature` | `base64(ed25519_sign("<timestamp>.<matchId>.<url>"))` under stomped's platform key, where `<url>` is your bot's registered URL, byte for byte |

The platform public key is `GET https://api.stomped.dev/.well-known/stomped.json`, `{"publicKey": "<base64 of the raw 32 bytes>"}`; fetch it once and cache it.

The URL is your bot's credential: it is one bot's alone (a second registration of it, by anyone and in any spelling — trailing slash, host case, default port; stomped stores the URL as it parses, refuses a query or fragment, and returns the URL it stored — is refused), so give it an unguessable path such as `https://bots.example.com/7f3a9c2e…`, not a bare hostname, which is public in DNS and certificate logs. Verifying the handshake is optional on top of that and stops spoofed games: reject a timestamp more than 30 seconds from your clock, verify the signature over your own registered URL (a handshake captured at another bot does not verify at yours), and refuse the upgrade with `401`.

Your server has 3 seconds to complete the upgrade; a failed connect is retried every second until the game starts, so a bot that is restarting when the match is seated still plays if it is back within the `character` and `ready` steps. The upgrade offers no WebSocket subprotocol; there is nothing to echo.

## The Deploy Check

Registration, and every later URL change, probes the server before the bot can be seated:

1. `GET <url>/ping` must answer `2xx` with the body `pong` within 1 second. Redirects are not followed.
2. `<url>` must accept the signed WebSocket upgrade, with `x-stomped-match: deploy-check`. stomped closes it right away; no step is sent.

Either step failing refuses the request with `400` and a message naming the step. In production the URL must be `https` and resolve to a public address; private and loopback ranges are refused at registration and again at every connect.

## The Steps

Four steps, in order. Every frame the server sends is `{"step": …}` plus the whole match state (`matchId`, your `port`, the `opponent`, the `stage`, `inputDelayFrames`, `sentAt`), so one message tells a bot everything, whether it just connected, reconnected, or asked. Each also carries `version`, the version of this spec the frames follow (1 today); a change that would break a bot written against it bumps the number. Your reply echoes the step. Each step's page has the exact fields and an example:

| step | the server sends | you reply | deadline |
| --- | --- | --- | --- |
| [`character`](/reference/match-socket/character/) | pick your character | `{"step":"character","character":2,"costume":1}` | 5 s, else a random character |
| [`ready`](/reference/match-socket/ready/) | both picks | `{"step":"ready"}` | 10 s, then the game starts anyway |
| [`game`](/reference/match-socket/game/) | the game is on | nothing: pads | |
| [`over`](/reference/match-socket/over/) | how it ended | nothing: the socket closes | |

Both bots get `character` at the same instant and neither reply is relayed: the picks are blind, revealed to both in `ready`. The stage is drawn before the picks, at random from Battlefield (31), Final Destination (32), Dream Land N64 (28), Yoshi's Story (8), Fountain of Dreams (2), Pokémon Stadium (3), and is in every frame. The characters, with how many costumes each has:

| id | character | costumes |
| --- | --- | --- |
| 0 | Captain Falcon | 0–5 |
| 1 | Donkey Kong | 0–4 |
| 2 | Fox | 0–3 |
| 3 | Mr. Game & Watch | 0–3 |
| 4 | Kirby | 0–5 |
| 5 | Bowser | 0–3 |
| 6 | Link | 0–4 |
| 7 | Luigi | 0–3 |
| 8 | Mario | 0–4 |
| 9 | Marth | 0–4 |
| 10 | Mewtwo | 0–3 |
| 11 | Ness | 0–3 |
| 12 | Peach | 0–4 |
| 13 | Pikachu | 0–3 |
| 14 | Ice Climbers | 0–3 |
| 15 | Jigglypuff | 0–4 |
| 16 | Samus | 0–4 |
| 17 | Yoshi | 0–5 |
| 18 | Zelda | 0–4 |
| 19 | Sheik | 0–4 |
| 20 | Falco | 0–3 |
| 21 | Young Link | 0–4 |
| 22 | Dr. Mario | 0–4 |
| 23 | Roy | 0–4 |
| 24 | Pichu | 0–3 |
| 25 | Ganondorf | 0–4 |

**Errors.** A rejected reply (not JSON, no or wrong `step`, a character id outside 0–25, a costume the character lacks, a text frame over 64 KB) gets the current step again with `error` set to the reason, same deadline, and you may reply again. A pad that is not 12 bytes is answered the same way. Each reason is told once per match. If the deadline passes with no valid reply, the step resolves on its own and the next step follows.

**Reconnects.** If your socket drops, stomped reconnects every second and sends the current step's frame. Nothing you missed is replayed on its own; those frames were played with neutral pads. In `game` the live frames resume at once, without the header: keep the parser from before the drop (each frame message is complete in itself, so it reads on) or ask for a [`replay`](/reference/match-socket/replay/). A [`ping`](/reference/match-socket/ping/) at any time gets the current step's frame back.

## The Game, Server → Bot

Binary frames carry the raw `.slp` event stream, byte for byte as the replay file:

1. The first message is everything before the first frame: the Event Payloads event (`0x35`), Game Start (`0x36`) and the Gecko List, carried as Message Splitter (`0x10`) events whose internal command is `0x3D` (about a hundred of them). A streaming parser sees the splitter events, not a Gecko List command; only a file reader reassembles them.
2. Then one message per game frame holding exactly that frame's events in file order: Frame Start (`0x3A`), Pre-Frame Update (`0x37`) per port, Item Update (`0x3B`) per item, Post-Frame Update (`0x38`) per port, Frame Bookend (`0x3C`). Character projectiles are items, so Item Updates appear with items off.
3. The last message is Game End (`0x39`).

Concatenating every binary message (after a reconnect, from the `replay` message on) is byte-identical to the `raw` block of the stored replay (`GET /matches/{id}.slp`). Feed the messages to slippi-js's `SlpStream`, or any streaming `.slp` parser, as they are. Frame numbering is the `.slp` frame index: the first frame is −123, play starts at 0. Your `port` is the Slippi player index; read your spawn from the first Post-Frame Update (on Battlefield port 0 starts bottom centre, port 1 above the top platform). The game is Melee NTSC 1.02, 4 stocks, 8 minutes, no items, damage ratio 1.0, Pokémon Stadium frozen, pause off, and vanilla: no UCF and no Slippi gameplay codes, so wobbling is legal and there is no ledge grab limit. Game Start's per-player controller fix still reads UCF because the recorder copies a toggle nothing sets; the Gecko List is the truth.

## Pads, Bot → Server

Binary frames of exactly 12 bytes, little-endian (the `Pad` schema after the frame):

| offset | type | field |
| --- | --- | --- |
| 0 | i32 | frame |
| 4 | u16 | buttons |
| 6 | u8 | stickX |
| 7 | u8 | stickY |
| 8 | u8 | cstickX |
| 9 | u8 | cstickY |
| 10 | u8 | L |
| 11 | u8 | R |

Buttons use the GameCube physical bit layout, as the slp pre-frame `physicalButtons`: DPAD_LEFT `0x0001`, DPAD_RIGHT `0x0002`, DPAD_DOWN `0x0004`, DPAD_UP `0x0008`, Z `0x0010`, R `0x0020`, L `0x0040`, A `0x0100`, B `0x0200`, X `0x0400`, Y `0x0800`, START `0x1000`. Sticks are raw 0–255 with centre 128; triggers are raw 0–255 with 0 released. The `L` and `R` bits are the triggers' digital click: Melee reads the bit as a full press whatever the byte says, and the byte alone as an analog press (light shield). The game applies its own dead zones; nothing is pre-processed. Neutral is `buttons=0, sticks=128, triggers=0`.

- The pad for frame F is what the game polls at frame F. You may send pads for any future frame up to 600 frames past the last poll; the last one received before the poll wins.
- A pad for a frame already polled, or beyond that window, is ignored. A message that is not 12 bytes is ignored, and told once.
- A frame polled without a pad from you plays neutral and counts in `missed`, on the `over` frame and on the match record.

## Timing

**The clock.** The engine runs at a real 60 Hz: frame n's pads are read at its tick, 16.7 ms after the previous frame's, and the frame is computed in about 5 ms. Frames never come faster than that. Whatever holds a frame past its tick stalls the game for that long and the ticks resume from the late frame; there is no catch-up.

**The delay.** You see the game `inputDelayFrames` (1 frame) behind the engine: frame F's events are sent to you once frame `F + inputDelayFrames` is done, so the pads up to that frame are already read. Having seen frame F's bookend, send the pad for `F + 1 + inputDelayFrames`: the first one still open. It is read at the next tick.

**The budget.** That pad is due 11 ms after frame F's events were sent to you, measured on stomped's clock from the send to the pad's arrival, so it covers the network both ways and your compute: one frame minus the engine's share. In a clean frame the budget ends just before the tick, so the game is exactly 60 Hz and a late pad is polled neutral at the tick. If the engine or the arena is slow, the send is later and the deadline moves with it: the budget is never shortened, the game waits. Whether your socket was up when the frame went out does not matter. The arena runs in Ashburn, Virginia (US East); host next to it. A bot whose round trip is longer than the budget still plays by aiming further ahead: the pad for `F + k` lands if it arrives before frame `F + k` is read, so a bot with a 25 ms round trip sends the pad for `F + 3` and plays two frames behind the rest. The stream tells you whether you made it: each frame's pre-frame event carries the controller the game read for your port, so a pad that arrived late shows up there as neutral.

**The first frames.** The `game` frame goes out before the engine boots and says the last frame the engine finished (`frame`, −124 at the start): send the pads for `frame + 1` through `frame + 1 + inputDelayFrames` as soon as you receive it, then one per bookend. The engine's boot takes seconds; they will be there in time. The same rule covers a reconnect. Frame −122 is a Melee lag frame and is polled with −123's pad, exactly as on hardware.

## How a Game Ends

- **Stocks.** The last bot with a stock wins, on the frame the other's last stock is lost. Both lost on the same frame is a draw.
- **Time.** When the timer runs out, more stocks wins; equal stocks, lower percent wins; equal both is a draw. Percent is what the game holds on the final frame, so a bot KO'd just before time stands at 0 after its respawn. No sudden death; the replay's own Game End placements are not used.
- **Forfeit.** A bot forfeits when no pad of its lands for 300 consecutive polls, 5 seconds of game time, whether its socket is down, it sends nothing, or its pads keep arriving after their frame was polled. The game is stopped; in the replay a forfeit is a no-contest Game End (method 7) whose LRAS initiator is the forfeiting port. A bot that has not connected by the time the game would start (the end of `ready`) forfeits before the game is run; the other bot picks and readies as usual and is then sent `over`. Forfeits are rated, `score` 0 against 4; both bots forfeiting together is a draw.
- **No game.** If neither bot connects the match is aborted: no result, no ratings, and both miss streaks grow. If the engine breaks (a crash, or the match outliving its 12-minute wall clock) the match is aborted unrated with `endReason: "engine"`, and both bots still get `over`.

## The End

`over` is the last frame; the server closes the socket after it. The result is on the match record, `GET /matches/{id}`, and the replay at `GET /matches/{id}.slp`. While the game runs, `GET /live/{id}` shows each port's latest stocks, percent and position.
