Files
FFF_vtt_frozen/client/README.md
T
SirStone 465259c853 feat(client): name prompt, connection badge, reconnect and resync (#68)
T5 of the M4 client (spec #63). All of it at the existing seams: the pure
reducer slice, the WS client and the browser e2e.

- First sign-in: a live player with `me.name === null` gets a name prompt
  instead of the HUD and no Pixi stage (the server sends no scene until named).
  `player.setname` is acked before the second `hello` (#33); `name-taken`,
  `invalid` and `not-allowed` become visible text and the prompt stays put.
  Acceptance is followed by `hello` + the 6.3 resync, so the prompt is keyed on
  state and is asked once.
- Badge (`#badge[data-state]`): connected / reconnecting / offline. Offline is
  driven by `navigator.onLine` (a phone with no network) and by terminal close
  outcomes; reconnecting keeps the stale map on screen.
- Reconnect: a transport close after a live connection retries with
  1/2/4/8/10 s base delays +/-20% jitter, then the cap. `revoked` and
  `proto-mismatch` stay terminal; pending `reqId`s die with the socket; the
  browser `online` event skips the rest of the wait. A drop does not clear the
  view: `hello` resets every slice and the resync rebuilds tokens, fog,
  explored, markers and lists.
- Two devices on one player key both stay live and receive the same frames.

Proof: new unit tests for the schedule/retry machine (`test/ws.test.ts`) and the
badge/reconnect/name transitions (`test/reducer.test.ts`); new
`client/e2e/session.test.mjs` runs the real server and TLS/HTTP2 vhost, kills
and restarts the server under a live page, drops the browser network with
`context.setOffline`, and asserts the full resync frames (roster, encountered,
vision, explored, marker, token) plus two pages on one key.

Refs #68
2026-11-21 00:18:17 +01:00

102 lines
6.6 KiB
Markdown

# FFF VTT client
Vite + TypeScript + PixiJS v8, plain DOM overlay, `proto` 2 (DESIGN §7, ADR-0011). No framework.
## Run and gates
All through devbox, from the repo root:
```sh
devbox run client-test # Vitest: pure reducer + protocol client + envelope (no DOM, no GPU)
devbox run e2e # builds client/dist, then Chromium against the real server through the
# TLS/HTTP2 test nginx (client/e2e/*.test.mjs, playwright-core)
devbox run test # the server gate; unaffected by the client
devbox run -- bash -c 'cd client && npm run dev' # local dev (Vite, no server API on :5173)
```
`npm run build` = `tsc --noEmit && vite build` into `client/dist`, which the test/production nginx
serves at `/` (DESIGN §11.1). `client/dist` and `client/node_modules` are not committed.
## The one seam that matters
`src/reducer.ts` is pure: server frames and connection events in, state out. **It must never import
Pixi, the DOM, or the socket.** Its unit tests (`test/*.test.ts`) are the contract. Everything else
is wiring:
| File | Owns |
|---|---|
| `src/protocol.ts` | Wire types, envelope/ack/closing guards, intent encoding, `/ws?proto=2` URL. Pure. |
| `src/reducer.ts` | `RootState`, phase machine (`boot/connecting/signin/live/reload/offline`), `hello` reset, slice registry. Pure. |
| `src/slices/*.ts` | Append-only domain state (roster today; fog, tokens, walls, panels later). |
| `src/ws.ts` | Socket, `reqId` counter, pending map, close-code mapping, backoff and retry. No DOM. |
| `src/shell.ts` | Sign-in, HUD/roster, fatal screens. Plain DOM, `textContent` only. |
| `src/render/stage.ts` | One Pixi `Application`, one world container, fixed layers. Pixi lives here and in modules only. |
| `src/render/modules/*.ts` | Append-only render modules. |
| `src/main.ts` | Wiring: store -> shell/stage, `/join`, connect. |
## Convention for the M4 fan-out (T7, T8/T9, T10, T11/T12)
The registry is a **build-time glob**, so parallel tickets are file-disjoint: no shared import list
to merge and no registry file to edit.
- **State slice**: add `src/slices/<feature>.ts` exporting `export const slice: Slice<YourState>`
(`name`, `init()`, `reduce(state, event, root)`). It is registered automatically. Read it with
`sliceOf<YourState>(state, '<name>')`. `hello` resets every slice for you (DESIGN §6.3): never
keep server-derived state outside a slice or the core session.
- **Render module**: add `src/render/modules/<feature>.ts` exporting
`export const renderModule: RenderModule` (`name`, `mount(context)`, optional teardown). Draw into
your layer: `tiles`, `grid`, `tokens`, `fog` (players), `gm` (GM only), `dom`.
- **Panel/DOM**: add `src/panels/<feature>.ts` and mount under the existing `#panels` host; do not
rewrite the sign-in flow. Server strings go through `textContent` (never `innerHTML`); token
labels use `BitmapText` with the pre-installed `BitmapFont` (DESIGN §7.2).
- **No per-frame full redraw**: subscribe to the store and touch only entities that changed
(DESIGN §7.2). Keep the DOM overlay work per frame minimal.
- **Protocol**: only the catalogue in DESIGN §6.1. The client ignores unknown frame types/fields
(§6.2). `closing` reasons map through `closeOutcome`: `proto-mismatch` -> reload, `revoked` ->
sign-in, `evicted`/`malformed-flood` -> offline. Do not invent wire fields; server-side additions
are separate tickets (e.g. `seen_by`, T12).
Suggested ownership (no file overlap): T7 fog = `slices/fog.ts` + `render/modules/fog.ts`; T8/T9
tokens/drag = `slices/tokens.ts`, `slices/move.ts`, `render/modules/tokens.ts`; T10 walls =
`slices/walls.ts` + `render/modules/walls.ts`; T11/T12 GM panels = `slices/admin.ts` +
`src/panels/*.ts`. `src/reducer.ts`, `src/main.ts` and `src/render/stage.ts` are already generic;
change them only if a slice needs a new *core* event, and say so in the PR.
## Protocol notes already encoded
- `/ws?proto=2`; a mismatch is `closing{reason:"proto-mismatch",code:4003}` then close. The reason
is read from the last `closing` frame because browsers report 1005 at the mummy pin (ADR-0019).
- `hello` resets all state; the resync after it rebuilds the view. Pending `reqId`s survive a
`hello` on a live connection and die with the socket (DESIGN §6.3).
- Sign-in is `POST /join` (form body, key never in the URL). A `401` for a wrong key and for an
unknown key is shown as the same "Key not recognised." (DESIGN §3.2, story 3).
- No game data (and no Pixi `Application`) exists before a live `hello`; a visitor without a session
only sees the sign-in page.
## Name, badge and reconnect (T5, #68)
- **First sign-in name prompt.** A live player with `me.name === null` gets the `#name` section
instead of the HUD, and no Pixi stage (the server sends no scene, no state, until named). Submitting
sends `player.setname`; the server acks *before* the second `hello` (FIXED #33), so a refusal's
code becomes visible text on the prompt (`name-taken` → "That name is taken. Choose another.",
`invalid` and `not-allowed` have their own copy) and an acceptance is followed by the `hello` that
resets every slice plus the 6.3 resync. The prompt is keyed on state, so it cannot come back and
survives a reload with the same session.
- **Badge** (`#badge[data-state]`): `connected` while live; `reconnecting` while the socket backs
off, with the stale map staying on screen; `offline` when the browser reports no network
(`navigator.onLine`/`online`/`offline` events) or after a terminal outcome (`evicted`,
`malformed-flood`, `server-shutdown`). Hidden on boot, sign-in and reload.
- **Reconnect** (DESIGN 6.3): a transport close (no `closing` frame) after a connection has been
live retries with `backoffDelay`: base 1/2/4/8/10 s then the cap, ±20 % jitter
(`RECONNECT_BASE_DELAYS_MS`, `RECONNECT_JITTER`). Terminal reasons never retry: `revoked` →
sign-in, `proto-mismatch` → reload. Pending `reqId`s die with the socket; a browser `online` event
calls `retryNow()` to skip the rest of the wait. The view is **not** cleared on a drop: `hello` is
the reset point, and the resync that follows rebuilds tokens, fog, explored, markers and lists.
- **Two devices** of one player both stay live and receive the same frame stream (each connection
has its own `known` table, DESIGN 6.3).
- **Proof**: `test/ws.test.ts` (schedule, retry state machine, pending drop) and
`test/reducer.test.ts` (badge transitions, reconnect reset, name errors) are the unit seam;
`e2e/session.test.mjs` kills and restarts the real server under a live page, drops the browser
network with `context.setOffline`, asserts the badge transitions and the full resync frames, and
runs two pages on one key.