@super-line/plugin-multiplayer 0.1.0 → 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 +6 -0
- package/README.md +90 -11
- package/THIRD_PARTY_NOTICES.md +1 -0
- package/dist/chunk-GRDMIVAD.js +127 -0
- package/dist/{chunk-QG5AWEMR.js → chunk-SR5BFY5T.js} +27 -15
- package/dist/client.cjs +76 -24
- package/dist/client.d.cts +2 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +42 -22
- package/dist/index.cjs +26 -15
- package/dist/index.d.cts +39 -3
- package/dist/index.d.ts +39 -3
- package/dist/index.js +1 -1
- package/dist/server.cjs +237 -47
- package/dist/server.d.cts +27 -3
- package/dist/server.d.ts +27 -3
- package/dist/server.js +102 -34
- package/package.json +3 -3
- package/dist/chunk-AUQT3TTM.js +0 -19
package/dist/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
+
applyPatch,
|
|
2
3
|
encodedBytes
|
|
3
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-GRDMIVAD.js";
|
|
4
5
|
|
|
5
6
|
// src/client.ts
|
|
6
7
|
import { getLogger } from "@logtape/logtape";
|
|
@@ -370,7 +371,9 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
370
371
|
let correction;
|
|
371
372
|
let correctFrom;
|
|
372
373
|
let frameAt = localNow();
|
|
374
|
+
let wire = first.frame;
|
|
373
375
|
let sized;
|
|
376
|
+
let syncing = 0;
|
|
374
377
|
let frameBytes = 0;
|
|
375
378
|
const rate = { since: frameAt, frames: 0, perSecond: 0 };
|
|
376
379
|
const emit = (type, event) => {
|
|
@@ -385,7 +388,7 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
385
388
|
const adopt = (j, sent) => {
|
|
386
389
|
const now = localNow();
|
|
387
390
|
joined = j;
|
|
388
|
-
latest = j.frame;
|
|
391
|
+
latest = wire = j.frame;
|
|
389
392
|
clock.roundTrip(now - sent);
|
|
390
393
|
clock.reading(j.frame.tick, j.tickRate, j.frame.ms, j.frame.epoch, now);
|
|
391
394
|
buffer.update(j.frame.state, j.frame.tick, now);
|
|
@@ -482,9 +485,9 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
482
485
|
serverTick: (now) => clock.tick(now),
|
|
483
486
|
stats() {
|
|
484
487
|
const now = localNow();
|
|
485
|
-
if (sized !==
|
|
486
|
-
sized =
|
|
487
|
-
frameBytes = encodedBytes(JSON.stringify({ t: "evt", e: "mp.state", d:
|
|
488
|
+
if (sized !== wire) {
|
|
489
|
+
sized = wire;
|
|
490
|
+
frameBytes = encodedBytes(JSON.stringify({ t: "evt", e: "mp.state", d: wire }));
|
|
488
491
|
}
|
|
489
492
|
if (correctFrom !== void 0) {
|
|
490
493
|
const after = predictor.predicted(now);
|
|
@@ -541,11 +544,37 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
541
544
|
});
|
|
542
545
|
}
|
|
543
546
|
};
|
|
547
|
+
const sync = () => {
|
|
548
|
+
syncing++;
|
|
549
|
+
const sent = localNow();
|
|
550
|
+
dyn.mpJoin({ world: id }).then(
|
|
551
|
+
(j) => {
|
|
552
|
+
syncing--;
|
|
553
|
+
if (closed) return;
|
|
554
|
+
adopt(j, sent);
|
|
555
|
+
emit("frame", latest);
|
|
556
|
+
},
|
|
557
|
+
(error) => {
|
|
558
|
+
syncing--;
|
|
559
|
+
if (failedWith(error, "DISCONNECTED")) return;
|
|
560
|
+
log.warn("re-joining world {world} failed: {error}", { world: id, error });
|
|
561
|
+
handle.close("disconnected");
|
|
562
|
+
}
|
|
563
|
+
);
|
|
564
|
+
};
|
|
544
565
|
const handle = {
|
|
545
566
|
world,
|
|
546
567
|
emit,
|
|
547
|
-
frame(
|
|
568
|
+
frame(received) {
|
|
548
569
|
if (closed) return;
|
|
570
|
+
let f;
|
|
571
|
+
if ("patch" in received) {
|
|
572
|
+
if (syncing || received.n <= (latest.n ?? -1)) return;
|
|
573
|
+
if (received.n !== latest.n + 1) return sync();
|
|
574
|
+
const { patch, ...rest } = received;
|
|
575
|
+
f = { ...rest, state: applyPatch(latest.state, patch) };
|
|
576
|
+
} else f = received;
|
|
577
|
+
wire = received;
|
|
549
578
|
const now = localNow();
|
|
550
579
|
const made = predictor.made;
|
|
551
580
|
correctFrom = game.entity && joined.tickRate > 0 && made && made.base === latest.state ? made.state : void 0;
|
|
@@ -566,21 +595,9 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
566
595
|
if (closed) return;
|
|
567
596
|
clock.reset();
|
|
568
597
|
predictor.reconnected();
|
|
569
|
-
|
|
570
|
-
dyn.mpJoin({ world: id }).then(
|
|
571
|
-
(j) => {
|
|
572
|
-
if (closed) return;
|
|
573
|
-
adopt(j, sent);
|
|
574
|
-
emit("frame", latest);
|
|
575
|
-
},
|
|
576
|
-
(error) => {
|
|
577
|
-
if (failedWith(error, "DISCONNECTED")) return;
|
|
578
|
-
log.warn("re-joining world {world} failed: {error}", { world: id, error });
|
|
579
|
-
handle.close("disconnected");
|
|
580
|
-
}
|
|
581
|
-
);
|
|
598
|
+
sync();
|
|
582
599
|
},
|
|
583
|
-
close(reason) {
|
|
600
|
+
close(reason, detail) {
|
|
584
601
|
if (closed) return;
|
|
585
602
|
closed = reason;
|
|
586
603
|
handles.delete(id);
|
|
@@ -593,7 +610,7 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
593
610
|
predictor.clear();
|
|
594
611
|
buffer.clear();
|
|
595
612
|
glide.clear();
|
|
596
|
-
emit("closed", { reason });
|
|
613
|
+
emit("closed", detail === void 0 ? { reason } : { reason, detail });
|
|
597
614
|
}
|
|
598
615
|
};
|
|
599
616
|
const predictor = new Predictor(game, world);
|
|
@@ -611,7 +628,10 @@ function multiplayerClient(client, fragment, opts = {}) {
|
|
|
611
628
|
"mp.seat",
|
|
612
629
|
(e) => handles.get(e.world)?.emit("seat", { player: e.player, change: e.change })
|
|
613
630
|
),
|
|
614
|
-
dyn.on(
|
|
631
|
+
dyn.on(
|
|
632
|
+
"mp.closed",
|
|
633
|
+
(e) => handles.get(e.world)?.close(e.reason, e.detail)
|
|
634
|
+
),
|
|
615
635
|
dyn.onReconnect(() => {
|
|
616
636
|
for (const h of handles.values()) h.rejoin();
|
|
617
637
|
})
|
package/dist/index.cjs
CHANGED
|
@@ -62,6 +62,15 @@ var hostSchema = (schema) => {
|
|
|
62
62
|
return result.value;
|
|
63
63
|
}).meta(shape);
|
|
64
64
|
};
|
|
65
|
+
var isJson = (value) => {
|
|
66
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return true;
|
|
67
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
68
|
+
if (Array.isArray(value)) return value.every(isJson);
|
|
69
|
+
if (typeof value !== "object") return false;
|
|
70
|
+
const proto = Object.getPrototypeOf(value);
|
|
71
|
+
if (proto !== Object.prototype && proto !== null) return false;
|
|
72
|
+
return Object.values(value).every(isJson);
|
|
73
|
+
};
|
|
65
74
|
|
|
66
75
|
// src/index.ts
|
|
67
76
|
var CommandRejected = class extends Error {
|
|
@@ -77,15 +86,6 @@ var CommandRejected = class extends Error {
|
|
|
77
86
|
function reject(code, reason) {
|
|
78
87
|
throw new CommandRejected(code, reason);
|
|
79
88
|
}
|
|
80
|
-
var isJson = (value) => {
|
|
81
|
-
if (value === null || typeof value === "string" || typeof value === "boolean") return true;
|
|
82
|
-
if (typeof value === "number") return Number.isFinite(value);
|
|
83
|
-
if (Array.isArray(value)) return value.every(isJson);
|
|
84
|
-
if (typeof value !== "object") return false;
|
|
85
|
-
const proto = Object.getPrototypeOf(value);
|
|
86
|
-
if (proto !== Object.prototype && proto !== null) return false;
|
|
87
|
-
return Object.values(value).every(isJson);
|
|
88
|
-
};
|
|
89
89
|
function defineWorld(world) {
|
|
90
90
|
const { maxPlayers, tickRate, sendRate } = world;
|
|
91
91
|
if (!Number.isFinite(maxPlayers) || maxPlayers <= 0) throw new TypeError("maxPlayers must be a positive number");
|
|
@@ -107,15 +107,20 @@ var rowSchema = z2.strictObject({
|
|
|
107
107
|
seats: z2.array(z2.strictObject({ player, profile: z2.unknown() })),
|
|
108
108
|
updatedAt: z2.number()
|
|
109
109
|
});
|
|
110
|
+
var element = z2.lazy(
|
|
111
|
+
() => z2.union([patchSchema, z2.tuple([z2.unknown()]), z2.tuple([z2.record(z2.string(), element), z2.number().int().min(0)])])
|
|
112
|
+
);
|
|
113
|
+
var patchSchema = z2.record(z2.string(), z2.union([element, z2.literal(0)]));
|
|
110
114
|
var surfaceOf = (world) => {
|
|
111
|
-
const
|
|
115
|
+
const head = {
|
|
112
116
|
world: worldId,
|
|
113
117
|
tick,
|
|
114
118
|
ms: z2.number().int().min(0).optional(),
|
|
115
119
|
epoch: z2.string().max(64).optional(),
|
|
116
|
-
acks: z2.record(player, z2.number().int().min(0))
|
|
117
|
-
|
|
118
|
-
|
|
120
|
+
acks: z2.record(player, z2.number().int().min(0))
|
|
121
|
+
};
|
|
122
|
+
const count = z2.number().int().min(0);
|
|
123
|
+
const frame = z2.strictObject({ ...head, n: count.optional(), state: hostSchema(world.state) });
|
|
119
124
|
const rate = z2.number().min(0);
|
|
120
125
|
const n = z2.number().min(0);
|
|
121
126
|
const stats = z2.strictObject({
|
|
@@ -154,7 +159,7 @@ var surfaceOf = (world) => {
|
|
|
154
159
|
}
|
|
155
160
|
},
|
|
156
161
|
serverToClient: {
|
|
157
|
-
"mp.state": { payload: frame },
|
|
162
|
+
"mp.state": { payload: z2.union([frame, z2.strictObject({ ...head, n: count, patch: patchSchema })]) },
|
|
158
163
|
"mp.message": {
|
|
159
164
|
payload: z2.strictObject({ world: worldId, message: world.message ? hostSchema(world.message) : z2.never() })
|
|
160
165
|
},
|
|
@@ -165,7 +170,13 @@ var surfaceOf = (world) => {
|
|
|
165
170
|
change: z2.enum(["joined", "left", "dropped", "resumed"])
|
|
166
171
|
})
|
|
167
172
|
},
|
|
168
|
-
"mp.closed": {
|
|
173
|
+
"mp.closed": {
|
|
174
|
+
payload: z2.strictObject({
|
|
175
|
+
world: worldId,
|
|
176
|
+
reason: z2.enum(["deleted", "replaced", "lost", "removed"]),
|
|
177
|
+
detail: z2.string().optional()
|
|
178
|
+
})
|
|
179
|
+
}
|
|
169
180
|
}
|
|
170
181
|
};
|
|
171
182
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
import { Schema, InferOut, InferIn, ContractPlugin } from '@super-line/core';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Deltas between two JSON values, used to send a player only what changed.
|
|
5
|
+
*
|
|
6
|
+
* A patch mirrors the shape of the object it changes. For each key:
|
|
7
|
+
* - a nested patch object: go into that key and patch what is there;
|
|
8
|
+
* - `[value]`: set the key to `value`;
|
|
9
|
+
* - `[changes, length]`: the key holds an array; `length` is how long it
|
|
10
|
+
* now is, and `changes` has, by index, what to do with the elements that
|
|
11
|
+
* changed, each in one of these same forms;
|
|
12
|
+
* - `0`: delete the key.
|
|
13
|
+
*
|
|
14
|
+
* An array is patched element by element where its elements kept their
|
|
15
|
+
* places: a unit that moved costs its new position, not the whole list. An
|
|
16
|
+
* array whose elements shifted, because one was put in or taken out before
|
|
17
|
+
* the end, is sent whole. So for things that come and go, an object keyed by
|
|
18
|
+
* id is still the better home.
|
|
19
|
+
*/
|
|
20
|
+
type StatePatch = {
|
|
21
|
+
[key: string]: StatePatch | [unknown] | ArrayPatch | 0;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* What changed in an array: by index, the elements to patch or replace, and
|
|
25
|
+
* the array's new length. Elements past the new length are gone.
|
|
26
|
+
*/
|
|
27
|
+
type ArrayPatch = [changes: {
|
|
28
|
+
[index: string]: StatePatch | [unknown] | ArrayPatch;
|
|
29
|
+
}, length: number];
|
|
30
|
+
|
|
3
31
|
type PlayerId = string;
|
|
4
32
|
type EntityId = string;
|
|
5
33
|
interface WorldContext<Msg> {
|
|
@@ -63,8 +91,15 @@ interface StateFrame<State> {
|
|
|
63
91
|
ms?: number;
|
|
64
92
|
epoch?: string;
|
|
65
93
|
acks: Record<PlayerId, number>;
|
|
94
|
+
/** The world's frame counter, present when the server sends patch frames. */
|
|
95
|
+
n?: number;
|
|
66
96
|
state: State;
|
|
67
97
|
}
|
|
98
|
+
/** A state frame of a world whose server sends patch frames: what changed since frame `n - 1`. */
|
|
99
|
+
type PatchFrame = Omit<StateFrame<never>, 'state' | 'n'> & {
|
|
100
|
+
n: number;
|
|
101
|
+
patch: StatePatch;
|
|
102
|
+
};
|
|
68
103
|
/** A world's own numbers, measured on the server when the kit runs with `stats: true`. */
|
|
69
104
|
interface WorldServerStats {
|
|
70
105
|
/** Duration of the latest tick (`onTick` and the messages it sent), ms. */
|
|
@@ -96,7 +131,7 @@ interface ClockReading {
|
|
|
96
131
|
stats?: WorldServerStats;
|
|
97
132
|
}
|
|
98
133
|
type SeatChange = 'joined' | 'left' | 'dropped' | 'resumed';
|
|
99
|
-
type WorldClosedReason = 'deleted' | 'replaced' | 'lost';
|
|
134
|
+
type WorldClosedReason = 'deleted' | 'replaced' | 'lost' | 'removed';
|
|
100
135
|
interface WorldRow {
|
|
101
136
|
id: string;
|
|
102
137
|
state: unknown;
|
|
@@ -149,7 +184,7 @@ interface MultiplayerSurface<W extends AnyWorld> {
|
|
|
149
184
|
};
|
|
150
185
|
serverToClient: {
|
|
151
186
|
'mp.state': {
|
|
152
|
-
payload: Schema<StateFrame<StateOf<W
|
|
187
|
+
payload: Schema<StateFrame<StateOf<W>> | PatchFrame>;
|
|
153
188
|
};
|
|
154
189
|
'mp.message': {
|
|
155
190
|
payload: Schema<{
|
|
@@ -168,6 +203,7 @@ interface MultiplayerSurface<W extends AnyWorld> {
|
|
|
168
203
|
payload: Schema<{
|
|
169
204
|
world: string;
|
|
170
205
|
reason: WorldClosedReason;
|
|
206
|
+
detail?: string;
|
|
171
207
|
}>;
|
|
172
208
|
};
|
|
173
209
|
};
|
|
@@ -191,4 +227,4 @@ declare const WORLDS_COLLECTION = "mpWorlds";
|
|
|
191
227
|
*/
|
|
192
228
|
declare function multiplayerContract<W extends AnyWorld>(world: W): MultiplayerContract<W>;
|
|
193
229
|
|
|
194
|
-
export { type AnyWorld, type ClockReading, type CommandOf, CommandRejected, type CommandResult, type EntityId, type EntityOf, type JoinResult, type MessageOf, type MultiplayerContract, type MultiplayerFragment, type MultiplayerSurface, type PlayerContext, type PlayerId, type ProfileOf, type SeatChange, type StateFrame, type StateOf, type TickContext, WORLDS_COLLECTION, type WorldClosedReason, type WorldContext, type WorldDefinition, type WorldRow, type WorldServerStats, defineWorld, multiplayerContract, reject };
|
|
230
|
+
export { type AnyWorld, type ArrayPatch, type ClockReading, type CommandOf, CommandRejected, type CommandResult, type EntityId, type EntityOf, type JoinResult, type MessageOf, type MultiplayerContract, type MultiplayerFragment, type MultiplayerSurface, type PatchFrame, type PlayerContext, type PlayerId, type ProfileOf, type SeatChange, type StateFrame, type StateOf, type StatePatch, type TickContext, WORLDS_COLLECTION, type WorldClosedReason, type WorldContext, type WorldDefinition, type WorldRow, type WorldServerStats, defineWorld, multiplayerContract, reject };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
import { Schema, InferOut, InferIn, ContractPlugin } from '@super-line/core';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Deltas between two JSON values, used to send a player only what changed.
|
|
5
|
+
*
|
|
6
|
+
* A patch mirrors the shape of the object it changes. For each key:
|
|
7
|
+
* - a nested patch object: go into that key and patch what is there;
|
|
8
|
+
* - `[value]`: set the key to `value`;
|
|
9
|
+
* - `[changes, length]`: the key holds an array; `length` is how long it
|
|
10
|
+
* now is, and `changes` has, by index, what to do with the elements that
|
|
11
|
+
* changed, each in one of these same forms;
|
|
12
|
+
* - `0`: delete the key.
|
|
13
|
+
*
|
|
14
|
+
* An array is patched element by element where its elements kept their
|
|
15
|
+
* places: a unit that moved costs its new position, not the whole list. An
|
|
16
|
+
* array whose elements shifted, because one was put in or taken out before
|
|
17
|
+
* the end, is sent whole. So for things that come and go, an object keyed by
|
|
18
|
+
* id is still the better home.
|
|
19
|
+
*/
|
|
20
|
+
type StatePatch = {
|
|
21
|
+
[key: string]: StatePatch | [unknown] | ArrayPatch | 0;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* What changed in an array: by index, the elements to patch or replace, and
|
|
25
|
+
* the array's new length. Elements past the new length are gone.
|
|
26
|
+
*/
|
|
27
|
+
type ArrayPatch = [changes: {
|
|
28
|
+
[index: string]: StatePatch | [unknown] | ArrayPatch;
|
|
29
|
+
}, length: number];
|
|
30
|
+
|
|
3
31
|
type PlayerId = string;
|
|
4
32
|
type EntityId = string;
|
|
5
33
|
interface WorldContext<Msg> {
|
|
@@ -63,8 +91,15 @@ interface StateFrame<State> {
|
|
|
63
91
|
ms?: number;
|
|
64
92
|
epoch?: string;
|
|
65
93
|
acks: Record<PlayerId, number>;
|
|
94
|
+
/** The world's frame counter, present when the server sends patch frames. */
|
|
95
|
+
n?: number;
|
|
66
96
|
state: State;
|
|
67
97
|
}
|
|
98
|
+
/** A state frame of a world whose server sends patch frames: what changed since frame `n - 1`. */
|
|
99
|
+
type PatchFrame = Omit<StateFrame<never>, 'state' | 'n'> & {
|
|
100
|
+
n: number;
|
|
101
|
+
patch: StatePatch;
|
|
102
|
+
};
|
|
68
103
|
/** A world's own numbers, measured on the server when the kit runs with `stats: true`. */
|
|
69
104
|
interface WorldServerStats {
|
|
70
105
|
/** Duration of the latest tick (`onTick` and the messages it sent), ms. */
|
|
@@ -96,7 +131,7 @@ interface ClockReading {
|
|
|
96
131
|
stats?: WorldServerStats;
|
|
97
132
|
}
|
|
98
133
|
type SeatChange = 'joined' | 'left' | 'dropped' | 'resumed';
|
|
99
|
-
type WorldClosedReason = 'deleted' | 'replaced' | 'lost';
|
|
134
|
+
type WorldClosedReason = 'deleted' | 'replaced' | 'lost' | 'removed';
|
|
100
135
|
interface WorldRow {
|
|
101
136
|
id: string;
|
|
102
137
|
state: unknown;
|
|
@@ -149,7 +184,7 @@ interface MultiplayerSurface<W extends AnyWorld> {
|
|
|
149
184
|
};
|
|
150
185
|
serverToClient: {
|
|
151
186
|
'mp.state': {
|
|
152
|
-
payload: Schema<StateFrame<StateOf<W
|
|
187
|
+
payload: Schema<StateFrame<StateOf<W>> | PatchFrame>;
|
|
153
188
|
};
|
|
154
189
|
'mp.message': {
|
|
155
190
|
payload: Schema<{
|
|
@@ -168,6 +203,7 @@ interface MultiplayerSurface<W extends AnyWorld> {
|
|
|
168
203
|
payload: Schema<{
|
|
169
204
|
world: string;
|
|
170
205
|
reason: WorldClosedReason;
|
|
206
|
+
detail?: string;
|
|
171
207
|
}>;
|
|
172
208
|
};
|
|
173
209
|
};
|
|
@@ -191,4 +227,4 @@ declare const WORLDS_COLLECTION = "mpWorlds";
|
|
|
191
227
|
*/
|
|
192
228
|
declare function multiplayerContract<W extends AnyWorld>(world: W): MultiplayerContract<W>;
|
|
193
229
|
|
|
194
|
-
export { type AnyWorld, type ClockReading, type CommandOf, CommandRejected, type CommandResult, type EntityId, type EntityOf, type JoinResult, type MessageOf, type MultiplayerContract, type MultiplayerFragment, type MultiplayerSurface, type PlayerContext, type PlayerId, type ProfileOf, type SeatChange, type StateFrame, type StateOf, type TickContext, WORLDS_COLLECTION, type WorldClosedReason, type WorldContext, type WorldDefinition, type WorldRow, type WorldServerStats, defineWorld, multiplayerContract, reject };
|
|
230
|
+
export { type AnyWorld, type ArrayPatch, type ClockReading, type CommandOf, CommandRejected, type CommandResult, type EntityId, type EntityOf, type JoinResult, type MessageOf, type MultiplayerContract, type MultiplayerFragment, type MultiplayerSurface, type PatchFrame, type PlayerContext, type PlayerId, type ProfileOf, type SeatChange, type StateFrame, type StateOf, type StatePatch, type TickContext, WORLDS_COLLECTION, type WorldClosedReason, type WorldContext, type WorldDefinition, type WorldRow, type WorldServerStats, defineWorld, multiplayerContract, reject };
|