tickflare 0.2.0 → 0.3.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.
- package/CHANGELOG.md +11 -0
- package/README.md +3 -2
- package/dist/client/buffered.d.ts +25 -5
- package/dist/client/buffered.js +30 -7
- package/dist/client/buffered.js.map +1 -1
- package/dist/client/index.d.ts +62 -5
- package/dist/client/index.js +193 -14
- package/dist/client/index.js.map +1 -1
- package/dist/client/interpolate.d.ts +6 -5
- package/dist/client/interpolate.js +6 -5
- package/dist/client/interpolate.js.map +1 -1
- package/dist/client/lockstep.d.ts +23 -4
- package/dist/client/lockstep.js +47 -4
- package/dist/client/lockstep.js.map +1 -1
- package/dist/client/predict.js +16 -3
- package/dist/client/predict.js.map +1 -1
- package/dist/lockstep.d.ts +43 -4
- package/dist/lockstep.js +33 -0
- package/dist/lockstep.js.map +1 -1
- package/dist/protocol.d.ts +93 -5
- package/dist/protocol.js +12 -3
- package/dist/protocol.js.map +1 -1
- package/dist/server/gateway.d.ts +5 -0
- package/dist/server/gateway.js +14 -0
- package/dist/server/gateway.js.map +1 -1
- package/dist/server/index.d.ts +4 -3
- package/dist/server/index.js +3 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/lockstep.d.ts +29 -4
- package/dist/server/lockstep.js +72 -5
- package/dist/server/lockstep.js.map +1 -1
- package/dist/server/room.d.ts +118 -1
- package/dist/server/room.js +557 -30
- package/dist/server/room.js.map +1 -1
- package/dist/server/schema.d.ts +67 -0
- package/dist/server/schema.js +44 -0
- package/dist/server/schema.js.map +1 -0
- package/dist/server/spectators.d.ts +74 -0
- package/dist/server/spectators.js +190 -0
- package/dist/server/spectators.js.map +1 -0
- package/dist/server/storage.d.ts +13 -1
- package/dist/server/storage.js +23 -5
- package/dist/server/storage.js.map +1 -1
- package/package.json +1 -1
- package/src/client/buffered.ts +43 -13
- package/src/client/index.ts +237 -14
- package/src/client/interpolate.ts +6 -5
- package/src/client/lockstep.ts +68 -7
- package/src/client/predict.ts +16 -3
- package/src/lockstep.ts +67 -4
- package/src/protocol.ts +94 -4
- package/src/server/gateway.ts +14 -0
- package/src/server/index.ts +6 -0
- package/src/server/lockstep.ts +70 -6
- package/src/server/room.ts +589 -31
- package/src/server/schema.ts +101 -0
- package/src/server/spectators.ts +221 -0
- package/src/server/storage.ts +33 -6
package/src/lockstep.ts
CHANGED
|
@@ -23,10 +23,7 @@ export interface LockstepView<Input = unknown> {
|
|
|
23
23
|
/** What happened in the latest turn, in the order every client must apply it. */
|
|
24
24
|
entries: TurnEntry<Input>[];
|
|
25
25
|
/** Set once two players reported different hashes for the same turn. */
|
|
26
|
-
desync:
|
|
27
|
-
/** The first turn two players' hashes differed on. */
|
|
28
|
-
turn: number;
|
|
29
|
-
} | null;
|
|
26
|
+
desync: Desync | null;
|
|
30
27
|
/**
|
|
31
28
|
* Set to `"players"` by a room whose `turnClock` is `"players"`: the room
|
|
32
29
|
* sleeps between turns, and closes them when a player's client asks.
|
|
@@ -40,6 +37,72 @@ export interface LockstepView<Input = unknown> {
|
|
|
40
37
|
snapshotEvery?: number;
|
|
41
38
|
}
|
|
42
39
|
|
|
40
|
+
/** Two players' simulations were found to differ: when, whose, and, once the room knows, where. */
|
|
41
|
+
export interface Desync {
|
|
42
|
+
/** The first turn two players' hashes differed on. */
|
|
43
|
+
turn: number;
|
|
44
|
+
/**
|
|
45
|
+
* The two players whose hashes differed. With more players in the room,
|
|
46
|
+
* the others agree with one of them or have not reported that turn.
|
|
47
|
+
* Absent in a room saved before this was noted.
|
|
48
|
+
*/
|
|
49
|
+
players?: [string, string];
|
|
50
|
+
/**
|
|
51
|
+
* Where the two simulations differed at that turn: see `differences`.
|
|
52
|
+
* Set once both players' clients have sent the room what they saved then,
|
|
53
|
+
* which they do by themselves when the simulation has `save`. Empty when
|
|
54
|
+
* what they saved is the same, and the difference is in something `save`
|
|
55
|
+
* leaves out. Absent until then, and for good when a client had nothing
|
|
56
|
+
* to send.
|
|
57
|
+
*/
|
|
58
|
+
differs?: DesyncDifference[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** One place where two simulations differed. */
|
|
62
|
+
export interface DesyncDifference {
|
|
63
|
+
/** Where, as a path into what `save` returned: `units.12.x`. Empty when the two differ as a whole. */
|
|
64
|
+
path: string;
|
|
65
|
+
/**
|
|
66
|
+
* What each of the two had there, as JSON text, in the order of
|
|
67
|
+
* `Desync.players`. `undefined` where one had nothing, and cut to a
|
|
68
|
+
* hundred characters where it is long.
|
|
69
|
+
*/
|
|
70
|
+
values: [string, string];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Where two values that JSON can hold differ: the first `limit` places, 8
|
|
75
|
+
* unless said otherwise, in the order of the first one's keys. It goes down
|
|
76
|
+
* to what cannot be taken apart further, so two units that differ in one
|
|
77
|
+
* coordinate are one difference, at `units.12.x`, and not two whole units.
|
|
78
|
+
*
|
|
79
|
+
* It is what a lockstep room uses to say where two players' simulations
|
|
80
|
+
* differed. Use it yourself to compare two saved simulations, in a test
|
|
81
|
+
* that runs the same recording twice for instance.
|
|
82
|
+
*/
|
|
83
|
+
export function differences(a: unknown, b: unknown, limit = 8): DesyncDifference[] {
|
|
84
|
+
const found: DesyncDifference[] = [];
|
|
85
|
+
const show = (value: unknown): string => {
|
|
86
|
+
const text = value === undefined ? "undefined" : (JSON.stringify(value) ?? "undefined");
|
|
87
|
+
return text.length > 100 ? `${text.slice(0, 99)}…` : text;
|
|
88
|
+
};
|
|
89
|
+
const walk = (x: unknown, y: unknown, path: string): void => {
|
|
90
|
+
if (found.length >= limit || x === y) return;
|
|
91
|
+
if (typeof x === "object" && x !== null && typeof y === "object" && y !== null && Array.isArray(x) === Array.isArray(y)) {
|
|
92
|
+
const keys = Array.isArray(x)
|
|
93
|
+
? Array.from({ length: Math.max(x.length, (y as unknown[]).length) }, (_, index) => String(index))
|
|
94
|
+
: [...new Set([...Object.keys(x), ...Object.keys(y)])];
|
|
95
|
+
for (const key of keys) {
|
|
96
|
+
walk((x as Record<string, unknown>)[key], (y as Record<string, unknown>)[key], path === "" ? key : `${path}.${key}`);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
found.push({ path, values: [show(x), show(y)] });
|
|
101
|
+
};
|
|
102
|
+
walk(a, b, "");
|
|
103
|
+
return found;
|
|
104
|
+
}
|
|
105
|
+
|
|
43
106
|
/**
|
|
44
107
|
* What a room with `turnClock = "players"` sends when it closes turns: turns
|
|
45
108
|
* `n` to `n + c - 1`, all empty but the last, which has `e`.
|
package/src/protocol.ts
CHANGED
|
@@ -22,8 +22,13 @@ import type { Patch } from "./patch.js";
|
|
|
22
22
|
* Version 4 adds the room's clock: while the simulation runs, `welcome`
|
|
23
23
|
* says where the clock is (`ms` and `epoch`), and `clock` frames say it
|
|
24
24
|
* again every few seconds, whenever it starts, stops or moves on.
|
|
25
|
+
*
|
|
26
|
+
* Version 5 adds commands that outlive a connection: a client that names
|
|
27
|
+
* itself with `client` in the query is told in `welcome` which of its
|
|
28
|
+
* commands the room has handled (`c`, `from` and `refused`), so that it can
|
|
29
|
+
* send the others again, and the room applies none of them twice.
|
|
25
30
|
*/
|
|
26
|
-
export const PROTOCOL_VERSION =
|
|
31
|
+
export const PROTOCOL_VERSION = 5;
|
|
27
32
|
|
|
28
33
|
/** The keepalive a client sends, as a plain text frame. @internal */
|
|
29
34
|
export const PING = "ping";
|
|
@@ -49,7 +54,13 @@ export interface Player {
|
|
|
49
54
|
export interface CommandFrame<Command = unknown> {
|
|
50
55
|
/** Frame type. */
|
|
51
56
|
t: "cmd";
|
|
52
|
-
/**
|
|
57
|
+
/**
|
|
58
|
+
* Chosen by the client and echoed in the answer. Unique per connection; a
|
|
59
|
+
* client that names itself (`client` in the query, version 5) numbers its
|
|
60
|
+
* commands and leaves 1, 2, 3 and so on for as long as it lives, across
|
|
61
|
+
* connections, and the room takes one whose `id` is not above the latest it
|
|
62
|
+
* has handled for a command it has seen before.
|
|
63
|
+
*/
|
|
53
64
|
id: number;
|
|
54
65
|
/** The command, as the room's `parseCommand` will receive it. */
|
|
55
66
|
data: Command;
|
|
@@ -80,16 +91,90 @@ export interface WelcomeFrame<View = unknown> {
|
|
|
80
91
|
tick: number;
|
|
81
92
|
/** Ticks a second; 0 in event mode. */
|
|
82
93
|
tickRate: number;
|
|
94
|
+
/**
|
|
95
|
+
* How many times a second the room sends what changed while it ticks, when
|
|
96
|
+
* that is less often than it ticks: see `Room.patchRate`. Absent from a
|
|
97
|
+
* room that sends after every tick. Sent whatever version the client asked
|
|
98
|
+
* for: one that does not know it loses nothing but the hint.
|
|
99
|
+
*/
|
|
100
|
+
patchRate?: number;
|
|
83
101
|
/** Version 4, while the simulation runs: as in `ClockFrame`. Absent while it does not. */
|
|
84
102
|
ms?: number;
|
|
85
103
|
/** Version 4, while the simulation runs: as in `ClockFrame`. */
|
|
86
104
|
epoch?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Version 5, to a client that named itself and that the room knows, on a
|
|
107
|
+
* new connection to a seat the player already had: the `id` of the latest
|
|
108
|
+
* command or leave of that client's the room has handled, applied or
|
|
109
|
+
* refused, or 0 for none. `state` shows every one of them that was
|
|
110
|
+
* applied. What the client sent after it never arrived, or was lost when
|
|
111
|
+
* the room went back to its last save: the client sends those again, in
|
|
112
|
+
* order, before anything new.
|
|
113
|
+
*
|
|
114
|
+
* Absent when the room knows nothing of what the client sent before, and
|
|
115
|
+
* the client gives up waiting for it: the seat is newly taken, the room
|
|
116
|
+
* last heard from another client of the same player, the room does not
|
|
117
|
+
* remember commands, or the `welcome` is one sent on a connection that was
|
|
118
|
+
* open already.
|
|
119
|
+
*/
|
|
120
|
+
c?: number;
|
|
121
|
+
/**
|
|
122
|
+
* With `c`, when the room cannot say what became of the client's earliest
|
|
123
|
+
* commands: it can say of those after this `id`, up to `c`, and not of
|
|
124
|
+
* those up to it. A command the client is still waiting to hear about, and
|
|
125
|
+
* whose `id` is not above this, is given up as one whose outcome is not
|
|
126
|
+
* known. Absent when the room can say of every command up to `c`, which is
|
|
127
|
+
* nearly always: see the protocol reference for when it cannot.
|
|
128
|
+
*/
|
|
129
|
+
from?: number;
|
|
130
|
+
/**
|
|
131
|
+
* With `c`: the latest of those commands that were refused, oldest first,
|
|
132
|
+
* each with the reason its `reject` frame gave, cut to 120 characters.
|
|
133
|
+
* Every other command after `from` and up to `c` was applied. The room
|
|
134
|
+
* remembers the last eight refusals of a client, and leaves this out when
|
|
135
|
+
* there are none.
|
|
136
|
+
*/
|
|
137
|
+
refused?: [id: number, reason: string][];
|
|
87
138
|
/** This player's whole view. */
|
|
88
139
|
state: View;
|
|
89
140
|
/** Everyone with a seat, in join order. */
|
|
90
141
|
players: Player[];
|
|
91
142
|
}
|
|
92
143
|
|
|
144
|
+
/**
|
|
145
|
+
* First frame on the connection of a spectator, who asked to watch with
|
|
146
|
+
* `spectate=1`, in place of a `welcome`. It says who they are and how the
|
|
147
|
+
* room runs, and nothing of the game: the spectators' view follows in a
|
|
148
|
+
* `state` frame, at once if other spectators have it already, and when its
|
|
149
|
+
* time has come if the room delays its spectators and this is the first to
|
|
150
|
+
* watch. After that the connection carries that view kept up to date with
|
|
151
|
+
* `state` and `patch` frames, who is here as whole `presence` lists, and
|
|
152
|
+
* what the room broadcasts as `msg` frames, each as long after the players
|
|
153
|
+
* were sent it as `delay` says.
|
|
154
|
+
*
|
|
155
|
+
* A spectator has no seat. Anything they send with an `id` is answered with
|
|
156
|
+
* a `reject` whose reason is `spectator`.
|
|
157
|
+
*/
|
|
158
|
+
export interface SpectatingFrame {
|
|
159
|
+
/** Frame type. */
|
|
160
|
+
t: "spectating";
|
|
161
|
+
/** The version of the protocol the room speaks on this connection. */
|
|
162
|
+
v: number;
|
|
163
|
+
/** The spectator's id. They are not among `players`. */
|
|
164
|
+
you: string;
|
|
165
|
+
/** Ticks a second; 0 in event mode. */
|
|
166
|
+
tickRate: number;
|
|
167
|
+
/** As in `WelcomeFrame`. */
|
|
168
|
+
patchRate?: number;
|
|
169
|
+
/** How far behind the room everything on this connection is, in milliseconds. 0 for not at all. */
|
|
170
|
+
delay: number;
|
|
171
|
+
/**
|
|
172
|
+
* Everyone with a seat, as the spectators have last been told. Empty for
|
|
173
|
+
* the first to watch: a `presence` frame follows with the view.
|
|
174
|
+
*/
|
|
175
|
+
players: Player[];
|
|
176
|
+
}
|
|
177
|
+
|
|
93
178
|
/**
|
|
94
179
|
* The state changed, and this is the player's whole view: sent when the room
|
|
95
180
|
* does not know what the player holds, or when the view is not an object and
|
|
@@ -234,6 +319,7 @@ export interface MessageFrame {
|
|
|
234
319
|
/** Every frame a room sends, apart from the `pong` keepalive. */
|
|
235
320
|
export type ServerMessage<View = unknown> =
|
|
236
321
|
| WelcomeFrame<View>
|
|
322
|
+
| SpectatingFrame
|
|
237
323
|
| StateFrame<View>
|
|
238
324
|
| PatchFrame
|
|
239
325
|
| PresenceFrame
|
|
@@ -247,9 +333,13 @@ export type ServerMessage<View = unknown> =
|
|
|
247
333
|
export const CloseCode = {
|
|
248
334
|
/** The same player opened a newer connection. */
|
|
249
335
|
Replaced: 4000,
|
|
250
|
-
/** Every seat is taken. */
|
|
336
|
+
/** Every seat is taken. For a spectator, with the reason `spectators_full`: as many are watching as may. */
|
|
251
337
|
RoomFull: 4001,
|
|
252
|
-
/**
|
|
338
|
+
/**
|
|
339
|
+
* `onJoin` refused the player; the close reason carries why. For a
|
|
340
|
+
* spectator it is `onSpectate` that refused, or the reason is
|
|
341
|
+
* `no_spectators`: the room takes none.
|
|
342
|
+
*/
|
|
253
343
|
JoinRejected: 4002,
|
|
254
344
|
/** The player no longer has a seat: they left, or the room removed them. The close reason says which. */
|
|
255
345
|
Removed: 4003,
|
package/src/server/gateway.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { Party } from "./tokens.js";
|
|
|
2
2
|
|
|
3
3
|
/** Header the gateway uses to tell a room who is connecting. Never trusted from the client. */
|
|
4
4
|
export const PLAYER_HEADER = "x-tickflare-player";
|
|
5
|
+
/**
|
|
6
|
+
* Header the gateway uses to tell a room who asks to watch it, in place of
|
|
7
|
+
* `PLAYER_HEADER`: see `routeToRoom`. Never trusted from the client.
|
|
8
|
+
*/
|
|
9
|
+
export const SPECTATOR_HEADER = "x-tickflare-spectator";
|
|
5
10
|
/** Header the gateway uses to tell a room the id it is known by. */
|
|
6
11
|
export const ROOM_HEADER = "x-tickflare-room";
|
|
7
12
|
/** Header the gateway uses to tell a room the player's name, URI-encoded. Never trusted from the client. */
|
|
@@ -98,6 +103,14 @@ export async function routeToRoom(
|
|
|
98
103
|
|
|
99
104
|
const headers = identityHeaders(request, identity);
|
|
100
105
|
headers.set(ROOM_HEADER, roomId);
|
|
106
|
+
if (upgrade && new URL(request.url).searchParams.get("spectate") === "1") {
|
|
107
|
+
// Someone who asks to watch is named under a header of their own. Cloudflare can leave a
|
|
108
|
+
// busy room on older code for minutes after the Worker in front of it has the new: a room
|
|
109
|
+
// from before there were spectators knows only the player's header, and would seat whoever
|
|
110
|
+
// it names. Without it, such a room turns the request away, and the client tries again.
|
|
111
|
+
headers.delete(PLAYER_HEADER);
|
|
112
|
+
headers.set(SPECTATOR_HEADER, identity.id);
|
|
113
|
+
}
|
|
101
114
|
|
|
102
115
|
const stub = options.rooms.get(options.rooms.idFromName(roomId));
|
|
103
116
|
return stub.fetch(new Request(request, { headers }));
|
|
@@ -137,6 +150,7 @@ type Checked = { id: string; name?: string; party?: Party };
|
|
|
137
150
|
function identityHeaders(request: Request, identity: Checked): Headers {
|
|
138
151
|
const headers = new Headers(request.headers);
|
|
139
152
|
headers.set(PLAYER_HEADER, identity.id);
|
|
153
|
+
headers.delete(SPECTATOR_HEADER);
|
|
140
154
|
if (identity.name === undefined) headers.delete(NAME_HEADER);
|
|
141
155
|
else headers.set(NAME_HEADER, encodeURIComponent(identity.name));
|
|
142
156
|
if (identity.party === undefined) headers.delete(PARTY_HEADER);
|
package/src/server/index.ts
CHANGED
|
@@ -7,14 +7,18 @@
|
|
|
7
7
|
* @module tickflare
|
|
8
8
|
*/
|
|
9
9
|
export { Room, Rejection, reject, type ActOutcome } from "./room.js";
|
|
10
|
+
export { parseWith, type StandardSchemaIssue, type StandardSchemaResult, type StandardSchemaV1 } from "./schema.js";
|
|
10
11
|
export { plain } from "./tracked.js";
|
|
11
12
|
export { LockstepRoom, type LockstepCommand, type LockstepState } from "./lockstep.js";
|
|
12
13
|
export {
|
|
14
|
+
differences,
|
|
13
15
|
fingerprint,
|
|
14
16
|
runReplay,
|
|
15
17
|
seededRandom,
|
|
16
18
|
stepThrough,
|
|
17
19
|
type CatchUp,
|
|
20
|
+
type Desync,
|
|
21
|
+
type DesyncDifference,
|
|
18
22
|
type LockstepView,
|
|
19
23
|
type TurnsMessage,
|
|
20
24
|
type Replay,
|
|
@@ -57,6 +61,7 @@ export {
|
|
|
57
61
|
routeToRoom,
|
|
58
62
|
NAME_HEADER,
|
|
59
63
|
PLAYER_HEADER,
|
|
64
|
+
SPECTATOR_HEADER,
|
|
60
65
|
type Authenticated,
|
|
61
66
|
type MatchmakerRouteOptions,
|
|
62
67
|
type RoomNamespace,
|
|
@@ -81,6 +86,7 @@ export {
|
|
|
81
86
|
type PresenceFrame,
|
|
82
87
|
type RejectFrame,
|
|
83
88
|
type ServerMessage,
|
|
89
|
+
type SpectatingFrame,
|
|
84
90
|
type StateFrame,
|
|
85
91
|
type WelcomeFrame,
|
|
86
92
|
} from "../protocol.js";
|
package/src/server/lockstep.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { differences, type CatchUp, type Desync, type DesyncDifference, type LockstepView, type Replay, type Snapshot, type TurnEntry, type TurnsMessage } from "../lockstep.js";
|
|
2
2
|
import type { Player } from "../protocol.js";
|
|
3
3
|
import { Room, patchEachTick, reject } from "./room.js";
|
|
4
4
|
import { pieces } from "./storage.js";
|
|
@@ -77,6 +77,15 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
77
77
|
/** Inputs one player may send within a single turn. More are refused. */
|
|
78
78
|
protected maxInputsPerTurn = 32;
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Off in a lockstep room. A turn is recorded as it closes and the room is
|
|
82
|
+
* saved less often, so after a restart the recording holds inputs that the
|
|
83
|
+
* room's own save knows nothing of: one that was sent again would be
|
|
84
|
+
* recorded twice. An input that was in flight when the connection dropped
|
|
85
|
+
* is answered with `disconnected`, as it always was.
|
|
86
|
+
*/
|
|
87
|
+
protected override rememberCommands = false;
|
|
88
|
+
|
|
80
89
|
/**
|
|
81
90
|
* Who keeps time.
|
|
82
91
|
*
|
|
@@ -133,6 +142,8 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
133
142
|
#hashes = new Map<number, { hash: string; by: string }>();
|
|
134
143
|
/** For the latest turns a snapshot could be of: the first hash reported, and everyone who reported that one. */
|
|
135
144
|
#vouched = new Map<number, { hash: string; by: Set<string> }>();
|
|
145
|
+
/** What the players of a desync have sent of their simulations at that turn, as JSON text, until both have. */
|
|
146
|
+
#explanations = new Map<string, string>();
|
|
136
147
|
/** A snapshot that has arrived and waits for the other players to report its hash, and who sent it. */
|
|
137
148
|
#candidate: (Snapshot & { by: string }) | undefined;
|
|
138
149
|
/** Names this run of the room: see `CatchUp.epoch`. */
|
|
@@ -150,8 +161,21 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
150
161
|
*/
|
|
151
162
|
protected abstract parseInput(raw: unknown): Input;
|
|
152
163
|
|
|
153
|
-
/**
|
|
154
|
-
|
|
164
|
+
/**
|
|
165
|
+
* Two players reported different hashes for `turn`: their simulations have
|
|
166
|
+
* diverged. `players` are the two. Where they differ is not known yet: see
|
|
167
|
+
* `onDesyncExplained`.
|
|
168
|
+
*/
|
|
169
|
+
protected onDesync(_turn: number, _players: [string, string]): void {}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Both players of a desync have sent what their simulations had saved at
|
|
173
|
+
* that turn, and the room has compared the two: `desync.differs` says
|
|
174
|
+
* where they differed. It is in the view too, so every client is told.
|
|
175
|
+
* The place to log it. Not called when a client had nothing to send: a
|
|
176
|
+
* simulation without `save`, or a player who was gone by then.
|
|
177
|
+
*/
|
|
178
|
+
protected onDesyncExplained(_desync: Desync & { players: [string, string]; differs: DesyncDifference[] }): void {}
|
|
155
179
|
|
|
156
180
|
protected initialState(): LockstepState<Input> {
|
|
157
181
|
const seed = crypto.getRandomValues(new Uint32Array(1))[0]!;
|
|
@@ -302,9 +326,9 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
302
326
|
}
|
|
303
327
|
} else if (first.by !== player.id && first.hash !== command.h && !this.state.desync) {
|
|
304
328
|
// It takes two players to disagree: one changing their mind proves nothing.
|
|
305
|
-
this.state.desync = { turn: command.n };
|
|
329
|
+
this.state.desync = { turn: command.n, players: [first.by, player.id] };
|
|
306
330
|
this.#candidate = undefined; // Nobody can say which simulation a snapshot should be of.
|
|
307
|
-
this.onDesync(command.n);
|
|
331
|
+
this.onDesync(command.n, [first.by, player.id]);
|
|
308
332
|
}
|
|
309
333
|
}
|
|
310
334
|
|
|
@@ -489,6 +513,41 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
489
513
|
this.#event({ player: player.id, event: "reconnect" });
|
|
490
514
|
}
|
|
491
515
|
|
|
516
|
+
/**
|
|
517
|
+
* One of the two players of a desync sends what their simulation had saved
|
|
518
|
+
* at that turn. When both have, the room says where the two differ.
|
|
519
|
+
*/
|
|
520
|
+
async #explain(request: Request, params: URLSearchParams, player: Player): Promise<Response> {
|
|
521
|
+
const waiting = (): Desync | undefined => {
|
|
522
|
+
const desync = this.state.desync;
|
|
523
|
+
return desync?.players?.includes(player.id) && desync.differs === undefined && Number(params.get("turn")) === desync.turn
|
|
524
|
+
? desync
|
|
525
|
+
: undefined;
|
|
526
|
+
};
|
|
527
|
+
// Checked before the body is read, and again after: the other player's may have settled it.
|
|
528
|
+
if (!waiting()) return new Response("Nothing this room is waiting for", { status: 409 });
|
|
529
|
+
const data = await textUpTo(request, this.maxSnapshotLength * 3);
|
|
530
|
+
if (data === undefined || data.length > this.maxSnapshotLength) return new Response("Too long", { status: 413 });
|
|
531
|
+
try {
|
|
532
|
+
JSON.parse(data);
|
|
533
|
+
} catch {
|
|
534
|
+
return new Response("Not JSON", { status: 400 });
|
|
535
|
+
}
|
|
536
|
+
const desync = waiting();
|
|
537
|
+
if (!desync) return new Response("Nothing this room is waiting for", { status: 409 });
|
|
538
|
+
this.#explanations.set(player.id, data);
|
|
539
|
+
const [one, other] = desync.players!.map((id) => this.#explanations.get(id));
|
|
540
|
+
if (one === undefined || other === undefined) return new Response("Held until the other player's arrives", { status: 202 });
|
|
541
|
+
this.#explanations.clear();
|
|
542
|
+
const differs = differences(JSON.parse(one), JSON.parse(other));
|
|
543
|
+
await this.act(() => {
|
|
544
|
+
const explained = { ...desync, players: desync.players!, differs };
|
|
545
|
+
this.state.desync = explained;
|
|
546
|
+
this.onDesyncExplained(explained);
|
|
547
|
+
});
|
|
548
|
+
return new Response("Compared", { status: 201 });
|
|
549
|
+
}
|
|
550
|
+
|
|
492
551
|
/**
|
|
493
552
|
* Closes the turn: whatever arrived since the last one becomes this turn's
|
|
494
553
|
* entries. A turn with something in it is one row in the recording, written
|
|
@@ -530,7 +589,9 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
530
589
|
|
|
531
590
|
/**
|
|
532
591
|
* `GET …/replay` is the recording, or the end of it for a client that is
|
|
533
|
-
* catching up. `POST …/snapshot` is a player's client sending a snapshot
|
|
592
|
+
* catching up. `POST …/snapshot` is a player's client sending a snapshot,
|
|
593
|
+
* and `POST …/desync` one sending what it had saved at a turn two players
|
|
594
|
+
* disagreed on.
|
|
534
595
|
* If you override this, pass on what is not yours with `super.onHttp`.
|
|
535
596
|
*/
|
|
536
597
|
protected override onHttp(request: Request, player: Player): Response | Promise<Response> {
|
|
@@ -541,6 +602,9 @@ export abstract class LockstepRoom<Input, Env = Cloudflare.Env> extends Room<
|
|
|
541
602
|
if (request.method === "POST" && url.pathname.endsWith("/snapshot")) {
|
|
542
603
|
return this.#offered(request, url.searchParams, player);
|
|
543
604
|
}
|
|
605
|
+
if (request.method === "POST" && url.pathname.endsWith("/desync")) {
|
|
606
|
+
return this.#explain(request, url.searchParams, player);
|
|
607
|
+
}
|
|
544
608
|
return new Response("Not found", { status: 404 });
|
|
545
609
|
}
|
|
546
610
|
|