@hadoku/game-server 0.1.200 → 0.1.202

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.
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A host that stands in for one that does not exist yet, or that may be swapped underneath its
3
+ * match: a queued start's host until its JVM is asked for (`start-queue.ts`), and the host a
4
+ * placed match talks to while a failed start is placed again (`placement/coordinator.ts`).
5
+ */
6
+ import type { MatchHost } from './host.js';
7
+ type Remembered = Parameters<NonNullable<MatchHost<{
8
+ t: string;
9
+ }>['remember']>>[0];
10
+ export declare class DeferredHost<C extends {
11
+ t: string;
12
+ }> implements MatchHost<C> {
13
+ private target;
14
+ private readonly onStopWithoutTarget;
15
+ private readonly held;
16
+ private remembered;
17
+ /** `onStopWithoutTarget`: what `stop` does while there is no target to stop. */
18
+ constructor(target?: MatchHost<C> | null, onStopWithoutTarget?: () => void);
19
+ /** The real host: the latest remembered state goes to it, then every held command, in order. */
20
+ become(target: MatchHost<C>): void;
21
+ send(command: C): void;
22
+ stop(): void;
23
+ remember(state: Remembered): void;
24
+ /** With no target there is no JVM to leave running. */
25
+ handOver(): boolean;
26
+ }
27
+ export {};
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A host that stands in for one that does not exist yet, or that may be swapped underneath its
3
+ * match: a queued start's host until its JVM is asked for (`start-queue.ts`), and the host a
4
+ * placed match talks to while a failed start is placed again (`placement/coordinator.ts`).
5
+ */
6
+ export class DeferredHost {
7
+ target;
8
+ onStopWithoutTarget;
9
+ held = [];
10
+ remembered = null;
11
+ /** `onStopWithoutTarget`: what `stop` does while there is no target to stop. */
12
+ constructor(target = null, onStopWithoutTarget = () => { }) {
13
+ this.target = target;
14
+ this.onStopWithoutTarget = onStopWithoutTarget;
15
+ }
16
+ /** The real host: the latest remembered state goes to it, then every held command, in order. */
17
+ become(target) {
18
+ this.target = target;
19
+ if (this.remembered !== null)
20
+ target.remember?.(this.remembered);
21
+ for (const command of this.held.splice(0))
22
+ target.send(command);
23
+ }
24
+ send(command) {
25
+ if (this.target !== null)
26
+ this.target.send(command);
27
+ else
28
+ this.held.push(command);
29
+ }
30
+ stop() {
31
+ if (this.target !== null)
32
+ this.target.stop();
33
+ else
34
+ this.onStopWithoutTarget();
35
+ }
36
+ remember(state) {
37
+ this.remembered = state;
38
+ this.target?.remember?.(state);
39
+ }
40
+ /** With no target there is no JVM to leave running. */
41
+ handOver() {
42
+ return this.target?.handOver?.() ?? false;
43
+ }
44
+ }
package/dist/play/host.js CHANGED
@@ -123,8 +123,9 @@
123
123
  import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmSync } from 'node:fs';
124
124
  import { randomBytes } from 'node:crypto';
125
125
  import { join } from 'node:path';
126
+ import { writeFileAtomicSync } from '@wolffm/host-wire';
126
127
  import { jsonLogger } from '@wolffm/service-kernel/log';
127
- import { JVM_HEAP_MB, MATCHES_PER_JVM, RUN_DIR_NAME, localJvmLauncher, writeFileAtomic } from './local-jvm.js';
128
+ import { JVM_HEAP_MB, MATCHES_PER_JVM, RUN_DIR_NAME, localJvmLauncher } from './local-jvm.js';
128
129
  const LIFECYCLE_T = new Set(['ready', 'fatal', 'frame', 'snapshot']);
129
130
  /** Narrows any engine's line onto the four the host itself understands. See {@link HostLifecycleLine}. */
