tickflare 0.0.0-stage → 0.1.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 +233 -0
- package/LICENSE +21 -0
- package/README.md +110 -2
- package/dist/binary.d.ts +91 -0
- package/dist/binary.js +466 -0
- package/dist/binary.js.map +1 -0
- package/dist/client/accounts.d.ts +193 -0
- package/dist/client/accounts.js +306 -0
- package/dist/client/accounts.js.map +1 -0
- package/dist/client/draft.d.ts +17 -0
- package/dist/client/draft.js +133 -0
- package/dist/client/draft.js.map +1 -0
- package/dist/client/index.d.ts +161 -0
- package/dist/client/index.js +436 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/interpolate.d.ts +31 -0
- package/dist/client/interpolate.js +59 -0
- package/dist/client/interpolate.js.map +1 -0
- package/dist/client/lockstep.d.ts +86 -0
- package/dist/client/lockstep.js +339 -0
- package/dist/client/lockstep.js.map +1 -0
- package/dist/client/matchmaking.d.ts +21 -0
- package/dist/client/matchmaking.js +55 -0
- package/dist/client/matchmaking.js.map +1 -0
- package/dist/client/pacer.d.ts +22 -0
- package/dist/client/pacer.js +65 -0
- package/dist/client/pacer.js.map +1 -0
- package/dist/client/predict.d.ts +75 -0
- package/dist/client/predict.js +203 -0
- package/dist/client/predict.js.map +1 -0
- package/dist/client/token.d.ts +10 -0
- package/dist/client/token.js +15 -0
- package/dist/client/token.js.map +1 -0
- package/dist/lockstep.d.ts +154 -0
- package/dist/lockstep.js +140 -0
- package/dist/lockstep.js.map +1 -0
- package/dist/patch.d.ts +54 -0
- package/dist/patch.js +186 -0
- package/dist/patch.js.map +1 -0
- package/dist/protocol.d.ts +230 -0
- package/dist/protocol.js +50 -0
- package/dist/protocol.js.map +1 -0
- package/dist/server/accounts.d.ts +320 -0
- package/dist/server/accounts.js +1223 -0
- package/dist/server/accounts.js.map +1 -0
- package/dist/server/delta.d.ts +50 -0
- package/dist/server/delta.js +426 -0
- package/dist/server/delta.js.map +1 -0
- package/dist/server/frames.d.ts +15 -0
- package/dist/server/frames.js +23 -0
- package/dist/server/frames.js.map +1 -0
- package/dist/server/gateway.d.ts +80 -0
- package/dist/server/gateway.js +144 -0
- package/dist/server/gateway.js.map +1 -0
- package/dist/server/index.d.ts +21 -0
- package/dist/server/index.js +22 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/limiter.d.ts +12 -0
- package/dist/server/limiter.js +43 -0
- package/dist/server/limiter.js.map +1 -0
- package/dist/server/lockstep.d.ts +126 -0
- package/dist/server/lockstep.js +544 -0
- package/dist/server/lockstep.js.map +1 -0
- package/dist/server/matches.d.ts +31 -0
- package/dist/server/matches.js +69 -0
- package/dist/server/matches.js.map +1 -0
- package/dist/server/matchmaker.d.ts +121 -0
- package/dist/server/matchmaker.js +479 -0
- package/dist/server/matchmaker.js.map +1 -0
- package/dist/server/room.d.ts +383 -0
- package/dist/server/room.js +1766 -0
- package/dist/server/room.js.map +1 -0
- package/dist/server/signals.d.ts +18 -0
- package/dist/server/signals.js +37 -0
- package/dist/server/signals.js.map +1 -0
- package/dist/server/storage.d.ts +117 -0
- package/dist/server/storage.js +330 -0
- package/dist/server/storage.js.map +1 -0
- package/dist/server/tokens.d.ts +74 -0
- package/dist/server/tokens.js +117 -0
- package/dist/server/tokens.js.map +1 -0
- package/dist/server/tracked.d.ts +47 -0
- package/dist/server/tracked.js +270 -0
- package/dist/server/tracked.js.map +1 -0
- package/dist/world.d.ts +20 -0
- package/dist/world.js +21 -0
- package/dist/world.js.map +1 -0
- package/package.json +74 -4
- package/src/binary.ts +496 -0
- package/src/client/accounts.ts +423 -0
- package/src/client/draft.ts +138 -0
- package/src/client/index.ts +573 -0
- package/src/client/interpolate.ts +60 -0
- package/src/client/lockstep.ts +398 -0
- package/src/client/matchmaking.ts +61 -0
- package/src/client/pacer.ts +67 -0
- package/src/client/predict.ts +239 -0
- package/src/client/token.ts +22 -0
- package/src/lockstep.ts +285 -0
- package/src/patch.ts +188 -0
- package/src/protocol.ts +260 -0
- package/src/server/accounts.ts +1474 -0
- package/src/server/delta.ts +467 -0
- package/src/server/frames.ts +23 -0
- package/src/server/gateway.ts +213 -0
- package/src/server/index.ts +85 -0
- package/src/server/limiter.ts +41 -0
- package/src/server/lockstep.ts +611 -0
- package/src/server/matches.ts +99 -0
- package/src/server/matchmaker.ts +537 -0
- package/src/server/room.ts +1867 -0
- package/src/server/signals.ts +46 -0
- package/src/server/storage.ts +392 -0
- package/src/server/tokens.ts +184 -0
- package/src/server/tracked.ts +260 -0
- package/src/world.ts +29 -0
- package/tools/rooms-report.mjs +168 -0
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
import { Room, reject } from "./room.js";
|
|
2
|
+
import { pieces } from "./storage.js";
|
|
3
|
+
/** How many turns' worth of reported hashes are kept for comparison. */
|
|
4
|
+
const HASHES_KEPT = 128;
|
|
5
|
+
/** How many snapshot turns' hashes are kept, for a snapshot that takes a while to arrive. */
|
|
6
|
+
const SNAPSHOT_TURNS_KEPT = 4;
|
|
7
|
+
/**
|
|
8
|
+
* With players asking for the turns: the most one request may close. If
|
|
9
|
+
* nobody asked for longer than this many turns, the game stood still for the
|
|
10
|
+
* rest of that time.
|
|
11
|
+
*/
|
|
12
|
+
const MOST_TURNS_AT_ONCE = 30;
|
|
13
|
+
/**
|
|
14
|
+
* A lockstep room. It never runs the game: it validates inputs, orders them
|
|
15
|
+
* into turns at `tickRate` turns a second, records every turn in which
|
|
16
|
+
* something happened, and sends all players the same stream. Players coming
|
|
17
|
+
* and going are part of that stream, so every simulation sees them at the
|
|
18
|
+
* same turn.
|
|
19
|
+
*
|
|
20
|
+
* The recording is served at `{prefix}/{roomId}/replay`. Clients read it when
|
|
21
|
+
* they (re)connect, to catch up, and it doubles as the match replay.
|
|
22
|
+
*
|
|
23
|
+
* By default the room closes the turns itself, on a timer, and so is awake
|
|
24
|
+
* and billed for as long as the match lasts. Set `turnClock = "players"` and
|
|
25
|
+
* it sleeps between turns instead: see `turnClock`.
|
|
26
|
+
*
|
|
27
|
+
* If you override `onJoin`, `onDisconnect` or `onReconnect`, call `super` so
|
|
28
|
+
* the event still reaches the turn stream. A seat that is given up, whether
|
|
29
|
+
* the player left, timed out or was removed with `removePlayer`, is
|
|
30
|
+
* announced to the stream for you.
|
|
31
|
+
*/
|
|
32
|
+
export class LockstepRoom extends Room {
|
|
33
|
+
/** Turns per second. */
|
|
34
|
+
tickRate = 10;
|
|
35
|
+
/** Inputs one player may send within a single turn. More are refused. */
|
|
36
|
+
maxInputsPerTurn = 32;
|
|
37
|
+
/**
|
|
38
|
+
* Who keeps time.
|
|
39
|
+
*
|
|
40
|
+
* `"room"`: the room closes a turn every `1 / tickRate` seconds on a timer
|
|
41
|
+
* of its own. It is awake for the whole match.
|
|
42
|
+
*
|
|
43
|
+
* `"players"`: the room has no timer. `LockstepClient` asks it for each
|
|
44
|
+
* turn as it falls due, one player's client at a time, and the room closes
|
|
45
|
+
* as many turns as its own clock says have passed, never more: a player
|
|
46
|
+
* can neither speed the game up nor hold it back while anyone else is
|
|
47
|
+
* connected. Between requests the room is asleep, which is most of the
|
|
48
|
+
* time. Orders take effect as soon as they do with `"room"`. Two things
|
|
49
|
+
* differ: the game stands still while nobody is connected to ask, and after
|
|
50
|
+
* a restart it skips ahead by up to `saveIntervalMs` of empty turns instead
|
|
51
|
+
* of going back by as much.
|
|
52
|
+
*/
|
|
53
|
+
turnClock = "room";
|
|
54
|
+
/**
|
|
55
|
+
* How often the room takes a snapshot of the simulation from its players,
|
|
56
|
+
* so that someone who joins, reloads or reconnects late starts from it and
|
|
57
|
+
* not from turn 1. 0 keeps none.
|
|
58
|
+
*
|
|
59
|
+
* The room has no simulation of its own. A snapshot is what one player's
|
|
60
|
+
* client saved, with `save`, at a turn every client agrees on, and it is
|
|
61
|
+
* kept once the hash it came with is the one another connected player
|
|
62
|
+
* reported for that turn; a player who is alone is taken at their word.
|
|
63
|
+
* The client that loads it hashes the result again before trusting it.
|
|
64
|
+
* Clients whose simulation has no `save`, `load` and `hash` send none and
|
|
65
|
+
* load none, and catch up from the recording as before.
|
|
66
|
+
*
|
|
67
|
+
* Only the latest is kept. The recording is not shortened: it is still the
|
|
68
|
+
* whole match.
|
|
69
|
+
*/
|
|
70
|
+
snapshotIntervalMs = 60_000;
|
|
71
|
+
/** The longest snapshot the room keeps, in characters of JSON. A longer one is refused. */
|
|
72
|
+
maxSnapshotLength = 8_000_000;
|
|
73
|
+
#resumed = false;
|
|
74
|
+
// With players asking for the turns, none of this is in `state`: a change to the state is a
|
|
75
|
+
// row written and a frame to everyone, and here turns change ten times a second.
|
|
76
|
+
/** The latest turn. `state.turn` is a checkpoint of it, written every `saveIntervalMs`. */
|
|
77
|
+
#turn = 0;
|
|
78
|
+
/** What happened in the latest turn. */
|
|
79
|
+
#entries = [];
|
|
80
|
+
/** Inputs and events waiting for the next turn. */
|
|
81
|
+
#waiting = [];
|
|
82
|
+
/** A moment and the turn that was current then: what "due" is counted from. */
|
|
83
|
+
#since;
|
|
84
|
+
/** Turns to pass over at the next close, so that no number a player may have seen is used again. */
|
|
85
|
+
#skip = 0;
|
|
86
|
+
/** The first hash reported for each recent turn, and who reported it. */
|
|
87
|
+
#hashes = new Map();
|
|
88
|
+
/** For the latest turns a snapshot could be of: the first hash reported, and everyone who reported that one. */
|
|
89
|
+
#vouched = new Map();
|
|
90
|
+
/** A snapshot that has arrived and waits for the other players to report its hash, and who sent it. */
|
|
91
|
+
#candidate;
|
|
92
|
+
/** Names this run of the room: see `CatchUp.epoch`. */
|
|
93
|
+
#epoch;
|
|
94
|
+
/** Two players reported different hashes for `turn`: their simulations have diverged. */
|
|
95
|
+
onDesync(_turn) { }
|
|
96
|
+
initialState() {
|
|
97
|
+
const seed = crypto.getRandomValues(new Uint32Array(1))[0];
|
|
98
|
+
return { seed, turn: 0, entries: [], pending: [], desync: null };
|
|
99
|
+
}
|
|
100
|
+
parseCommand(raw) {
|
|
101
|
+
const frame = raw;
|
|
102
|
+
if (frame?.k === "t")
|
|
103
|
+
return { k: "t" };
|
|
104
|
+
if (frame?.k === "i")
|
|
105
|
+
return { k: "i", d: this.parseInput(frame.d) };
|
|
106
|
+
if (frame?.k === "h" &&
|
|
107
|
+
Number.isSafeInteger(frame.n) &&
|
|
108
|
+
typeof frame.h === "string" &&
|
|
109
|
+
frame.h.length <= 64) {
|
|
110
|
+
return { k: "h", n: frame.n, h: frame.h };
|
|
111
|
+
}
|
|
112
|
+
return reject("invalid_command");
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Squares the saved state with the recording, once, before this instance
|
|
116
|
+
* adds anything to `pending`.
|
|
117
|
+
*
|
|
118
|
+
* A turn is written to the recording as it closes; the room itself is
|
|
119
|
+
* saved less often. So after a restart the recording can be ahead of the
|
|
120
|
+
* saved state. Whatever was waiting when that state was saved went into
|
|
121
|
+
* the first turn that closed after it: if the recording has a later turn,
|
|
122
|
+
* those entries are in it already, and are dropped here instead of being
|
|
123
|
+
* recorded twice.
|
|
124
|
+
*/
|
|
125
|
+
#resume() {
|
|
126
|
+
if (this.#resumed)
|
|
127
|
+
return;
|
|
128
|
+
this.#resumed = true;
|
|
129
|
+
const last = this.#recorded()
|
|
130
|
+
? this.ctx.storage.sql
|
|
131
|
+
.exec("SELECT n, entries FROM tickflare_turns ORDER BY n DESC LIMIT 1")
|
|
132
|
+
.toArray()[0]
|
|
133
|
+
: undefined;
|
|
134
|
+
if (last && last.n > this.state.turn) {
|
|
135
|
+
this.state.turn = last.n; // Never reuse a number.
|
|
136
|
+
this.state.entries = JSON.parse(last.entries);
|
|
137
|
+
this.state.pending = [];
|
|
138
|
+
}
|
|
139
|
+
// A snapshot is of a turn that has happened. If the saved counter is behind it, every turn in
|
|
140
|
+
// between was empty, or the recording would have it: carry on from the snapshot's.
|
|
141
|
+
const kept = this.#kept();
|
|
142
|
+
if (kept && kept.turn > this.state.turn) {
|
|
143
|
+
this.state.turn = kept.turn;
|
|
144
|
+
this.state.entries = [];
|
|
145
|
+
}
|
|
146
|
+
if (this.turnClock === "players") {
|
|
147
|
+
this.#turn = this.state.turn;
|
|
148
|
+
this.#entries = last?.n === this.#turn ? JSON.parse(last.entries) : [];
|
|
149
|
+
this.state.entries = [];
|
|
150
|
+
// Turns closed since the checkpoint were only in memory, and players still connected have
|
|
151
|
+
// seen them. There were fewer than a checkpoint's worth: pass over that many.
|
|
152
|
+
if (this.#turn > 0)
|
|
153
|
+
this.#skip = this.#checkpointEvery();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** With players asking for the turns: how many turns go by between checkpoints of the counter. */
|
|
157
|
+
#checkpointEvery() {
|
|
158
|
+
return Math.max(1, Math.round((this.saveIntervalMs * this.tickRate) / 1000));
|
|
159
|
+
}
|
|
160
|
+
/** Queues an input or an event for the next turn. */
|
|
161
|
+
#add(entry) {
|
|
162
|
+
this.#resume();
|
|
163
|
+
if (this.turnClock === "players")
|
|
164
|
+
this.#waiting.push(entry);
|
|
165
|
+
else
|
|
166
|
+
this.state.pending.push(entry);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* With players asking for the turns: closes the turns that are due by this
|
|
170
|
+
* room's clock, and puts what was waiting into the last of them. `now`
|
|
171
|
+
* closes one even if none is due, for something that must not wait in
|
|
172
|
+
* memory: a player coming or going is recorded in the same breath as the
|
|
173
|
+
* change of seats.
|
|
174
|
+
*/
|
|
175
|
+
#close(now, by) {
|
|
176
|
+
this.#resume();
|
|
177
|
+
const time = Date.now();
|
|
178
|
+
const turnMs = 1000 / this.tickRate;
|
|
179
|
+
// The first request after a (re)start finds one turn due.
|
|
180
|
+
this.#since ??= { at: time - turnMs, turn: this.#turn };
|
|
181
|
+
// Half a turn early is on time: requests arrive a little before and a little after the beat.
|
|
182
|
+
let count = Math.floor(this.#since.turn + (time - this.#since.at) / turnMs - this.#turn + 0.5);
|
|
183
|
+
const stoodStill = count > MOST_TURNS_AT_ONCE;
|
|
184
|
+
if (stoodStill)
|
|
185
|
+
count = MOST_TURNS_AT_ONCE;
|
|
186
|
+
if (count < 1) {
|
|
187
|
+
if (!now)
|
|
188
|
+
return;
|
|
189
|
+
count = 1;
|
|
190
|
+
}
|
|
191
|
+
// Turns passed over after a restart are not turns the clock is owed back.
|
|
192
|
+
count += this.#skip;
|
|
193
|
+
this.#since.turn += this.#skip;
|
|
194
|
+
this.#skip = 0;
|
|
195
|
+
const first = this.#turn + 1;
|
|
196
|
+
const last = this.#turn + count;
|
|
197
|
+
const entries = this.#waiting;
|
|
198
|
+
if (entries.length > 0) {
|
|
199
|
+
this.#turns().exec("INSERT OR REPLACE INTO tickflare_turns (n, entries) VALUES (?, ?)", last, JSON.stringify(entries));
|
|
200
|
+
}
|
|
201
|
+
this.#waiting = [];
|
|
202
|
+
this.#entries = entries;
|
|
203
|
+
this.#turn = last;
|
|
204
|
+
if (stoodStill)
|
|
205
|
+
this.#since = { at: time, turn: last };
|
|
206
|
+
if (last - this.state.turn >= this.#checkpointEvery())
|
|
207
|
+
this.state.turn = last;
|
|
208
|
+
this.broadcast({ tickflare: "turns", n: first, c: count, e: entries, ...(by === undefined ? {} : { b: by }) });
|
|
209
|
+
}
|
|
210
|
+
/** The room keeps time only when it is asked to: with players asking, it has no simulation to wake for. */
|
|
211
|
+
shouldTick() {
|
|
212
|
+
return this.turnClock === "room";
|
|
213
|
+
}
|
|
214
|
+
onCommand(player, command) {
|
|
215
|
+
this.#resume();
|
|
216
|
+
const clocked = this.turnClock === "players";
|
|
217
|
+
if (command.k === "t") {
|
|
218
|
+
// A room that keeps its own time has nothing to be asked for.
|
|
219
|
+
if (clocked)
|
|
220
|
+
this.#close(false, player.id);
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (command.k === "i") {
|
|
224
|
+
const queue = clocked ? this.#waiting : this.state.pending;
|
|
225
|
+
const waiting = queue.filter((entry) => entry.player === player.id && entry.input !== undefined);
|
|
226
|
+
if (waiting.length >= this.maxInputsPerTurn)
|
|
227
|
+
reject("too_many_inputs");
|
|
228
|
+
this.#add({ player: player.id, input: command.d });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
// Only turns that have happened, and recently enough to still be compared.
|
|
232
|
+
const turn = clocked ? this.#turn : this.state.turn;
|
|
233
|
+
if (command.n > turn)
|
|
234
|
+
return;
|
|
235
|
+
this.#vouch(player.id, command.n, command.h, turn);
|
|
236
|
+
if (command.n <= turn - HASHES_KEPT)
|
|
237
|
+
return;
|
|
238
|
+
const first = this.#hashes.get(command.n);
|
|
239
|
+
if (first === undefined) {
|
|
240
|
+
this.#hashes.set(command.n, { hash: command.h, by: player.id });
|
|
241
|
+
if (this.#hashes.size > HASHES_KEPT) {
|
|
242
|
+
for (const n of this.#hashes.keys()) {
|
|
243
|
+
if (n <= turn - HASHES_KEPT)
|
|
244
|
+
this.#hashes.delete(n);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
else if (first.by !== player.id && first.hash !== command.h && !this.state.desync) {
|
|
249
|
+
// It takes two players to disagree: one changing their mind proves nothing.
|
|
250
|
+
this.state.desync = { turn: command.n };
|
|
251
|
+
this.#candidate = undefined; // Nobody can say which simulation a snapshot should be of.
|
|
252
|
+
this.onDesync(command.n);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/** Turns between snapshots, or 0 when the room keeps none. */
|
|
256
|
+
#snapshotEvery() {
|
|
257
|
+
const turns = (this.snapshotIntervalMs * this.tickRate) / 1000;
|
|
258
|
+
return turns > 0 && Number.isFinite(turns) ? Math.max(1, Math.round(turns)) : 0;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Notes that `playerId` reported `hash` for `turn`, if a snapshot could be
|
|
262
|
+
* of that turn, and keeps the snapshot that was waiting for it.
|
|
263
|
+
*/
|
|
264
|
+
#vouch(playerId, turn, hash, latest) {
|
|
265
|
+
const every = this.#snapshotEvery();
|
|
266
|
+
if (every === 0 || turn <= 0 || turn % every !== 0 || turn <= latest - every * SNAPSHOT_TURNS_KEPT)
|
|
267
|
+
return;
|
|
268
|
+
let seen = this.#vouched.get(turn);
|
|
269
|
+
if (seen === undefined) {
|
|
270
|
+
seen = { hash, by: new Set() };
|
|
271
|
+
this.#vouched.set(turn, seen);
|
|
272
|
+
for (const n of this.#vouched.keys()) {
|
|
273
|
+
if (n <= latest - every * SNAPSHOT_TURNS_KEPT)
|
|
274
|
+
this.#vouched.delete(n);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (seen.hash === hash)
|
|
278
|
+
seen.by.add(playerId);
|
|
279
|
+
const candidate = this.#candidate;
|
|
280
|
+
if (candidate?.turn === turn && this.#agreed(candidate, candidate.by)) {
|
|
281
|
+
this.#candidate = undefined;
|
|
282
|
+
this.#keep(candidate);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Whether a snapshot sent by `by` is one to keep: every other player who
|
|
287
|
+
* is connected has reported the hash it came with. A player with the room
|
|
288
|
+
* to themselves has nobody to be wrong about it.
|
|
289
|
+
*/
|
|
290
|
+
#agreed(snapshot, by) {
|
|
291
|
+
const seen = this.#vouched.get(snapshot.turn);
|
|
292
|
+
if (seen === undefined || seen.hash !== snapshot.hash)
|
|
293
|
+
return false;
|
|
294
|
+
const others = this.players.filter((player) => player.id !== by);
|
|
295
|
+
if (others.length === 0)
|
|
296
|
+
return true;
|
|
297
|
+
const connected = others.filter((player) => player.connected);
|
|
298
|
+
return connected.length > 0 && connected.every((player) => seen.by.has(player.id));
|
|
299
|
+
}
|
|
300
|
+
/** Stores a snapshot in place of the one before it. One row, or several for a long one. */
|
|
301
|
+
#keep(snapshot) {
|
|
302
|
+
this.ctx.storage.transactionSync(() => {
|
|
303
|
+
const sql = this.ctx.storage.sql;
|
|
304
|
+
sql.exec("CREATE TABLE IF NOT EXISTS tickflare_snapshot (part INTEGER PRIMARY KEY, turn INTEGER NOT NULL, hash TEXT NOT NULL, data TEXT NOT NULL)");
|
|
305
|
+
const parts = pieces(snapshot.data);
|
|
306
|
+
parts.forEach((piece, part) => {
|
|
307
|
+
sql.exec("INSERT OR REPLACE INTO tickflare_snapshot (part, turn, hash, data) VALUES (?, ?, ?, ?)", part, snapshot.turn, snapshot.hash, piece);
|
|
308
|
+
});
|
|
309
|
+
sql.exec("DELETE FROM tickflare_snapshot WHERE part >= ?", parts.length);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
/** The turn and hash of the snapshot the room has, if it has one. */
|
|
313
|
+
#kept() {
|
|
314
|
+
const sql = this.ctx.storage.sql;
|
|
315
|
+
if (sql.exec("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'tickflare_snapshot'").toArray().length === 0) {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
return sql.exec("SELECT turn, hash FROM tickflare_snapshot WHERE part = 0").toArray()[0];
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* A player's client sends the simulation it saved at `turn`. Kept at once
|
|
322
|
+
* if the other players have reported the same hash already, and held until
|
|
323
|
+
* they do otherwise.
|
|
324
|
+
*/
|
|
325
|
+
async #offered(request, params, player) {
|
|
326
|
+
const every = this.#snapshotEvery();
|
|
327
|
+
if (every === 0)
|
|
328
|
+
return new Response("Not found", { status: 404 });
|
|
329
|
+
if (!this.players.some((seated) => seated.id === player.id && seated.connected)) {
|
|
330
|
+
return new Response("Only a connected player can send a snapshot", { status: 403 });
|
|
331
|
+
}
|
|
332
|
+
const turn = Number(params.get("turn"));
|
|
333
|
+
const hash = params.get("hash") ?? "";
|
|
334
|
+
if (!Number.isSafeInteger(turn) || turn <= 0 || turn % every !== 0 || hash === "" || hash.length > 64) {
|
|
335
|
+
return new Response("Not a snapshot turn, or no hash", { status: 400 });
|
|
336
|
+
}
|
|
337
|
+
// Checked before the body is read, so that one of no use costs nothing, and again after:
|
|
338
|
+
// the room moves on while it waits.
|
|
339
|
+
const useless = () => {
|
|
340
|
+
const latest = Math.max(this.state.turn, this.#turn);
|
|
341
|
+
const seen = this.#vouched.get(turn);
|
|
342
|
+
return (!this.#resumed || // This run of the room has closed no turn yet: it cannot tell what has happened.
|
|
343
|
+
this.state.desync !== null ||
|
|
344
|
+
turn > latest ||
|
|
345
|
+
turn <= latest - every * SNAPSHOT_TURNS_KEPT ||
|
|
346
|
+
turn <= (this.#kept()?.turn ?? 0) ||
|
|
347
|
+
(this.#candidate !== undefined && turn <= this.#candidate.turn) ||
|
|
348
|
+
(seen !== undefined && seen.hash !== hash));
|
|
349
|
+
};
|
|
350
|
+
if (useless())
|
|
351
|
+
return new Response("Not a snapshot this room can use", { status: 409 });
|
|
352
|
+
// A character of JSON is at most three bytes on the wire.
|
|
353
|
+
const data = await textUpTo(request, this.maxSnapshotLength * 3);
|
|
354
|
+
if (data === undefined || data.length > this.maxSnapshotLength)
|
|
355
|
+
return new Response("Snapshot too long", { status: 413 });
|
|
356
|
+
if (data.length === 0)
|
|
357
|
+
return new Response("Empty snapshot", { status: 400 });
|
|
358
|
+
if (useless())
|
|
359
|
+
return new Response("Not a snapshot this room can use", { status: 409 });
|
|
360
|
+
const latest = Math.max(this.state.turn, this.#turn);
|
|
361
|
+
this.#candidate = { turn, hash, data, by: player.id };
|
|
362
|
+
this.#vouch(player.id, turn, hash, latest);
|
|
363
|
+
return this.#candidate === undefined
|
|
364
|
+
? new Response("Kept", { status: 201 })
|
|
365
|
+
: new Response("Held until the other players report the same hash", { status: 202 });
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* What a client that is catching up is sent. One that says which turn its
|
|
369
|
+
* simulation is at, and got there in this run of the room, is sent the
|
|
370
|
+
* turns after it; one that has fallen a snapshot or more behind, or has no
|
|
371
|
+
* simulation, is sent the snapshot and the turns after that.
|
|
372
|
+
*/
|
|
373
|
+
#catchUp(params) {
|
|
374
|
+
if (!params.has("since"))
|
|
375
|
+
return this.replay();
|
|
376
|
+
const epoch = (this.#epoch ??= crypto.randomUUID().slice(0, 8));
|
|
377
|
+
const since = Number(params.get("since"));
|
|
378
|
+
const latest = Math.max(this.state.turn, this.#turn);
|
|
379
|
+
let from = params.get("epoch") === epoch && Number.isSafeInteger(since) && since > 0 && since <= latest ? since : 0;
|
|
380
|
+
// `snapshot=N`: the client can load one, and has no use for any of turn N or before.
|
|
381
|
+
const newerThan = params.has("snapshot") ? Number(params.get("snapshot")) : Number.POSITIVE_INFINITY;
|
|
382
|
+
const kept = this.#kept();
|
|
383
|
+
let snapshot;
|
|
384
|
+
if (kept && kept.turn > newerThan && (from === 0 || kept.turn - from > this.#snapshotEvery())) {
|
|
385
|
+
const data = this.ctx.storage.sql
|
|
386
|
+
.exec("SELECT data FROM tickflare_snapshot ORDER BY part")
|
|
387
|
+
.toArray()
|
|
388
|
+
.map((row) => row.data)
|
|
389
|
+
.join("");
|
|
390
|
+
snapshot = { ...kept, data };
|
|
391
|
+
from = kept.turn;
|
|
392
|
+
}
|
|
393
|
+
const recording = this.#recording(from);
|
|
394
|
+
return {
|
|
395
|
+
...recording,
|
|
396
|
+
// Before this run of the room has closed a turn, a snapshot can be ahead of the saved counter.
|
|
397
|
+
turn: Math.max(recording.turn, from, kept?.turn ?? 0),
|
|
398
|
+
epoch,
|
|
399
|
+
...(from > 0 ? { from } : {}),
|
|
400
|
+
...(snapshot ? { snapshot } : {}),
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
onJoin(player) {
|
|
404
|
+
this.#event({ player: player.id, event: "join" });
|
|
405
|
+
}
|
|
406
|
+
/** A player came or went. With players asking for the turns, it is in a turn before this hook returns. */
|
|
407
|
+
#event(entry) {
|
|
408
|
+
this.#add(entry);
|
|
409
|
+
if (this.turnClock === "players")
|
|
410
|
+
this.#close(true);
|
|
411
|
+
}
|
|
412
|
+
/** Every way of losing a seat ends here, so every simulation hears about it. */
|
|
413
|
+
removePlayer(playerId, reason) {
|
|
414
|
+
if (this.players.some((player) => player.id === playerId)) {
|
|
415
|
+
this.#event({ player: playerId, event: "leave" });
|
|
416
|
+
}
|
|
417
|
+
super.removePlayer(playerId, reason);
|
|
418
|
+
}
|
|
419
|
+
onDisconnect(player) {
|
|
420
|
+
this.#event({ player: player.id, event: "disconnect" });
|
|
421
|
+
}
|
|
422
|
+
onReconnect(player) {
|
|
423
|
+
this.#event({ player: player.id, event: "reconnect" });
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Closes the turn: whatever arrived since the last one becomes this turn's
|
|
427
|
+
* entries. A turn with something in it is one row in the recording, written
|
|
428
|
+
* before any player is sent it; `#resume` is what lets the room itself be
|
|
429
|
+
* saved on its usual schedule instead of with every turn.
|
|
430
|
+
*/
|
|
431
|
+
onTick() {
|
|
432
|
+
if (this.turnClock === "players")
|
|
433
|
+
return; // Only if a subclass turned the timer back on.
|
|
434
|
+
this.#resume();
|
|
435
|
+
const state = this.state;
|
|
436
|
+
state.turn += 1;
|
|
437
|
+
state.entries = state.pending;
|
|
438
|
+
state.pending = [];
|
|
439
|
+
if (state.entries.length > 0) {
|
|
440
|
+
this.#turns().exec("INSERT OR REPLACE INTO tickflare_turns (n, entries) VALUES (?, ?)", state.turn, JSON.stringify(state.entries));
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
view() {
|
|
444
|
+
const { seed, turn, entries, desync } = this.state;
|
|
445
|
+
const every = this.#snapshotEvery();
|
|
446
|
+
const snapshots = every > 0 ? { snapshotEvery: every } : {};
|
|
447
|
+
if (this.turnClock !== "players")
|
|
448
|
+
return { seed, turn, entries, desync, ...snapshots };
|
|
449
|
+
// Before anything has woken this instance up, the checkpoint is all it knows.
|
|
450
|
+
const latest = Math.max(turn, this.#turn);
|
|
451
|
+
return {
|
|
452
|
+
seed,
|
|
453
|
+
turn: latest,
|
|
454
|
+
entries: latest === this.#turn ? this.#entries : [],
|
|
455
|
+
desync,
|
|
456
|
+
clock: "players",
|
|
457
|
+
...snapshots,
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* `GET …/replay` is the recording, or the end of it for a client that is
|
|
462
|
+
* catching up. `POST …/snapshot` is a player's client sending a snapshot.
|
|
463
|
+
* If you override this, pass on what is not yours with `super.onHttp`.
|
|
464
|
+
*/
|
|
465
|
+
onHttp(request, player) {
|
|
466
|
+
const url = new URL(request.url);
|
|
467
|
+
if (request.method === "GET" && url.pathname.endsWith("/replay")) {
|
|
468
|
+
return Response.json(this.#catchUp(url.searchParams));
|
|
469
|
+
}
|
|
470
|
+
if (request.method === "POST" && url.pathname.endsWith("/snapshot")) {
|
|
471
|
+
return this.#offered(request, url.searchParams, player);
|
|
472
|
+
}
|
|
473
|
+
return new Response("Not found", { status: 404 });
|
|
474
|
+
}
|
|
475
|
+
/** Announces the end of the match and, if `matchBucket` is set, stores the recording as its replay. */
|
|
476
|
+
finishMatch(result = {}) {
|
|
477
|
+
super.finishMatch(result, this.replay());
|
|
478
|
+
}
|
|
479
|
+
/** The recording so far. */
|
|
480
|
+
replay() {
|
|
481
|
+
return this.#recording(0);
|
|
482
|
+
}
|
|
483
|
+
/** The recording so far, without the turns up to `after`. */
|
|
484
|
+
#recording(after) {
|
|
485
|
+
const rows = this.#recorded()
|
|
486
|
+
? this.ctx.storage.sql
|
|
487
|
+
.exec("SELECT n, entries FROM tickflare_turns WHERE n > ? ORDER BY n", after)
|
|
488
|
+
.toArray()
|
|
489
|
+
: [];
|
|
490
|
+
const turns = rows.map((row) => [row.n, JSON.parse(row.entries)]);
|
|
491
|
+
const last = turns[turns.length - 1]?.[0] ?? 0;
|
|
492
|
+
return {
|
|
493
|
+
v: 2,
|
|
494
|
+
seed: this.state.seed,
|
|
495
|
+
turnRate: this.tickRate,
|
|
496
|
+
turn: Math.max(this.state.turn, this.#turn, last),
|
|
497
|
+
turns,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* The recording, to write to. The table is made with the first turn that
|
|
502
|
+
* has something in it, not before: a room that has recorded nothing stores
|
|
503
|
+
* nothing. Asked for each time, because a hook that fails takes back
|
|
504
|
+
* everything it wrote, a new table included.
|
|
505
|
+
*/
|
|
506
|
+
#turns() {
|
|
507
|
+
const sql = this.ctx.storage.sql;
|
|
508
|
+
sql.exec("CREATE TABLE IF NOT EXISTS tickflare_turns (n INTEGER PRIMARY KEY, entries TEXT NOT NULL)");
|
|
509
|
+
return sql;
|
|
510
|
+
}
|
|
511
|
+
/** Whether there is a recording to read. */
|
|
512
|
+
#recorded() {
|
|
513
|
+
return this.ctx.storage.sql.exec("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'tickflare_turns'").toArray().length > 0;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/** The request's body as text, or undefined if it is longer than `bytes`: no more than that is read. */
|
|
517
|
+
async function textUpTo(request, bytes) {
|
|
518
|
+
if (Number(request.headers.get("Content-Length") ?? 0) > bytes)
|
|
519
|
+
return undefined;
|
|
520
|
+
const reader = request.body?.getReader();
|
|
521
|
+
if (!reader)
|
|
522
|
+
return "";
|
|
523
|
+
const chunks = [];
|
|
524
|
+
let length = 0;
|
|
525
|
+
for (;;) {
|
|
526
|
+
const { done, value } = await reader.read();
|
|
527
|
+
if (done)
|
|
528
|
+
break;
|
|
529
|
+
length += value.byteLength;
|
|
530
|
+
if (length > bytes) {
|
|
531
|
+
await reader.cancel();
|
|
532
|
+
return undefined;
|
|
533
|
+
}
|
|
534
|
+
chunks.push(value);
|
|
535
|
+
}
|
|
536
|
+
const all = new Uint8Array(length);
|
|
537
|
+
let at = 0;
|
|
538
|
+
for (const chunk of chunks) {
|
|
539
|
+
all.set(chunk, at);
|
|
540
|
+
at += chunk.byteLength;
|
|
541
|
+
}
|
|
542
|
+
return new TextDecoder().decode(all);
|
|
543
|
+
}
|
|
544
|
+
//# sourceMappingURL=lockstep.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lockstep.js","sourceRoot":"","sources":["../../src/server/lockstep.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAA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AAC;IACvB,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,SAAS,CAAC;QACR,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;QAC5C,IAAI,IAAI;YAAE,MAAM;QAChB,MAAM,IAAI,KAAK,CAAC,UAAU,CAAC;QAC3B,IAAI,MAAM,GAAG,KAAK,EAAE,CAAC;YACnB,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC;YACtB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IACnC,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QACnB,EAAE,IAAI,KAAK,CAAC,UAAU,CAAC;IACzB,CAAC;IACD,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { MatchResult } from "../protocol.js";
|
|
2
|
+
/** A finished match as stored in D1. */
|
|
3
|
+
export interface MatchRecord {
|
|
4
|
+
/** The match id, as in `MatchResult.id`. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** The room it was played in, if the room knew its id. */
|
|
7
|
+
room: string | null;
|
|
8
|
+
/** When it ended, in epoch milliseconds. */
|
|
9
|
+
endedAt: number;
|
|
10
|
+
/** Everyone who held a seat when it ended. */
|
|
11
|
+
players: string[];
|
|
12
|
+
/** What the room passed to `finishMatch`. */
|
|
13
|
+
result: Record<string, unknown>;
|
|
14
|
+
/** Where its replay is in R2, if it has one. */
|
|
15
|
+
replayKey: string | null;
|
|
16
|
+
}
|
|
17
|
+
/** The table `recordMatches` writes to. It creates it on first use; this is here if you prefer migrations. */
|
|
18
|
+
export declare const MATCHES_SCHEMA = "\nCREATE TABLE IF NOT EXISTS tickflare_matches (\n id TEXT PRIMARY KEY,\n room TEXT,\n ended_at INTEGER NOT NULL,\n players TEXT NOT NULL,\n result TEXT NOT NULL,\n replay_key TEXT\n);\nCREATE INDEX IF NOT EXISTS tickflare_matches_ended_at ON tickflare_matches (ended_at DESC);\n";
|
|
19
|
+
/**
|
|
20
|
+
* Queue consumer: stores finished matches in D1. Use it as your Worker's
|
|
21
|
+
* `queue` handler, or call it from one:
|
|
22
|
+
*
|
|
23
|
+
* async queue(batch, env) {
|
|
24
|
+
* await recordMatches(batch, env.DB);
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* A match is stored once however often its message is delivered.
|
|
28
|
+
*/
|
|
29
|
+
export declare function recordMatches(batch: MessageBatch<MatchResult>, db: D1Database): Promise<void>;
|
|
30
|
+
/** The most recently finished matches, newest first. */
|
|
31
|
+
export declare function recentMatches(db: D1Database, limit?: number): Promise<MatchRecord[]>;
|