tickflare 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/CHANGELOG.md +240 -0
  2. package/LICENSE +21 -0
  3. package/README.md +112 -2
  4. package/dist/binary.d.ts +91 -0
  5. package/dist/binary.js +466 -0
  6. package/dist/binary.js.map +1 -0
  7. package/dist/client/accounts.d.ts +193 -0
  8. package/dist/client/accounts.js +306 -0
  9. package/dist/client/accounts.js.map +1 -0
  10. package/dist/client/buffered.d.ts +88 -0
  11. package/dist/client/buffered.js +229 -0
  12. package/dist/client/buffered.js.map +1 -0
  13. package/dist/client/clock.d.ts +36 -0
  14. package/dist/client/clock.js +83 -0
  15. package/dist/client/clock.js.map +1 -0
  16. package/dist/client/draft.d.ts +17 -0
  17. package/dist/client/draft.js +133 -0
  18. package/dist/client/draft.js.map +1 -0
  19. package/dist/client/index.d.ts +182 -0
  20. package/dist/client/index.js +496 -0
  21. package/dist/client/index.js.map +1 -0
  22. package/dist/client/interpolate.d.ts +31 -0
  23. package/dist/client/interpolate.js +59 -0
  24. package/dist/client/interpolate.js.map +1 -0
  25. package/dist/client/lockstep.d.ts +86 -0
  26. package/dist/client/lockstep.js +339 -0
  27. package/dist/client/lockstep.js.map +1 -0
  28. package/dist/client/matchmaking.d.ts +21 -0
  29. package/dist/client/matchmaking.js +55 -0
  30. package/dist/client/matchmaking.js.map +1 -0
  31. package/dist/client/pacer.d.ts +22 -0
  32. package/dist/client/pacer.js +65 -0
  33. package/dist/client/pacer.js.map +1 -0
  34. package/dist/client/predict.d.ts +75 -0
  35. package/dist/client/predict.js +203 -0
  36. package/dist/client/predict.js.map +1 -0
  37. package/dist/client/token.d.ts +10 -0
  38. package/dist/client/token.js +15 -0
  39. package/dist/client/token.js.map +1 -0
  40. package/dist/lockstep.d.ts +154 -0
  41. package/dist/lockstep.js +140 -0
  42. package/dist/lockstep.js.map +1 -0
  43. package/dist/patch.d.ts +54 -0
  44. package/dist/patch.js +186 -0
  45. package/dist/patch.js.map +1 -0
  46. package/dist/protocol.d.ts +268 -0
  47. package/dist/protocol.js +54 -0
  48. package/dist/protocol.js.map +1 -0
  49. package/dist/server/accounts.d.ts +320 -0
  50. package/dist/server/accounts.js +1223 -0
  51. package/dist/server/accounts.js.map +1 -0
  52. package/dist/server/delta.d.ts +50 -0
  53. package/dist/server/delta.js +426 -0
  54. package/dist/server/delta.js.map +1 -0
  55. package/dist/server/frames.d.ts +15 -0
  56. package/dist/server/frames.js +23 -0
  57. package/dist/server/frames.js.map +1 -0
  58. package/dist/server/gateway.d.ts +80 -0
  59. package/dist/server/gateway.js +144 -0
  60. package/dist/server/gateway.js.map +1 -0
  61. package/dist/server/index.d.ts +21 -0
  62. package/dist/server/index.js +22 -0
  63. package/dist/server/index.js.map +1 -0
  64. package/dist/server/limiter.d.ts +12 -0
  65. package/dist/server/limiter.js +43 -0
  66. package/dist/server/limiter.js.map +1 -0
  67. package/dist/server/lockstep.d.ts +127 -0
  68. package/dist/server/lockstep.js +549 -0
  69. package/dist/server/lockstep.js.map +1 -0
  70. package/dist/server/matches.d.ts +31 -0
  71. package/dist/server/matches.js +69 -0
  72. package/dist/server/matches.js.map +1 -0
  73. package/dist/server/matchmaker.d.ts +121 -0
  74. package/dist/server/matchmaker.js +479 -0
  75. package/dist/server/matchmaker.js.map +1 -0
  76. package/dist/server/room.d.ts +402 -0
  77. package/dist/server/room.js +1896 -0
  78. package/dist/server/room.js.map +1 -0
  79. package/dist/server/signals.d.ts +18 -0
  80. package/dist/server/signals.js +37 -0
  81. package/dist/server/signals.js.map +1 -0
  82. package/dist/server/storage.d.ts +117 -0
  83. package/dist/server/storage.js +330 -0
  84. package/dist/server/storage.js.map +1 -0
  85. package/dist/server/tokens.d.ts +74 -0
  86. package/dist/server/tokens.js +117 -0
  87. package/dist/server/tokens.js.map +1 -0
  88. package/dist/server/tracked.d.ts +47 -0
  89. package/dist/server/tracked.js +270 -0
  90. package/dist/server/tracked.js.map +1 -0
  91. package/dist/world.d.ts +20 -0
  92. package/dist/world.js +21 -0
  93. package/dist/world.js.map +1 -0
  94. package/package.json +74 -4
  95. package/src/binary.ts +496 -0
  96. package/src/client/accounts.ts +423 -0
  97. package/src/client/buffered.ts +264 -0
  98. package/src/client/clock.ts +86 -0
  99. package/src/client/draft.ts +138 -0
  100. package/src/client/index.ts +631 -0
  101. package/src/client/interpolate.ts +60 -0
  102. package/src/client/lockstep.ts +398 -0
  103. package/src/client/matchmaking.ts +61 -0
  104. package/src/client/pacer.ts +67 -0
  105. package/src/client/predict.ts +239 -0
  106. package/src/client/token.ts +22 -0
  107. package/src/lockstep.ts +285 -0
  108. package/src/patch.ts +188 -0
  109. package/src/protocol.ts +300 -0
  110. package/src/server/accounts.ts +1474 -0
  111. package/src/server/delta.ts +467 -0
  112. package/src/server/frames.ts +23 -0
  113. package/src/server/gateway.ts +213 -0
  114. package/src/server/index.ts +86 -0
  115. package/src/server/limiter.ts +41 -0
  116. package/src/server/lockstep.ts +617 -0
  117. package/src/server/matches.ts +99 -0
  118. package/src/server/matchmaker.ts +537 -0
  119. package/src/server/room.ts +1999 -0
  120. package/src/server/signals.ts +46 -0
  121. package/src/server/storage.ts +392 -0
  122. package/src/server/tokens.ts +184 -0
  123. package/src/server/tracked.ts +260 -0
  124. package/src/world.ts +29 -0
  125. package/tools/rooms-report.mjs +168 -0
