diff --git a/README.md b/README.md new file mode 100644 index 0000000..7c487fd --- /dev/null +++ b/README.md @@ -0,0 +1,269 @@ +# FFF_vtt + +A self-hosted browser virtual tabletop for one table: one GM and up to six players on phones, +tablets or laptops. Everyone opens the same site link; each person signs in with a personal key. +The server decides what each player sees — line of sight, explored area, tokens, map tiles — so a +modified client cannot reveal anything its player is not entitled to. No accounts, no external +database, one scene, no RPG rules or dice. + +Status: the client (M4) is built; the manual phone playtest is still pending. The NixOS +deployment module is M6 and not written yet — today you run the binary behind your own TLS nginx. + +--- + +## Using it + +### What you need + +- A Linux host with [devbox](https://www.jetify.com/devbox), or a machine that already has the + `fff-vtt` binary and the built client. +- An HTTPS site in front of it. The session cookie is `Secure`, so plain HTTP will not keep you + signed in. The server itself always binds `127.0.0.1`; nginx is the only public face. + +### Build it once + +From the repo root, inside devbox: + +```sh +devbox run -- nimble install -d -y +devbox run -- nim c --mm:orc --nimblePath:.devbox/nimble/pkgs2 \ + --nimcache:run/nimcache/fff-vtt -o:run/fff-vtt server/main.nim +devbox run -- bash -c 'cd client && npm ci && npm run build' # writes client/dist +``` + +`--nimcache` inside `run/` matters in a worktree: Nim's default cache is shared by every checkout +of the repo and two parallel builds corrupt each other. + +### Create the GM key + +Only the command line can create or rotate the GM: + +```sh +devbox run -- ./run/fff-vtt gm-key --state-dir=run +# prints: GM key: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX +``` + +Re-running prints the same key. `gm-key --new` rotates it and revokes every GM session; it refuses +(exit 3) while the server is running on that state dir, so stop the server first. There is no +in-browser way to become GM and no first-visitor claim: without the key you only ever see the +sign-in page. + +### Start the server + +```sh +devbox run -- ./run/fff-vtt --port=9000 --state-dir=run \ + --public-origin=https://vtt.example.com +``` + +| flag | env | default | meaning | +|---|---|---|---| +| `--public-origin` | `FFF_VTT_PUBLIC_ORIGIN` | `http://localhost:8080` | origin the site is served from; compared byte for byte with the browser's `Origin` header, so it must match exactly | +| `--port` | `FFF_VTT_PORT` | `9000` | listen port on `127.0.0.1` | +| `--state-dir` | `FFF_VTT_STATE_DIR` | `run` | holds `state.db`, `instance.lock` and `tiles/` | + +Flags win over env, env wins over defaults. A second server on the same state dir refuses to start +(the `instance.lock`). + +### Put nginx in front + +The server answers `/healthz`, `/join`, `/tiles/` and the `/ws` upgrade. Everything else — the +client build, TLS, the internal tile route — is nginx. This is the shape; `tools/nginx.mjs` +generates exactly this config for the tests and is the reference to copy: + +```nginx +# inside http { ... } +map $http_upgrade $conn_up { default upgrade; '' close; } + +server { + listen 443 ssl; + http2 on; + ssl_certificate /path/cert.pem; + ssl_certificate_key /path/key.pem; + + location = /healthz { proxy_pass http://127.0.0.1:9000; } + location = /join { proxy_pass http://127.0.0.1:9000; access_log off; + add_header Referrer-Policy no-referrer; } + location /tiles/ { proxy_pass http://127.0.0.1:9000; } # server decides, answers X-Accel-Redirect + location = /ws { proxy_pass http://127.0.0.1:9000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $conn_up; + proxy_read_timeout 1h; + proxy_buffering off; } + location /_tiles/ { internal; alias /srv/fff-vtt/tiles/; } # never reachable from outside + + location /assets/ { root /srv/fff-vtt/dist; try_files $uri =404; + add_header Cache-Control "public, max-age=31536000, immutable"; } + location = /index.html { root /srv/fff-vtt/dist; try_files /index.html =404; + add_header Cache-Control "no-cache"; } + location / { root /srv/fff-vtt/dist; try_files $uri $uri/ /index.html; + add_header Cache-Control "no-cache"; } +} +``` + +`/_tiles/` must point at `/tiles`; the server only tells nginx which tile to serve and +never sends the bytes itself. Nothing about production deployment (systemd unit, NixOS module, +backup timer) exists in the repo yet; that is M6. + +### At the table, as GM + +1. Open the site, paste the GM key. The GM is created with the display name `GM` and is not asked to + pick one. +2. **Player keys.** In the GM panel, *Create player key* makes an unused key (24 characters in five + groups of four; case and dashes do not matter). Hand it to one player and that player is bound to + it. *Show* reveals it again, *Replace* rotates it and closes all that player's sessions, *Remove* + frees the slot and hands their characters and tokens to the GM. Maximum six players; unused keys + count toward the cap. +3. **Map.** Only one scene exists. It is seeded at 4096×4096 px with 32 px tiles and cells, a 400 px + default vision radius, a 20 px default token radius and no grid (the grid toggle exists, but the + seeded grid size is 0 and only `scene.set` can change it). There is **no map upload and no + import command yet**: browser upload is post-v1 and the map import CLI is M6. Today you put the + tile images in `/tiles/1/v1/_.png` yourself (32 px PNGs, `x` left to right and + `y` top to bottom from the world origin, no leading zeros). A player is served a tile only after + they have explored a cell that overlaps it; the GM + always gets every tile. Because one 32 px cell is the unit of disclosure, do not paint secret + doors, traps or notes into map art near places players can reach — keep secrets as GM-only tokens. +4. **Tokens.** *Create token* takes a kind, a descriptive name, an actual name, a position, a radius + (blank = scene default), a `gm_only` flag and a controller (a player, or blank for the GM). + *Select* a token to edit or remove it, resize it, or add and clear `stuck`/`squeezed` statuses. + The GM may drag any token; a player only their own. +5. **Characters and reveals.** Create a character with a descriptive name, an actual name and an + owner; rename it, move it to another owner, reveal its actual name to one player or to everyone, + remove it (refused while a token still links it). Each player's encountered list is visible to you + and you can delete entries. +6. **Who sees it.** The GM token panel shows which players currently see a token. In this build the + answer is binary — seen or not seen; the graded visible fraction arrives with wall heights. +7. **Walls.** Arm a preset (`wall`, `door`, `window`, `one_way`) and click twice to draw a segment. + Select a wall for its derived kind, a free-text label (never sent to players), the *Players see a + marker* switch, open/close, `secret`/`locked`, the one-way flip, the raw switches behind *Advanced + switches*, delete, and a single-step *Undo*. A secret or locked door looks exactly like a solid + wall to players. Thickness, height and placement controls are not in this build (ADR-0012). + +### Not in this build yet + +The server already speaks these; the browser has no control for them, so a table needs them only if +you write to the wire yourself: + +- the token `vision` switch. It lives in the token's `flags` on the wire, and a token created from the + panel has no vision, so a player token made only through the UI sees no fog of its own; +- the token's character link (`character_id`); +- a property editor (`prop.set` / `prop.unset`, the `public`/`owner`/`gm` buckets); +- a scene settings panel (`scene.set`: name, grid size, default vision and token radii); +- renaming a player (`player.rename`). + +Still missing entirely: + +- map upload or an import command (browser upload is post-v1, the import CLI is M6); +- wall thickness, height, see-over and wall placement (built after the phone playtest, ADR-0012); +- the graded who-sees-it fraction (needs wall heights); +- the NixOS module, systemd unit and backup timer (M6); +- chat, dice, rulers, pings, audio, multiple scenes, accounts. + +### As a player + +1. Open the same link and enter your key. The first sign-in asks for a display name once; it must be + unique at the table. +2. One-finger drag pans, wheel or pinch zooms (0.5× to 4×). +3. Drag your own token. The move is drawn immediately, then confirmed by the server or rolled back + when a wall or the step limit refuses it. +4. Fog colours: opaque black is floor you have never seen, dimmed map is floor you explored earlier, + clear map is what you can see right now. The little lit-window icon is a window you have already + seen part of. +5. The badge shows `connected`, `reconnecting` or `offline`. A dropped connection keeps the map on + screen and retries; the server then resyncs everything. + +### When something goes wrong + +- **"Key not recognised."** — wrong or unknown key; the server deliberately says the same thing for + both. Ask the GM to *Show* the key again. Case, dashes and spaces are ignored. +- **The page says to reload** — the client and server disagree about the protocol version (a + deployment replaced one side only). Reload; if it keeps happening, rebuild the client so both sides + come from the same commit. +- **You are back at the sign-in page** — the key was replaced, you were removed from the table, or + the GM key was rotated. Ask the GM for the current key. +- **The badge says reconnecting** — the network dropped. Retries back off 1, 2, 4, 8, 10 seconds with + jitter; a terminal reason (`revoked`, version mismatch) never retries. +- **A move snaps back** — a wall or the per-step distance limit refused it. Refusals never reveal + where the wall is. +- **The server will not start** — `state dir locked` means another instance already runs on that + directory; `gm-key --new` hits the same lock. Exit 1 is a database or state-dir error, 2 a bad + flag or env value. + +--- + +## For developers + +### Rules of the environment + +- Everything runs through devbox: `devbox run