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.
- package/CHANGELOG.md +240 -0
- package/LICENSE +21 -0
- package/README.md +112 -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/buffered.d.ts +88 -0
- package/dist/client/buffered.js +229 -0
- package/dist/client/buffered.js.map +1 -0
- package/dist/client/clock.d.ts +36 -0
- package/dist/client/clock.js +83 -0
- package/dist/client/clock.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 +182 -0
- package/dist/client/index.js +496 -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 +268 -0
- package/dist/protocol.js +54 -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 +127 -0
- package/dist/server/lockstep.js +549 -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 +402 -0
- package/dist/server/room.js +1896 -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/buffered.ts +264 -0
- package/src/client/clock.ts +86 -0
- package/src/client/draft.ts +138 -0
- package/src/client/index.ts +631 -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 +300 -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 +86 -0
- package/src/server/limiter.ts +41 -0
- package/src/server/lockstep.ts +617 -0
- package/src/server/matches.ts +99 -0
- package/src/server/matchmaker.ts +537 -0
- package/src/server/room.ts +1999 -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,229 @@
|
|
|
1
|
+
import { ServerClock, localNow } from "./clock.js";
|
|
2
|
+
/** Snapshots kept per id at the most, whatever the delay. */
|
|
3
|
+
const MAX_SNAPSHOTS = 64;
|
|
4
|
+
/**
|
|
5
|
+
* Smooths values that arrive once per tick by drawing them a little in the
|
|
6
|
+
* past, at an even pace, from the ticks they belong to.
|
|
7
|
+
*
|
|
8
|
+
* `Interpolator` glides to each new value as it arrives: it adds almost no
|
|
9
|
+
* delay, but things speed up when two patches arrive together and stop when
|
|
10
|
+
* one is late. This one keeps the last few states of each id, keyed by the
|
|
11
|
+
* room's tick, and draws the moment `delay` ticks behind the tick the room
|
|
12
|
+
* is on (`RoomClient.serverTick()`), blending the states on either side.
|
|
13
|
+
* Motion then keeps the pace of the room, whatever the network does to the
|
|
14
|
+
* patches, at the cost of the delay: two ticks and a margin for the jitter
|
|
15
|
+
* it measures, which it keeps adjusting.
|
|
16
|
+
*
|
|
17
|
+
* When the newest state is older than the moment it draws, it carries each
|
|
18
|
+
* thing on at its last speed for up to `maxExtrapolationTicks`, then holds
|
|
19
|
+
* it. A jump, such as a respawn, is drawn as one: say so with `snap(id)`, or
|
|
20
|
+
* set `snapDistance`.
|
|
21
|
+
*
|
|
22
|
+
* const glide = new BufferedInterpolator(room, ["x", "y"], { snapDistance: 200 });
|
|
23
|
+
* room.on("state", (state) => glide.update(state.units));
|
|
24
|
+
* // in the render loop:
|
|
25
|
+
* const { x, y } = glide.at(id) ?? state.units[id];
|
|
26
|
+
*
|
|
27
|
+
* Against a room older than 0.2, which does not say where its clock is, it
|
|
28
|
+
* times the patches as they arrive instead.
|
|
29
|
+
*/
|
|
30
|
+
export class BufferedInterpolator {
|
|
31
|
+
#source;
|
|
32
|
+
#fields;
|
|
33
|
+
#angles;
|
|
34
|
+
#delayTicks;
|
|
35
|
+
#maxExtrapolation;
|
|
36
|
+
#snapDistance;
|
|
37
|
+
#tracks = new Map();
|
|
38
|
+
/** Times the patches as they arrive, for a room that does not say where its clock is. */
|
|
39
|
+
#arrivals = new ServerClock();
|
|
40
|
+
/** How many ticks behind the room's clock a state arrives, smoothed, and how much that varies. */
|
|
41
|
+
#lateness;
|
|
42
|
+
#jitter = 0;
|
|
43
|
+
#newestTick = -Infinity;
|
|
44
|
+
/** The latest moment drawn, and when it was asked for: what is drawn never goes back. */
|
|
45
|
+
#shown;
|
|
46
|
+
/**
|
|
47
|
+
* @param source where the ticks come from: the `RoomClient`
|
|
48
|
+
* @param fields the numeric fields to smooth, e.g. `["x", "y"]`
|
|
49
|
+
* @param options see `BufferedInterpolatorOptions`
|
|
50
|
+
*/
|
|
51
|
+
constructor(source, fields, options = {}) {
|
|
52
|
+
this.#source = source;
|
|
53
|
+
this.#fields = fields;
|
|
54
|
+
this.#angles = new Set(options.angles ?? []);
|
|
55
|
+
this.#delayTicks = Math.max(0, options.delayTicks ?? 2);
|
|
56
|
+
this.#maxExtrapolation = Math.max(0, options.maxExtrapolationTicks ?? 3);
|
|
57
|
+
this.#snapDistance = options.snapDistance ?? Infinity;
|
|
58
|
+
}
|
|
59
|
+
/** The delay it draws at right now, in ticks behind the room's clock: see the class. */
|
|
60
|
+
get delay() {
|
|
61
|
+
return (this.#lateness ?? 0) + this.#delayTicks + 2 * this.#jitter;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Feed the latest values, keyed by id, as of tick `tick`: the source's
|
|
65
|
+
* latest unless given. Ids missing from `targets` are forgotten.
|
|
66
|
+
*
|
|
67
|
+
* @param now when they arrived, from `performance.now()`
|
|
68
|
+
*/
|
|
69
|
+
update(targets, tick = this.#source.tick, now = localNow()) {
|
|
70
|
+
if (tick < this.#newestTick) {
|
|
71
|
+
// The room went back to a save: what was kept is of a timeline that no longer is.
|
|
72
|
+
this.#tracks.clear();
|
|
73
|
+
this.#arrivals.reset();
|
|
74
|
+
this.#lateness = undefined;
|
|
75
|
+
this.#jitter = 0;
|
|
76
|
+
this.#shown = undefined;
|
|
77
|
+
}
|
|
78
|
+
if (tick > this.#newestTick) {
|
|
79
|
+
this.#newestTick = tick;
|
|
80
|
+
this.#measure(tick, now);
|
|
81
|
+
}
|
|
82
|
+
const drawn = this.#shown?.tick ?? -Infinity;
|
|
83
|
+
const tracks = new Map();
|
|
84
|
+
for (const id of Object.keys(targets)) {
|
|
85
|
+
const target = targets[id];
|
|
86
|
+
const values = {};
|
|
87
|
+
for (const field of this.#fields)
|
|
88
|
+
values[field] = target[field];
|
|
89
|
+
const track = this.#tracks.get(id) ?? [];
|
|
90
|
+
const last = track[track.length - 1];
|
|
91
|
+
if (last?.tick === tick) {
|
|
92
|
+
last.values = values;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
track.push({ tick, values, snap: last !== undefined && this.#distance(last.values, values) > this.#snapDistance });
|
|
96
|
+
}
|
|
97
|
+
// What lies behind the moment drawn is no longer needed, apart from the one it starts from.
|
|
98
|
+
let drop = 0;
|
|
99
|
+
while (drop < track.length - 2 && track[drop + 1].tick <= drawn)
|
|
100
|
+
drop += 1;
|
|
101
|
+
drop = Math.max(drop, track.length - MAX_SNAPSHOTS);
|
|
102
|
+
if (drop > 0)
|
|
103
|
+
track.splice(0, drop);
|
|
104
|
+
tracks.set(id, track);
|
|
105
|
+
}
|
|
106
|
+
this.#tracks = tracks;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The latest values given for `id` were a jump, such as a respawn: draw
|
|
110
|
+
* them in one step when their tick comes, not as a slide. Call it after
|
|
111
|
+
* the `update` that brought them, for instance when a message says so:
|
|
112
|
+
* messages arrive after the patch of the same tick.
|
|
113
|
+
*/
|
|
114
|
+
snap(id) {
|
|
115
|
+
const track = this.#tracks.get(id);
|
|
116
|
+
const last = track?.[track.length - 1];
|
|
117
|
+
if (last && track.length > 1)
|
|
118
|
+
last.snap = true;
|
|
119
|
+
}
|
|
120
|
+
/** The values to draw for `id` right now, or undefined if it is not known. */
|
|
121
|
+
at(id, now = localNow()) {
|
|
122
|
+
const track = this.#tracks.get(id);
|
|
123
|
+
if (!track || track.length === 0)
|
|
124
|
+
return undefined;
|
|
125
|
+
const newest = track[track.length - 1];
|
|
126
|
+
const tick = this.#drawnTick(now);
|
|
127
|
+
if (tick === undefined)
|
|
128
|
+
return { ...newest.values };
|
|
129
|
+
if (tick >= newest.tick) {
|
|
130
|
+
const before = track[track.length - 2];
|
|
131
|
+
const ahead = Math.min(tick - newest.tick, this.#maxExtrapolation);
|
|
132
|
+
if (!before || newest.snap || ahead <= 0)
|
|
133
|
+
return { ...newest.values };
|
|
134
|
+
// Carried on at the speed it last had, for as long as that is allowed.
|
|
135
|
+
return this.#blend(before.values, newest.values, 1 + ahead / (newest.tick - before.tick));
|
|
136
|
+
}
|
|
137
|
+
let i = track.length - 1;
|
|
138
|
+
while (i > 0 && track[i - 1].tick > tick)
|
|
139
|
+
i -= 1;
|
|
140
|
+
const to = track[i];
|
|
141
|
+
const from = track[i - 1];
|
|
142
|
+
// Before the first it knows, it shows the first.
|
|
143
|
+
if (!from)
|
|
144
|
+
return { ...to.values };
|
|
145
|
+
if (to.snap)
|
|
146
|
+
return { ...from.values };
|
|
147
|
+
return this.#blend(from.values, to.values, (tick - from.tick) / (to.tick - from.tick));
|
|
148
|
+
}
|
|
149
|
+
/** Forget everything, e.g. after moving to another room. */
|
|
150
|
+
clear() {
|
|
151
|
+
this.#tracks.clear();
|
|
152
|
+
this.#arrivals.reset();
|
|
153
|
+
this.#lateness = undefined;
|
|
154
|
+
this.#jitter = 0;
|
|
155
|
+
this.#newestTick = -Infinity;
|
|
156
|
+
this.#shown = undefined;
|
|
157
|
+
}
|
|
158
|
+
/** The room's tick at `now`, from its clock if it says where that is, else from when patches arrive. */
|
|
159
|
+
#clockTick(now) {
|
|
160
|
+
const told = this.#source.serverTick?.(now);
|
|
161
|
+
if (told !== undefined) {
|
|
162
|
+
// A clock that stands still: the simulation has stopped, and the latest state is final.
|
|
163
|
+
if (this.#source.serverTick(now + 1000) === told)
|
|
164
|
+
return Infinity;
|
|
165
|
+
return told;
|
|
166
|
+
}
|
|
167
|
+
return this.#arrivals.tick(now);
|
|
168
|
+
}
|
|
169
|
+
/** A state of `tick` arrived at `now`: how late was it, by the room's clock? */
|
|
170
|
+
#measure(tick, now) {
|
|
171
|
+
const rate = this.#source.tickRate;
|
|
172
|
+
if (!(rate > 0))
|
|
173
|
+
return;
|
|
174
|
+
if (this.#source.serverTick?.(now) === undefined)
|
|
175
|
+
this.#arrivals.reading(tick, rate, 0, 0, now);
|
|
176
|
+
const clock = this.#clockTick(now);
|
|
177
|
+
if (clock === undefined || clock === Infinity)
|
|
178
|
+
return;
|
|
179
|
+
const late = clock - tick;
|
|
180
|
+
if (this.#lateness === undefined) {
|
|
181
|
+
this.#lateness = late;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
// As TCP smooths its round trip: the mean takes an eighth of each sample, the spread a quarter.
|
|
185
|
+
this.#jitter += (Math.abs(late - this.#lateness) - this.#jitter) / 4;
|
|
186
|
+
this.#lateness += (late - this.#lateness) / 8;
|
|
187
|
+
}
|
|
188
|
+
/** The tick drawn at `now`, with its fraction. Undefined when there is no clock to go by. */
|
|
189
|
+
#drawnTick(now) {
|
|
190
|
+
if (!(this.#source.tickRate > 0))
|
|
191
|
+
return undefined;
|
|
192
|
+
const clock = this.#clockTick(now);
|
|
193
|
+
if (clock === undefined || clock === Infinity)
|
|
194
|
+
return undefined;
|
|
195
|
+
let tick = clock - this.delay;
|
|
196
|
+
const shown = this.#shown;
|
|
197
|
+
// A delay that grows would take what is drawn back in time. It waits for the clock instead.
|
|
198
|
+
if (shown && now >= shown.now)
|
|
199
|
+
tick = Math.max(tick, shown.tick);
|
|
200
|
+
if (!shown || now >= shown.now)
|
|
201
|
+
this.#shown = { tick, now };
|
|
202
|
+
return tick;
|
|
203
|
+
}
|
|
204
|
+
#blend(from, to, t) {
|
|
205
|
+
const shown = {};
|
|
206
|
+
for (const field of this.#fields) {
|
|
207
|
+
const a = from[field];
|
|
208
|
+
const b = to[field];
|
|
209
|
+
shown[field] = this.#angles.has(field) ? wrap(a + wrap(b - a) * t) : a + (b - a) * t;
|
|
210
|
+
}
|
|
211
|
+
return shown;
|
|
212
|
+
}
|
|
213
|
+
#distance(a, b) {
|
|
214
|
+
if (this.#snapDistance === Infinity)
|
|
215
|
+
return 0;
|
|
216
|
+
let sum = 0;
|
|
217
|
+
for (const field of this.#fields) {
|
|
218
|
+
if (!this.#angles.has(field))
|
|
219
|
+
sum += (b[field] - a[field]) ** 2;
|
|
220
|
+
}
|
|
221
|
+
return Math.sqrt(sum);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/** An angle in radians, brought into (-π, π]. */
|
|
225
|
+
function wrap(angle) {
|
|
226
|
+
const turned = angle - 2 * Math.PI * Math.floor((angle + Math.PI) / (2 * Math.PI));
|
|
227
|
+
return turned === -Math.PI ? Math.PI : turned;
|
|
228
|
+
}
|
|
229
|
+
//# sourceMappingURL=buffered.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffered.js","sourceRoot":"","sources":["../../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,CAAC;QAClE,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACxB,CAAC;CACF;AAED,iDAAiD;AACjD,SAAS,IAAI,CAAC,KAAa;IACzB,MAAM,MAAM,GAAG,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACnF,OAAO,MAAM,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps an estimate of which tick a room is on, from the clock readings the
|
|
3
|
+
* room sends and the round trip to it.
|
|
4
|
+
*
|
|
5
|
+
* A reading says that tick `tick` fell due `ms` milliseconds before the
|
|
6
|
+
* frame was sent. The frame took about half a round trip to arrive, so on
|
|
7
|
+
* this machine `tick` fell due `ms + rtt / 2` before it arrived, and the room
|
|
8
|
+
* has gone on at `tickRate` ticks a second since. Readings of one epoch are
|
|
9
|
+
* averaged, so that one frame held up on the way moves the estimate only a
|
|
10
|
+
* little; a new epoch replaces the estimate.
|
|
11
|
+
*/
|
|
12
|
+
export declare class ServerClock {
|
|
13
|
+
#private;
|
|
14
|
+
/** How much of a new reading's difference from the estimate is taken in: the rest is the estimate's. */
|
|
15
|
+
static readonly SMOOTHING = 0.25;
|
|
16
|
+
/** The smoothed round trip to the room, in milliseconds. Undefined until one has been measured. */
|
|
17
|
+
get rtt(): number | undefined;
|
|
18
|
+
/** A round trip took `ms` milliseconds. Averaged with the earlier ones as TCP does: each new one counts for an eighth. */
|
|
19
|
+
roundTrip(ms: number): void;
|
|
20
|
+
/**
|
|
21
|
+
* The room said where its clock is, in a frame that arrived at `now`
|
|
22
|
+
* (local, from `performance.now()`). With `ms` absent the simulation has
|
|
23
|
+
* stopped at `tick`.
|
|
24
|
+
*/
|
|
25
|
+
reading(tick: number, tickRate: number, ms: number | undefined, epoch: number | undefined, now?: number): void;
|
|
26
|
+
/** Forget the clock, keeping the round trip: the connection was lost, or the room told nothing yet. */
|
|
27
|
+
reset(): void;
|
|
28
|
+
/**
|
|
29
|
+
* The tick the room is on at `now` (local, from `performance.now()`), with
|
|
30
|
+
* the fraction of the next that has passed. The tick it stopped at while
|
|
31
|
+
* its simulation is stopped, and undefined when there is nothing to go on.
|
|
32
|
+
*/
|
|
33
|
+
tick(now?: number): number | undefined;
|
|
34
|
+
}
|
|
35
|
+
/** A clock that only goes forward, in milliseconds. */
|
|
36
|
+
export declare function localNow(): number;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps an estimate of which tick a room is on, from the clock readings the
|
|
3
|
+
* room sends and the round trip to it.
|
|
4
|
+
*
|
|
5
|
+
* A reading says that tick `tick` fell due `ms` milliseconds before the
|
|
6
|
+
* frame was sent. The frame took about half a round trip to arrive, so on
|
|
7
|
+
* this machine `tick` fell due `ms + rtt / 2` before it arrived, and the room
|
|
8
|
+
* has gone on at `tickRate` ticks a second since. Readings of one epoch are
|
|
9
|
+
* averaged, so that one frame held up on the way moves the estimate only a
|
|
10
|
+
* little; a new epoch replaces the estimate.
|
|
11
|
+
*/
|
|
12
|
+
export class ServerClock {
|
|
13
|
+
#tickRate = 0;
|
|
14
|
+
#epoch;
|
|
15
|
+
/** The local time (`performance.now()`) at which `#base` fell due, before half the round trip is added. */
|
|
16
|
+
#baseAt;
|
|
17
|
+
#base = 0;
|
|
18
|
+
/** The tick the simulation stopped at, while it is stopped. */
|
|
19
|
+
#stopped;
|
|
20
|
+
#rtt;
|
|
21
|
+
/** How much of a new reading's difference from the estimate is taken in: the rest is the estimate's. */
|
|
22
|
+
static SMOOTHING = 0.25;
|
|
23
|
+
/** The smoothed round trip to the room, in milliseconds. Undefined until one has been measured. */
|
|
24
|
+
get rtt() {
|
|
25
|
+
return this.#rtt;
|
|
26
|
+
}
|
|
27
|
+
/** A round trip took `ms` milliseconds. Averaged with the earlier ones as TCP does: each new one counts for an eighth. */
|
|
28
|
+
roundTrip(ms) {
|
|
29
|
+
if (!Number.isFinite(ms) || ms < 0)
|
|
30
|
+
return;
|
|
31
|
+
this.#rtt = this.#rtt === undefined ? ms : this.#rtt + (ms - this.#rtt) / 8;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The room said where its clock is, in a frame that arrived at `now`
|
|
35
|
+
* (local, from `performance.now()`). With `ms` absent the simulation has
|
|
36
|
+
* stopped at `tick`.
|
|
37
|
+
*/
|
|
38
|
+
reading(tick, tickRate, ms, epoch, now = localNow()) {
|
|
39
|
+
if (ms === undefined || !(tickRate > 0)) {
|
|
40
|
+
this.#stopped = tick;
|
|
41
|
+
this.#baseAt = undefined;
|
|
42
|
+
this.#epoch = undefined;
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.#stopped = undefined;
|
|
46
|
+
const at = now - ms;
|
|
47
|
+
if (this.#baseAt === undefined || epoch !== this.#epoch || tickRate !== this.#tickRate) {
|
|
48
|
+
this.#tickRate = tickRate;
|
|
49
|
+
this.#epoch = epoch;
|
|
50
|
+
this.#base = tick;
|
|
51
|
+
this.#baseAt = at;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// When `tick` fell due by the estimate so far, then a step towards the new reading.
|
|
55
|
+
const expected = this.#baseAt + ((tick - this.#base) * 1000) / tickRate;
|
|
56
|
+
this.#base = tick;
|
|
57
|
+
this.#baseAt = expected + (at - expected) * ServerClock.SMOOTHING;
|
|
58
|
+
}
|
|
59
|
+
/** Forget the clock, keeping the round trip: the connection was lost, or the room told nothing yet. */
|
|
60
|
+
reset() {
|
|
61
|
+
this.#baseAt = undefined;
|
|
62
|
+
this.#epoch = undefined;
|
|
63
|
+
this.#stopped = undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The tick the room is on at `now` (local, from `performance.now()`), with
|
|
67
|
+
* the fraction of the next that has passed. The tick it stopped at while
|
|
68
|
+
* its simulation is stopped, and undefined when there is nothing to go on.
|
|
69
|
+
*/
|
|
70
|
+
tick(now = localNow()) {
|
|
71
|
+
if (this.#stopped !== undefined)
|
|
72
|
+
return this.#stopped;
|
|
73
|
+
if (this.#baseAt === undefined)
|
|
74
|
+
return undefined;
|
|
75
|
+
const since = now - this.#baseAt + (this.#rtt ?? 0) / 2;
|
|
76
|
+
return this.#base + (since * this.#tickRate) / 1000;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** A clock that only goes forward, in milliseconds. */
|
|
80
|
+
export function localNow() {
|
|
81
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=clock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clock.js","sourceRoot":"","sources":["../../src/client/clock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,OAAO,WAAW;IACtB,SAAS,GAAG,CAAC,CAAC;IACd,MAAM,CAAqB;IAC3B,2GAA2G;IAC3G,OAAO,CAAqB;IAC5B,KAAK,GAAG,CAAC,CAAC;IACV,+DAA+D;IAC/D,QAAQ,CAAqB;IAC7B,IAAI,CAAqB;IAEzB,wGAAwG;IACxG,MAAM,CAAU,SAAS,GAAG,IAAI,CAAC;IAEjC,mGAAmG;IACnG,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,0HAA0H;IAC1H,SAAS,CAAC,EAAU;QAClB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,CAAC;YAAE,OAAO;QAC3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE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G,SAAS,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,GAAG,GAAG,QAAQ,EAAE;QACnB,IAAI,IAAI,CAAC,QAAQ,KAAK,SAAS;YAAE,OAAO,IAAI,CAAC,QAAQ,CAAC;QACtD,IAAI,IAAI,CAAC,OAAO,KAAK,SAAS;YAAE,OAAO,SAAS,CAAC;QACjD,MAAM,KAAK,GAAG,GAAG,GAAG,IAAI,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;QACxD,OAAO,IAAI,CAAC,KAAK,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;IACtD,CAAC;CACF;AAED,uDAAuD;AACvD,MAAM,UAAU,QAAQ;IACtB,OAAO,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Changing a copy of a state without copying the state.
|
|
3
|
+
*
|
|
4
|
+
* `produce(base, recipe)` hands `recipe` a draft of `base` to change in
|
|
5
|
+
* place, as a room's hooks change `this.state`, and returns the result.
|
|
6
|
+
* `base` is not touched. Only the objects on the way to something that
|
|
7
|
+
* changed are copied; everything else in the result is the same object as in
|
|
8
|
+
* `base`, so the result can be compared with it by reference, part by part.
|
|
9
|
+
*
|
|
10
|
+
* It follows plain objects and arrays, which is what a state made of JSON
|
|
11
|
+
* has. Anything else in it (a `Date`, a `Map`) is handed over as it is.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* The state `recipe` makes of `base`, with `base` left as it was. If the
|
|
15
|
+
* recipe changes nothing, the result is `base` itself.
|
|
16
|
+
*/
|
|
17
|
+
export declare function produce<T>(base: T, recipe: (draft: T) => void): T;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Changing a copy of a state without copying the state.
|
|
3
|
+
*
|
|
4
|
+
* `produce(base, recipe)` hands `recipe` a draft of `base` to change in
|
|
5
|
+
* place, as a room's hooks change `this.state`, and returns the result.
|
|
6
|
+
* `base` is not touched. Only the objects on the way to something that
|
|
7
|
+
* changed are copied; everything else in the result is the same object as in
|
|
8
|
+
* `base`, so the result can be compared with it by reference, part by part.
|
|
9
|
+
*
|
|
10
|
+
* It follows plain objects and arrays, which is what a state made of JSON
|
|
11
|
+
* has. Anything else in it (a `Date`, a `Map`) is handed over as it is.
|
|
12
|
+
*/
|
|
13
|
+
const nodes = new WeakMap();
|
|
14
|
+
/**
|
|
15
|
+
* The state `recipe` makes of `base`, with `base` left as it was. If the
|
|
16
|
+
* recipe changes nothing, the result is `base` itself.
|
|
17
|
+
*/
|
|
18
|
+
export function produce(base, recipe) {
|
|
19
|
+
if (!isDraftable(base)) {
|
|
20
|
+
recipe(base);
|
|
21
|
+
return base;
|
|
22
|
+
}
|
|
23
|
+
const root = draftOf(base, undefined, undefined);
|
|
24
|
+
recipe(root.proxy);
|
|
25
|
+
return (root.copy ?? root.base);
|
|
26
|
+
}
|
|
27
|
+
function isDraftable(value) {
|
|
28
|
+
if (typeof value !== "object" || value === null)
|
|
29
|
+
return false;
|
|
30
|
+
if (Array.isArray(value))
|
|
31
|
+
return true;
|
|
32
|
+
const prototype = Object.getPrototypeOf(value);
|
|
33
|
+
return prototype === Object.prototype || prototype === null;
|
|
34
|
+
}
|
|
35
|
+
function draftOf(base, parent, key) {
|
|
36
|
+
const node = { base, copy: undefined, parent, key, proxy: undefined, children: new Map() };
|
|
37
|
+
// The proxy's own target is an empty stand-in: nothing is read from it or written to it, and a
|
|
38
|
+
// base that was frozen cannot get in the way.
|
|
39
|
+
node.proxy = new Proxy(Array.isArray(base) ? [] : {}, handlerFor(node));
|
|
40
|
+
nodes.set(node.proxy, node);
|
|
41
|
+
return node;
|
|
42
|
+
}
|
|
43
|
+
/** What the draft holds now: its copy if it has one. */
|
|
44
|
+
const current = (node) => (node.copy ?? node.base);
|
|
45
|
+
/** Gives the node a copy to write to, and puts that copy in its parent's, all the way up. */
|
|
46
|
+
function writable(node) {
|
|
47
|
+
if (node.copy === undefined) {
|
|
48
|
+
node.copy = Array.isArray(node.base) ? node.base.slice() : { ...node.base };
|
|
49
|
+
if (node.parent && node.key !== undefined)
|
|
50
|
+
writable(node.parent)[node.key] = node.copy;
|
|
51
|
+
}
|
|
52
|
+
return node.copy;
|
|
53
|
+
}
|
|
54
|
+
/** A value on its way into a draft, with any drafts in it replaced by what they hold. */
|
|
55
|
+
function settled(value) {
|
|
56
|
+
if (typeof value !== "object" || value === null)
|
|
57
|
+
return value;
|
|
58
|
+
const node = nodes.get(value);
|
|
59
|
+
if (node)
|
|
60
|
+
return current(node);
|
|
61
|
+
if (!isDraftable(value))
|
|
62
|
+
return value;
|
|
63
|
+
// A new object or array: it is the recipe's own, so drafts inside it are replaced in place.
|
|
64
|
+
const holder = value;
|
|
65
|
+
for (const key of Object.keys(holder)) {
|
|
66
|
+
const inner = holder[key];
|
|
67
|
+
if (typeof inner === "object" && inner !== null) {
|
|
68
|
+
const plain = settled(inner);
|
|
69
|
+
if (plain !== inner)
|
|
70
|
+
holder[key] = plain;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
function handlerFor(node) {
|
|
76
|
+
return {
|
|
77
|
+
get(_target, key, receiver) {
|
|
78
|
+
const source = current(node);
|
|
79
|
+
if (typeof key === "symbol")
|
|
80
|
+
return Reflect.get(source, key, receiver);
|
|
81
|
+
const value = source[key];
|
|
82
|
+
if (!isDraftable(value)) {
|
|
83
|
+
// An array's own methods run on the draft, so that what they write is followed.
|
|
84
|
+
return typeof value === "function" ? Reflect.get(source, key, receiver) : value;
|
|
85
|
+
}
|
|
86
|
+
let child = node.children.get(key);
|
|
87
|
+
if (!child || current(child) !== value) {
|
|
88
|
+
child = draftOf(value, node, key);
|
|
89
|
+
node.children.set(key, child);
|
|
90
|
+
}
|
|
91
|
+
return child.proxy;
|
|
92
|
+
},
|
|
93
|
+
set(_target, key, value) {
|
|
94
|
+
if (typeof key === "symbol")
|
|
95
|
+
return false;
|
|
96
|
+
const next = settled(value);
|
|
97
|
+
const source = current(node);
|
|
98
|
+
// Writing what is there already is not a change, and must not cost a copy.
|
|
99
|
+
if (Object.prototype.hasOwnProperty.call(source, key) && Object.is(source[key], next))
|
|
100
|
+
return true;
|
|
101
|
+
writable(node)[key] = next;
|
|
102
|
+
node.children.delete(key);
|
|
103
|
+
return true;
|
|
104
|
+
},
|
|
105
|
+
deleteProperty(_target, key) {
|
|
106
|
+
if (typeof key === "symbol")
|
|
107
|
+
return false;
|
|
108
|
+
if (!Object.prototype.hasOwnProperty.call(current(node), key))
|
|
109
|
+
return true;
|
|
110
|
+
delete writable(node)[key];
|
|
111
|
+
node.children.delete(key);
|
|
112
|
+
return true;
|
|
113
|
+
},
|
|
114
|
+
defineProperty(_target, key, descriptor) {
|
|
115
|
+
if (typeof key === "symbol" || !("value" in descriptor))
|
|
116
|
+
return false;
|
|
117
|
+
writable(node)[key] = settled(descriptor.value);
|
|
118
|
+
node.children.delete(key);
|
|
119
|
+
return true;
|
|
120
|
+
},
|
|
121
|
+
has: (_target, key) => Reflect.has(current(node), key),
|
|
122
|
+
ownKeys: () => Reflect.ownKeys(current(node)),
|
|
123
|
+
getOwnPropertyDescriptor(_target, key) {
|
|
124
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(current(node), key);
|
|
125
|
+
// What the draft holds is not the proxy's own target, so nothing in it may claim to be fixed
|
|
126
|
+
// there, except an array's length, which is.
|
|
127
|
+
if (descriptor && !(Array.isArray(node.base) && key === "length"))
|
|
128
|
+
descriptor.configurable = true;
|
|
129
|
+
return descript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