diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-13 00:04:16 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-13 00:04:16 +0100 |
| commit | 3ebbdf6ffa16f751c82e3bd2e74294f4fe843155 (patch) | |
| tree | db4c92ce26f274073453e2a085af3105be823f74 /server/lib/net/protocol.dart | |
| parent | 808294adad3f378ad8d66b698acf8df7f49c2302 (diff) | |
Phase 2: networking, sessions, secrets over WebSocket (M2)
Server owns all state; browsers send input only. New server/lib/net/
layer on top of the Phase 1 engine:
- protocol.dart: JSON wire protocol (v1) — hello/welcome handshake,
claim, cmd, undo/redo, ping/pong, update, seats, error
- sessions.dart: 32-hex session ids carried in the x-haven-session
cookie; role derived from seat ownership (player vs spectator);
resolve() adopts a well-formed cookie id so a seat survives restarts
- seat_store.dart: seat table persisted to saves/seats.json beside the
state snapshot; restored on startup
- visibility.dart: per-connection state serialization; the initiative
rule masks every non-owned character's initiative to null until the
round is drawn — the server never sends a secret to a non-owner
- game_server.dart: the command execution path (role check → game-rule
guard → registry build → execute → per-connection update broadcast);
seat assign/claim/release with seats broadcasts; heartbeat sweep
closes dead connections and releases their seats
- ws_upgrade.dart: custom WS upgrade handler so the server reads the
session cookie (shelf_web_socket hides the Request); app.dart wires
shelf + static SPA; bin/main.dart starts the server
- game_state.dart: onAction setter so the network layer installs its
broadcast hook; save_store.dart / seat_store.dart writes made
synchronous (writeAsStringSync) so a snapshot/seat table is on disk
when write() returns — the kill -9 persistence test exposed the async
write race
Server-side guards replace the reference app's 300 ms draw-button
lockout: sequential execution makes a duplicate draw deterministically
lose, with a clear error to the sender only. The session that adds a
character owns it and becomes a player; spectators may only claim/
addCharacter. Command index resets to -1 on restart (the snapshot
carries state, not undo history).
Tests: 34 new network tests over a real WS stack on an ephemeral port
(sessions 9, visibility 5, app 9, multiplayer 9, persistence 2) —
secret initiative never reaches non-owners, reveal on draw, reconnect
resync, rejected duplicate draw, claim/lose/re-claim seats, undo/redo
broadcast to every screen, dead-connection cleanup, snapshot + seat
file restored across a restart. Replaces the Phase 0 smoke test.
dart analyze warning-free; dart test 591/591; scripts/ci.sh green.
Diffstat (limited to 'server/lib/net/protocol.dart')
| -rw-r--r-- | server/lib/net/protocol.dart | 66 |
1 files changed, 60 insertions, 6 deletions
diff --git a/server/lib/net/protocol.dart b/server/lib/net/protocol.dart index 54328fc..c3030e1 100644 --- a/server/lib/net/protocol.dart +++ b/server/lib/net/protocol.dart @@ -1,12 +1,10 @@ /// JSON wire protocol for X-Haven, shared by the server, its tests and (via /// mirrored types) the web client. /// -/// See PLAN.md section 3.5 for the protocol design. Phase 0 implements the -/// handshake (hello/welcome/ping/pong/error); the rest of the message types -/// are declared here and added to in later phases. +/// See PLAN.md section 3.5 for the protocol design. library; -/// Protocol message type identifiers. +/// Protocol message type identifiers and builders. abstract class Protocol { Protocol._(); @@ -32,18 +30,48 @@ abstract class Protocol { static const String rolePlayer = 'player'; /// Error codes. + /// + /// * [errBadRequest] — malformed JSON or a message with missing/wrong fields + /// * [errUnknownMessage] — unknown `t` value + /// * [errRejected] — the command was not executed (unknown name, invalid + /// args, or a game-rule guard such as a duplicate draw) + /// * [errForbidden] — the session's role does not allow the message + /// (spectator sending a gated command, undo/redo as a spectator) + /// * [errProtocol] — protocol version mismatch (the connection is closed) static const String errBadRequest = 'badRequest'; static const String errUnknownMessage = 'unknownMessage'; static const String errRejected = 'rejected'; + static const String errForbidden = 'forbidden'; static const String errProtocol = 'protocol'; - /// First client message: the handshake. The session id rides in a cookie - /// (Phase 2); in Phase 0 the server assigns a throwaway session. + /// First client message: the handshake. The session id rides in the + /// `x-haven-session` cookie (or the `?session=` query parameter) of the + /// upgrade request, so it is not part of the message. static Map<String, dynamic> helloMessage() => {'t': hello, 'v': version}; + /// Client message claiming a character seat (or re-asserting an existing + /// ownership). [characterId] is the character's id in the current list. + static Map<String, dynamic> claimMessage(String characterId) => { + 't': claim, + 'characterId': characterId, + }; + + /// Client message asking the server to execute a registered command + /// (players only — see PLAN.md section 3.3 for the role rules). + static Map<String, dynamic> commandMessage( + String name, [ + Map<String, dynamic> args = const {}, + ]) => { + 't': cmd, + 'name': name, + 'args': args, + }; + /// Full state snapshot for a new (or reconnecting) connection. [state] is /// already serialized for this connection, i.e. with that connection's /// secrets included and everyone else's masked (PLAN.md section 3.4). + /// [seats] maps every character in the current list to its owning session + /// id, or null when unclaimed. static Map<String, dynamic> welcomeMessage({ required String session, required String role, @@ -66,6 +94,32 @@ abstract class Protocol { }; } + /// Full-state push sent to every connection after a command, undo or redo + /// (the equivalent of the reference app's `{i, d, e, s}` envelope). + /// [state] is serialized per connection by the server. + static Map<String, dynamic> updateMessage({ + required int index, + required String description, + required Object? event, + required Map<String, dynamic> state, + }) { + return { + 't': update, + 'index': index, + 'description': description, + 'event': event, + 'state': state, + }; + } + + /// Seat-ownership broadcast: every character in the current list mapped to + /// its owning session id, or null when unclaimed. The UI enables inputs + /// only on the owning player's screen from this map. + static Map<String, dynamic> seatsMessage(Map<String, String?> seatsTable) => { + 't': seats, + 'seats': seatsTable, + }; + static Map<String, dynamic> errorMessage(String code, String message) => { 't': error, 'code': code, |
