summaryrefslogtreecommitdiff
path: root/server/lib/net/protocol.dart
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2026-11-08 21:56:29 +0100
committerJoel Klinghed <the_jk@spawned.biz>2026-11-08 21:56:29 +0100
commitf6955e34489c79f6d5b764cb6a39b1387366b433 (patch)
tree83dad99ad81964036820468ad23fdc9ebe5c20a5 /server/lib/net/protocol.dart
parent0578ed0e5a08e6790a2e02416c4bf79f8c471f2d (diff)
Phase 0: project scaffolding
- server/: Dart game-server package (shelf + WebSocket) with the hello/welcome handshake, ping/pong, error responses, healthz, static SPA serving with SPA fallback, and smoke tests (5/5) - web/: Svelte 5 + TypeScript SPA with the protocol client, connection-status page, vitest and svelte-check; Vite dev server proxies /ws to the Dart server - game data assets copied from the read-only reference app into server/assets/ (data, images, i18n, fonts, testData) - PLAN.md implementation plan and progress tracker; README rewritten for the web app; AGPLv3 LICENSE inherited from the reference app; .gitignore; scripts/ci.sh (full pipeline) - CLAUDE.md project guidance Verified: scripts/ci.sh passes end-to-end; live smoke confirms the server serves the built SPA at / and answers a WebSocket hello with a welcome message.
Diffstat (limited to 'server/lib/net/protocol.dart')
-rw-r--r--server/lib/net/protocol.dart73
1 files changed, 73 insertions, 0 deletions
diff --git a/server/lib/net/protocol.dart b/server/lib/net/protocol.dart
new file mode 100644
index 0000000..e8135e9
--- /dev/null
+++ b/server/lib/net/protocol.dart
@@ -0,0 +1,73 @@
+/// 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.
+library;
+
+/// Protocol message type identifiers.
+abstract class Protocol {
+ Protocol._();
+
+ static const int version = 1;
+
+ // Client -> server message types.
+ static const String hello = 'hello';
+ static const String claim = 'claim';
+ static const String cmd = 'cmd';
+ static const String undo = 'undo';
+ static const String redo = 'redo';
+ static const String ping = 'ping';
+
+ // Server -> client message types.
+ static const String welcome = 'welcome';
+ static const String update = 'update';
+ static const String seats = 'seats';
+ static const String error = 'error';
+ static const String pong = 'pong';
+
+ // Roles.
+ static const String roleSpectator = 'spectator';
+ static const String rolePlayer = 'player';
+
+ /// Error codes.
+ static const String errBadRequest = 'badRequest';
+ static const String errUnknownMessage = 'unknownMessage';
+ static const String errRejected = 'rejected';
+ 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.
+ static Map<String, dynamic> helloMessage() => {'t': hello, 'v': version};
+
+ /// 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).
+ static Map<String, dynamic> welcomeMessage({
+ required String session,
+ required String role,
+ required int index,
+ required Map<String, dynamic> state,
+ required Map<String, dynamic> seats,
+ String? description,
+ Object? event,
+ }) {
+ return {
+ 't': welcome,
+ 'v': version,
+ 'session': session,
+ 'role': role,
+ 'index': index,
+ 'description': description,
+ 'event': event,
+ 'state': state,
+ 'seats': seats,
+ };
+ }
+
+ static Map<String, dynamic> errorMessage(String code, String message) =>
+ {'t': error, 'code': code, 'message': message};
+
+ static Map<String, dynamic> pongMessage() => {'t': pong};
+}