summaryrefslogtreecommitdiff
path: root/server/lib/net/protocol.dart
blob: 9356cf17901f4386acb62fb27ef7e6073d4cf62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/// 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.
library;

/// Protocol message type identifiers and builders.
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';

  // Phase 6 (settings + multi-save) — players only.
  static const String setSetting = 'setSetting';
  static const String save = 'save';
  static const String loadSave = 'loadSave';
  static const String deleteSave = 'deleteSave';
  static const String saveCharacter = 'saveCharacter';
  static const String loadCharacterSave = 'loadCharacterSave';
  static const String deleteCharacterSave = 'deleteCharacterSave';

  // Server -> client message types.
  static const String welcome = 'welcome';
  static const String update = 'update';
  static const String seats = 'seats';
  static const String settings = 'settings';
  static const String error = 'error';
  static const String pong = 'pong';

  // Roles.
  static const String roleSpectator = 'spectator';
  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 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.
  ///
  /// [settings] carries the server-side (game-relevant) settings + save
  /// names (Phase 6). [historyLength] / [redoDescription] /
  /// [undoDescription] let the client render the undo/redo UI without
  /// holding the history itself (the labels are the reference app's
  /// `MainMenuViewModel.undoDescription` / `redoDescription` — null when
  /// that direction is exhausted).
  static Map<String, dynamic> welcomeMessage({
    required String session,
    required String role,
    required int index,
    required Map<String, dynamic> state,
    required Map<String, dynamic> seats,
    required Map<String, dynamic> settings,
    required int historyLength,
    String? description,
    String? redoDescription,
    String? undoDescription,
    Object? event,
  }) {
    return {
      't': welcome,
      'v': version,
      'session': session,
      'role': role,
      'index': index,
      'description': description,
      'historyLength': historyLength,
      'redoDescription': redoDescription,
      'undoDescription': undoDescription,
      'event': event,
      'state': state,
      'seats': seats,
      'settings': settings,
    };
  }

  /// 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.
  ///
  /// [index] always equals the state position (the undo path reports the
  /// post-decrement index; see `ActionHandler.undo`). [historyLength] /
  /// [redoDescription] / [undoDescription] let the client render the
  /// undo/redo UI (enablement + labels) from the snapshot alone.
  static Map<String, dynamic> updateMessage({
    required int index,
    required String description,
    required Object? event,
    required Map<String, dynamic> state,
    required int historyLength,
    String? redoDescription,
    String? undoDescription,
  }) {
    return {
      't': update,
      'index': index,
      'description': description,
      'historyLength': historyLength,
      'redoDescription': redoDescription,
      'undoDescription': undoDescription,
      'event': event,
      'state': state,
    };
  }

  /// Settings broadcast (Phase 6): the server-side settings + save names,
  /// sent to every connection when a setting changes or the save list does.
  /// Also embedded in `welcome`. The save *content* never crosses the wire.
  static Map<String, dynamic> settingsMessage(Map<String, dynamic> settings) =>
      {
        't': Protocol.settings,
        'settings': settings,
      };

  // --- Phase 6 client messages (players only) -------------------------------

  /// Client message setting one server-side (game-relevant) setting.
  /// [name] is one of the known setting names; [value] its new value
  /// (booleans, or the style enum index / locale code strings).
  static Map<String, dynamic> setSettingMessage(
    String name,
    Object value,
  ) => {
    't': setSetting,
    'name': name,
    'value': value,
  };

  /// Client message saving the current game state to a named save slot.
  static Map<String, dynamic> saveMessage(String name) => {
    't': save,
    'name': name,
  };

  /// Client message loading a named save (runs `LoadSaveCommand` — an
  /// undoable action, so it advances the command index).
  static Map<String, dynamic> loadSaveMessage(String name) => {
    't': loadSave,
    'name': name,
  };

  /// Client message deleting a named save.
  static Map<String, dynamic> deleteSaveMessage(String name) => {
    't': deleteSave,
    'name': name,
  };

  /// Client message saving one character's state under [name].
  static Map<String, dynamic> saveCharacterMessage(
    String name,
    String characterId,
  ) => {
    't': saveCharacter,
    'name': name,
    'characterId': characterId,
  };

  /// Client message loading a character save. [saveId] is the
  /// `"$name\n${characterId}"` key the server stores it under.
  static Map<String, dynamic> loadCharacterSaveMessage(String saveId) => {
    't': loadCharacterSave,
    'saveId': saveId,
  };

  /// Client message deleting a character save.
  static Map<String, dynamic> deleteCharacterSaveMessage(String saveId) => {
    't': deleteCharacterSave,
    'saveId': saveId,
  };

  /// 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,
    'message': message,
  };

  static Map<String, dynamic> pongMessage() => {'t': pong};
}