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
package/dist/lockstep.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lockstep: the room does not simulate. It puts everyone's inputs in order,
|
|
3
|
+
* cuts them into numbered turns and sends every player the same turns. Each
|
|
4
|
+
* client runs the same deterministic simulation over them.
|
|
5
|
+
*/
|
|
6
|
+
/** Runs a recording from the start and returns the simulation as it stands after turn `upTo`. */
|
|
7
|
+
export function runReplay(replay, simulation, upTo = replay.turn) {
|
|
8
|
+
const sim = simulation.create(replay.seed);
|
|
9
|
+
stepThrough(sim, replay, simulation, 0, upTo);
|
|
10
|
+
return sim;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Advances `sim`, which is at turn `from`, to turn `upTo` over a recording.
|
|
14
|
+
* The recording may start anywhere at or before `from`.
|
|
15
|
+
*/
|
|
16
|
+
export function stepThrough(sim, replay, simulation, from, upTo = replay.turn) {
|
|
17
|
+
const legacy = replay.v === 1;
|
|
18
|
+
let next = 0;
|
|
19
|
+
while (next < replay.turns.length && replay.turns[next][0] <= from)
|
|
20
|
+
next += 1;
|
|
21
|
+
for (let turn = from + 1; turn <= upTo; turn++) {
|
|
22
|
+
const recorded = replay.turns[next];
|
|
23
|
+
if (recorded && recorded[0] === turn) {
|
|
24
|
+
simulation.step(sim, legacy ? recorded[1].map(fromVersion1) : recorded[1], turn);
|
|
25
|
+
next += 1;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
simulation.step(sim, [], turn);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Version 1 recordings spelled entries `{ p, i, e }`. */
|
|
33
|
+
function fromVersion1(entry) {
|
|
34
|
+
const { p, i, e } = entry;
|
|
35
|
+
return e === undefined ? { player: p, input: i } : { player: p, event: e };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A small deterministic random number generator (mulberry32). `Math.random`
|
|
39
|
+
* differs on every machine; this gives every client the same sequence for the
|
|
40
|
+
* same seed. Returns numbers in [0, 1).
|
|
41
|
+
*/
|
|
42
|
+
export function seededRandom(seed) {
|
|
43
|
+
let a = seed >>> 0;
|
|
44
|
+
const next = () => {
|
|
45
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
46
|
+
let t = a;
|
|
47
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
48
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
49
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
50
|
+
};
|
|
51
|
+
return Object.defineProperty(next, "state", { get: () => a, enumerable: true });
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A fingerprint of anything JSON can hold: the first 32 hex digits of the
|
|
55
|
+
* SHA-256 of its JSON text. Two simulations that save to the same JSON have
|
|
56
|
+
* the same one, and nobody can make a different simulation that has it too,
|
|
57
|
+
* which is what makes it the right `hash` for snapshots. It is what
|
|
58
|
+
* `LockstepClient` uses when a simulation has `save` and no `hash`.
|
|
59
|
+
*
|
|
60
|
+
* It reads the whole value each time: about a millisecond for 100 KB of
|
|
61
|
+
* JSON. For a very large simulation, report it less often (`hashEvery`).
|
|
62
|
+
*/
|
|
63
|
+
export function fingerprint(value) {
|
|
64
|
+
return fingerprintOfText(JSON.stringify(value) ?? "");
|
|
65
|
+
}
|
|
66
|
+
/** `fingerprint` of a value whose JSON text is at hand already. */
|
|
67
|
+
export function fingerprintOfText(text) {
|
|
68
|
+
return sha256(new TextEncoder().encode(text)).slice(0, 32);
|
|
69
|
+
}
|
|
70
|
+
const K = new Int32Array([
|
|
71
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be,
|
|
72
|
+
0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa,
|
|
73
|
+
0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85,
|
|
74
|
+
0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
|
75
|
+
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
|
|
76
|
+
0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
77
|
+
]);
|
|
78
|
+
/**
|
|
79
|
+
* SHA-256, as hex. Written out because a simulation is hashed in the middle
|
|
80
|
+
* of a turn, and the one browsers offer, `crypto.subtle.digest`, answers
|
|
81
|
+
* later.
|
|
82
|
+
*/
|
|
83
|
+
function sha256(bytes) {
|
|
84
|
+
const length = bytes.length;
|
|
85
|
+
const padded = new Uint8Array(((length + 9 + 63) >> 6) << 6);
|
|
86
|
+
padded.set(bytes);
|
|
87
|
+
padded[length] = 0x80;
|
|
88
|
+
const view = new DataView(padded.buffer);
|
|
89
|
+
view.setUint32(padded.length - 8, Math.floor(length / 0x20000000));
|
|
90
|
+
view.setUint32(padded.length - 4, (length << 3) >>> 0);
|
|
91
|
+
const w = new Int32Array(64);
|
|
92
|
+
const rotate = (n, by) => (n >>> by) | (n << (32 - by));
|
|
93
|
+
let h0 = 0x6a09e667;
|
|
94
|
+
let h1 = 0xbb67ae85 | 0;
|
|
95
|
+
let h2 = 0x3c6ef372;
|
|
96
|
+
let h3 = 0xa54ff53a | 0;
|
|
97
|
+
let h4 = 0x510e527f;
|
|
98
|
+
let h5 = 0x9b05688c | 0;
|
|
99
|
+
let h6 = 0x1f83d9ab;
|
|
100
|
+
let h7 = 0x5be0cd19;
|
|
101
|
+
for (let at = 0; at < padded.length; at += 64) {
|
|
102
|
+
for (let i = 0; i < 16; i++)
|
|
103
|
+
w[i] = view.getInt32(at + i * 4);
|
|
104
|
+
for (let i = 16; i < 64; i++) {
|
|
105
|
+
const x = w[i - 15];
|
|
106
|
+
const y = w[i - 2];
|
|
107
|
+
w[i] = (w[i - 16] + (rotate(x, 7) ^ rotate(x, 18) ^ (x >>> 3)) + w[i - 7] + (rotate(y, 17) ^ rotate(y, 19) ^ (y >>> 10))) | 0;
|
|
108
|
+
}
|
|
109
|
+
let a = h0;
|
|
110
|
+
let b = h1;
|
|
111
|
+
let c = h2;
|
|
112
|
+
let d = h3;
|
|
113
|
+
let e = h4;
|
|
114
|
+
let f = h5;
|
|
115
|
+
let g = h6;
|
|
116
|
+
let h = h7;
|
|
117
|
+
for (let i = 0; i < 64; i++) {
|
|
118
|
+
const t1 = (h + (rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]) | 0;
|
|
119
|
+
const t2 = ((rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
120
|
+
h = g;
|
|
121
|
+
g = f;
|
|
122
|
+
f = e;
|
|
123
|
+
e = (d + t1) | 0;
|
|
124
|
+
d = c;
|
|
125
|
+
c = b;
|
|
126
|
+
b = a;
|
|
127
|
+
a = (t1 + t2) | 0;
|
|
128
|
+
}
|
|
129
|
+
h0 = (h0 + a) | 0;
|
|
130
|
+
h1 = (h1 + b) | 0;
|
|
131
|
+
h2 = (h2 + c) | 0;
|
|
132
|
+
h3 = (h3 + d) | 0;
|
|
133
|
+
h4 = (h4 + e) | 0;
|
|
134
|
+
h5 = (h5 + f) | 0;
|
|
135
|
+
h6 = (h6 + g) | 0;
|
|
136
|
+
h7 = (h7 + h) | 0;
|
|
137
|
+
}
|
|
138
|
+
return [h0, h1, h2, h3, h4, h5, h6, h7].map((n) => (n >>> 0).toString(16).padStart(8, "0")).join("");
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=lockstep.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lockstep.js","sourceRoot":"","sources":["../src/lockstep.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA0HH,iGAAiG;AACjG,MAAM,UAAU,SAAS,CACvB,MAAqB,EACrB,UAAkC,EAClC,IAAI,GAAG,MAAM,CAAC,IAAI;IAElB,MAAM,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3C,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;IAC9C,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CACzB,GAAQ,EACR,MAAqB,EACrB,UAAkC,EAClC,IAAY,EACZ,IAAI,GAAG,MAAM,CAAC,IAAI;IAElB,MAAM,MAAM,GAAI,MAAwB,CAAC,CAAC,KAAK,CAAC,CAAC;IACjD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,OAAO,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,IAAI,MAAM,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|
|
3
|
+
*
|
|
4
|
+
* A patch mirrors the shape of the object it changes. For each key:
|
|
5
|
+
* - a nested patch object: go into that key and patch what is there;
|
|
6
|
+
* - `[value]`: set the key to `value`;
|
|
7
|
+
* - `[changes, length]`: the key holds an array; `length` is how long it
|
|
8
|
+
* now is, and `changes` has, by index, what to do with the elements that
|
|
9
|
+
* changed, each in one of these same forms;
|
|
10
|
+
* - `0`: delete the key.
|
|
11
|
+
*
|
|
12
|
+
* An array is patched element by element where its elements kept their
|
|
13
|
+
* places: a unit that moved costs its new position, not the whole list. An
|
|
14
|
+
* array whose elements shifted, because one was put in or taken out before
|
|
15
|
+
* the end, is sent whole. So for things that come and go, an object keyed by
|
|
16
|
+
* id is still the better home.
|
|
17
|
+
*/
|
|
18
|
+
export type Patch = {
|
|
19
|
+
[key: string]: Patch | [unknown] | ArrayPatch | 0;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* What changed in an array: by index, the elements to patch or replace, and
|
|
23
|
+
* the array's new length. Elements past the new length are gone.
|
|
24
|
+
*/
|
|
25
|
+
export type ArrayPatch = [changes: {
|
|
26
|
+
[index: string]: Patch | [unknown] | ArrayPatch;
|
|
27
|
+
}, length: number];
|
|
28
|
+
/**
|
|
29
|
+
* The patch that turns `prev` into `next`. Returns `undefined` when they are
|
|
30
|
+
* equal, and `null` when they cannot be patched because one of them is not a
|
|
31
|
+
* plain object: send `next` whole in that case.
|
|
32
|
+
*
|
|
33
|
+
* `next` is read as JSON would write it: keys holding `undefined` count as absent.
|
|
34
|
+
*/
|
|
35
|
+
export declare function diff(prev: unknown, next: unknown): Patch | null | undefined;
|
|
36
|
+
/** Applies a patch without touching `base`: unchanged branches are shared with it. */
|
|
37
|
+
export declare function applyPatch<T>(base: T, patch: Patch): T;
|
|
38
|
+
/**
|
|
39
|
+
* The same change for a client that does not know array patches: wherever
|
|
40
|
+
* the patch has one, the whole array as it is in `next`, the state the patch
|
|
41
|
+
* leads to. Returns the patch itself when there are none in it.
|
|
42
|
+
*/
|
|
43
|
+
export declare function withoutArrayPatches(patch: Patch, next: unknown): Patch;
|
|
44
|
+
/**
|
|
45
|
+
* How to turn the array `prev` into `next`: a patch of its elements,
|
|
46
|
+
* `undefined` when they are equal, or `null` when sending `next` whole is the
|
|
47
|
+
* better way, which it is when most of what changed are elements replaced
|
|
48
|
+
* outright.
|
|
49
|
+
*/
|
|
50
|
+
export declare function diffArrays(prev: unknown[], next: unknown[]): ArrayPatch | null | undefined;
|
|
51
|
+
/** Whether two values are the same as JSON. @internal */
|
|
52
|
+
export declare function jsonEqual(a: unknown, b: unknown): boolean;
|
|
53
|
+
/** @internal */
|
|
54
|
+
export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
package/dist/patch.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The patch that turns `prev` into `next`. Returns `undefined` when they are
|
|
3
|
+
* equal, and `null` when they cannot be patched because one of them is not a
|
|
4
|
+
* plain object: send `next` whole in that case.
|
|
5
|
+
*
|
|
6
|
+
* `next` is read as JSON would write it: keys holding `undefined` count as absent.
|
|
7
|
+
*/
|
|
8
|
+
export function diff(prev, next) {
|
|
9
|
+
if (isPlainObject(prev) && isPlainObject(next))
|
|
10
|
+
return diffObjects(prev, next);
|
|
11
|
+
return jsonEqual(prev, next) ? undefined : null;
|
|
12
|
+
}
|
|
13
|
+
/** Applies a patch without touching `base`: unchanged branches are shared with it. */
|
|
14
|
+
export function applyPatch(base, patch) {
|
|
15
|
+
const out = isPlainObject(base) ? { ...base } : {};
|
|
16
|
+
for (const key of Object.keys(patch)) {
|
|
17
|
+
if (key === "__proto__")
|
|
18
|
+
continue;
|
|
19
|
+
const change = patch[key];
|
|
20
|
+
if (change === 0)
|
|
21
|
+
delete out[key];
|
|
22
|
+
else if (Array.isArray(change))
|
|
23
|
+
out[key] = change.length === 2 ? applyArrayPatch(own(out, key), change) : change[0];
|
|
24
|
+
else if (change)
|
|
25
|
+
out[key] = applyPatch(own(out, key), change);
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
/** An index as a patch writes it: a whole number from 0 up, with no sign and no leading zero. */
|
|
30
|
+
const INDEX = /^(0|[1-9][0-9]*)$/;
|
|
31
|
+
function applyArrayPatch(base, [changes, length]) {
|
|
32
|
+
const out = Array.isArray(base) ? base.slice(0, length) : [];
|
|
33
|
+
for (const key of Object.keys(changes)) {
|
|
34
|
+
const index = Number(key);
|
|
35
|
+
// Nothing is put beyond the length the patch gives, so a patch cannot make a huge array.
|
|
36
|
+
if (!INDEX.test(key) || index >= length)
|
|
37
|
+
continue;
|
|
38
|
+
const change = changes[key];
|
|
39
|
+
if (Array.isArray(change))
|
|
40
|
+
out[index] = change.length === 2 ? applyArrayPatch(out[index], change) : change[0];
|
|
41
|
+
else
|
|
42
|
+
out[index] = applyPatch(out[index], change);
|
|
43
|
+
}
|
|
44
|
+
// An element the patch left out, past the old end, is null rather than a hole.
|
|
45
|
+
for (let index = out.length; index < length; index++)
|
|
46
|
+
out[index] = null;
|
|
47
|
+
for (let index = 0; index < length; index++)
|
|
48
|
+
if (!(index in out))
|
|
49
|
+
out[index] = null;
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The same change for a client that does not know array patches: wherever
|
|
54
|
+
* the patch has one, the whole array as it is in `next`, the state the patch
|
|
55
|
+
* leads to. Returns the patch itself when there are none in it.
|
|
56
|
+
*/
|
|
57
|
+
export function withoutArrayPatches(patch, next) {
|
|
58
|
+
let out;
|
|
59
|
+
const after = isPlainObject(next) ? next : {};
|
|
60
|
+
for (const key of Object.keys(patch)) {
|
|
61
|
+
const change = patch[key];
|
|
62
|
+
let plain = change;
|
|
63
|
+
if (Array.isArray(change)) {
|
|
64
|
+
if (change.length === 2)
|
|
65
|
+
plain = [own(after, key)];
|
|
66
|
+
}
|
|
67
|
+
else if (change !== 0) {
|
|
68
|
+
plain = withoutArrayPatches(change, own(after, key));
|
|
69
|
+
}
|
|
70
|
+
if (plain !== change)
|
|
71
|
+
(out ??= { ...patch })[key] = plain;
|
|
72
|
+
}
|
|
73
|
+
return out ?? patch;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* How to turn the array `prev` into `next`: a patch of its elements,
|
|
77
|
+
* `undefined` when they are equal, or `null` when sending `next` whole is the
|
|
78
|
+
* better way, which it is when most of what changed are elements replaced
|
|
79
|
+
* outright.
|
|
80
|
+
*/
|
|
81
|
+
export function diffArrays(prev, next) {
|
|
82
|
+
const changes = {};
|
|
83
|
+
let changed = 0;
|
|
84
|
+
let replaced = 0;
|
|
85
|
+
const shared = Math.min(prev.length, next.length);
|
|
86
|
+
for (let index = 0; index < shared; index++) {
|
|
87
|
+
const from = prev[index];
|
|
88
|
+
const to = next[index] === undefined ? null : next[index];
|
|
89
|
+
if (isPlainObject(from) && isPlainObject(to)) {
|
|
90
|
+
const inner = diffObjects(from, to);
|
|
91
|
+
if (!inner)
|
|
92
|
+
continue;
|
|
93
|
+
changes[index] = inner;
|
|
94
|
+
// An element most of whose keys changed is as good as replaced.
|
|
95
|
+
if (Object.keys(inner).length * 2 > Object.keys(to).length)
|
|
96
|
+
replaced += 1;
|
|
97
|
+
}
|
|
98
|
+
else if (Array.isArray(from) && Array.isArray(to)) {
|
|
99
|
+
const inner = diffArrays(from, to);
|
|
100
|
+
if (inner === undefined)
|
|
101
|
+
continue;
|
|
102
|
+
if (inner === null)
|
|
103
|
+
replaced += 1;
|
|
104
|
+
changes[index] = inner ?? [to];
|
|
105
|
+
}
|
|
106
|
+
else if (jsonEqual(from, to)) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
changes[index] = [to];
|
|
111
|
+
replaced += 1;
|
|
112
|
+
}
|
|
113
|
+
changed += 1;
|
|
114
|
+
// Elements replaced cost what they cost in the whole array, plus their index. Once they are
|
|
115
|
+
// more than half of it the whole array is no longer than the patch, and there is no need to
|
|
116
|
+
// look at the rest.
|
|
117
|
+
if (replaced * 2 > next.length)
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
replaced += next.length - shared;
|
|
121
|
+
if (replaced * 2 > next.length)
|
|
122
|
+
return null;
|
|
123
|
+
for (let index = shared; index < next.length; index++) {
|
|
124
|
+
changes[index] = [next[index] === undefined ? null : next[index]];
|
|
125
|
+
changed += 1;
|
|
126
|
+
}
|
|
127
|
+
if (changed === 0 && prev.length === next.length)
|
|
128
|
+
return undefined;
|
|
129
|
+
return [changes, next.length];
|
|
130
|
+
}
|
|
131
|
+
function diffObjects(prev, next) {
|
|
132
|
+
let patch;
|
|
133
|
+
for (const key of Object.keys(next)) {
|
|
134
|
+
const to = next[key];
|
|
135
|
+
if (to === undefined)
|
|
136
|
+
continue;
|
|
137
|
+
const from = own(prev, key);
|
|
138
|
+
if (isPlainObject(from) && isPlainObject(to)) {
|
|
139
|
+
const inner = diffObjects(from, to);
|
|
140
|
+
if (inner)
|
|
141
|
+
(patch ??= {})[key] = inner;
|
|
142
|
+
}
|
|
143
|
+
else if (Array.isArray(from) && Array.isArray(to)) {
|
|
144
|
+
const inner = diffArrays(from, to);
|
|
145
|
+
if (inner !== undefined)
|
|
146
|
+
(patch ??= {})[key] = inner ?? [to];
|
|
147
|
+
}
|
|
148
|
+
else if (from === undefined || !jsonEqual(from, to)) {
|
|
149
|
+
(patch ??= {})[key] = [to];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const key of Object.keys(prev)) {
|
|
153
|
+
if (prev[key] !== undefined && own(next, key) === undefined)
|
|
154
|
+
(patch ??= {})[key] = 0;
|
|
155
|
+
}
|
|
156
|
+
return patch;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* `object[key]`, but only if the object itself has the key. A unit called
|
|
160
|
+
* "toString" or "constructor" must not be mistaken for what every object inherits.
|
|
161
|
+
*/
|
|
162
|
+
function own(object, key) {
|
|
163
|
+
return Object.prototype.hasOwnProperty.call(object, key) ? object[key] : undefined;
|
|
164
|
+
}
|
|
165
|
+
/** Whether two values are the same as JSON. @internal */
|
|
166
|
+
export function jsonEqual(a, b) {
|
|
167
|
+
if (a === b)
|
|
168
|
+
return true;
|
|
169
|
+
if (Array.isArray(a)) {
|
|
170
|
+
if (!Array.isArray(b) || a.length !== b.length)
|
|
171
|
+
return false;
|
|
172
|
+
for (let i = 0; i < a.length; i++) {
|
|
173
|
+
if (!jsonEqual(a[i], b[i]))
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
if (isPlainObject(a) && isPlainObject(b))
|
|
179
|
+
return diffObjects(a, b) === undefined;
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
/** @internal */
|
|
183
|
+
export function isPlainObject(value) {
|
|
184
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=patch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../src/patch.ts"],"names":[],"mappings":"AAyBA;;;;;;GAMG;AACH,MAAM,UAAU,IAAI,CAAC,IAAa,EAAE,IAAa;IAC/C,IAAI,aAAa,CAAC,IAAI,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;QAAE,OAAO,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC/E,OAAO,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC;AAClD,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,UAAU,CAAI,IAAO,EAAE,KAAY;IACjD,MAAM,GAAG,GAA4B,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5E,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK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CAAC;AAED,gBAAgB;AAChB,MAAM,UAAU,aAAa,CAAC,KAAc;IAC1C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire protocol shared by the room kernel and the client.
|
|
3
|
+
*
|
|
4
|
+
* Every frame is a JSON text frame, except the keepalive: the client sends the
|
|
5
|
+
* literal string "ping" and the runtime answers "pong" without waking the room.
|
|
6
|
+
*/
|
|
7
|
+
import type { Patch } from "./patch.js";
|
|
8
|
+
/**
|
|
9
|
+
* The latest version of the protocol. A client asks for a version with `v`
|
|
10
|
+
* in the query when it connects, and the room's `welcome` frame says which
|
|
11
|
+
* it will speak: the one asked for, or this one if that is later. A client
|
|
12
|
+
* that does not ask is spoken to in version 1.
|
|
13
|
+
*
|
|
14
|
+
* Version 2 adds array patches: an array that changes can be sent element
|
|
15
|
+
* by element, as `[changes, length]` in a patch.
|
|
16
|
+
*
|
|
17
|
+
* Version 3 adds presence as changes: a `presence` frame can say who is new
|
|
18
|
+
* or different (`set`) and who is gone (`gone`) in place of the whole list.
|
|
19
|
+
*/
|
|
20
|
+
export declare const PROTOCOL_VERSION = 3;
|
|
21
|
+
/** The keepalive a client sends, as a plain text frame. @internal */
|
|
22
|
+
export declare const PING = "ping";
|
|
23
|
+
/** The runtime's answer to `PING`, sent without waking the room. @internal */
|
|
24
|
+
export declare const PONG = "pong";
|
|
25
|
+
/** A seat in a room. A player keeps their seat while disconnected. */
|
|
26
|
+
export interface Player {
|
|
27
|
+
/** The id the gateway's `authenticate` gave this player. */
|
|
28
|
+
id: string;
|
|
29
|
+
/** Whether the player has a connection open right now. */
|
|
30
|
+
connected: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* The player's name, when the gateway's `authenticate` gave one (signed-in
|
|
33
|
+
* players with `Accounts` have one). Only set in the hooks that run for a
|
|
34
|
+
* connecting request: `onJoin`, `onReconnect`, `onArrive` and `onHttp`.
|
|
35
|
+
* Keep it in your state if you need it later.
|
|
36
|
+
*/
|
|
37
|
+
name?: string;
|
|
38
|
+
}
|
|
39
|
+
/** A command for the room. Answered with `ack` or `reject` carrying the same `id`. */
|
|
40
|
+
export interface CommandFrame<Command = unknown> {
|
|
41
|
+
/** Frame type. */
|
|
42
|
+
t: "cmd";
|
|
43
|
+
/** Chosen by the client, unique per connection, echoed in the answer. */
|
|
44
|
+
id: number;
|
|
45
|
+
/** The command, as the room's `parseCommand` will receive it. */
|
|
46
|
+
data: Command;
|
|
47
|
+
}
|
|
48
|
+
/** Give up the seat. On `ack` the room closes the connection with `CloseCode.Removed`. */
|
|
49
|
+
export interface LeaveFrame {
|
|
50
|
+
/** Frame type. */
|
|
51
|
+
t: "leave";
|
|
52
|
+
/** Chosen by the client, echoed in the answer. */
|
|
53
|
+
id: number;
|
|
54
|
+
}
|
|
55
|
+
/** Every frame a client sends, apart from the `ping` keepalive. */
|
|
56
|
+
export type ClientMessage<Command = unknown> = CommandFrame<Command> | LeaveFrame;
|
|
57
|
+
/** First frame on every connection, reconnects included. */
|
|
58
|
+
export interface WelcomeFrame<View = unknown> {
|
|
59
|
+
/** Frame type. */
|
|
60
|
+
t: "welcome";
|
|
61
|
+
/** The version of the protocol the room speaks on this connection: see `PROTOCOL_VERSION`. */
|
|
62
|
+
v: number;
|
|
63
|
+
/** This player's id. */
|
|
64
|
+
you: string;
|
|
65
|
+
/** The room's change counter. Every later `state` or `patch` carries the next one. */
|
|
66
|
+
seq: number;
|
|
67
|
+
/** Ticks run so far; 0 in event mode. */
|
|
68
|
+
tick: number;
|
|
69
|
+
/** Ticks a second; 0 in event mode. */
|
|
70
|
+
tickRate: number;
|
|
71
|
+
/** This player's whole view. */
|
|
72
|
+
state: View;
|
|
73
|
+
/** Everyone with a seat, in join order. */
|
|
74
|
+
players: Player[];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The state changed, and this is the player's whole view: sent when the room
|
|
78
|
+
* does not know what the player holds, or when the view is not an object and
|
|
79
|
+
* so cannot be patched.
|
|
80
|
+
*/
|
|
81
|
+
export interface StateFrame<View = unknown> {
|
|
82
|
+
/** Frame type. */
|
|
83
|
+
t: "state";
|
|
84
|
+
/** The room's change counter after this change. */
|
|
85
|
+
seq: number;
|
|
86
|
+
/** Ticks run so far. */
|
|
87
|
+
tick?: number;
|
|
88
|
+
/** As in `PatchFrame`. */
|
|
89
|
+
c?: number;
|
|
90
|
+
/** This player's whole view. */
|
|
91
|
+
state: View;
|
|
92
|
+
}
|
|
93
|
+
/** The state changed: only what differs from what this player last received. */
|
|
94
|
+
export interface PatchFrame {
|
|
95
|
+
/** Frame type. */
|
|
96
|
+
t: "patch";
|
|
97
|
+
/** The room's change counter after this change. */
|
|
98
|
+
seq: number;
|
|
99
|
+
/** Ticks run so far. While the room is ticking, the tick that made the change. */
|
|
100
|
+
tick: number;
|
|
101
|
+
/**
|
|
102
|
+
* Outside the simulation, the `seq` of the view this is a patch of. A client
|
|
103
|
+
* that holds any other has missed something: it must not apply the patch,
|
|
104
|
+
* and reconnects to be sent its whole view.
|
|
105
|
+
*/
|
|
106
|
+
from?: number;
|
|
107
|
+
/**
|
|
108
|
+
* The `id` of the latest command of this player's that the view now
|
|
109
|
+
* reflects: that one and every earlier one have been applied, and their
|
|
110
|
+
* `ack` frames follow. Present when the frame is the first to show a
|
|
111
|
+
* command of theirs.
|
|
112
|
+
*/
|
|
113
|
+
c?: number;
|
|
114
|
+
/** Apply it with `applyPatch` to the view you have. */
|
|
115
|
+
p: Patch;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Someone joined, dropped, came back or left. Either the whole list, in
|
|
119
|
+
* `players`, or what changed in it since the last frame, in `set` and
|
|
120
|
+
* `gone`: a client of version 3 or later is sent whichever is shorter.
|
|
121
|
+
*/
|
|
122
|
+
export interface PresenceFrame {
|
|
123
|
+
/** Frame type. */
|
|
124
|
+
t: "presence";
|
|
125
|
+
/** Everyone with a seat, in join order. When present, it replaces the list. */
|
|
126
|
+
players?: Player[];
|
|
127
|
+
/**
|
|
128
|
+
* Players who are new, or whose `connected` or `name` changed. One the
|
|
129
|
+
* list has already takes the place of the old entry; a new one goes to
|
|
130
|
+
* the end, in the order given.
|
|
131
|
+
*/
|
|
132
|
+
set?: Player[];
|
|
133
|
+
/** The ids of players who no longer have a seat. */
|
|
134
|
+
gone?: string[];
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The command or leave with this `id` was applied, and the view this player
|
|
138
|
+
* holds reflects it. It follows the `state` or `patch` frame that shows the
|
|
139
|
+
* command's effect: at once in event mode, and after the next tick in a room
|
|
140
|
+
* that is ticking, since that is when the effect is sent. A leave is always
|
|
141
|
+
* answered at once.
|
|
142
|
+
*/
|
|
143
|
+
export interface AckFrame {
|
|
144
|
+
/** Frame type. */
|
|
145
|
+
t: "ack";
|
|
146
|
+
/** The `id` of the command or leave. */
|
|
147
|
+
id: number;
|
|
148
|
+
/** The change it made, or the current one if it changed nothing. */
|
|
149
|
+
seq: number;
|
|
150
|
+
/** In a room that is ticking: the tick the view is now of, whether or not a frame was sent for it. */
|
|
151
|
+
tick?: number;
|
|
152
|
+
}
|
|
153
|
+
/** The command or leave with this `id` was refused, and nothing changed. */
|
|
154
|
+
export interface RejectFrame {
|
|
155
|
+
/** Frame type. */
|
|
156
|
+
t: "reject";
|
|
157
|
+
/** The `id` of the command or leave. */
|
|
158
|
+
id: number;
|
|
159
|
+
/**
|
|
160
|
+
* What the room passed to `reject()`; else `invalid_command` if
|
|
161
|
+
* `parseCommand` threw, `internal_error` if a hook threw, or `rate_limited`.
|
|
162
|
+
*/
|
|
163
|
+
reason: string;
|
|
164
|
+
}
|
|
165
|
+
/** A frame the room could not read at all. */
|
|
166
|
+
export interface ErrorFrame {
|
|
167
|
+
/** Frame type. */
|
|
168
|
+
t: "error";
|
|
169
|
+
/** `invalid_json`, `invalid_message`, `message_too_large`, `binary_not_supported` or `rate_limited`. */
|
|
170
|
+
reason: string;
|
|
171
|
+
}
|
|
172
|
+
/** A message the room sent this player with `send` or `broadcast`. */
|
|
173
|
+
export interface MessageFrame {
|
|
174
|
+
/** Frame type. */
|
|
175
|
+
t: "msg";
|
|
176
|
+
/** The message, as the room passed it. */
|
|
177
|
+
d: unknown;
|
|
178
|
+
}
|
|
179
|
+
/** Every frame a room sends, apart from the `pong` keepalive. */
|
|
180
|
+
export type ServerMessage<View = unknown> = WelcomeFrame<View> | StateFrame<View> | PatchFrame | PresenceFrame | AckFrame | RejectFrame | ErrorFrame | MessageFrame;
|
|
181
|
+
/** WebSocket close codes the room uses. The client does not reconnect after those from 4000 to 4099. */
|
|
182
|
+
export declare const CloseCode: {
|
|
183
|
+
/** The same player opened a newer connection. */
|
|
184
|
+
readonly Replaced: 4000;
|
|
185
|
+
/** Every seat is taken. */
|
|
186
|
+
readonly RoomFull: 4001;
|
|
187
|
+
/** `onJoin` refused the player; the close reason carries why. */
|
|
188
|
+
readonly JoinRejected: 4002;
|
|
189
|
+
/** The player no longer has a seat: they left, or the room removed them. The close reason says which. */
|
|
190
|
+
readonly Removed: 4003;
|
|
191
|
+
/**
|
|
192
|
+
* The room handed the player over to another room; the close reason is that
|
|
193
|
+
* room's id. The client reconnects there by itself.
|
|
194
|
+
*/
|
|
195
|
+
readonly Moved: 4004;
|
|
196
|
+
/**
|
|
197
|
+
* A ticking room heard nothing on this connection for `silentAfterMs`, not
|
|
198
|
+
* even a keepalive, and took it for dead. A client that is there after
|
|
199
|
+
* all connects again: this code is outside those it gives up on.
|
|
200
|
+
*/
|
|
201
|
+
readonly Silent: 4100;
|
|
202
|
+
};
|
|
203
|
+
/** Sent by a matchmaker to every player of a match it has just put together. */
|
|
204
|
+
export interface MatchFound {
|
|
205
|
+
/** Frame type. */
|
|
206
|
+
t: "match";
|
|
207
|
+
/** The room to connect to. Only these players can take a seat in it. */
|
|
208
|
+
room: string;
|
|
209
|
+
/** The players of the match, this one included. */
|
|
210
|
+
players: string[];
|
|
211
|
+
/** With a matchmaker that splits matches into sides: the players of each, in order. */
|
|
212
|
+
teams?: string[][];
|
|
213
|
+
}
|
|
214
|
+
/** What a room puts on its queue when it calls `finishMatch`. */
|
|
215
|
+
export interface MatchResult {
|
|
216
|
+
/** Unique per finished match. Safe to use as a primary key: redeliveries carry the same id. */
|
|
217
|
+
id: string;
|
|
218
|
+
/** The room id, if the room knows it (it does when players came through the gateway). */
|
|
219
|
+
room: string | null;
|
|
220
|
+
/** Epoch milliseconds. */
|
|
221
|
+
endedAt: number;
|
|
222
|
+
/** Everyone who held a seat when the match ended. */
|
|
223
|
+
players: string[];
|
|
224
|
+
/** Whatever the room passed to `finishMatch`. */
|
|
225
|
+
result: Record<string, unknown>;
|
|
226
|
+
/** Where the replay was stored in the room's R2 bucket, if it stored one. */
|
|
227
|
+
replayKey?: string;
|
|
228
|
+
}
|
|
229
|
+
/** Whether a client should stop reconnecting after this close code. @internal */
|
|
230
|
+
export declare function isFatalCloseCode(code: number): boolean;
|