Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ npm install && npm run build --prefix web
npm run test --prefix web # frontend tests
```

**Versus:** open the VERSUS tab, create a room and send the invite link —
two humans, server-authoritative, with a turn clock, concede and
reconnect-on-refresh ([docs/multiplayer.md](docs/multiplayer.md)).

Right now matches pit the two starter decks (`neon_frequency` vs `cinderworks`) against each other; the AI is a greedy heuristic opponent — no search, no LLM. Going second gets a **Signal Spike** (a free Track that grants +1 Bandwidth this turn) as second-player compensation — the coin analog.

## CLI
Expand Down Expand Up @@ -80,7 +84,8 @@ FastAPI app (`sunder/server/app.py`), docs at `/api/docs`:
- `POST /api/registry` — propose a tag
- `POST /api/registry/{kind}/{id}/approve` — approve a proposal
- `POST /api/generate` — run `generate_pack` (async thread)
- `WS /ws` — the match protocol: `start` / `restart` / `play {hand, target?}` / `attack {board, target}` / `end_turn`, all answers as full `state` snapshots
- `GET /api/rooms/{code}` — versus room status (never exposes tokens)
- `WS /ws` — the match protocol: `start` / `restart` / `play {hand, target?}` / `attack {board, target}` / `end_turn` / `concede`, plus the versus lobby `create_room` / `join_room` / `resume` / `leave_room`; every mutation answers each seat with its own `state` snapshot

Snapshots (`state.py`) are viewer-aware: opponent hand is a count, hidden B-SIDEs are masked AND precomputed server-side (`is_playable`, `attack_targets`, `legal_targets`) — the client stays dumb.

Expand Down Expand Up @@ -137,6 +142,7 @@ Full documentation lives in [docs/](docs/index.md):
- [Architecture](docs/architecture.md) — module map, closed vocabulary, engine design
- [Card format](docs/cards.md) — JSON schema, budget model, registry governance
- [Card generation](docs/generation.md) — the LLM pipeline + review gate
- [Multiplayer](docs/multiplayer.md) — versus rooms, clocks, resume, server authority, roadmap
- [Server API](docs/api.md) — REST + WebSocket protocol, snapshot shape
- [Developing](docs/developing.md) — setup, tests, lint, CI, Docker/homelab deploy

Expand All @@ -150,9 +156,11 @@ pytest tests -x
- **Match** — AI vs AI across seeds: every match terminates, every seed is deterministic
- **Generation** — every module in isolation (client/SSE, profile, sampler, ledger, humanizer, legality, end-to-end with a `FakeLLM` double), including graceful-degradation paths
- **Web** — API routes, tag registry, WS loop
- **Versus** — seat-aware rooms, clocks, grace/resume, lobby protocol, full PvP match over two sockets

## Status / notes

- v0.4.0 — **Versus.** Invite-code PvP rooms with server-side turn clocks, concede, and resume-on-refresh; every match runs in a seat-aware `MatchRoom` (the bot is just another seat). Fixes a Station-1-win bug (`winner == 0` read as "no winner") and hidden B-SIDE names leaking through the log.
- v0.3.7.5 — card faces redrawn at the standard 5:7 ratio, with an edition/rarity badge, a station watermark, clamped rules, and a playability signal on the hand cost bars; flavor moves to the inspector. Profile icons are a separate pick from sleeves: the station avatar opens an emblem grid, and the glyph takes your sleeve's ink.
- v0.3.6.1 — changelog inside the game UI (Changelog tab) with a short roadmap.
- Card backs are hand-editable SVG art (`web/src/art/sleeves/*.svg`), one file per sleeve, themed via palette channels from `sunder/cards/sleeves.json`. Browse + pick in the codex (SLEEVES tab), carried server-side per station so opponents see your back too. Adding a sleeve = one JSON entry + one SVG.
Expand Down
30 changes: 22 additions & 8 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,52 @@ by the test suite). The module also exposes a ready `app = create_app()` for
| GET | `/api/health` | `{"ok": true}` |
| GET | `/api/sleeves` | card-back manifest from `sunder/cards/sleeves.json` |
| GET | `/api/avatars` | profile-icon manifest from `sunder/cards/avatars.json` |
| GET | `/api/rooms/{code}` | versus room status: `{code, status: "waiting" \| "live" \| "over"}`, `404` if unknown — never exposes seat tokens ([multiplayer](multiplayer.md)) |

The built SPA is served at `/` **iff `web/dist` exists** (StaticFiles, html
mode). Rails: dev frontend via `npm run dev` in `web/` proxies `/api` + `/ws`.

## WebSocket `/ws`

One match per connection; client-driven request/response; every mutation
answers with a full `state` snapshot. Protocol:
Every connection is routed through a `MatchRoom` (`sunder/server/match.py`).
A practice match is Human(seat 0) vs `BotSeat`; a versus match is two
`HumanSeat`s created by the lobby (see [multiplayer.md](multiplayer.md)).
Actions are validated against **the sender's seat**, and every mutation is
answered with a `state` to **each** seat, built for that seat's viewpoint.

| Client → | Payload | Effect |
|---|---|---|
| `start` / `restart` | `{seed?}` | new GameState + deal; answers a fresh state |
| `play` | `{hand: <index>, target?: <serial>}` | play hand card (indexes into *your* hand); `target` auto-looked up on the enemy board |
| `start` / `restart` | `{seed?}` | new practice match vs the bot (refused while a live PvP match is running) |
| `play` | `{hand: <index>, target?: <serial>}` | play a card from *your* hand; `target` must be one of the card's `legal_targets` (friendly or enemy), else `bad target`. A target sent for an untargeted card is ignored |
| `attack` | `{board: <index>, target: <serial> \| "face"}` | attack (indexes into your board) |
| `end_turn` | `{turn: <n>}` | guarded — a stale turn number is ignored; then the AI's turn is **staged**: before it speaks (director pause), then one snapshot per AI action, paced by the jitter windows |
| `end_turn` | `{turn: <n>}` | guarded — a stale turn number or out-of-turn request is ignored (answered with a fresh state). Against the bot, its turn then runs as a background task: a think pause, then one `state` per AI action, paced by the jitter windows |
| `concede` | — | forfeit the current match (`end_reason: "concede"`) |
| `create_room` / `join_room {code}` / `resume {code, token}` / `leave_room` | | versus lobby — see [multiplayer.md](multiplayer.md#protocol) |
| `set_sleeve` | `{sleeve: "<id>"}` | set your station's card back; id validated against the sleeve manifest (invalid → error, no state change); the opponent sees the new sleeve in `players.{}.sleeve` |
| `set_avatar` | `{avatar: "<id>"}` | set your station's profile icon; id validated against `avatars.json` (invalid → error, no state change); glyph + sleeve ink ride the snapshot |
| `ping` | — | answers `{"type": "pong"}` |

Server →:

* `{"type": "state", "snapshot": {...}}`
* `{"type": "state", "snapshot": {...}, "match": {...}}` — `match` is
per-seat: `mode` (`"bot"`/`"pvp"`), `code`, `seat`,
`opponent_connected`, `turn_seconds`, `turn_remaining`
* `{"type": "room", ...}` / `{"type": "room_closed", "reason": ...}` — versus lobby
* `{"type": "error", "message": "…"}` — never closes the socket

While the bot is playing, the socket stays responsive: a `play` sent during
its turn gets `not your turn` immediately, and `restart` cancels the bot's
turn cleanly.

## Snapshot shape (`state.py`)

Viewer-aware (`viewer = 0` for the human station):
Viewer-aware — each seat gets `snapshot(gs, viewer=<its station>)`:

```jsonc
{
"turn": 1, "current": 0, "phase": "MAIN",
"winner": null, "status": "Turn 1 | …",
"winner": null, "end_reason": null, // "signal" | "concede" | "timeout" | "disconnect"
"status": "Turn 1 | …",
"viewer": 0,
"can_act": true, // winner is None AND it is your turn
"players": {"0": {...}, "1": {...}},
Expand Down
19 changes: 19 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ sunder/
codex.py CLI card browser
cards/ JSON sets + registry.json
server/ FastAPI app (REST + WS) + uvicorn launcher
match.py MatchRoom + seats (HumanSeat / BotSeat), clocks
lobby.py versus rooms: invite codes, seat tokens, resume
gen/ LLM card-generation pipeline
web/ React + TypeScript + Vite SPA (served from web/dist)
tests/ ~20 Python modules + vitest suite in web/
Expand Down Expand Up @@ -73,13 +75,30 @@ Card JSON ──parse_card()──▶ Card ──▶ CardInstance (zone-aware ru
counter effects can CUT a track mid-cast; `resolving` guards re-entrancy
(`COPY_LAST_TRACK` can't copy itself).

## Match rooms

`game.py` has no notion of sockets or players-as-people; the server wraps
each `GameState` in a `MatchRoom` with one **seat** per station. Seats are
pluggable — `HumanSeat` (a WebSocket) or `BotSeat` (the heuristic AI run
as a background task) — so practice and versus share one code path. The
room validates every action against the sender's station, serializes
mutations behind one lock, and broadcasts a per-viewer snapshot to each
seat. Versus rooms add a turn clock, AFK strikes and a disconnect grace;
the in-memory `Lobby` pairs connections via invite codes. Details:
[multiplayer.md](multiplayer.md).

Game end is always `winner` (0/1) + `end_reason`; use `gs.game_over`,
never truthiness of `winner` (station 0 winning is `winner == 0`).

## Snapshot masks

* Viewer's own board: full — `atk`/`def` are *effective* values, `damage`,
fuse progress, rules text.
* Hidden B-SIDE (opponent view): name `???`, type `None`, only the B-SIDE
keyword. The owner additionally sees its resolved rules text.
* Rival hand: a count only.
* Log/history text: a hidden B-SIDE is named "Hidden B-SIDE" until revealed
(`CardInstance.label()`), so damage/death lines can't leak it.

## Resource model

Expand Down
16 changes: 14 additions & 2 deletions docs/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,22 @@ installed in the image layer above the source layer, so code-only changes
rebuild fast. `.dockerignore` keeps node_modules, local dist, caches, and
development docs out of the build context.

### Runtime settings

| Env var | Default | |
|---|---|---|
| `SUNDER_TURN_SECONDS` | `75` | versus turn clock (`0`/`off` = untimed) |
| `SUNDER_GRACE_SECONDS` | `60` | versus reconnect window before a disconnect forfeits |
| `SUNDER_LLM_*` | see README | card-generation gateway |

Versus rooms are held **in memory in one process**: keep uvicorn at a single
worker (the image's default) and expect a restart/redeploy to end live
matches until Phase 2 persistence lands — see
[multiplayer.md](multiplayer.md#configuration).

### Homelab (efx-lab) flow

The lab host consumes a tar-over-ssh stream (no rsync, no clone needed — the
repo is private):
The lab host consumes a tar-over-ssh stream (no rsync, no clone needed):

```bash
tar -czf - --exclude='.git' --exclude='web/node_modules' \
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ the airwaves, and an LLM generates new card packs that never break the rules.
| [Architecture](architecture.md) | Module map, the closed vocabulary, why the engine never special-cases cards |
| [Card format](cards.md) | JSON schema for sets, the cost budget, tag + keyword governance |
| [Card generation](generation.md) | The LLM pipeline end-to-end: profile → sampler → ledger → budget → humanizer |
| [Multiplayer](multiplayer.md) | Versus rooms, clocks, resume + seat tokens, server authority, roadmap to accounts/ratings |
| [Server API](api.md) | REST endpoints, the WebSocket match protocol, snapshot shape |
| [Developing](developing.md) | Setup, tests, lint, CI, and deploying the web server (Docker, efx-lab) |

Expand Down
Loading
Loading