130
131
  export function isLifecycle(line) {
@@ -280,12 +281,12 @@ export function jvmHostFactory(config, options = {}) {
280
281
  matches: hosted.map((h) => recordEntry(h.entry)),
281
282
  };
282
283
  try {
283
- writeFileAtomic(join(jvm.dir, JVM_RECORD_FILE), `${JSON.stringify(record, null, 2)}\n`);
284
+ writeFileAtomicSync(join(jvm.dir, JVM_RECORD_FILE), `${JSON.stringify(record, null, 2)}\n`);
284
285
  for (const h of hosted)
285
286
  h.changed = false;
286
287
  }
287
288
  catch (err) {
288
- // The old record is whole (`writeFileAtomic`), so it names each match as it was. One whose
289
+ // The old record is whole (`writeFileAtomicSync`), so it names each match as it was. One whose
289
290
  // entry changed since cannot be re-attached as it is now, and must not be handed over.
290
291
  poolLog({ evt: 'jvm-record-failed', id: jvm.hostId, error: err.message });
291
292
  for (const h of hosted)
@@ -252,13 +252,6 @@ export declare function bridgeLaunch(config: BridgeConfig, log: (entry: Record<s
252
252
  export declare function bridgeVersion(config: BridgeConfig): string;
253
253
  /** `{hostId, pid, startedAt}`: what a later process finds a running JVM by. Written at launch. */
254
254
  export declare const LAUNCH_RECORD_FILE = "launch.json";
255
- /**
256
- * Write `file` whole or not at all: into a temporary file beside it, then renamed over
257
- * it. `writeFileSync` in place truncates first, so a full disk left `jvm.json` empty and
258
- * its match un-re-attachable (hosting-test-plan C6); here a failed write leaves the old
259
- * file as it was, and throws.
260
- */
261
- export declare function writeFileAtomic(file: string, text: string): void;
262
255
  /**
263
256
  * Forge resolves `res/` against the working directory (`ForgeConstants.RES_DIR`
264
257
  * is "res/" when the version string carries no "git"), and reads its user and
@@ -78,6 +78,7 @@ import { closeSync, existsSync, mkdirSync, openSync, readdirSync, readSync, read
78
78
  import { homedir, tmpdir } from 'node:os';
79
79
  import { basename, dirname, join, resolve } from 'node:path';
80
80
  import { createInterface } from 'node:readline';
81
+ import { writeFileAtomicSync } from '@wolffm/host-wire';
81
82
  import { bridgeDiagnostics } from './diagnostics.js';
82
83
  import { CRASH_MARKER, IN_FIFO, JVM_TMP_DIR, OUT_FIFO, PID_POLL_MS, REAP_CONFIRM_MS, cwdIsUnder, defaultJava, makeFifos, openJvmIo, pidIsRunning, pidIsStandingIn, pidIsStandingInRun, positiveInt, processCwds, pruneCrashRecords, runDirs, sleepSync, wrapChildProcess, } from './engine-runtime.js';
83
84
  /**
@@ -301,23 +302,6 @@ export const LAUNCH_RECORD_FILE = 'launch.json';
301
302
  // under the default `/tmp` every killed match leaked one copy (3,036 of them on the host's
302
303
  // RAM-backed /tmp, 2026-10-10). Inside the run dir it goes wherever the run dir goes: removed on
303
304
  // a clean end, pruned on a crash.
304
- /**
305
- * Write `file` whole or not at all: into a temporary file beside it, then renamed over
306
- * it. `writeFileSync` in place truncates first, so a full disk left `jvm.json` empty and
307
- * its match un-re-attachable (hosting-test-plan C6); here a failed write leaves the old
308
- * file as it was, and throws.
309
- */
310
- export function writeFileAtomic(file, text) {
311
- const tmp = `${file}.tmp-${process.pid}-${randomBytes(4).toString('hex')}`;
312
- try {
313
- writeFileSync(tmp, text);
314
- renameSync(tmp, file);
315
- }
316
- catch (err) {
317
- rmSync(tmp, { force: true });
318
- throw err;
319
- }
320
- }
321
305
  /**
322
306
  * Forge resolves `res/` against the working directory (`ForgeConstants.RES_DIR`
323
307
  * is "res/" when the version string carries no "git"), and reads its user and
@@ -445,7 +429,7 @@ function keepCrashRecord(run, record) {
445
429
  if (!keep.has(entry))
446
430
  rmSync(join(run, entry), { recursive: true, force: true });
447
431
  }
448
- writeFileAtomic(join(run, CRASH_MARKER), `${JSON.stringify(record, null, 2)}\n`);
432
+ writeFileAtomicSync(join(run, CRASH_MARKER), `${JSON.stringify(record, null, 2)}\n`);
449
433
  }
450
434
  /** `<run>/launch.json`, or null when it is missing, unreadable or not one. */
451
435
  export function readLaunchRecord(run) {
@@ -584,7 +568,7 @@ class LocalJvm {
584
568
  this.pid = engineProcess.pid;
585
569
  live.add(this);
586
570
  log({ evt: 'jvm-spawned', id: hostId, pid: this.pid, run, bridge: bridge.how });
587
- writeFileAtomic(join(run, LAUNCH_RECORD_FILE), `${JSON.stringify({ hostId, pid: this.pid, startedAt: new Date().toISOString() })}\n`);
571
+ writeFileAtomicSync(join(run, LAUNCH_RECORD_FILE), `${JSON.stringify({ hostId, pid: this.pid, startedAt: new Date().toISOString() })}\n`);
588
572
  this.read();
589
573
  engineProcess.onError((err) => this.finish(`the JVM could not be started: ${err.message}`, { code: null, signal: null }));
590
574
  engineProcess.onExit((code, signal) => {
@@ -26,6 +26,7 @@
26
26
  * everything else on the factory.
27
27
  */
28
28
  import { positiveInt } from './engine-runtime.js';
29
+ import { DeferredHost } from './deferred-host.js';
29
30
  /**
30
31
  * JVMs booting at once before a start that needs another waits. A lone boot on a claw under CI
31
32
  * load took 7.8–10.5 s and ten together ~50 s, so two keeps one well inside the 60 s start timeout
@@ -36,61 +37,6 @@ export const COLD_STARTS = 2;
36
37
  export function coldStartsFrom(env = process.env) {
37
38
  return positiveInt(env.CARDTABLE_PLAY_COLD_STARTS, COLD_STARTS);
38
39
  }
39
- /** A waiting start's host until its JVM is asked for. */
40
- class WaitingHost {
41
- leave;
42
- events;
43
- host = null;
44
- held = [];
45
- remembered = null;
46
- gone = false;
47
- constructor(leave, events) {
48
- this.leave = leave;
49
- this.events = events;
50
- }
51
- /** The real host: everything held goes to it, in order. */
52
- become(host) {
53
- this.host = host;
54
- if (this.remembered !== null)
55
- host.remember?.(this.remembered);
56
- for (const command of this.held.splice(0))
57
- host.send(command);
58
- }
59
- /** Never booted: the queue was dropped (the server is closing or handing over). */
60
- drop(reason) {
61
- if (this.gone)
62
- return;
63
- this.gone = true;
64
- this.events.exit(reason);
65
- }
66
- send(command) {
67
- if (this.host !== null)
68
- this.host.send(command);
69
- else
70
- this.held.push(command);
71
- }
72
- stop() {
73
- if (this.host !== null) {
74
- this.host.stop();
75
- return;
76
- }
77
- if (this.gone)
78
- return;
79
- this.leave();
80
- // A host's exit always comes after `stop` returns; so does this one.
81
- queueMicrotask(() => this.drop('stopped before its JVM started'));
82
- }
83
- remember(state) {
84
- if (this.host !== null)
85
- this.host.remember?.(state);
86
- else
87
- this.remembered = state;
88
- }
89
- /** A start still waiting has no JVM to leave running. */
90
- handOver() {
91
- return this.host?.handOver?.() ?? false;
92
- }
93
- }
94
40
  export function queueColdStarts(factory, limit, log) {
95
41
  const waiting = [];
96
42
  const booting = () => factory.booting?.() ?? 0;
@@ -120,7 +66,7 @@ export function queueColdStarts(factory, limit, log) {
120
66
  }
121
67
  catch (err) {
122
68
  next.events.unstartable?.(err);
123
- next.host.drop(`Forge could not be started: ${err.message}`);
69
+ next.drop(`Forge could not be started: ${err.message}`);
124
70
  continue;
125
71
  }
126
72
  next.host.become(host);
@@ -138,23 +84,35 @@ export function queueColdStarts(factory, limit, log) {
138
84
  }
139
85
  function dropAll(reason) {
140
86
  for (const w of waiting.splice(0))
141
- w.host.drop(reason);
87
+ w.drop(reason);
142
88
  }
143
89
  const queued = ((matchId, spec, events) => {
144
90
  if (waiting.length === 0 && free())
145
91
  return begin(matchId, spec, events, null);
92
+ let gone = false;
93
+ const drop = (reason) => {
94
+ if (gone)
95
+ return;
96
+ gone = true;
97
+ events.exit(reason);
98
+ };
146
99
  const entry = {
147
100
  matchId,
148
101
  spec,
149
102
  events,
103
+ drop,
150
104
  place: 0,
151
105
  since: Date.now(),
152
- host: new WaitingHost(() => {
106
+ host: new DeferredHost(null, () => {
107
+ if (gone)
108
+ return;
153
109
  const i = waiting.indexOf(entry);
154
110
  if (i >= 0)
155
111
  waiting.splice(i, 1);
156
112
  pump();
157
- }, events),
113
+ // A host's exit always comes after `stop` returns; so does this one.
114
+ queueMicrotask(() => drop('stopped before its JVM started'));
115
+ }),
158
116
  };
159
117
  waiting.push(entry);
160
118
  log({ evt: 'start-queued', matchId, place: waiting.length, booting: booting(), limit });
@@ -679,13 +679,35 @@ export class RoomLobby {
679
679
  this.#avatarListeners.delete(listener);
680
680
  };
681
681
  }
682
- /** The room by id when it seats through its game seats, with the refusal every seat op shares otherwise. */
683
- #seatRoom(roomId, memberId, override = false) {
682
+ /** The room by id, refused `no-room`, then `not-member` unless `override` (an operator's reach). */
683
+ #memberRoom(roomId, memberId, override = false) {
684
684
  const room = this.#rooms.get(roomId);
685
685
  if (room === undefined)
686
686
  return { refused: 'no-room' };
687
687
  if (!override && !isRoomMember(room, memberId))
688
688
  return { refused: 'not-member' };
689
+ return { room };
690
+ }
691
+ /** `found`, or `not-leader` when `memberId` does not lead its room; `override` (an operator's reach) skips the check. */
692
+ #led(found, memberId, override) {
693
+ return 'refused' in found || override || found.room.leader === memberId ? found : { refused: 'not-leader' };
694
+ }
695
+ /** The room `memberId` leads: `no-room`, then `not-member`, then `not-leader`, the opening the leader's ops share. */
696
+ #leaderRoom(roomId, memberId, override) {
697
+ return this.#led(this.#memberRoom(roomId, memberId, override), memberId, override);
698
+ }
699
+ /** A seat op's change, kept: saved, logged, the type told its seats changed (given `hooks`), and the room answered. */
700
+ #kept(room, entry, hooks) {
701
+ this.persist(room);
702
+ this.log(entry);
703
+ hooks?.seatsChanged?.(room);
704
+ return { ok: true, room: this.view(room) };
705
+ }
706
+ /** `found`'s room when it seats through its game seats, with the refusal every seat op shares otherwise. */
707
+ #seatRoom(found) {
708
+ if ('refused' in found)
709
+ return found;
710
+ const { room } = found;
689
711
  const hooks = this.hooksOf(room);
690
712
  // `not-draft`, the wire's name for a room that does not seat through its game seats.
691
713
  if (!hooks.usesSeats(room))
@@ -707,12 +729,10 @@ export class RoomLobby {
707
729
  * this way). `room.seats` always `supportsBot` (its `AI_SEAT` marker); a type's own stand-in
708
730
  * says for itself.
709
731
  */
710
- #seatOpRoom(roomId, memberId, override = false) {
711
- const room = this.#rooms.get(roomId);
712
- if (room === undefined)
713
- return { refused: 'no-room' };
714
- if (!override && !isRoomMember(room, memberId))
715
- return { refused: 'not-member' };
732
+ #seatOpRoom(found) {
733
+ if ('refused' in found)
734
+ return found;
735
+ const { room } = found;
716
736
  const hooks = this.hooksOf(room);
717
737
  if (hooks.usesSeats(room)) {
718
738
  if (hooks.seatsFixed(room))
@@ -741,7 +761,7 @@ export class RoomLobby {
741
761
  * any seat they held.
742
762
  */
743
763
  sitSeat(roomId, memberId, seat) {
744
- const found = this.#seatRoom(roomId, memberId);
764
+ const found = this.#seatRoom(this.#memberRoom(roomId, memberId));
745
765
  if ('refused' in found)
746
766
  return { ok: false, reason: found.refused };
747
767
  const { room, hooks } = found;
@@ -763,33 +783,26 @@ export class RoomLobby {
763
783
  hooks.growSeats(room, target);
764
784
  takeGameSeat(room, memberId, target);
765
785
  hooks.trimSeats(room);
766
- this.persist(room);
767
- this.log({ event: 'pod-sit', roomId, memberId, seat: target });
768
- hooks.seatsChanged?.(room);
769
- return { ok: true, room: this.view(room) };
786
+ return this.#kept(room, { event: 'pod-sit', roomId, memberId, seat: target }, hooks);
770
787
  }
771
788
  /** Give up a game seat. Idempotent: not seated is not a failure. */
772
789
  standSeat(roomId, memberId) {
773
- const found = this.#seatRoom(roomId, memberId);
790
+ const found = this.#seatRoom(this.#memberRoom(roomId, memberId));
774
791
  if ('refused' in found)
775
792
  return { ok: false, reason: found.refused };
776
793
  const { room, hooks } = found;
777
794
  if (!leaveGameSeat(room, memberId))
778
795
  return { ok: true, room: this.view(room) };
779
796
  hooks.trimSeats(room);
780
- this.persist(room);
781
- this.log({ event: 'pod-stand', roomId, memberId });
782
- hooks.seatsChanged?.(room);
783
- return { ok: true, room: this.view(room) };
797
+ return this.#kept(room, { event: 'pod-stand', roomId, memberId }, hooks);
784
798
  }
785
799
  /** Put the AI in an open seat (`on`), or take it out. The leader's, or `override`'s (see `removeFromSeat`). */
786
800
  aiSeat(roomId, memberId, seat, on, override = false) {
787
- const found = this.#seatRoom(roomId, memberId, override);
801
+ // The seat checks come before the leader's here, as they always have.
802
+ const found = this.#led(this.#seatRoom(this.#memberRoom(roomId, memberId, override)), memberId, override);
788
803
  if ('refused' in found)
789
804
  return { ok: false, reason: found.refused };
790
805
  const { room, hooks } = found;
791
- if (!override && room.leader !== memberId)
792
- return { ok: false, reason: 'not-leader' };
793
806
  if (hooks.fieldFixed?.(room) === true)
794
807
  return { ok: false, reason: 'historical' };
795
808
  if (on && this.seatsShut(room) !== null)
@@ -805,24 +818,14 @@ export class RoomLobby {
805
818
  setGameSeat(room, seat, on ? AI_SEAT : null);
806
819
  hooks.aiSeated?.(room, seat, on);
807
820
  hooks.trimSeats(room);
808
- this.persist(room);
809
- this.log({ event: on ? 'pod-ai-sit' : 'pod-ai-stand', roomId, memberId, seat });
810
- hooks.seatsChanged?.(room);
811
- return { ok: true, room: this.view(room) };
821
+ return this.#kept(room, { event: on ? 'pod-ai-sit' : 'pod-ai-stand', roomId, memberId, seat }, hooks);
812
822
  }
813
823
  /** Close an empty seat, or open a closed one: the leader's, or `override`'s (see `removeFromSeat`); never fewer than `MIN_OPEN_SEATS` open. */
814
824
  closeSeat(roomId, memberId, seat, closed, override = false) {
815
- const room = this.#rooms.get(roomId);
816
- if (room === undefined)
817
- return { ok: false, reason: 'no-room' };
818
- if (!override && !isRoomMember(room, memberId))
819
- return { ok: false, reason: 'not-member' };
820
- if (!override && room.leader !== memberId)
821
- return { ok: false, reason: 'not-leader' };
822
- const found = this.#seatRoom(roomId, memberId, override);
825
+ const found = this.#seatRoom(this.#leaderRoom(roomId, memberId, override));
823
826
  if ('refused' in found)
824
827
  return { ok: false, reason: found.refused };
825
- const { hooks } = found;
828
+ const { room, hooks } = found;
826
829
  if (hooks.fieldFixed?.(room) === true)
827
830
  return { ok: false, reason: 'historical' };
828
831
  if (!this.#inReach(room, hooks, seat) || seat >= room.seats.length)
@@ -837,30 +840,23 @@ export class RoomLobby {
837
840
  return { ok: false, reason: 'too-few' };
838
841
  setGameSeat(room, seat, closed ? CLOSED_SEAT : null);
839
842
  hooks.trimSeats(room);
840
- this.persist(room);
841
- this.log({ event: closed ? 'pod-seat-closed' : 'pod-seat-opened', roomId, memberId, seat, size: size() });
842
- return { ok: true, room: this.view(room) };
843
+ return this.#kept(room, { event: closed ? 'pod-seat-closed' : 'pod-seat-opened', roomId, memberId, seat, size: size() });
843
844
  }
844
845
  /**
845
846
  * The leader hands the room's leadership to `targetId`, another member, seated or watching.
846
847
  * Passing it to yourself is answered `ok` and changes nothing.
847
848
  */
848
849
  passLeader(roomId, memberId, targetId, override = false) {
849
- const room = this.#rooms.get(roomId);
850
- if (room === undefined)
851
- return { ok: false, reason: 'no-room' };
852
- if (!override && !isRoomMember(room, memberId))
853
- return { ok: false, reason: 'not-member' };
854
- if (!override && room.leader !== memberId)
855
- return { ok: false, reason: 'not-leader' };
850
+ const found = this.#leaderRoom(roomId, memberId, override);
851
+ if ('refused' in found)
852
+ return { ok: false, reason: found.refused };
853
+ const { room } = found;
856
854
  // Already theirs: nothing to move. An operator naming themselves takes a lead that was someone else's.
857
855
  if (targetId === room.leader)
858
856
  return { ok: true, room: this.view(room) };
859
857
  if (!passRoomLeader(room, targetId))
860
858
  return { ok: false, reason: 'target-not-member' };
861
- this.persist(room);
862
- this.log({ event: 'leader-passed', roomId, memberId, targetId });
863
- return { ok: true, room: this.view(room) };
859
+ return this.#kept(room, { event: 'leader-passed', roomId, memberId, targetId });
864
860
  }
865
861
  /**
866
862
  * The leader removes `targetId` from whatever game seat they hold, to the spectator row.
@@ -877,30 +873,21 @@ export class RoomLobby {
877
873
  * needing to be neither the leader nor a member.
878
874
  */
879
875
  removeFromSeat(roomId, memberId, targetId, override = false) {
880
- const room = this.#rooms.get(roomId);
881
- if (room === undefined)
882
- return { ok: false, reason: 'no-room' };
883
- if (!override && !isRoomMember(room, memberId))
884
- return { ok: false, reason: 'not-member' };
885
- if (!override && room.leader !== memberId)
886
- return { ok: false, reason: 'not-leader' };
887
- if (!isRoomMember(room, targetId))
876
+ const led = this.#leaderRoom(roomId, memberId, override);
877
+ if (!('refused' in led) && !isRoomMember(led.room, targetId))
888
878
  return { ok: false, reason: 'target-not-member' };
889
- const found = this.#seatOpRoom(roomId, memberId, override);
879
+ const found = this.#seatOpRoom(led);
890
880
  if ('refused' in found)
891
881
  return { ok: false, reason: found.refused };
892
- const { room: r, hooks, target, generic } = found;
882
+ const { room, hooks, target, generic } = found;
893
883
  const seats = target.read();
894
884
  const at = seats.indexOf(targetId);
895
885
  if (at === -1)
896
- return { ok: true, room: this.view(r) };
886
+ return { ok: true, room: this.view(room) };
897
887
  target.write(seats.map((s, i) => (i === at ? null : s)));
898
888
  if (generic)
899
- hooks.trimSeats(r);
900
- this.persist(r);
901
- this.log({ event: 'seat-removed-by-leader', roomId, memberId, targetId });
902
- hooks.seatsChanged?.(r);
903
- return { ok: true, room: this.view(r) };
889
+ hooks.trimSeats(room);
890
+ return this.#kept(room, { event: 'seat-removed-by-leader', roomId, memberId, targetId }, hooks);
904
891
  }
905
892
  /**
906
893
  * The leader closes seats from the TOP down until `count` are open, evicting any occupant
@@ -913,32 +900,22 @@ export class RoomLobby {
913
900
  * Resolved through `#seatOpRoom` (wave 120 §S2): see `removeFromSeat`'s header.
914
901
  */
915
902
  lowerSeatCapacity(roomId, memberId, count, override = false) {
916
- const room = this.#rooms.get(roomId);
917
- if (room === undefined)
918
- return { ok: false, reason: 'no-room' };
919
- if (!override && !isRoomMember(room, memberId))
920
- return { ok: false, reason: 'not-member' };
921
- if (!override && room.leader !== memberId)
922
- return { ok: false, reason: 'not-leader' };
923
- const found = this.#seatOpRoom(roomId, memberId, override);
903
+ const found = this.#seatOpRoom(this.#leaderRoom(roomId, memberId, override));
924
904
  if ('refused' in found)
925
905
  return { ok: false, reason: found.refused };
926
- const { room: r, hooks, target, generic } = found;
927
- if (hooks.fieldFixed?.(r) === true)
906
+ const { room, hooks, target, generic } = found;
907
+ if (hooks.fieldFixed?.(room) === true)
928
908
  return { ok: false, reason: 'historical' };
929
909
  if (!Number.isInteger(count) || count < MIN_OPEN_SEATS)
930
910
  return { ok: false, reason: 'too-few' };
931
911
  const before = target.read();
932
912
  const { seats, evicted } = lowerCapacity(before, count);
933
913
  if (evicted.length === 0 && seats.every((s, i) => s === before[i]))
934
- return { ok: true, room: this.view(r), evicted };
914
+ return { ok: true, room: this.view(room), evicted };
935
915
  target.write(seats);
936
916
  if (generic)
937
- hooks.trimSeats(r);
938
- this.persist(r);
939
- this.log({ event: 'seat-capacity-lowered', roomId, memberId, count, evicted: evicted.length });
940
- hooks.seatsChanged?.(r);
941
- return { ok: true, room: this.view(r), evicted };
917
+ hooks.trimSeats(room);
918
+ return { ...this.#kept(room, { event: 'seat-capacity-lowered', roomId, memberId, count, evicted: evicted.length }, hooks), evicted };
942
919
  }
943
920
  /**
944
921
  * The leader puts the AI in `seat`, evicting whoever holds it first — a human to the spectator
@@ -952,35 +929,25 @@ export class RoomLobby {
952
929
  * needing to be neither the leader nor a member.
953
930
  */
954
931
  botReplacesHuman(roomId, memberId, seat, override = false) {
955
- const room = this.#rooms.get(roomId);
956
- if (room === undefined)
957
- return { ok: false, reason: 'no-room' };
958
- if (!override && !isRoomMember(room, memberId))
959
- return { ok: false, reason: 'not-member' };
960
- if (!override && room.leader !== memberId)
961
- return { ok: false, reason: 'not-leader' };
962
- const found = this.#seatOpRoom(roomId, memberId, override);
932
+ const found = this.#seatOpRoom(this.#leaderRoom(roomId, memberId, override));
963
933
  if ('refused' in found)
964
934
  return { ok: false, reason: found.refused };
965
- const { room: r, hooks, target, generic } = found;
966
- if (hooks.fieldFixed?.(r) === true)
935
+ const { room, hooks, target, generic } = found;
936
+ if (hooks.fieldFixed?.(room) === true)
967
937
  return { ok: false, reason: 'historical' };
968
938
  if (!target.supportsBot)
969
939
  return { ok: false, reason: 'no-bot-seat' };
970
- if (!this.#inReach(r, hooks, seat) || seat >= r.seats.length)
940
+ if (!this.#inReach(room, hooks, seat) || seat >= room.seats.length)
971
941
  return { ok: false, reason: 'bad-seat' };
972
942
  const before = target.read();
973
943
  const { seats, evicted } = replaceWithBot(before, seat);
974
944
  if (evicted === null && seats[seat] === before[seat])
975
- return { ok: true, room: this.view(r), evicted: null };
945
+ return { ok: true, room: this.view(room), evicted: null };
976
946
  target.write(seats);
977
- hooks.aiSeated?.(r, seat, true);
947
+ hooks.aiSeated?.(room, seat, true);
978
948
  if (generic)
979
- hooks.trimSeats(r);
980
- this.persist(r);
981
- this.log({ event: 'seat-bot-replaced-human', roomId, memberId, seat, evicted: evicted !== null });
982
- hooks.seatsChanged?.(r);
983
- return { ok: true, room: this.view(r), evicted };
949
+ hooks.trimSeats(room);
950
+ return { ...this.#kept(room, { event: 'seat-bot-replaced-human', roomId, memberId, seat, evicted: evicted !== null }, hooks), evicted };
984
951
  }
985
952
  /**
986
953
  * Forget every room untouched for longer than `idleMs` (or its type's own idle window), on disk as
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.200",
4
- "gitHead": "7322504089601dad1d2e8d06319a29f821c89e42",
3
+ "version": "0.1.202",
4
+ "gitHead": "4ba2350938ced47e5519c9acf331d81ff6f6235c",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any hosted game shares, above the kernel: the app config and edge auth, the room core (identity, seats, sessions, membership, the room list, the generic room routes), match hosting (the host contract, local and remote JVMs, placement, the start queue, recordings, the in-process and relay runtimes), streams, erase, telemetry and usage. Knows no game; Magic plugs in from apps/server. MIT; imports only node built-ins, express, socket.io, @wolffm/* and the platform packages.",
@@ -17,9 +17,9 @@
17
17
  "play/remote/"
18
18
  ],
19
19
  "dependencies": {
20
- "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.200",
21
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.200",
22
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.233",
20
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.202",
21
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.202",
22
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.235",
23
23
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.3",
24
24
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.150",
25
25
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.150",