@super-line/plugin-multiplayer 0.2.0 → 0.2.1

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 CHANGED
@@ -3,6 +3,12 @@
3
3
  Generated from conventional commits by [git-cliff](https://git-cliff.org) —
4
4
  run `pnpm changelog` rather than editing by hand.
5
5
 
6
+ ## [plugin-multiplayer-v0.2.1] — 2026-12-01
7
+
8
+ ### Bug Fixes
9
+
10
+ - **plugin-multiplayer:** Stop the own player stepping back when a key is released ([3a623bb])
11
+
6
12
  ## [plugin-multiplayer-v0.2.0] — 2026-11-30
7
13
 
8
14
  ### Features
package/README.md CHANGED
@@ -257,11 +257,11 @@ draws its own player pays nothing extra; a client that never predicts, such as a
257
257
 
258
258
  | Field | Unit | Meaning |
259
259
  | --- | --- | --- |
260
- | `rttMs` | ms | smoothed round trip of the join and clock requests; undefined until one returns |
260
+ | `rttMs` | ms | smoothed round trip of the clock requests; undefined until one returns, and in a world with no tick |
261
261
  | `clockErrorMs` | ms | how far clock readings land from the client's estimate of the server clock, smoothed |
262
262
  | `renderDelayTicks`, `renderDelayMs` | ticks, ms | how far behind the server clock other entities are drawn |
263
263
  | `latenessMs`, `jitterMs` | ms | how late frames arrive against the clock, and how much that varies |
264
- | `predictionLeadTicks` | ticks | how far ahead of the latest frame the prediction runs |
264
+ | `predictionLeadTicks` | ticks | how many ticks after it is sent a command first shows; the prediction runs that far ahead of the server's clock |
265
265
  | `framesPerSecond` | per second | state frames received over about the last second |
266
266
  | `frameBytes` | bytes | the latest frame's size on the wire with the default JSON serializer; with patch frames, the patch |
267
267
  | `msSinceFrame` | ms | time since the latest frame arrived |
package/dist/client.cjs CHANGED
@@ -260,7 +260,7 @@ var Predictor = class {
260
260
  this.#world = world;
261
261
  this.#source = source;
262
262
  }
263
- /** How many ticks ahead of the latest frame the prediction runs: how long a command takes to show. */
263
+ /** How many ticks after it is sent a command first shows: how far ahead of the clock the prediction runs. */
264
264
  get lead() {
265
265
  return this.#lead;
266
266
  }
@@ -279,6 +279,15 @@ var Predictor = class {
279
279
  this.#pending.push({ seq, command, sentAt, link: this.#link });
280
280
  this.#version++;
281
281
  }
282
+ /** The server applied a command to the state of `tick`, so it first shows at the tick after. */
283
+ applied(seq, tick) {
284
+ const p = this.#pending.find((q) => q.seq === seq);
285
+ if (!p) return;
286
+ p.at = tick + 1;
287
+ const { tickRate } = this.#source;
288
+ if (tickRate > 0 && p.link === this.#link) this.#learn(p.at - p.sentAt, tickRate);
289
+ this.#version++;
290
+ }
282
291
  /** A command was rejected or its request failed: its effect goes at once. */
283
292
  settle(seq) {
284
293
  const at = this.#pending.findIndex((p) => p.seq === seq);
@@ -289,12 +298,7 @@ var Predictor = class {
289
298
  /** A frame arrived, with the highest seq of this player's the server has handled. Call it after the source moved on. */
290
299
  shown(acked) {
291
300
  if (acked === void 0) return;
292
- const { tick, tickRate } = this.#source;
293
- const kept = this.#pending.filter((p) => {
294
- if (p.seq > acked) return true;
295
- if (tickRate > 0 && p.link === this.#link) this.#learn(tick - p.sentAt, tickRate);
296
- return false;
297
- });
301
+ const kept = this.#pending.filter((p) => p.seq > acked);
298
302
  if (kept.length === this.#pending.length) return;
299
303
  this.#pending = kept;
300
304
  this.#version++;
@@ -334,7 +338,7 @@ var Predictor = class {
334
338
  let next = 0;
335
339
  const base_ = { now: Date.now(), players: [seat], predicting: true, send: noop, broadcast: noop };
336
340
  const applyDue = (before, at) => {
337
- while (next < pending.length && pending[next].sentAt + this.#lead <= before) {
341
+ while (next < pending.length && (pending[next].at ?? pending[next].sentAt + this.#lead) <= before) {
338
342
  const copy = structuredClone(state);
339
343
  try {
340
344
  world.onCommand(copy, pending[next].command, { ...base_, tick: at, player: seat, profile: void 0 });
@@ -359,7 +363,7 @@ var Predictor = class {
359
363
  }
360
364
  #learn(ticks, tickRate) {
361
365
  if (!Number.isFinite(ticks)) return;
362
- this.#leads.push(Math.max(1, Math.min(ticks, 2 * tickRate)));
366
+ this.#leads.push(Math.max(-2 * tickRate, Math.min(ticks, 2 * tickRate)));
363
367
  if (this.#leads.length > LEADS_KEPT) this.#leads.shift();
364
368
  const sorted = [...this.#leads].sort((a, b) => a - b);
365
369
  this.#lead = sorted[Math.floor(sorted.length / 2)];
@@ -424,7 +428,7 @@ function multiplayerClient(client, fragment, opts = {}) {
424
428
  const joining = /* @__PURE__ */ new Map();
425
429
  let nextSeq = 0;
426
430
  let detached = false;
427
- const open = (id, first, sentAt) => {
431
+ const open = (id, first) => {
428
432
  const clock = new ServerClock();
429
433
  const listeners = /* @__PURE__ */ new Map();
430
434
  let joined = first;
@@ -453,11 +457,11 @@ function multiplayerClient(client, fragment, opts = {}) {
453
457
  }
454
458
  }
455
459
  };
456
- const adopt = (j, sent) => {
460
+ const adopt = (j) => {
457
461
  const now = localNow();
458
462
  joined = j;
459
463
  latest = wire = j.frame;
460
- clock.roundTrip(now - sent);
464
+ nextSeq = Math.max(nextSeq, j.frame.acks[j.seat] ?? 0);
461
465
  clock.reading(j.frame.tick, j.tickRate, j.frame.ms, j.frame.epoch, now);
462
466
  buffer.update(j.frame.state, j.frame.tick, now);
463
467
  };
@@ -469,8 +473,10 @@ function multiplayerClient(client, fragment, opts = {}) {
469
473
  count.sent++;
470
474
  return dyn.mpCommand({ world: id, seq, command }).then(
471
475
  (result) => {
472
- if (result.applied) count.applied++;
473
- else {
476
+ if (result.applied) {
477
+ count.applied++;
478
+ predictor.applied(seq, result.tick);
479
+ } else {
474
480
  count.rejected++;
475
481
  predictor.settle(seq);
476
482
  emit("rejected", { seq, code: result.code, ...result.reason !== void 0 ? { reason: result.reason } : {} });
@@ -483,10 +489,14 @@ function multiplayerClient(client, fragment, opts = {}) {
483
489
  }
484
490
  );
485
491
  };
492
+ const sendTick = (now) => {
493
+ const t = clock.tick(now);
494
+ return t === void 0 || !(joined.tickRate > 0) ? t : t + 0.5;
495
+ };
486
496
  const untilNextTick = () => {
487
497
  const msPerTick = 1e3 / joined.tickRate;
488
- const t = clock.tick();
489
- return t === void 0 ? msPerTick : (Math.floor(t) + 1 - t) * msPerTick;
498
+ const t = sendTick();
499
+ return t === void 0 ? msPerTick : Math.ceil((Math.floor(t) + 1 - t) * msPerTick);
490
500
  };
491
501
  const flush = () => {
492
502
  flushTimer = void 0;
@@ -614,12 +624,11 @@ function multiplayerClient(client, fragment, opts = {}) {
614
624
  };
615
625
  const sync = () => {
616
626
  syncing++;
617
- const sent = localNow();
618
627
  dyn.mpJoin({ world: id }).then(
619
628
  (j) => {
620
629
  syncing--;
621
630
  if (closed) return;
622
- adopt(j, sent);
631
+ adopt(j);
623
632
  emit("frame", latest);
624
633
  },
625
634
  (error) => {
@@ -681,12 +690,12 @@ function multiplayerClient(client, fragment, opts = {}) {
681
690
  emit("closed", detail === void 0 ? { reason } : { reason, detail });
682
691
  }
683
692
  };
684
- const predictor = new Predictor(game, world);
693
+ const predictor = new Predictor(game, Object.create(world, { serverTick: { value: sendTick } }));
685
694
  const buffer = new BufferedInterpolator(game, world);
686
695
  const glide = new Glide((a, b, t) => game.lerp ? game.lerp(a, b, t) : b, 1e3 / (first.tickRate || 1));
687
696
  let glidedFrom;
688
- adopt(first, sentAt);
689
- if (first.tickRate > 0) pingTimer = setTimeout(ping, clockEveryMs);
697
+ adopt(first);
698
+ if (first.tickRate > 0) ping();
690
699
  return handle;
691
700
  };
692
701
  const offs = [
@@ -711,14 +720,13 @@ function multiplayerClient(client, fragment, opts = {}) {
711
720
  if (held) return Promise.resolve(held.world);
712
721
  const inFlight = joining.get(world);
713
722
  if (inFlight) return inFlight;
714
- const sent = localNow();
715
723
  const p = dyn.mpJoin({ world }).then((j) => {
716
724
  if (detached) {
717
725
  void dyn.mpLeave({ world }).catch(() => {
718
726
  });
719
727
  throw new import_core.SuperLineError("DISCONNECTED", "Multiplayer client closed");
720
728
  }
721
- const h = open(world, j, sent);
729
+ const h = open(world, j);
722
730
  handles.set(world, h);
723
731
  return h.world;
724
732
  }).finally(() => joining.delete(world));
package/dist/client.d.cts CHANGED
@@ -24,7 +24,7 @@ interface WorldEvents<W extends AnyWorld> {
24
24
  }
25
25
  /** What a world handle measures, read with `world.stats()`. Durations are local milliseconds. */
26
26
  interface WorldStats {
27
- /** Smoothed round trip of the join and clock requests, ms. Undefined until one has come back. */
27
+ /** Smoothed round trip of the clock requests, ms. Undefined until one has come back, and in a world with no tick. */
28
28
  rttMs: number | undefined;
29
29
  /** How far clock readings land from the client's estimate of the server clock, smoothed, ms. Lower is steadier. */
30
30
  clockErrorMs: number | undefined;
@@ -36,7 +36,7 @@ interface WorldStats {
36
36
  latenessMs: number | undefined;
37
37
  /** How much that lateness varies, smoothed, ms. */
38
38
  jitterMs: number;
39
- /** How many ticks ahead of the latest frame the prediction runs. */
39
+ /** How many ticks after it is sent a command first shows: how far ahead of the server's clock the prediction runs. */
40
40
  predictionLeadTicks: number;
41
41
  /** State frames received per second, over about the last second. */
42
42
  framesPerSecond: number;
package/dist/client.d.ts CHANGED
@@ -24,7 +24,7 @@ interface WorldEvents<W extends AnyWorld> {
24
24
  }
25
25
  /** What a world handle measures, read with `world.stats()`. Durations are local milliseconds. */
26
26
  interface WorldStats {
27
- /** Smoothed round trip of the join and clock requests, ms. Undefined until one has come back. */
27
+ /** Smoothed round trip of the clock requests, ms. Undefined until one has come back, and in a world with no tick. */
28
28
  rttMs: number | undefined;
29
29
  /** How far clock readings land from the client's estimate of the server clock, smoothed, ms. Lower is steadier. */
30
30
  clockErrorMs: number | undefined;
@@ -36,7 +36,7 @@ interface WorldStats {
36
36
  latenessMs: number | undefined;
37
37
  /** How much that lateness varies, smoothed, ms. */
38
38
  jitterMs: number;
39
- /** How many ticks ahead of the latest frame the prediction runs. */
39
+ /** How many ticks after it is sent a command first shows: how far ahead of the server's clock the prediction runs. */
40
40
  predictionLeadTicks: number;
41
41
  /** State frames received per second, over about the last second. */
42
42
  framesPerSecond: number;
package/dist/client.js CHANGED
@@ -225,7 +225,7 @@ var Predictor = class {
225
225
  this.#world = world;
226
226
  this.#source = source;
227
227
  }
228
- /** How many ticks ahead of the latest frame the prediction runs: how long a command takes to show. */
228
+ /** How many ticks after it is sent a command first shows: how far ahead of the clock the prediction runs. */
229
229
  get lead() {
230
230
  return this.#lead;
231
231
  }
@@ -244,6 +244,15 @@ var Predictor = class {
244
244
  this.#pending.push({ seq, command, sentAt, link: this.#link });
245
245
  this.#version++;
246
246
  }
247
+ /** The server applied a command to the state of `tick`, so it first shows at the tick after. */
248
+ applied(seq, tick) {
249
+ const p = this.#pending.find((q) => q.seq === seq);
250
+ if (!p) return;
251
+ p.at = tick + 1;
252
+ const { tickRate } = this.#source;
253
+ if (tickRate > 0 && p.link === this.#link) this.#learn(p.at - p.sentAt, tickRate);
254
+ this.#version++;
255
+ }
247
256
  /** A command was rejected or its request failed: its effect goes at once. */
248
257
  settle(seq) {
249
258
  const at = this.#pending.findIndex((p) => p.seq === seq);
@@ -254,12 +263,7 @@ var Predictor = class {
254
263
  /** A frame arrived, with the highest seq of this player's the server has handled. Call it after the source moved on. */
255
264
  shown(acked) {
256
265
  if (acked === void 0) return;
257
- const { tick, tickRate } = this.#source;
258
- const kept = this.#pending.filter((p) => {
259
- if (p.seq > acked) return true;
260
- if (tickRate > 0 && p.link === this.#link) this.#learn(tick - p.sentAt, tickRate);
261
- return false;
262
- });
266
+ const kept = this.#pending.filter((p) => p.seq > acked);
263
267
  if (kept.length === this.#pending.length) return;
264
268
  this.#pending = kept;
265
269
  this.#version++;
@@ -299,7 +303,7 @@ var Predictor = class {
299
303
  let next = 0;
300
304
  const base_ = { now: Date.now(), players: [seat], predicting: true, send: noop, broadcast: noop };
301
305
  const applyDue = (before, at) => {
302
- while (next < pending.length && pending[next].sentAt + this.#lead <= before) {
306
+ while (next < pending.length && (pending[next].at ?? pending[next].sentAt + this.#lead) <= before) {
303
307
  const copy = structuredClone(state);
304
308
  try {
305
309
  world.onCommand(copy, pending[next].command, { ...base_, tick: at, player: seat, profile: void 0 });
@@ -324,7 +328,7 @@ var Predictor = class {
324
328
  }
325
329
  #learn(ticks, tickRate) {
326
330
  if (!Number.isFinite(ticks)) return;
327
- this.#leads.push(Math.max(1, Math.min(ticks, 2 * tickRate)));
331
+ this.#leads.push(Math.max(-2 * tickRate, Math.min(ticks, 2 * tickRate)));
328
332
  if (this.#leads.length > LEADS_KEPT) this.#leads.shift();
329
333
  const sorted = [...this.#leads].sort((a, b) => a - b);
330
334
  this.#lead = sorted[Math.floor(sorted.length / 2)];
@@ -356,7 +360,7 @@ function multiplayerClient(client, fragment, opts = {}) {
356
360
  const joining = /* @__PURE__ */ new Map();
357
361
  let nextSeq = 0;
358
362
  let detached = false;
359
- const open = (id, first, sentAt) => {
363
+ const open = (id, first) => {
360
364
  const clock = new ServerClock();
361
365
  const listeners = /* @__PURE__ */ new Map();
362
366
  let joined = first;
@@ -385,11 +389,11 @@ function multiplayerClient(client, fragment, opts = {}) {
385
389
  }
386
390
  }
387
391
  };
388
- const adopt = (j, sent) => {
392
+ const adopt = (j) => {
389
393
  const now = localNow();
390
394
  joined = j;
391
395
  latest = wire = j.frame;
392
- clock.roundTrip(now - sent);
396
+ nextSeq = Math.max(nextSeq, j.frame.acks[j.seat] ?? 0);
393
397
  clock.reading(j.frame.tick, j.tickRate, j.frame.ms, j.frame.epoch, now);
394
398
  buffer.update(j.frame.state, j.frame.tick, now);
395
399
  };
@@ -401,8 +405,10 @@ function multiplayerClient(client, fragment, opts = {}) {
401
405
  count.sent++;
402
406
  return dyn.mpCommand({ world: id, seq, command }).then(
403
407
  (result) => {
404
- if (result.applied) count.applied++;
405
- else {
408
+ if (result.applied) {
409
+ count.applied++;
410
+ predictor.applied(seq, result.tick);
411
+ } else {
406
412
  count.rejected++;
407
413
  predictor.settle(seq);
408
414
  emit("rejected", { seq, code: result.code, ...result.reason !== void 0 ? { reason: result.reason } : {} });
@@ -415,10 +421,14 @@ function multiplayerClient(client, fragment, opts = {}) {
415
421
  }
416
422
  );
417
423
  };
424
+ const sendTick = (now) => {
425
+ const t = clock.tick(now);
426
+ return t === void 0 || !(joined.tickRate > 0) ? t : t + 0.5;
427
+ };
418
428
  const untilNextTick = () => {
419
429
  const msPerTick = 1e3 / joined.tickRate;
420
- const t = clock.tick();
421
- return t === void 0 ? msPerTick : (Math.floor(t) + 1 - t) * msPerTick;
430
+ const t = sendTick();
431
+ return t === void 0 ? msPerTick : Math.ceil((Math.floor(t) + 1 - t) * msPerTick);
422
432
  };
423
433
  const flush = () => {
424
434
  flushTimer = void 0;
@@ -546,12 +556,11 @@ function multiplayerClient(client, fragment, opts = {}) {
546
556
  };
547
557
  const sync = () => {
548
558
  syncing++;
549
- const sent = localNow();
550
559
  dyn.mpJoin({ world: id }).then(
551
560
  (j) => {
552
561
  syncing--;
553
562
  if (closed) return;
554
- adopt(j, sent);
563
+ adopt(j);
555
564
  emit("frame", latest);
556
565
  },
557
566
  (error) => {
@@ -613,12 +622,12 @@ function multiplayerClient(client, fragment, opts = {}) {
613
622
  emit("closed", detail === void 0 ? { reason } : { reason, detail });
614
623
  }
615
624
  };
616
- const predictor = new Predictor(game, world);
625
+ const predictor = new Predictor(game, Object.create(world, { serverTick: { value: sendTick } }));
617
626
  const buffer = new BufferedInterpolator(game, world);
618
627
  const glide = new Glide((a, b, t) => game.lerp ? game.lerp(a, b, t) : b, 1e3 / (first.tickRate || 1));
619
628
  let glidedFrom;
620
- adopt(first, sentAt);
621
- if (first.tickRate > 0) pingTimer = setTimeout(ping, clockEveryMs);
629
+ adopt(first);
630
+ if (first.tickRate > 0) ping();
622
631
  return handle;
623
632
  };
624
633
  const offs = [
@@ -643,14 +652,13 @@ function multiplayerClient(client, fragment, opts = {}) {
643
652
  if (held) return Promise.resolve(held.world);
644
653
  const inFlight = joining.get(world);
645
654
  if (inFlight) return inFlight;
646
- const sent = localNow();
647
655
  const p = dyn.mpJoin({ world }).then((j) => {
648
656
  if (detached) {
649
657
  void dyn.mpLeave({ world }).catch(() => {
650
658
  });
651
659
  throw new SuperLineError("DISCONNECTED", "Multiplayer client closed");
652
660
  }
653
- const h = open(world, j, sent);
661
+ const h = open(world, j);
654
662
  handles.set(world, h);
655
663
  return h.world;
656
664
  }).finally(() => joining.delete(world));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super-line/plugin-multiplayer",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Server-run game worlds for super-line — the server owns one JSON state per world and sends it to every seated player. A paired plugin (contract · server · client).",
6
6
  "keywords": [
@@ -86,11 +86,11 @@
86
86
  },
87
87
  "devDependencies": {
88
88
  "@super-line/client": "^0.18.2",
89
- "@super-line/collections-memory": "0.3.0",
90
89
  "@super-line/collections-sqlite": "0.5.2",
91
- "@super-line/server": "^0.25.0",
90
+ "@super-line/collections-memory": "0.3.0",
91
+ "@super-line/core": "^0.24.0",
92
92
  "@super-line/transport-loopback": "0.7.0",
93
- "@super-line/core": "^0.24.0"
93
+ "@super-line/server": "^0.25.0"
94
94
  },
95
95
  "scripts": {
96
96
  "build": "tsup"