@margoengine/netcode 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1387 -2
- package/dist/client.d.ts +167 -0
- package/dist/clock.d.ts +29 -0
- package/dist/extrapolate.d.ts +42 -0
- package/dist/extrapolate.js +25 -0
- package/dist/game.d.ts +68 -0
- package/dist/generate-rust.d.ts +14 -0
- package/dist/generate-rust.js +184 -0
- package/dist/generate.d.ts +25 -0
- package/dist/generate.js +156 -0
- package/dist/hitboxes.d.ts +74 -0
- package/dist/hitboxes.js +124 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +457 -0
- package/dist/interest.d.ts +101 -0
- package/dist/interest.js +166 -0
- package/dist/interpolation-delay.d.ts +114 -0
- package/dist/interpolation-delay.js +79 -0
- package/dist/interpolation-fields.d.ts +117 -0
- package/dist/interpolation-fields.js +147 -0
- package/dist/interpolation-timeline.d.ts +100 -0
- package/dist/interpolation-timeline.js +122 -0
- package/dist/interpolation.d.ts +39 -0
- package/dist/interpolation.js +61 -0
- package/dist/lag.d.ts +62 -0
- package/dist/lobby.d.ts +77 -0
- package/dist/lobby.js +96 -0
- package/dist/loopback.d.ts +92 -0
- package/dist/media.d.ts +215 -0
- package/dist/media.js +305 -0
- package/dist/migrate.d.ts +113 -0
- package/dist/migrate.js +289 -0
- package/dist/node.d.ts +101 -0
- package/dist/node.js +166 -0
- package/dist/physics.d.ts +133 -0
- package/dist/physics.js +169 -0
- package/dist/presence.d.ts +156 -0
- package/dist/presence.js +226 -0
- package/dist/protocol-asi_qndb.js +122 -0
- package/dist/protocol.d.ts +75 -0
- package/dist/relay.d.ts +152 -0
- package/dist/relay.js +235 -0
- package/dist/rollback.d.ts +131 -0
- package/dist/rollback.js +194 -0
- package/dist/rooms.d.ts +366 -0
- package/dist/rooms.js +479 -0
- package/dist/schema-CMfqK4Gu.js +616 -0
- package/dist/schema.d.ts +263 -0
- package/dist/server-DAVzHcWW.js +203 -0
- package/dist/server.d.ts +147 -0
- package/dist/shared/bytes.d.ts +38 -0
- package/dist/signal.d.ts +156 -0
- package/dist/signal.js +261 -0
- package/dist/smoothing.d.ts +76 -0
- package/dist/smoothing.js +155 -0
- package/dist/transport-DeQodwyv.js +49 -0
- package/dist/transport.d.ts +76 -0
- package/dist/webrtc.d.ts +32 -0
- package/dist/webrtc.js +64 -0
- package/dist/websocket.d.ts +27 -0
- package/dist/websocket.js +42 -0
- package/dist/webtransport.d.ts +83 -0
- package/dist/webtransport.js +120 -0
- package/package.json +138 -4
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { n as e, t } from "./schema-CMfqK4Gu.js";
|
|
2
|
+
//#region src/protocol.ts
|
|
3
|
+
var n = 1, r = 32, i = 1024, a = (e) => {
|
|
4
|
+
let t = 2166136261;
|
|
5
|
+
for (let n of e) t = Math.imul(t ^ n, 16777619) >>> 0;
|
|
6
|
+
return t;
|
|
7
|
+
}, o = [
|
|
8
|
+
"hello",
|
|
9
|
+
"welcome",
|
|
10
|
+
"inputs",
|
|
11
|
+
"snapshot",
|
|
12
|
+
"ping",
|
|
13
|
+
"pong",
|
|
14
|
+
"message"
|
|
15
|
+
], s = (e, t) => {
|
|
16
|
+
if (!Number.isInteger(t) || t < 0 || t > 2 ** 32 - 1) throw RangeError(`Envelope ${e} is ${t}; pass an integer from 0 to 2^32 - 1`);
|
|
17
|
+
return t;
|
|
18
|
+
};
|
|
19
|
+
function c(t) {
|
|
20
|
+
let n = new e(32);
|
|
21
|
+
switch (n.u8(o.indexOf(t.kind) + 1), t.kind) {
|
|
22
|
+
case "hello":
|
|
23
|
+
n.varint(s("version", t.version)), n.u32(s("schema", t.schema));
|
|
24
|
+
break;
|
|
25
|
+
case "welcome":
|
|
26
|
+
n.varint(s("player", t.player)), n.varint(s("tickRate", t.tickRate)), n.varint(s("tick", t.tick)), n.varint(s("snapshotEvery", t.snapshotEvery));
|
|
27
|
+
break;
|
|
28
|
+
case "inputs":
|
|
29
|
+
if (t.inputs.length > 32) throw RangeError(`Envelope carries ${t.inputs.length} inputs; send at most 32`);
|
|
30
|
+
n.varint(s("ack", t.ack)), n.varint(s("first", t.first)), n.varint(t.inputs.length);
|
|
31
|
+
for (let e of t.inputs) {
|
|
32
|
+
if (e.length > 1024) throw RangeError(`Input is ${e.length} bytes; keep inputs under 1025`);
|
|
33
|
+
n.varint(e.length), n.raw(e);
|
|
34
|
+
}
|
|
35
|
+
break;
|
|
36
|
+
case "snapshot":
|
|
37
|
+
n.varint(s("tick", t.tick)), n.varint(s("baseline", t.baseline)), n.varint(s("input", t.input)), n.varint(s("missed", t.missed)), n.raw(t.data);
|
|
38
|
+
break;
|
|
39
|
+
case "ping":
|
|
40
|
+
n.f64(t.time);
|
|
41
|
+
break;
|
|
42
|
+
case "pong":
|
|
43
|
+
n.f64(t.time), n.varint(s("tick", t.tick)), n.f32(t.fraction);
|
|
44
|
+
break;
|
|
45
|
+
case "message": n.varint(s("type", t.type)), n.raw(t.data);
|
|
46
|
+
}
|
|
47
|
+
return n.done();
|
|
48
|
+
}
|
|
49
|
+
function l(e) {
|
|
50
|
+
let n = new t(e, "Netcode message", "send what encodeEnvelope returned"), r = o[n.u8() - 1], a = () => {
|
|
51
|
+
let t = e.slice(n.at);
|
|
52
|
+
return n.at = e.length, t;
|
|
53
|
+
}, s;
|
|
54
|
+
switch (r) {
|
|
55
|
+
case "hello":
|
|
56
|
+
s = {
|
|
57
|
+
kind: r,
|
|
58
|
+
version: n.varint(),
|
|
59
|
+
schema: n.u32()
|
|
60
|
+
};
|
|
61
|
+
break;
|
|
62
|
+
case "welcome":
|
|
63
|
+
s = {
|
|
64
|
+
kind: r,
|
|
65
|
+
player: n.varint(),
|
|
66
|
+
tickRate: n.varint(),
|
|
67
|
+
tick: n.varint(),
|
|
68
|
+
snapshotEvery: n.varint()
|
|
69
|
+
};
|
|
70
|
+
break;
|
|
71
|
+
case "inputs": {
|
|
72
|
+
let t = n.varint(), a = n.varint(), o = n.count(32), c = [];
|
|
73
|
+
for (let t = 0; t < o; t++) {
|
|
74
|
+
let t = n.count(i);
|
|
75
|
+
n.need(t), c.push(e.slice(n.at, n.at += t));
|
|
76
|
+
}
|
|
77
|
+
if (a + o > 2 ** 32) throw n.fail("names a tick past 2^32 - 1");
|
|
78
|
+
s = {
|
|
79
|
+
kind: r,
|
|
80
|
+
ack: t,
|
|
81
|
+
first: a,
|
|
82
|
+
inputs: c
|
|
83
|
+
};
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case "snapshot":
|
|
87
|
+
s = {
|
|
88
|
+
kind: r,
|
|
89
|
+
tick: n.varint(),
|
|
90
|
+
baseline: n.varint(),
|
|
91
|
+
input: n.varint(),
|
|
92
|
+
missed: n.varint(),
|
|
93
|
+
data: a()
|
|
94
|
+
};
|
|
95
|
+
break;
|
|
96
|
+
case "ping":
|
|
97
|
+
s = {
|
|
98
|
+
kind: r,
|
|
99
|
+
time: n.f64()
|
|
100
|
+
};
|
|
101
|
+
break;
|
|
102
|
+
case "pong":
|
|
103
|
+
s = {
|
|
104
|
+
kind: r,
|
|
105
|
+
time: n.f64(),
|
|
106
|
+
tick: n.varint(),
|
|
107
|
+
fraction: n.f32()
|
|
108
|
+
};
|
|
109
|
+
break;
|
|
110
|
+
case "message":
|
|
111
|
+
s = {
|
|
112
|
+
kind: r,
|
|
113
|
+
type: n.varint(),
|
|
114
|
+
data: a()
|
|
115
|
+
};
|
|
116
|
+
break;
|
|
117
|
+
default: throw n.fail(`has unknown kind ${e[0]}`);
|
|
118
|
+
}
|
|
119
|
+
return n.end(), s;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
export { l as a, a as i, r as n, c as o, i as r, n as t };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** Version of the envelope; a server closes connections that say another. */
|
|
2
|
+
export declare const ENVELOPE_VERSION = 1;
|
|
3
|
+
/** Most inputs one `inputs` message may carry. */
|
|
4
|
+
export declare const MAX_INPUTS_PER_MESSAGE = 32;
|
|
5
|
+
/** Most bytes of one encoded input. */
|
|
6
|
+
export declare const MAX_INPUT_BYTES = 1024;
|
|
7
|
+
/** The kind byte of a rollback session's inputs message. */
|
|
8
|
+
export declare const ROLLBACK_INPUTS = 32;
|
|
9
|
+
/** FNV-1a over raw bytes (the schema hash's function runs over text): what rollback peers and spectators compare. */
|
|
10
|
+
export declare const checksumOf: (bytes: Uint8Array) => number;
|
|
11
|
+
/** Most bytes of a snapshot message around its data: the kind and four 32-bit varints. */
|
|
12
|
+
export declare const SNAPSHOT_HEADER_BYTES = 21;
|
|
13
|
+
/** One envelope message. Ticks are unsigned 32-bit; tick 0 is the state before the first step. */
|
|
14
|
+
export type Envelope =
|
|
15
|
+
/** Client to server, first: the envelope version and the schema hash. */
|
|
16
|
+
{
|
|
17
|
+
kind: 'hello';
|
|
18
|
+
version: number;
|
|
19
|
+
schema: number;
|
|
20
|
+
}
|
|
21
|
+
/** Server to client: your player id, the tick rate, the server's tick and how often it sends snapshots. */
|
|
22
|
+
| {
|
|
23
|
+
kind: 'welcome';
|
|
24
|
+
player: number;
|
|
25
|
+
tickRate: number;
|
|
26
|
+
tick: number;
|
|
27
|
+
snapshotEvery: number;
|
|
28
|
+
}
|
|
29
|
+
/** Client to server: inputs for ticks `first`, `first + 1`, ... and the newest snapshot tick the client holds. */
|
|
30
|
+
| {
|
|
31
|
+
kind: 'inputs';
|
|
32
|
+
ack: number;
|
|
33
|
+
first: number;
|
|
34
|
+
inputs: Uint8Array[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Server to client: the state after `tick`, encoded against the snapshot of tick `baseline` (0: in full); the
|
|
38
|
+
* newest tick of this client's inputs the server has, and how many ticks it had to run without one.
|
|
39
|
+
*/
|
|
40
|
+
| {
|
|
41
|
+
kind: 'snapshot';
|
|
42
|
+
tick: number;
|
|
43
|
+
baseline: number;
|
|
44
|
+
input: number;
|
|
45
|
+
missed: number;
|
|
46
|
+
data: Uint8Array;
|
|
47
|
+
}
|
|
48
|
+
/** Client to server: the client's clock in milliseconds. */
|
|
49
|
+
| {
|
|
50
|
+
kind: 'ping';
|
|
51
|
+
time: number;
|
|
52
|
+
}
|
|
53
|
+
/** Server to client: the ping's time back, the server's last tick and how far (0 to 1) it is into the next. */
|
|
54
|
+
| {
|
|
55
|
+
kind: 'pong';
|
|
56
|
+
time: number;
|
|
57
|
+
tick: number;
|
|
58
|
+
fraction: number;
|
|
59
|
+
}
|
|
60
|
+
/** Either way, reliable: a game message of schema type number `type`. */
|
|
61
|
+
| {
|
|
62
|
+
kind: 'message';
|
|
63
|
+
type: number;
|
|
64
|
+
data: Uint8Array;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Bytes of one envelope message.
|
|
68
|
+
* @throws RangeError for a field out of range or more than {@link MAX_INPUTS_PER_MESSAGE} inputs.
|
|
69
|
+
*/
|
|
70
|
+
export declare function encodeEnvelope(message: Envelope): Uint8Array;
|
|
71
|
+
/**
|
|
72
|
+
* The envelope message in `bytes`.
|
|
73
|
+
* @throws RangeError for truncated or malformed bytes (never anything else).
|
|
74
|
+
*/
|
|
75
|
+
export declare function decodeEnvelope(bytes: Uint8Array): Envelope;
|
package/dist/relay.d.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { Game } from './game.js';
|
|
2
|
+
import { type Connection } from './transport.js';
|
|
3
|
+
/** Options of {@link RollbackRelay}. */
|
|
4
|
+
export interface RollbackRelayOptions {
|
|
5
|
+
/** Ids of every player of the session, the same list the sessions get. */
|
|
6
|
+
players: readonly number[];
|
|
7
|
+
/** The sessions' `inputDelay` (default 2). */
|
|
8
|
+
inputDelay?: number;
|
|
9
|
+
/** Frames spectators stay behind the players, so a spectator cannot help a player (default 0). */
|
|
10
|
+
spectatorDelay?: number;
|
|
11
|
+
/** Frames kept for spectators who arrive late, who replay from the start (default 216,000: an hour at 60 Hz). */
|
|
12
|
+
history?: number;
|
|
13
|
+
/** Most spectators at once (default 256); the next is closed with `CloseCode.full`. */
|
|
14
|
+
maxSpectators?: number;
|
|
15
|
+
/** Return false to refuse a connection; `player` is undefined for a spectator. Default: anyone. */
|
|
16
|
+
authorize?(player: number | undefined, token: string): boolean;
|
|
17
|
+
}
|
|
18
|
+
/** Counts since the relay started. */
|
|
19
|
+
export interface RollbackRelayStats {
|
|
20
|
+
/** Messages passed between players. */
|
|
21
|
+
forwarded: number;
|
|
22
|
+
/** Frames every player's input is known for. */
|
|
23
|
+
frames: number;
|
|
24
|
+
/** Frames sent to spectators, summed over them. */
|
|
25
|
+
spectated: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Where the players of a rollback session meet: pass it each peer's and spectator's connection with
|
|
29
|
+
* {@link RollbackRelay.accept}. It trusts nothing a peer says about who it is once joined, and runs no game.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const relay = new RollbackRelay({ players: [1, 2, 3, 4] });
|
|
34
|
+
* listener.onconnection = (connection) => relay.accept(connection);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class RollbackRelay {
|
|
38
|
+
private readonly options;
|
|
39
|
+
/** Counts since the relay started. */
|
|
40
|
+
readonly stats: RollbackRelayStats;
|
|
41
|
+
private readonly players;
|
|
42
|
+
private readonly links;
|
|
43
|
+
private readonly inputs;
|
|
44
|
+
private readonly spectators;
|
|
45
|
+
private readonly log;
|
|
46
|
+
private readonly delay;
|
|
47
|
+
private readonly hold;
|
|
48
|
+
private readonly history;
|
|
49
|
+
private readonly maxSpectators;
|
|
50
|
+
private base;
|
|
51
|
+
private complete;
|
|
52
|
+
private closed;
|
|
53
|
+
/** @throws TypeError for an empty player list; RangeError for an option out of range. */
|
|
54
|
+
constructor(options: RollbackRelayOptions);
|
|
55
|
+
/** The newest frame every player's input is known for. */
|
|
56
|
+
get frame(): number;
|
|
57
|
+
/** How many spectators are watching. */
|
|
58
|
+
get watching(): number;
|
|
59
|
+
/**
|
|
60
|
+
* Takes a connection: its first reliable message says whether it is a player or a spectator.
|
|
61
|
+
* @throws MargoError `disposed` once the relay is closed.
|
|
62
|
+
*/
|
|
63
|
+
accept(connection: Connection): void;
|
|
64
|
+
private refuse;
|
|
65
|
+
private greet;
|
|
66
|
+
private leave;
|
|
67
|
+
private pass;
|
|
68
|
+
/** Keeps the inputs in a session message on its way through. Bytes that are not inputs are none of the relay's. */
|
|
69
|
+
private learn;
|
|
70
|
+
private advance;
|
|
71
|
+
/** Sends `connection` every frame it may see and has not had. */
|
|
72
|
+
private feed;
|
|
73
|
+
private watch;
|
|
74
|
+
/** Closes every connection. */
|
|
75
|
+
close(): void;
|
|
76
|
+
/** Same as {@link RollbackRelay.close}, for `using`. */
|
|
77
|
+
[Symbol.dispose](): void;
|
|
78
|
+
}
|
|
79
|
+
/** Options of {@link relayPeers}. */
|
|
80
|
+
export interface RelayPeersOptions {
|
|
81
|
+
/** Ids of every player of the session. */
|
|
82
|
+
players: readonly number[];
|
|
83
|
+
/** This peer's player id. */
|
|
84
|
+
local: number;
|
|
85
|
+
/** What the relay's `authorize` gets. */
|
|
86
|
+
token?: string;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The other players of a session, seen through one `link` to a {@link RollbackRelay}: pass the result as the session's
|
|
90
|
+
* `peers`. Closing them all closes the link.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const session = new RollbackSession(game, {
|
|
95
|
+
* players: [1, 2, 3, 4],
|
|
96
|
+
* local: 1,
|
|
97
|
+
* peers: relayPeers(await openWebSocket(relayUrl), { players: [1, 2, 3, 4], local: 1 }),
|
|
98
|
+
* input: () => readInput(),
|
|
99
|
+
* });
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare function relayPeers(link: Connection, { players, local, token }: RelayPeersOptions): Map<number, Connection>;
|
|
103
|
+
/** Options of {@link RollbackSpectator}. */
|
|
104
|
+
export interface RollbackSpectatorOptions {
|
|
105
|
+
/** What the relay's `authorize` gets. */
|
|
106
|
+
token?: string;
|
|
107
|
+
/** Most frames one {@link RollbackSpectator.update} runs (default 8; a late arrival catches up at this pace). */
|
|
108
|
+
maxFrames?: number;
|
|
109
|
+
/** Called once per frame with the same FNV-1a checksum the players' `onconfirm` gets. */
|
|
110
|
+
onconfirm?(frame: number, checksum: number): void;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Watches a rollback session through a {@link RollbackRelay}: it runs the game on the players' confirmed inputs, so it
|
|
114
|
+
* is behind them by what confirming takes, and what it shows is final. Call {@link RollbackSpectator.update} every
|
|
115
|
+
* frame and draw {@link RollbackSpectator.state}; it starts at the first frame, however late it arrives.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const watcher = new RollbackSpectator(game, await openWebSocket(relayUrl));
|
|
120
|
+
* engine.onFrame(() => { watcher.update(); draw(watcher.state); });
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare class RollbackSpectator<State, Input> {
|
|
124
|
+
readonly game: Game<State, Input>;
|
|
125
|
+
private readonly connection;
|
|
126
|
+
private readonly options;
|
|
127
|
+
private current;
|
|
128
|
+
private frameNumber;
|
|
129
|
+
private players;
|
|
130
|
+
private delay;
|
|
131
|
+
private closed;
|
|
132
|
+
private readonly queue;
|
|
133
|
+
private readonly maxFrames;
|
|
134
|
+
/** @throws RangeError for a `maxFrames` that is not an integer of 1 or more. */
|
|
135
|
+
constructor(game: Game<State, Input>, connection: Connection, options?: RollbackSpectatorOptions);
|
|
136
|
+
/** The newest frame run. */
|
|
137
|
+
get frame(): number;
|
|
138
|
+
/** The state after {@link RollbackSpectator.frame}. Replaced as frames run. */
|
|
139
|
+
get state(): State;
|
|
140
|
+
/** Frames received and not run yet. */
|
|
141
|
+
get behind(): number;
|
|
142
|
+
/** Whether the relay is still connected. */
|
|
143
|
+
get open(): boolean;
|
|
144
|
+
private receive;
|
|
145
|
+
private read;
|
|
146
|
+
/** Runs the frames that arrived, at most `maxFrames`. Returns how many ran. */
|
|
147
|
+
update(): number;
|
|
148
|
+
/** Stops watching. */
|
|
149
|
+
close(): void;
|
|
150
|
+
/** Same as {@link RollbackSpectator.close}, for `using`. */
|
|
151
|
+
[Symbol.dispose](): void;
|
|
152
|
+
}
|
package/dist/relay.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { n as e, t } from "./schema-CMfqK4Gu.js";
|
|
2
|
+
import { r as n, t as r } from "./transport-DeQodwyv.js";
|
|
3
|
+
import { i, r as a } from "./protocol-asi_qndb.js";
|
|
4
|
+
//#region src/relay.ts
|
|
5
|
+
var o = 1, s = 2, c = 3, l = 4, u = 5, d = 6, f = 1024, p = new TextEncoder(), m = new TextDecoder(), h = (t, n) => {
|
|
6
|
+
let r = new e(32), i = p.encode(n);
|
|
7
|
+
return r.u8(o), r.varint(t === void 0 ? 0 : t + 1), r.varint(i.length), r.raw(i), r.done();
|
|
8
|
+
}, g = class {
|
|
9
|
+
constructor(e) {
|
|
10
|
+
if (this.options = e, this.stats = {
|
|
11
|
+
forwarded: 0,
|
|
12
|
+
frames: 0,
|
|
13
|
+
spectated: 0
|
|
14
|
+
}, this.links = /* @__PURE__ */ new Map(), this.inputs = /* @__PURE__ */ new Map(), this.spectators = /* @__PURE__ */ new Map(), this.log = [], this.base = 0, this.closed = !1, this.players = [...new Set(e?.players ?? [])].sort((e, t) => e - t), !this.players.length) throw TypeError("RollbackRelay needs players; pass the list the sessions get");
|
|
15
|
+
let t = (e, t, n, r) => {
|
|
16
|
+
if (!Number.isInteger(t) || t < n || t > r) throw RangeError(`RollbackRelay ${e} is ${t}; pass an integer from ${n} to ${r}`);
|
|
17
|
+
return t;
|
|
18
|
+
};
|
|
19
|
+
this.delay = t("inputDelay", e.inputDelay ?? 2, 0, 30), this.hold = t("spectatorDelay", e.spectatorDelay ?? 0, 0, 1 << 20), this.history = t("history", e.history ?? 216e3, 1, 2 ** 31), this.maxSpectators = t("maxSpectators", e.maxSpectators ?? 256, 0, 65536), this.complete = this.delay;
|
|
20
|
+
for (let e of this.players) this.inputs.set(e, /* @__PURE__ */ new Map());
|
|
21
|
+
}
|
|
22
|
+
get frame() {
|
|
23
|
+
return this.complete;
|
|
24
|
+
}
|
|
25
|
+
get watching() {
|
|
26
|
+
return this.spectators.size;
|
|
27
|
+
}
|
|
28
|
+
accept(e) {
|
|
29
|
+
if (this.closed) throw n("disposed", "RollbackRelay is closed; create a new RollbackRelay");
|
|
30
|
+
e.onmessage = (t, n) => {
|
|
31
|
+
n !== "unreliable" && this.greet(e, t);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
refuse(e, t, n) {
|
|
35
|
+
e.onmessage = null, e.close(t, n);
|
|
36
|
+
}
|
|
37
|
+
greet(e, n) {
|
|
38
|
+
if (this.closed) return this.refuse(e, r.removed, "relay closed");
|
|
39
|
+
let i, a;
|
|
40
|
+
try {
|
|
41
|
+
let e = new t(n, "Rollback relay hello", "send what relayPeers or RollbackSpectator sends");
|
|
42
|
+
if (e.u8() !== o) throw e.fail("is not a hello");
|
|
43
|
+
let r = e.varint(), s = e.count(1024);
|
|
44
|
+
e.need(s), a = m.decode(n.subarray(e.at, e.at += s)), e.end(), i = r ? r - 1 : void 0;
|
|
45
|
+
} catch (t) {
|
|
46
|
+
if (!(t instanceof RangeError)) throw t;
|
|
47
|
+
return this.refuse(e, r.protocol, t.message.slice(0, 120));
|
|
48
|
+
}
|
|
49
|
+
if (this.options.authorize && !this.options.authorize(i, a)) return this.refuse(e, r.removed, "not allowed");
|
|
50
|
+
if (i === void 0) return this.watch(e);
|
|
51
|
+
if (!this.inputs.has(i)) return this.refuse(e, r.protocol, `no player ${i} here`);
|
|
52
|
+
if (this.links.has(i)) return this.refuse(e, r.full, `player ${i} is already here`);
|
|
53
|
+
this.links.set(i, e);
|
|
54
|
+
let s = i;
|
|
55
|
+
e.onmessage = (t, n) => this.pass(s, e, t, n), e.onclose = () => this.leave(s, e);
|
|
56
|
+
}
|
|
57
|
+
leave(t, n) {
|
|
58
|
+
if (this.links.get(t) !== n) return;
|
|
59
|
+
this.links.delete(t);
|
|
60
|
+
let r = new e(8);
|
|
61
|
+
r.u8(l), r.varint(t);
|
|
62
|
+
let i = r.done();
|
|
63
|
+
for (let e of this.links.values()) e.send(i, "reliable");
|
|
64
|
+
}
|
|
65
|
+
pass(n, i, a, o) {
|
|
66
|
+
try {
|
|
67
|
+
let r = new t(a, "Rollback relay message", "send what relayPeers sends");
|
|
68
|
+
if (r.u8() !== s) throw r.fail("is not a forward");
|
|
69
|
+
let i = r.varint(), l = r.at, u = this.links.get(i);
|
|
70
|
+
if (this.learn(n, a.subarray(l)), !u) return;
|
|
71
|
+
let d = new e(a.length + 8);
|
|
72
|
+
d.u8(c), d.varint(n), d.raw(a.subarray(l)), u.send(d.done(), o), this.stats.forwarded++;
|
|
73
|
+
} catch (e) {
|
|
74
|
+
if (!(e instanceof RangeError)) throw e;
|
|
75
|
+
i.close(r.protocol, e.message.slice(0, 120));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
learn(e, n) {
|
|
79
|
+
let r = new t(n, "Rollback message", "send what RollbackSession sends");
|
|
80
|
+
if (r.u8() !== 32) return;
|
|
81
|
+
r.varint(), r.varint(), r.varint();
|
|
82
|
+
let i = r.varint(), o = r.count(32), s = this.inputs.get(e);
|
|
83
|
+
for (let e = 0; e < o; e++) {
|
|
84
|
+
let t = r.count(a);
|
|
85
|
+
r.need(t);
|
|
86
|
+
let o = i + e, c = n.subarray(r.at, r.at += t);
|
|
87
|
+
o > this.complete && o <= this.complete + f && !s.has(o) && s.set(o, c.slice());
|
|
88
|
+
}
|
|
89
|
+
r.end(), this.advance();
|
|
90
|
+
}
|
|
91
|
+
advance() {
|
|
92
|
+
for (;;) {
|
|
93
|
+
let t = this.complete + 1, n = [];
|
|
94
|
+
for (let e of this.players) {
|
|
95
|
+
let r = this.inputs.get(e)?.get(t);
|
|
96
|
+
r && n.push(r);
|
|
97
|
+
}
|
|
98
|
+
if (n.length < this.players.length) break;
|
|
99
|
+
let r = new e(64);
|
|
100
|
+
r.u8(d), r.varint(t);
|
|
101
|
+
for (let e of n) r.varint(e.length), r.raw(e);
|
|
102
|
+
for (let e of this.inputs.values()) e.delete(t);
|
|
103
|
+
this.log.push(r.done()), this.log.length > this.history + this.hold && (this.log.shift(), this.base++), this.complete = t, this.stats.frames++;
|
|
104
|
+
}
|
|
105
|
+
for (let e of this.spectators.keys()) this.feed(e);
|
|
106
|
+
}
|
|
107
|
+
feed(e) {
|
|
108
|
+
let t = this.base + this.log.length - this.hold, n = this.spectators.get(e);
|
|
109
|
+
for (; n < t; n++) e.send(this.log[n - this.base], "reliable"), this.stats.spectated++;
|
|
110
|
+
this.spectators.set(e, n);
|
|
111
|
+
}
|
|
112
|
+
watch(t) {
|
|
113
|
+
if (this.spectators.size >= this.maxSpectators) return this.refuse(t, r.full, `the relay holds ${this.maxSpectators} spectators (maxSpectators)`);
|
|
114
|
+
if (this.base > 0) return this.refuse(t, r.full, "the session passed the history; raise history to watch it");
|
|
115
|
+
let n = new e(32);
|
|
116
|
+
n.u8(u), n.varint(this.delay), n.varint(this.players.length);
|
|
117
|
+
for (let e of this.players) n.varint(e);
|
|
118
|
+
t.send(n.done(), "reliable"), t.onmessage = null, t.onclose = () => this.spectators.delete(t), this.spectators.set(t, 0), this.feed(t);
|
|
119
|
+
}
|
|
120
|
+
close() {
|
|
121
|
+
if (!this.closed) {
|
|
122
|
+
this.closed = !0;
|
|
123
|
+
for (let e of [...this.links.values()]) e.close(r.removed, "relay closed");
|
|
124
|
+
for (let e of [...this.spectators.keys()]) e.close(r.removed, "relay closed");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
[Symbol.dispose]() {
|
|
128
|
+
this.close();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
function _(n, { players: i, local: a, token: o = "" }) {
|
|
132
|
+
if (!n || typeof n.send != "function") throw TypeError("relayPeers needs a connection to the relay; pass one");
|
|
133
|
+
if (!i?.includes(a)) throw TypeError(`relayPeers local player ${a} is not in players; add it`);
|
|
134
|
+
let u = /* @__PURE__ */ new Map(), d = i.length - 1;
|
|
135
|
+
for (let t of i) {
|
|
136
|
+
if (t === a) continue;
|
|
137
|
+
let i = !1, o = (e, t) => {
|
|
138
|
+
i || (i = !0, c.onclose?.(e, t), --d === 0 && n.close(r.normal, "left"));
|
|
139
|
+
}, c = {
|
|
140
|
+
get open() {
|
|
141
|
+
return !i && n.open;
|
|
142
|
+
},
|
|
143
|
+
send(r, a = "reliable") {
|
|
144
|
+
if (i) return;
|
|
145
|
+
let o = new e(r.length + 8);
|
|
146
|
+
o.u8(s), o.varint(t), o.raw(r), n.send(o.done(), a);
|
|
147
|
+
},
|
|
148
|
+
close: (e = r.normal, t = "") => o(e, t),
|
|
149
|
+
onmessage: null,
|
|
150
|
+
onclose: null
|
|
151
|
+
};
|
|
152
|
+
u.set(t, c);
|
|
153
|
+
}
|
|
154
|
+
return n.onmessage = (e, i) => {
|
|
155
|
+
let a, o, s;
|
|
156
|
+
try {
|
|
157
|
+
let n = new t(e, "Rollback relay message", "update the client to the relay version");
|
|
158
|
+
a = n.u8(), o = n.varint(), s = n.at;
|
|
159
|
+
} catch (e) {
|
|
160
|
+
if (!(e instanceof RangeError)) throw e;
|
|
161
|
+
return n.close(r.protocol, e.message);
|
|
162
|
+
}
|
|
163
|
+
let d = u.get(o);
|
|
164
|
+
d && (a === c ? d.onmessage?.(e.subarray(s), i) : a === l && d.close(r.normal, "player left"));
|
|
165
|
+
}, n.onclose = (e, t) => {
|
|
166
|
+
for (let n of u.values()) n.close(e, t);
|
|
167
|
+
}, n.send(h(a, o), "reliable"), u;
|
|
168
|
+
}
|
|
169
|
+
var v = class {
|
|
170
|
+
constructor(e, t, n = {}) {
|
|
171
|
+
if (this.game = e, this.connection = t, this.options = n, this.frameNumber = 0, this.delay = 0, this.closed = !1, this.queue = [], this.maxFrames = n.maxFrames ?? 8, !Number.isInteger(this.maxFrames) || this.maxFrames < 1) throw RangeError(`RollbackSpectator maxFrames is ${this.maxFrames}; pass an integer of 1 or more`);
|
|
172
|
+
this.current = e.initial(), t.onmessage = (e) => this.receive(e), t.onclose = () => this.closed = !0, t.send(h(void 0, n.token ?? ""), "reliable");
|
|
173
|
+
}
|
|
174
|
+
get frame() {
|
|
175
|
+
return this.frameNumber;
|
|
176
|
+
}
|
|
177
|
+
get state() {
|
|
178
|
+
return this.current;
|
|
179
|
+
}
|
|
180
|
+
get behind() {
|
|
181
|
+
return this.queue.length;
|
|
182
|
+
}
|
|
183
|
+
get open() {
|
|
184
|
+
return !this.closed;
|
|
185
|
+
}
|
|
186
|
+
receive(e) {
|
|
187
|
+
try {
|
|
188
|
+
this.read(e);
|
|
189
|
+
} catch (e) {
|
|
190
|
+
if (!(e instanceof RangeError)) throw e;
|
|
191
|
+
this.closed = !0, this.connection.close(r.protocol, e.message);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
read(e) {
|
|
195
|
+
let n = new t(e, "Rollback spectator message", "update the client to the relay version"), r = n.u8();
|
|
196
|
+
if (r === u) {
|
|
197
|
+
this.delay = n.varint();
|
|
198
|
+
let e = [];
|
|
199
|
+
for (let t = n.count(65536); t > 0; t--) e.push(n.varint());
|
|
200
|
+
this.players = e;
|
|
201
|
+
let t = this.game.initial();
|
|
202
|
+
for (let n of e) this.game.join(t, n);
|
|
203
|
+
let r = this.game;
|
|
204
|
+
this.current = r.schema.decode(r.stateType, r.schema.encode(r.stateType, t));
|
|
205
|
+
for (let e = 1; e <= this.delay; e++) this.queue.push([]);
|
|
206
|
+
} else if (r === d) {
|
|
207
|
+
if (!this.players) throw n.fail("is a frame before the welcome");
|
|
208
|
+
n.varint();
|
|
209
|
+
let t = [];
|
|
210
|
+
for (let r = 0; r < this.players.length; r++) {
|
|
211
|
+
let r = n.count(a);
|
|
212
|
+
n.need(r), t.push(e.subarray(n.at, n.at += r));
|
|
213
|
+
}
|
|
214
|
+
n.end(), this.queue.push(t);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
update() {
|
|
218
|
+
let e = this.game, t = this.players, n = 0;
|
|
219
|
+
for (; t && n < this.maxFrames && this.queue.length; n++) {
|
|
220
|
+
let n = this.queue.shift(), r = /* @__PURE__ */ new Map(), a = ++this.frameNumber;
|
|
221
|
+
n.forEach((n, i) => r.set(t[i], e.schema.decode(e.inputType, n))), e.step(this.current, r, a, 1 / e.tickRate);
|
|
222
|
+
let o = e.schema.encode(e.stateType, this.current);
|
|
223
|
+
this.current = e.schema.decode(e.stateType, o), this.options.onconfirm?.(a, i(o));
|
|
224
|
+
}
|
|
225
|
+
return n;
|
|
226
|
+
}
|
|
227
|
+
close() {
|
|
228
|
+
this.closed || (this.closed = !0, this.connection.close(r.normal, "left"));
|
|
229
|
+
}
|
|
230
|
+
[Symbol.dispose]() {
|
|
231
|
+
this.close();
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
//#endregion
|
|
235
|
+
export { g as RollbackRelay, v as RollbackSpectator, _ as relayPeers };
|