Co-op robustness, audio/a11y, new towers and enemies, Uplink rebalance

Co-op (QA'd with two to three real browsers, TCP-proxy chaos scenarios):
- partner's tower changes are never dropped after a render stall or snapshot burst
- half-open connections show "reconnecting" in ~6 s instead of 45-65 s
- upgrade and next-wave are idempotent (stale commands refused), restart message,
  promoted host gets Play again, failed joins never kill a live game, listener leaks

Audio / keyboard / a11y:
- one Audio element per track, fade/stinger supersede tokens, play() retry on the
  next gesture, 5 s fallback to the synth, mute works everywhere, dialogs with
  role/aria-modal/focus handling, accessible names for sound/speed/pause

New content:
- enemies: Daemon (healer), Proxy (regenerating shield), Botnet (flyer swarm);
  waves weave them in from wave 9 (waves 1-8 unchanged)
- towers: Sandbox (stun pulse), Load Balancer (fire-rate aura); hotkeys 1-9
- glyphs/shapes, stun/heal effects, gallery e2e that pixel-checks every kind

Orbital Uplink rebalanced after playtesting (was affordable by wave ~6 and buyable
in bulk from boss rewards): 1500 credits, upgrades 1200/2000/3200, and every Uplink
already owned makes the next one 25% dearer to buy AND to upgrade (each tower
remembers its multiplier, carried in the tower list as a 9th field). Shared
TD.priceFor/upgradePrice so the UI shows exactly what the sim charges.

Also: tablet/foldable viewports in the layout matrix (tolerant colour check),
docs (README tower/enemy tables, PROTOCOL shield/stun/pm encodings, NEXT roadmap).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
This commit is contained in:
Claude 2026-10-10 21:18:43 +00:00
parent 962189419a
commit 28502984ec
29 changed files with 3370 additions and 230 deletions

View file

@ -29,13 +29,24 @@ and a check that the layout holds on **tablets and foldables**.
| Wave | Content | State |
|---|---|---|
| 1 | Phone panels/layout + touch precision (one owner of main.js/style.css), proxy/server doc fixes, QA | **done 2026-10-10** (QA passed) |
| 2 | Co-op robustness (stalled-render tower loss, half-open sockets, idempotent upgrade/wave), audio + keyboard + a11y | drafted |
| 3 | Content: new towers/enemies (healer, shielded, Sandbox, Load Balancer). Done already: Orbital Uplink (sim, glyph, beam, sound, help), 4 new maps (Canyon, Archipelago, Fortress, Pinwheel) | in progress |
| 4 | Attack-vs-defend versus mode, bots for both sides | design in `docs/VERSUS.md` |
| 5 | Difficulty picker + ONE time-boxed balance pass (IDS dominance, weak Mortar/Arc, credit pile-up; maze stays strong) | after 3 and 4 |
| 1 | Phone panels/layout + touch precision, proxy/server doc fixes, QA | **done 2026-10-10** |
| 2 | Co-op robustness (stalled-render tower loss, half-open sockets, idempotent upgrade/wave, restart message), audio + keyboard + a11y | **done 2026-10-10** (QA passed) |
| 3 | New enemies (Daemon healer, Proxy shielded, Botnet swarm) and towers (Sandbox stun, Load Balancer buff), glyphs/shapes, gallery e2e; Orbital Uplink (4 levels, priced 1500 + 25% per owned, upgrades scale equally); 4 new maps | **done 2026-10-10** (QA passed) |
| 4 | Difficulty picker (Easy/Normal/Hard) + ONE time-boxed balance pass (IDS dominance, weak Mortar/Arc, credit snowball and boss rewards, new content) | drafted (`wave4d`) |
| 5 | Attack-vs-defend versus mode, bots for both sides | design in `docs/VERSUS.md`, workflow drafted |
| 6 | Server leaderboard (needs `StateDirectory=netwall` + `DATA_DIR`; solo scores are client-reported) and an opt-in public room list | after 5 |
| any | Tablet/foldable viewports in the e2e matrix (near-square 690x830, 840x700, half-folded 841x400, cover 344x800) | todo |
| any | Fold/tablet real-device check; open items below | |
### Open items from wave 2 QA (all minor)
* The loser of a simultaneous Sell sees a red toast ("That tower no longer exists"); the loser of a simultaneous Keep going sees "Nothing to continue". Silence them like the stale upgrade/wave.
* A host who closes the tab (no Leave) stays host for the whole grace period (60 s), so the guest on the end screen waits that long for Play again.
* `room.js` checks the 1500-unit 'busy' cap before the sim's stale check; main.js has no wording for 'busy'.
* Any `no_room` after a successful join says "The server restarted..." even if the room merely expired (a lone sleeping phone); the tests pin the string.
* Tablet/fold polish: the co-op top bar wraps to two rows at 690x830; at 1180x820 the board letterboxes (~249 px dead band); the Players panel (Invite, Leave) is below the fold on stacked layouts; "Waiting for the host" sits flush under the Menu button.
* Test strength: the e2e 'stall' scenario passes even without the `_carryTw` fix (the sync backstop heals it; unit tests pin it); the audio fade-supersede guard is not pinned by 09-audio.
* If room codes ever get longer than 4 characters, boot's regex and the `inCode` maxlength must change together.
## Suggested order of work

View file

@ -115,7 +115,7 @@ pid for the viewer's own entry and an opaque, stable per-room alias for the othe
| message | meaning |
|---|---|
| `{t:'cmd', c:'place', type, x, y}` | build. Server calls `sim.cmd({...msg, by: slot})` (never trust a `by` from the client) |
| `{t:'cmd', c:'sell', id}` / `'upgrade'` `id` / `'target'` `id,mode` / `'wave'` / `'auto'` `v` / `'continue'` | other sim commands, same path |
| `{t:'cmd', c:'sell', id}` / `'upgrade'` `id, lv?` / `'target'` `id,mode` / `'wave'` `n?` / `'auto'` `v` / `'continue'` | other sim commands, same path. `lv` and `n` make `upgrade` and `wave` idempotent, see below |
| `{t:'ctl', c:'pause', v:bool}` / `{t:'ctl', c:'speed', v:1\|2\|3}` | room-level; reflected in `snap.pa` / `snap.sp` |
| `{t:'cursor', x, y}` | hover position in cell units (floats) or `x:null` to hide; clamp to board, round to 2 dp |
| `{t:'mark', x, y}` | map ping; at most 1 per 400 ms per player; server broadcasts `mark` (below) |
@ -124,12 +124,35 @@ pid for the viewer's own entry and an opaque, stable per-room alias for the othe
| `{t:'newgame', map}` | **host only**: replace the sim with a fresh `new Sim({map})`; resets pause/speed |
| `{t:'leave'}` | leave now |
**Idempotent `upgrade` and `wave`.** Two players who press Upgrade (or Next wave) at the same moment both send the command; without
a guard the tower would be upgraded twice and the second player charged for a level they never asked for, or two waves would start.
Both commands therefore carry what the sender *saw*: `lv` (upgrade) is the tower level on the sender's screen, `n` (wave) is the wave
number the sender expects the command to start (the wave counter they saw, plus one). The sim applies the command only if that still
holds; otherwise nothing changes and the sender gets `{ t:'reject', c, reason:'stale', id? }`. Clients treat `stale` as "your partner
was a moment faster" and show nothing. Both fields are optional (no field, no check: older clients, the balance bot and the server's
own auto-wave timer send none); when present they must be integers, anything else is a malformed message. `stale` is checked after
`over` / `no_tower` and before `max` / `gold`, so a sender who is behind is told it is stale rather than that the tower is maxed out.
Command validation is the server's job: `t:'cmd'` must carry a string `c` in the whitelist above; unknown
`c`, non-object, etc. count as malformed. A command the sim refuses (`{ok:false, reason}`) gets
`{ t:'reject', c, reason, x?, y?, id? }` sent **to the sender only** (echo `x,y`/`id` when present).
Successful commands send nothing extra; the effect arrives via `snap`.
`newgame` from a non-host -> `reject` with `reason:'not_host'`; `newgame` with a bad map -> `reject` `bad_map`.
## Client behaviour that the protocol relies on
* **Dead connections.** A joined socket gets ~20 `snap`s per second even when the game is paused, so the client treats 5 s without any
frame as a dead socket (a half-open connection after a Wi-Fi / cell switch or a NAT / proxy drop never delivers a close for a minute
or more). It shows its "reconnecting" state at once, detaches the old socket and opens a new one with the same `join` (same `pid`);
the server hands the seat to the new connection and closes the old one with 4001, which the abandoned socket ignores. A socket that
has not reached `joined` 10 s after it was opened is abandoned the same way. Retries back off up to 5 s, 14 in all.
* **Tower lists.** `tw` is sent once per `rev` change and must never be lost, whatever the render rate: a client that is stalled (hidden
tab, long frame) and gets a burst of snapshots keeps the newest undisplayed `tw` when it trims its interpolation buffer, and asks for
`sync` if `snap.rev` and the rev of the last applied list ever disagree.
* **"No such room" after a reconnect** (`error` / `no_room` in answer to a `join` for the room the client was in) means the server
restarted (rooms live in memory only) or the room expired. The client says so ("The server restarted, your game was lost.") and
forgets the saved room; after a plain connection failure it keeps the saved room, because the seat is held for `graceMs`.
## Server -> client
```
@ -148,6 +171,15 @@ Successful commands send nothing extra; the effect arrives via `snap`.
* Snapshots are sent every `1000/snapshotHz` ms **whether or not** the sim advanced (paused rooms still send, so
cursors keep moving) but only to online players, and only if at least one player is online.
* `ev` events are drained from the sim once per snapshot, so every client sees every event exactly once.
* **Tower rows** (`tw`, `state.towers`) are `[id, type, x, y, level, mode, spent, owner, pm]`; `pm` is the price multiplier the tower was bought at (1 for
almost every tower; the Orbital Uplink gets dearer for every one owned, and its upgrades cost the same multiple). Clients that ignore the ninth
field still work; the UI uses it to show the same upgrade price the sim will charge.
* **Monster entries** in `snap.mo` are `[id, kindIndex, x, y, hp, maxhp, slow]`. `kindIndex` indexes `TD.ENEMY_ORDER` (append-only, so
old and new clients agree). Two encodings keep the format fixed: a **shielded** enemy (the Proxy) sends `hp = hp + shield` and
`maxhp = maxhp + maxShield` (the client derives the shield part from the kind's base hp : shield ratio); and the `slow` flag is `1`
while the enemy is slowed **or stunned** (a stunned enemy simply stops moving, the client infers the stun from that).
* New events: `stun {x, y, r, n}` (a Sandbox pulse: centre, radius, number of enemies hit) and `heal {x, y, r}` (a Daemon pulse), besides
the Orbital Uplink's `beam {x, y, tx, ty, w, bx, by, br}`.
* Because WebSocket is reliable and ordered, `tw` deltas cannot be lost; `snap.rev` lets a client detect a
bug/desync (`rev` differs from the `rev` it last applied and no `tw` came along) and ask for `sync`.

View file

@ -23,7 +23,7 @@ opponent for either side so it can be played alone.
| Base income | 4 credits/s, rising slowly (+0.02/s per second of the round) |
| **Lifetime bonus** | every living attacker unit earns `cost x 0.004` credits per second alive, capped at 50% of its cost in total |
| Leak bonus | a unit that reaches the server pays back its full cost (and damages integrity) |
| Unit shop | every enemy kind has a `buyCost` (pinger 10, worm 14, spam pack 20, ghost 40, trojan 60, healer 70, rootkit 400, ...) |
| Unit shop | every enemy kind has a `buyCost` in the sim's `ENEMIES` table (currently ping 10, worm 6, spam 3, ghost 15, trojan 30, daemon 25, proxy 30, botnet 3, rootkit 300; about 3x the kill reward, to be tuned by the versus balance pass) |
| Caps | at most 250 units alive, 200 staged (performance) |
**Save up and launch.** Units can be bought instantly (`spawn`) or **staged** (`stage`): the price is paid now, the units wait in