package/src/patch.ts ADDED
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Deltas between two JSON values, used to send a player only what changed.
3
+ *
4
+ * A patch mirrors the shape of the object it changes. For each key:
5
+ * - a nested patch object: go into that key and patch what is there;
6
+ * - `[value]`: set the key to `value`;
7
+ * - `[changes, length]`: the key holds an array; `length` is how long it
8
+ * now is, and `changes` has, by index, what to do with the elements that
9
+ * changed, each in one of these same forms;
10
+ * - `0`: delete the key.
11
+ *
12
+ * An array is patched element by element where its elements kept their
13
+ * places: a unit that moved costs its new position, not the whole list. An
14
+ * array whose elements shifted, because one was put in or taken out before
15
+ * the end, is sent whole. So for things that come and go, an object keyed by
16
+ * id is still the better home.
17
+ */
18
+ export type Patch = { [key: string]: Patch | [unknown] | ArrayPatch | 0 };
19
+
20
+ /**
21
+ * What changed in an array: by index, the elements to patch or replace, and
22
+ * the array's new length. Elements past the new length are gone.
23
+ */
24
+ export type ArrayPatch = [changes: { [index: string]: Patch | [unknown] | ArrayPatch }, length: number];
25
+
26
+ /**
27
+ * The patch that turns `prev` into `next`. Returns `undefined` when they are
28
+ * equal, and `null` when they cannot be patched because one of them is not a
29
+ * plain object: send `next` whole in that case.
30
+ *
31
+ * `next` is read as JSON would write it: keys holding `undefined` count as absent.
32
+ */
33
+ export function diff(prev: unknown, next: unknown): Patch | null | undefined {
34
+ if (isPlainObject(prev) && isPlainObject(next)) return diffObjects(prev, next);
35
+ return jsonEqual(prev, next) ? undefined : null;
36
+ }
37
+
38
+ /** Applies a patch without touching `base`: unchanged branches are shared with it. */
39
+ export function applyPatch<T>(base: T, patch: Patch): T {
40
+ const out: Record<string, unknown> = isPlainObject(base) ? { ...base } : {};
41
+ for (const key of Object.keys(patch)) {
42
+ if (key === "__proto__") continue;
43
+ const change = patch[key];
44
+ if (change === 0) delete out[key];
45
+ else if (Array.isArray(change)) out[key] = change.length === 2 ? applyArrayPatch(own(out, key), change) : change[0];
46
+ else if (change) out[key] = applyPatch(own(out, key), change);
47
+ }
48
+ return out as T;
49
+ }
50
+
51
+ /** An index as a patch writes it: a whole number from 0 up, with no sign and no leading zero. */
52
+ const INDEX = /^(0|[1-9][0-9]*)$/;
53
+
54
+ function applyArrayPatch(base: unknown, [changes, length]: ArrayPatch): unknown[] {
55
+ const out: unknown[] = Array.isArray(base) ? base.slice(0, length) : [];
56
+ for (const key of Object.keys(changes)) {
57
+ const index = Number(key);
58
+ // Nothing is put beyond the length the patch gives, so a patch cannot make a huge array.
59
+ if (!INDEX.test(key) || index >= length) continue;
60
+ const change = changes[key]!;
61
+ if (Array.isArray(change)) out[index] = change.length === 2 ? applyArrayPatch(out[index], change) : change[0];
62
+ else out[index] = applyPatch(out[index], change);
63
+ }
64
+ // An element the patch left out, past the old end, is null rather than a hole.
65
+ for (let index = out.length; index < length; index++) out[index] = null;
66
+ for (let index = 0; index < length; index++) if (!(index in out)) out[index] = null;
67
+ return out;
68
+ }
69
+
70
+ /**
71
+ * The same change for a client that does not know array patches: wherever
72
+ * the patch has one, the whole array as it is in `next`, the state the patch
73
+ * leads to. Returns the patch itself when there are none in it.
74
+ */
75
+ export function withoutArrayPatches(patch: Patch, next: unknown): Patch {
76
+ let out: Patch | undefined;
77
+ const after = isPlainObject(next) ? next : {};
78
+ for (const key of Object.keys(patch)) {
79
+ const change = patch[key]!;
80
+ let plain: Patch[string] = change;
81
+ if (Array.isArray(change)) {
82
+ if (change.length === 2) plain = [own(after, key)];
83
+ } else if (change !== 0) {
84
+ plain = withoutArrayPatches(change, own(after, key));
85
+ }
86
+ if (plain !== change) (out ??= { ...patch })[key] = plain;
87
+ }
88
+ return out ?? patch;
89
+ }
90
+
91
+ /**
92
+ * How to turn the array `prev` into `next`: a patch of its elements,
93
+ * `undefined` when they are equal, or `null` when sending `next` whole is the
94
+ * better way, which it is when most of what changed are elements replaced
95
+ * outright.
96
+ */
97
+ export function diffArrays(prev: unknown[], next: unknown[]): ArrayPatch | null | undefined {
98
+ const changes: ArrayPatch[0] = {};
99
+ let changed = 0;
100
+ let replaced = 0;
101
+ const shared = Math.min(prev.length, next.length);
102
+ for (let index = 0; index < shared; index++) {
103
+ const from = prev[index];
104
+ const to = next[index] === undefined ? null : next[index];
105
+ if (isPlainObject(from) && isPlainObject(to)) {
106
+ const inner = diffObjects(from, to);
107
+ if (!inner) continue;
108
+ changes[index] = inner;
109
+ // An element most of whose keys changed is as good as replaced.
110
+ if (Object.keys(inner).length * 2 > Object.keys(to).length) replaced += 1;
111
+ } else if (Array.isArray(from) && Array.isArray(to)) {
112
+ const inner = diffArrays(from, to);
113
+ if (inner === undefined) continue;
114
+ if (inner === null) replaced += 1;
115
+ changes[index] = inner ?? [to];
116
+ } else if (jsonEqual(from, to)) {
117
+ continue;
118
+ } else {
119
+ changes[index] = [to];
120
+ replaced += 1;
121
+ }
122
+ changed += 1;
123
+ // Elements replaced cost what they cost in the whole array, plus their index. Once they are
124
+ // more than half of it the whole array is no longer than the patch, and there is no need to
125
+ // look at the rest.
126
+ if (replaced * 2 > next.length) return null;
127
+ }
128
+ replaced += next.length - shared;
129
+ if (replaced * 2 > next.length) return null;
130
+ for (let index = shared; index < next.length; index++) {
131
+ changes[index] = [next[index] === undefined ? null : next[index]];
132
+ changed += 1;
133
+ }
134
+ if (changed === 0 && prev.length === next.length) return undefined;
135
+ return [changes, next.length];
136
+ }
137
+
138
+ function diffObjects(
139
+ prev: Record<string, unknown>,
140
+ next: Record<string, unknown>,
141
+ ): Patch | undefined {
142
+ let patch: Patch | undefined;
143
+ for (const key of Object.keys(next)) {
144
+ const to = next[key];
145
+ if (to === undefined) continue;
146
+ const from = own(prev, key);
147
+ if (isPlainObject(from) && isPlainObject(to)) {
148
+ const inner = diffObjects(from, to);
149
+ if (inner) (patch ??= {})[key] = inner;
150
+ } else if (Array.isArray(from) && Array.isArray(to)) {
151
+ const inner = diffArrays(from, to);
152
+ if (inner !== undefined) (patch ??= {})[key] = inner ?? [to];
153
+ } else if (from === undefined || !jsonEqual(from, to)) {
154
+ (patch ??= {})[key] = [to];
155
+ }
156
+ }
157
+ for (const key of Object.keys(prev)) {
158
+ if (prev[key] !== undefined && own(next, key) === undefined) (patch ??= {})[key] = 0;
159
+ }
160
+ return patch;
161
+ }
162
+
163
+ /**
164
+ * `object[key]`, but only if the object itself has the key. A unit called
165
+ * "toString" or "constructor" must not be mistaken for what every object inherits.
166
+ */
167
+ function own(object: Record<string, unknown>, key: string): unknown {
168
+ return Object.prototype.hasOwnProperty.call(object, key) ? object[key] : undefined;
169
+ }
170
+
171
+ /** Whether two values are the same as JSON. @internal */
172
+ export function jsonEqual(a: unknown, b: unknown): boolean {
173
+ if (a === b) return true;
174
+ if (Array.isArray(a)) {
175
+ if (!Array.isArray(b) || a.length !== b.length) return false;
176
+ for (let i = 0; i < a.length; i++) {
177
+ if (!jsonEqual(a[i], b[i])) return false;
178
+ }
179
+ return true;
180
+ }
181
+ if (isPlainObject(a) && isPlainObject(b)) return diffObjects(a, b) === undefined;
182
+ return false;
183
+ }
184
+
185
+ /** @internal */
186
+ export function isPlainObject(value: unknown): value is Record<string, unknown> {
187
+ return typeof value === "object" && value !== null && !Array.isArray(value);
188
+ }
@@ -0,0 +1,300 @@
1
+ /**
2
+ * Wire protocol shared by the room kernel and the client.
3
+ *
4
+ * Every frame is a JSON text frame, except the keepalive: the client sends the
5
+ * literal string "ping" and the runtime answers "pong" without waking the room.
6
+ */
7
+
8
+ import type { Patch } from "./patch.js";
9
+
10
+ /**
11
+ * The latest version of the protocol. A client asks for a version with `v`
12
+ * in the query when it connects, and the room's `welcome` frame says which
13
+ * it will speak: the one asked for, or this one if that is later. A client
14
+ * that does not ask is spoken to in version 1.
15
+ *
16
+ * Version 2 adds array patches: an array that changes can be sent element
17
+ * by element, as `[changes, length]` in a patch.
18
+ *
19
+ * Version 3 adds presence as changes: a `presence` frame can say who is new
20
+ * or different (`set`) and who is gone (`gone`) in place of the whole list.
21
+ *
22
+ * Version 4 adds the room's clock: while the simulation runs, `welcome`
23
+ * says where the clock is (`ms` and `epoch`), and `clock` frames say it
24
+ * again every few seconds, whenever it starts, stops or moves on.
25
+ */
26
+ export const PROTOCOL_VERSION = 4;
27
+
28
+ /** The keepalive a client sends, as a plain text frame. @internal */
29
+ export const PING = "ping";
30
+ /** The runtime's answer to `PING`, sent without waking the room. @internal */
31
+ export const PONG = "pong";
32
+
33
+ /** A seat in a room. A player keeps their seat while disconnected. */
34
+ export interface Player {
35
+ /** The id the gateway's `authenticate` gave this player. */
36
+ id: string;
37
+ /** Whether the player has a connection open right now. */
38
+ connected: boolean;
39
+ /**
40
+ * The player's name, when the gateway's `authenticate` gave one (signed-in
41
+ * players with `Accounts` have one). Only set in the hooks that run for a
42
+ * connecting request: `onJoin`, `onReconnect`, `onArrive` and `onHttp`.
43
+ * Keep it in your state if you need it later.
44
+ */
45
+ name?: string;
46
+ }
47
+
48
+ /** A command for the room. Answered with `ack` or `reject` carrying the same `id`. */
49
+ export interface CommandFrame<Command = unknown> {
50
+ /** Frame type. */
51
+ t: "cmd";
52
+ /** Chosen by the client, unique per connection, echoed in the answer. */
53
+ id: number;
54
+ /** The command, as the room's `parseCommand` will receive it. */
55
+ data: Command;
56
+ }
57
+
58
+ /** Give up the seat. On `ack` the room closes the connection with `CloseCode.Removed`. */
59
+ export interface LeaveFrame {
60
+ /** Frame type. */
61
+ t: "leave";
62
+ /** Chosen by the client, echoed in the answer. */
63
+ id: number;
64
+ }
65
+
66
+ /** Every frame a client sends, apart from the `ping` keepalive. */
67
+ export type ClientMessage<Command = unknown> = CommandFrame<Command> | LeaveFrame;
68
+
69
+ /** First frame on every connection, reconnects included. */
70
+ export interface WelcomeFrame<View = unknown> {
71
+ /** Frame type. */
72
+ t: "welcome";
73
+ /** The version of the protocol the room speaks on this connection: see `PROTOCOL_VERSION`. */
74
+ v: number;
75
+ /** This player's id. */
76
+ you: string;
77
+ /** The room's change counter. Every later `state` or `patch` carries the next one. */
78
+ seq: number;
79
+ /** Ticks run so far; 0 in event mode. */
80
+ tick: number;
81
+ /** Ticks a second; 0 in event mode. */
82
+ tickRate: number;
83
+ /** Version 4, while the simulation runs: as in `ClockFrame`. Absent while it does not. */
84
+ ms?: number;
85
+ /** Version 4, while the simulation runs: as in `ClockFrame`. */
86
+ epoch?: number;
87
+ /** This player's whole view. */
88
+ state: View;
89
+ /** Everyone with a seat, in join order. */
90
+ players: Player[];
91
+ }
92
+
93
+ /**
94
+ * The state changed, and this is the player's whole view: sent when the room
95
+ * does not know what the player holds, or when the view is not an object and
96
+ * so cannot be patched.
97
+ */
98
+ export interface StateFrame<View = unknown> {
99
+ /** Frame type. */
100
+ t: "state";
101
+ /** The room's change counter after this change. */
102
+ seq: number;
103
+ /** Ticks run so far. */
104
+ tick?: number;
105
+ /** As in `PatchFrame`. */
106
+ c?: number;
107
+ /** This player's whole view. */
108
+ state: View;
109
+ }
110
+
111
+ /** The state changed: only what differs from what this player last received. */
112
+ export interface PatchFrame {
113
+ /** Frame type. */
114
+ t: "patch";
115
+ /** The room's change counter after this change. */
116
+ seq: number;
117
+ /** Ticks run so far. While the room is ticking, the tick that made the change. */
118
+ tick: number;
119
+ /**
120
+ * Outside the simulation, the `seq` of the view this is a patch of. A client
121
+ * that holds any other has missed something: it must not apply the patch,
122
+ * and reconnects to be sent its whole view.
123
+ */
124
+ from?: number;
125
+ /**
126
+ * The `id` of the latest command of this player's that the view now
127
+ * reflects: that one and every earlier one have been applied, and their
128
+ * `ack` frames follow. Present when the frame is the first to show a
129
+ * command of theirs.
130
+ */
131
+ c?: number;
132
+ /** Apply it with `applyPatch` to the view you have. */
133
+ p: Patch;
134
+ }
135
+
136
+ /**
137
+ * Someone joined, dropped, came back or left. Either the whole list, in
138
+ * `players`, or what changed in it since the last frame, in `set` and
139
+ * `gone`: a client of version 3 or later is sent whichever is shorter.
140
+ */
141
+ export interface PresenceFrame {
142
+ /** Frame type. */
143
+ t: "presence";
144
+ /** Everyone with a seat, in join order. When present, it replaces the list. */
145
+ players?: Player[];
146
+ /**
147
+ * Players who are new, or whose `connected` or `name` changed. One the
148
+ * list has already takes the place of the old entry; a new one goes to
149
+ * the end, in the order given.
150
+ */
151
+ set?: Player[];
152
+ /** The ids of players who no longer have a seat. */
153
+ gone?: string[];
154
+ }
155
+
156
+ /**
157
+ * The command or leave with this `id` was applied, and the view this player
158
+ * holds reflects it. It follows the `state` or `patch` frame that shows the
159
+ * command's effect: at once in event mode, and after the next tick in a room
160
+ * that is ticking, since that is when the effect is sent. A leave is always
161
+ * answered at once.
162
+ */
163
+ export interface AckFrame {
164
+ /** Frame type. */
165
+ t: "ack";
166
+ /** The `id` of the command or leave. */
167
+ id: number;
168
+ /** The change it made, or the current one if it changed nothing. */
169
+ seq: number;
170
+ /** In a room that is ticking: the tick the view is now of, whether or not a frame was sent for it. */
171
+ tick?: number;
172
+ }
173
+
174
+ /**
175
+ * Version 4: where the room's clock is. Ticks fall due on a fixed clock, one
176
+ * every `1 / tickRate` seconds from when the simulation started, so a client
177
+ * that knows how long ago one tick was due can tell which tick the room is on
178
+ * at any moment: `tick + (ms + elapsed) * tickRate / 1000`, where `elapsed`
179
+ * is the time since the frame was sent, about half a round trip when it
180
+ * arrives. `RoomClient.serverTick()` works this out.
181
+ *
182
+ * Sent every five seconds while the simulation runs, and at once when it
183
+ * starts, stops, or lets ticks go after a stall. The same reading is in
184
+ * `welcome` for a client that connects while it runs.
185
+ */
186
+ export interface ClockFrame {
187
+ /** Frame type. */
188
+ t: "clock";
189
+ /** The latest tick the room has run. */
190
+ tick: number;
191
+ /**
192
+ * How long before this frame was sent `tick` fell due, in milliseconds:
193
+ * usually less than one tick, more when the room is running late. Absent
194
+ * when the simulation has stopped, at `tick`, until a frame says otherwise.
195
+ */
196
+ ms?: number;
197
+ /**
198
+ * Which run of the clock this is. It changes when the simulation starts
199
+ * again, and when the room lets ticks go after a stall; while it stays the
200
+ * same, readings can be averaged.
201
+ */
202
+ epoch?: number;
203
+ }
204
+
205
+ /** The command or leave with this `id` was refused, and nothing changed. */
206
+ export interface RejectFrame {
207
+ /** Frame type. */
208
+ t: "reject";
209
+ /** The `id` of the command or leave. */
210
+ id: number;
211
+ /**
212
+ * What the room passed to `reject()`; else `invalid_command` if
213
+ * `parseCommand` threw, `internal_error` if a hook threw, or `rate_limited`.
214
+ */
215
+ reason: string;
216
+ }
217
+
218
+ /** A frame the room could not read at all. */
219
+ export interface ErrorFrame {
220
+ /** Frame type. */
221
+ t: "error";
222
+ /** `invalid_json`, `invalid_message`, `message_too_large`, `binary_not_supported` or `rate_limited`. */
223
+ reason: string;
224
+ }
225
+
226
+ /** A message the room sent this player with `send` or `broadcast`. */
227
+ export interface MessageFrame {
228
+ /** Frame type. */
229
+ t: "msg";
230
+ /** The message, as the room passed it. */
231
+ d: unknown;
232
+ }
233
+
234
+ /** Every frame a room sends, apart from the `pong` keepalive. */
235
+ export type ServerMessage<View = unknown> =
236
+ | WelcomeFrame<View>
237
+ | StateFrame<View>
238
+ | PatchFrame
239
+ | PresenceFrame
240
+ | ClockFrame
241
+ | AckFrame
242
+ | RejectFrame
243
+ | ErrorFrame
244
+ | MessageFrame;
245
+
246
+ /** WebSocket close codes the room uses. The client does not reconnect after those from 4000 to 4099. */
247
+ export const CloseCode = {
248
+ /** The same player opened a newer connection. */
249
+ Replaced: 4000,
250
+ /** Every seat is taken. */
251
+ RoomFull: 4001,
252
+ /** `onJoin` refused the player; the close reason carries why. */
253
+ JoinRejected: 4002,
254
+ /** The player no longer has a seat: they left, or the room removed them. The close reason says which. */
255
+ Removed: 4003,
256
+ /**
257
+ * The room handed the player over to another room; the close reason is that
258
+ * room's id. The client reconnects there by itself.
259
+ */
260
+ Moved: 4004,
261
+ /**
262
+ * A ticking room heard nothing on this connection for `silentAfterMs`, not
263
+ * even a keepalive, and took it for dead. A client that is there after
264
+ * all connects again: this code is outside those it gives up on.
265
+ */
266
+ Silent: 4100,
267
+ } as const;
268
+
269
+ /** Sent by a matchmaker to every player of a match it has just put together. */
270
+ export interface MatchFound {
271
+ /** Frame type. */
272
+ t: "match";
273
+ /** The room to connect to. Only these players can take a seat in it. */
274
+ room: string;
275
+ /** The players of the match, this one included. */
276
+ players: string[];
277
+ /** With a matchmaker that splits matches into sides: the players of each, in order. */
278
+ teams?: string[][];
279
+ }
280
+
281
+ /** What a room puts on its queue when it calls `finishMatch`. */
282
+ export interface MatchResult {
283
+ /** Unique per finished match. Safe to use as a primary key: redeliveries carry the same id. */
284
+ id: string;
285
+ /** The room id, if the room knows it (it does when players came through the gateway). */
286
+ room: string | null;
287
+ /** Epoch milliseconds. */
288
+ endedAt: number;
289
+ /** Everyone who held a seat when the match ended. */
290
+ players: string[];
291
+ /** Whatever the room passed to `finishMatch`. */
292
+ result: Record<string, unknown>;
293
+ /** Where the replay was stored in the room's R2 bucket, if it stored one. */
294
+ replayKey?: string;
295
+ }
296
+
297
+ /** Whether a client should stop reconnecting after this close code. @internal */
298
+ export function isFatalCloseCode(code: number): boolean {
299
+ return code >= 4000 && code < 4100;
300
+ }