summaryrefslogtreecommitdiff
path: root/server/lib/net/protocol.dart
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/net/protocol.dart')
-rw-r--r--server/lib/net/protocol.dart66
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,