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,86 @@
|
|
|
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: number | undefined;
|
|
15
|
+
/** The local time (`performance.now()`) at which `#base` fell due, before half the round trip is added. */
|
|
16
|
+
#baseAt: number | undefined;
|
|
17
|
+
#base = 0;
|
|
18
|
+
/** The tick the simulation stopped at, while it is stopped. */
|
|
19
|
+
#stopped: number | undefined;
|
|
20
|
+
#rtt: number | undefined;
|
|
21
|
+
|
|
22
|
+
/** How much of a new reading's difference from the estimate is taken in: the rest is the estimate's. */
|
|
23
|
+
static readonly SMOOTHING = 0.25;
|
|
24
|
+
|
|
25
|
+
/** The smoothed round trip to the room, in milliseconds. Undefined until one has been measured. */
|
|
26
|
+
get rtt(): number | undefined {
|
|
27
|
+
return this.#rtt;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A round trip took `ms` milliseconds. Averaged with the earlier ones as TCP does: each new one counts for an eighth. */
|
|
31
|
+
roundTrip(ms: number): void {
|
|
32
|
+
if (!Number.isFinite(ms) || ms < 0) return;
|
|
33
|
+
this.#rtt = this.#rtt === undefined ? ms : this.#rtt + (ms - this.#rtt) / 8;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The room said where its clock is, in a frame that arrived at `now`
|
|
38
|
+
* (local, from `performance.now()`). With `ms` absent the simulation has
|
|
39
|
+
* stopped at `tick`.
|
|
40
|
+
*/
|
|
41
|
+
reading(tick: number, tickRate: number, ms: number | undefined, epoch: number | undefined, now = localNow()): void {
|
|
42
|
+
if (ms === undefined || !(tickRate > 0)) {
|
|
43
|
+
this.#stopped = tick;
|
|
44
|
+
this.#baseAt = undefined;
|
|
45
|
+
this.#epoch = undefined;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this.#stopped = undefined;
|
|
49
|
+
const at = now - ms;
|
|
50
|
+
if (this.#baseAt === undefined || epoch !== this.#epoch || tickRate !== this.#tickRate) {
|
|
51
|
+
this.#tickRate = tickRate;
|
|
52
|
+
this.#epoch = epoch;
|
|
53
|
+
this.#base = tick;
|
|
54
|
+
this.#baseAt = at;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// When `tick` fell due by the estimate so far, then a step towards the new reading.
|
|
58
|
+
const expected = this.#baseAt + ((tick - this.#base) * 1000) / tickRate;
|
|
59
|
+
this.#base = tick;
|
|
60
|
+
this.#baseAt = expected + (at - expected) * ServerClock.SMOOTHING;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Forget the clock, keeping the round trip: the connection was lost, or the room told nothing yet. */
|
|
64
|
+
reset(): void {
|
|
65
|
+
this.#baseAt = undefined;
|
|
66
|
+
this.#epoch = undefined;
|
|
67
|
+
this.#stopped = undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The tick the room is on at `now` (local, from `performance.now()`), with
|
|
72
|
+
* the fraction of the next that has passed. The tick it stopped at while
|
|
73
|
+
* its simulation is stopped, and undefined when there is nothing to go on.
|
|
74
|
+
*/
|
|
75
|
+
tick(now = localNow()): number | undefined {
|
|
76
|
+
if (this.#stopped !== undefined) return this.#stopped;
|
|
77
|
+
if (this.#baseAt === undefined) return undefined;
|
|
78
|
+
const since = now - this.#baseAt + (this.#rtt ?? 0) / 2;
|
|
79
|
+
return this.#base + (since * this.#tickRate) / 1000;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A clock that only goes forward, in milliseconds. */
|
|
84
|
+
export function localNow(): number {
|
|
85
|
+
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
86
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
/** One object or array of the base, and its copy once something in it has changed. */
|
|
15
|
+
interface Node {
|
|
16
|
+
base: object;
|
|
17
|
+
copy: object | undefined;
|
|
18
|
+
parent: Node | undefined;
|
|
19
|
+
/** Where it is in its parent. */
|
|
20
|
+
key: string | undefined;
|
|
21
|
+
proxy: object;
|
|
22
|
+
/** Drafts handed out for what is inside, by key. */
|
|
23
|
+
children: Map<string, Node>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const nodes = new WeakMap<object, Node>();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The state `recipe` makes of `base`, with `base` left as it was. If the
|
|
30
|
+
* recipe changes nothing, the result is `base` itself.
|
|
31
|
+
*/
|
|
32
|
+
export function produce<T>(base: T, recipe: (draft: T) => void): T {
|
|
33
|
+
if (!isDraftable(base)) {
|
|
34
|
+
recipe(base);
|
|
35
|
+
return base;
|
|
36
|
+
}
|
|
37
|
+
const root = draftOf(base, undefined, undefined);
|
|
38
|
+
recipe(root.proxy as T);
|
|
39
|
+
return (root.copy ?? root.base) as T;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isDraftable(value: unknown): value is object {
|
|
43
|
+
if (typeof value !== "object" || value === null) return false;
|
|
44
|
+
if (Array.isArray(value)) return true;
|
|
45
|
+
const prototype = Object.getPrototypeOf(value) as unknown;
|
|
46
|
+
return prototype === Object.prototype || prototype === null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function draftOf(base: object, parent: Node | undefined, key: string | undefined): Node {
|
|
50
|
+
const node: Node = { base, copy: undefined, parent, key, proxy: undefined as unknown as object, children: new Map() };
|
|
51
|
+
// The proxy's own target is an empty stand-in: nothing is read from it or written to it, and a
|
|
52
|
+
// base that was frozen cannot get in the way.
|
|
53
|
+
node.proxy = new Proxy(Array.isArray(base) ? [] : {}, handlerFor(node));
|
|
54
|
+
nodes.set(node.proxy, node);
|
|
55
|
+
return node;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** What the draft holds now: its copy if it has one. */
|
|
59
|
+
const current = (node: Node): Record<string, unknown> => (node.copy ?? node.base) as Record<string, unknown>;
|
|
60
|
+
|
|
61
|
+
/** Gives the node a copy to write to, and puts that copy in its parent's, all the way up. */
|
|
62
|
+
function writable(node: Node): Record<string, unknown> {
|
|
63
|
+
if (node.copy === undefined) {
|
|
64
|
+
node.copy = Array.isArray(node.base) ? node.base.slice() : { ...node.base };
|
|
65
|
+
if (node.parent && node.key !== undefined) writable(node.parent)[node.key] = node.copy;
|
|
66
|
+
}
|
|
67
|
+
return node.copy as Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A value on its way into a draft, with any drafts in it replaced by what they hold. */
|
|
71
|
+
function settled(value: unknown): unknown {
|
|
72
|
+
if (typeof value !== "object" || value === null) return value;
|
|
73
|
+
const node = nodes.get(value);
|
|
74
|
+
if (node) return current(node);
|
|
75
|
+
if (!isDraftable(value)) return value;
|
|
76
|
+
// A new object or array: it is the recipe's own, so drafts inside it are replaced in place.
|
|
77
|
+
const holder = value as Record<string, unknown>;
|
|
78
|
+
for (const key of Object.keys(holder)) {
|
|
79
|
+
const inner = holder[key];
|
|
80
|
+
if (typeof inner === "object" && inner !== null) {
|
|
81
|
+
const plain = settled(inner);
|
|
82
|
+
if (plain !== inner) holder[key] = plain;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function handlerFor(node: Node): ProxyHandler<object> {
|
|
89
|
+
return {
|
|
90
|
+
get(_target, key, receiver) {
|
|
91
|
+
const source = current(node);
|
|
92
|
+
if (typeof key === "symbol") return Reflect.get(source, key, receiver);
|
|
93
|
+
const value = source[key];
|
|
94
|
+
if (!isDraftable(value)) {
|
|
95
|
+
// An array's own methods run on the draft, so that what they write is followed.
|
|
96
|
+
return typeof value === "function" ? Reflect.get(source, key, receiver) : value;
|
|
97
|
+
}
|
|
98
|
+
let child = node.children.get(key);
|
|
99
|
+
if (!child || current(child) !== value) {
|
|
100
|
+
child = draftOf(value, node, key);
|
|
101
|
+
node.children.set(key, child);
|
|
102
|
+
}
|
|
103
|
+
return child.proxy;
|
|
104
|
+
},
|
|
105
|
+
set(_target, key, value) {
|
|
106
|
+
if (typeof key === "symbol") return false;
|
|
107
|
+
const next = settled(value);
|
|
108
|
+
const source = current(node);
|
|
109
|
+
// Writing what is there already is not a change, and must not cost a copy.
|
|
110
|
+
if (Object.prototype.hasOwnProperty.call(source, key) && Object.is(source[key], next)) return true;
|
|
111
|
+
writable(node)[key] = next;
|
|
112
|
+
node.children.delete(key);
|
|
113
|
+
return true;
|
|
114
|
+
},
|
|
115
|
+
deleteProperty(_target, key) {
|
|
116
|
+
if (typeof key === "symbol") return false;
|
|
117
|
+
if (!Object.prototype.hasOwnProperty.call(current(node), key)) return true;
|
|
118
|
+
delete writable(node)[key];
|
|
119
|
+
node.children.delete(key);
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
defineProperty(_target, key, descriptor) {
|
|
123
|
+
if (typeof key === "symbol" || !("value" in descriptor)) return false;
|
|
124
|
+
writable(node)[key] = settled(descriptor.value);
|
|
125
|
+
node.children.delete(key);
|
|
126
|
+
return true;
|
|
127
|
+
},
|
|
128
|
+
has: (_target, key) => Reflect.has(current(node), key),
|
|
129
|
+
ownKeys: () => Reflect.ownKeys(current(node)),
|
|
130
|
+
getOwnPropertyDescriptor(_target, key) {
|
|
131
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(current(node), key);
|
|
132
|
+
// What the draft holds is not the proxy's own target, so nothing in it may claim to be fixed
|
|
133
|
+
// there, except an array's length, which is.
|
|
134
|
+
if (descriptor && !(Array.isArray(node.base) && key === "length")) descriptor.configurable = true;
|
|
135
|
+
return descriptor;
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|