@hadoku/game-server 0.1.179 → 0.1.180

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.
@@ -181,9 +181,8 @@ export declare const JOIN_RATE_LIMIT: RateLimitConfig;
181
181
  export declare const ROOM_CREATE_RATE_LIMIT: RateLimitConfig;
182
182
  /**
183
183
  * The seat ops every member and the leader take on a room's own game seats (`room/lobby.ts`'s
184
- * `RoomLobby`): sit, stand, close or open an empty seat, give the AI one or take it back. A game whose
185
- * room IS its seats (`plainRoomType`) hands its lobby to `RoomCoreDeps.seatRoutes`; Magic seats its
186
- * pods through routes of its own and does not.
184
+ * `RoomLobby`): sit, stand, close or open an empty seat, give the AI one or take it back. Every game
185
+ * hands its lobby to `RoomCoreDeps.seatRoutes`: Magic's pods and a plain room sit through the same routes.
187
186
  */
188
187
  export interface SeatRouteLobby<V> {
189
188
  sitSeat(roomId: string, callerId: string, seat?: number): {
@@ -218,8 +217,9 @@ export interface SeatRouteLobby<V> {
218
217
  export interface RoomCoreDeps<V extends RoomViewCore, C, P extends object, G = never> {
219
218
  /**
220
219
  * Serve the seat routes on this lobby: `/:id/seats/sit` `{ seat? }`, `/:id/seats/stand` `{}`,
221
- * `/:id/seats/close` `{ seat, closed }` (the leader's) and `/:id/seats/bot-off` `{ seat }` (the
222
- * leader's; `/seats/bot` puts one in). Absent: none of the four is registered.
220
+ * `/:id/seats/close` `{ seat, closed }`, `/:id/seats/bot-on` `{ seat }` (an open seat only) and
221
+ * `/:id/seats/bot-off` `{ seat }` (the last three the leader's; `/seats/bot` also takes a human's
222
+ * seat). No body at all is `{}`. Absent: none of the five is registered.
223
223
  */
224
224
  seatRoutes?: SeatRouteLobby<V>;
225
225
  /**
@@ -23,11 +23,12 @@
23
23
  import { Router } from 'express';
24
24
  import { MultiRateLimiter } from '@wolffm/rooms-server';
25
25
  import { isDurableParticipantId } from '@wolffm/rooms-protocol';
26
+ import { SEATS_OPEN_BEFORE_MS } from '@wolffm/platform-wire/events';
26
27
  import { edgeTierOf, isAdmin } from '../edge-auth.js';
27
28
  import { tierAdmits } from '../room/tier-floor.js';
28
- import { bodyErrors, callerOf, parseBody, perCallerLimit } from './request.js';
29
+ import { bodyErrors, callerOf, parseBody, perCallerLimit, readBody } from './request.js';
29
30
  import { isWatchpartyRoomId, watchpartyRoomId } from '../room/watchparty-link.js';
30
- import { jsonLogger } from '../log.js';
31
+ import { jsonLogger } from '@wolffm/service-kernel/log';
31
32
  /**
32
33
  * `{ settings }`' refusals, by the lobby's reason: the leader's alone to change (403), and a lock
33
34
  * that holds (409, with the lock's reason so a page can tell which). A game whose locks have
@@ -52,13 +53,16 @@ function field(check, message) {
52
53
  },
53
54
  };
54
55
  }
55
- const removeFromSeatBody = field((v) => typeof v === 'object' && v !== null && typeof v.memberId === 'string', 'memberId must be a string');
56
- const lowerCapacityBody = field((v) => typeof v === 'object' && v !== null && Number.isInteger(v.count), 'count must be an integer');
57
- const sitSeatBody = field((v) => typeof v === 'object' && v !== null && (v.seat === undefined || (Number.isInteger(v.seat) && (v.seat >= 0))), 'seat must be a non-negative integer, or absent');
58
- const emptyBody = field((v) => typeof v === 'object' && v !== null && Object.keys(v).length === 0, 'expected an empty body');
59
- const closeSeatBody = field((v) => typeof v === 'object' && v !== null && Number.isInteger(v.seat) && v.seat >= 0 && typeof v.closed === 'boolean', 'seat must be a non-negative integer and closed a boolean');
60
- const botReplacesHumanBody = field((v) => typeof v === 'object' && v !== null && Number.isInteger(v.seat), 'seat must be an integer');
61
- /** Every one of the leader's three seat ops' own refusal, mapped to a status. `'no-room'`/`no such room` reuses the create/join answer on purpose: never telling a non-member whether a room id exists. */
56
+ /** A JSON object body, never an array. */
57
+ const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
58
+ const isSeat = (v) => Number.isInteger(v) && v >= 0;
59
+ const removeFromSeatBody = field((v) => isObject(v) && typeof v.memberId === 'string', 'memberId must be a string');
60
+ const lowerCapacityBody = field((v) => isObject(v) && Number.isInteger(v.count), 'count must be an integer');
61
+ const sitSeatBody = field((v) => isObject(v) && (v.seat === undefined || isSeat(v.seat)), 'seat must be a non-negative integer, or absent');
62
+ const emptyBody = field((v) => isObject(v) && Object.keys(v).length === 0, 'expected an empty body');
63
+ const closeSeatBody = field((v) => isObject(v) && isSeat(v.seat) && typeof v.closed === 'boolean', 'seat must be a non-negative integer and closed a boolean');
64
+ const botReplacesHumanBody = field((v) => isObject(v) && Number.isInteger(v.seat), 'seat must be an integer');
65
+ /** Every seat op's own refusal, mapped to a status. `'no-room'`/`no such room` reuses the create/join answer on purpose: never telling a non-member whether a room id exists. */
62
66
  function seatOpStatus(res, reason) {
63
67
  if (reason === 'no-room')
64
68
  return noMatch(res);
@@ -88,6 +92,18 @@ function seatOpStatus(res, reason) {
88
92
  res.status(409).json({ error: 'this room has no seat the bot can take' });
89
93
  return;
90
94
  }
95
+ if (reason === 'seat-taken') {
96
+ res.status(409).json({ error: 'that seat is already taken' });
97
+ return;
98
+ }
99
+ if (reason === 'pod-full') {
100
+ res.status(409).json({ error: 'every seat is taken' });
101
+ return;
102
+ }
103
+ if (reason === 'not-open') {
104
+ res.status(409).json({ error: `seats open ${String(SEATS_OPEN_BEFORE_MS / 60_000)} minutes before the event starts` });
105
+ return;
106
+ }
91
107
  res.status(400).json({ error: reason });
92
108
  }
93
109
  /** Every one of the leader's three seat ops, per caller: a burst of 10, then one every 10 s — the same shape as a join attempt, spent only by a leader who is actually a member of the room (checked before this is asked). */
@@ -403,12 +419,16 @@ export function attachRoomCore(api, deps, opts = {}) {
403
419
  });
404
420
  const seats = deps.seatRoutes;
405
421
  if (seats !== undefined) {
406
- // Opted in (`RoomCoreDeps.seatRoutes`): a member sits and stands; the leader closes, opens and takes the AI back out.
422
+ // Opted in (`RoomCoreDeps.seatRoutes`): a member sits and stands; the leader closes, opens, and puts the AI in an open seat or takes it back out.
407
423
  const seatRoute = (path, schema, leaders, run) => {
408
424
  rooms.post(`/:id/seats/${path}`, (req, res) => {
409
- const body = parseBody(schema, req, res);
425
+ // No body at all is `{}`, as it is for every other room route.
426
+ const read = readBody(schema, req.body ?? {});
427
+ if (!read.ok)
428
+ return void res.status(400).json({ error: read.error });
429
+ const body = read.value;
410
430
  // The leader's ops spend the seat-op limit, as `remove`/`capacity`/`bot` do; a member's own sit and stand do not.
411
- if (body === null || (leaders && !seatOpLimit(req, res)))
431
+ if (leaders && !seatOpLimit(req, res))
412
432
  return;
413
433
  const caller = callerOf(req);
414
434
  const id = String(req.params.id);
@@ -423,6 +443,7 @@ export function attachRoomCore(api, deps, opts = {}) {
423
443
  seatRoute('sit', sitSeatBody, false, (id, callerId, body) => seats.sitSeat(id, callerId, body.seat));
424
444
  seatRoute('stand', emptyBody, false, (id, callerId) => seats.standSeat(id, callerId));
425
445
  seatRoute('close', closeSeatBody, true, (id, callerId, body, override) => seats.closeSeat(id, callerId, body.seat, body.closed, override));
446
+ seatRoute('bot-on', botReplacesHumanBody, true, (id, callerId, body, override) => seats.aiSeat(id, callerId, body.seat, true, override));
426
447
  seatRoute('bot-off', botReplacesHumanBody, true, (id, callerId, body, override) => seats.aiSeat(id, callerId, body.seat, false, override));
427
448
  }
428
449
  // The leader hands the room to another member, seated or watching. Spends the seat-op limit.
@@ -128,8 +128,6 @@ export interface RoomTypeHooks<G, I extends RoomCreateInput = RoomCreateInput> {
128
128
  };
129
129
  /** The game part as stored. */
130
130
  encode(room: LobbyRoom<G>): unknown;
131
- /** The stored `app` as written: anything the type writes beside it (a copy an older build reads). Default: as it is. */
132
- writes?(app: StoredRoomApp): object;
133
131
  /**
134
132
  * The stored `app` read back: its game seats and its game part, as stored. Throws to quarantine the room;
135
133
  * the message names the field, never a value. `version` is the app blob's own (`ROOM_APP_VERSION` or older).
@@ -397,7 +395,7 @@ export interface LobbyCoreOptions<I extends RoomCreateInput> {
397
395
  * type. Its errors name the FIELD, never a value: the message reaches the quarantine reason and the
398
396
  * log, and a value here could be the hash.
399
397
  */
400
- export declare function roomAppCodec(typeOf: (gameType: string) => Pick<RoomTypeHooks<unknown>, 'decode' | 'writes'> | undefined): RoomCodec<StoredRoomApp, unknown>;
398
+ export declare function roomAppCodec(typeOf: (gameType: string) => Pick<RoomTypeHooks<unknown>, 'decode'> | undefined): RoomCodec<StoredRoomApp, unknown>;
401
399
  /**
402
400
  * The lobby core. `V` is the view a room of the hosting game reads as (the platform's fields and
403
401
  * the type's part); `I` the create input. A game's lobby extends it with its own operations, which
@@ -422,8 +420,8 @@ export declare class RoomLobby<V extends PlatformRoomView, I extends RoomCreateI
422
420
  /** Tell every `onChange` listener `roomId` reads differently now, with nothing written. */
423
421
  protected changed(roomId: string): void;
424
422
  /**
425
- * Be told every time a room is written, created or forgotten. Every mutation goes through one
426
- * persist, so this is every change a read could see and nothing else: a read (`touch`, `get`,
423
+ * Be told every time a room is written, created or forgotten, or a member's name, avatar or look
424
+ * changes (none of which is written), so this is every change a read could see and nothing else: a read (`touch`, `get`,
427
425
  * `list`) never fires it. Returns the unsubscribe.
428
426
  */
429
427
  onChange(listener: (roomId: string) => void): () => void;
@@ -511,7 +509,8 @@ export declare class RoomLobby<V extends PlatformRoomView, I extends RoomCreateI
511
509
  /**
512
510
  * Call `caller.id` by `caller.name` in every room they are a member of. The name was copied once, at
513
511
  * join, so a key renamed in the registry kept its first name in every room it had joined
514
- * (2026-10-26). Writes only the rooms whose copy moved.
512
+ * (2026-10-26). Tells only the rooms whose copy moved, and writes none: a name is never stored (R12),
513
+ * so a rename is not a change to the room and leaves its age alone.
515
514
  */
516
515
  rename(caller: Pick<Caller, 'id' | 'name'>): void;
517
516
  /** false when there is no such room or the caller is not a member. Persisted. */
@@ -520,7 +519,7 @@ export declare class RoomLobby<V extends PlatformRoomView, I extends RoomCreateI
520
519
  * The member's OWN avatar, and nobody else's: `memberId` is always a derived participant id, so a
521
520
  * browser cannot name a row that is not its own. Three states in, two stored: a non-blank string
522
521
  * sets it; `null`, `''`, whitespace, an over-long value and a non-string clear it (`avatarIdOf`).
523
- * `false` only when there is no such room or member. Written only when it changed.
522
+ * `false` only when there is no such room or member. Never written (R12): told to the room when it changed.
524
523
  */
525
524
  setAvatar(roomId: string, memberId: string, avatarId: string | null): boolean;
526
525
  /** Each member's registry userId, for looking up how they look. Never part of a view. */
@@ -101,7 +101,8 @@ function isSecret(v) {
101
101
  export function roomAppCodec(typeOf) {
102
102
  const app = {
103
103
  version: ROOM_APP_VERSION,
104
- serialize: (value) => structuredClone(typeOf(value.gameType)?.writes?.(value) ?? value),
104
+ // No copy: the store stringifies the snapshot in the call (`OffLoopRoomStore.saveSync`).
105
+ serialize: (value) => value,
105
106
  deserialize(raw, version, ctx) {
106
107
  if (typeof raw !== 'object' || raw === null)
107
108
  throw new Error('app is not an object');
@@ -230,7 +231,7 @@ export class RoomLobby {
230
231
  lastSeen: m.joinedAt,
231
232
  // Boxed, so a state of `null` survives a restart as `null`, and `ready` rides the same
232
233
  // blob. A member with neither writes `null`.
233
- app: m.state === undefined && !ready ? null : { state: structuredClone(m.state), ready },
234
+ app: m.state === undefined && !ready ? null : { state: m.state, ready },
234
235
  };
235
236
  });
236
237
  const app = {
@@ -274,8 +275,8 @@ export class RoomLobby {
274
275
  }
275
276
  }
276
277
  /**
277
- * Be told every time a room is written, created or forgotten. Every mutation goes through one
278
- * persist, so this is every change a read could see and nothing else: a read (`touch`, `get`,
278
+ * Be told every time a room is written, created or forgotten, or a member's name, avatar or look
279
+ * changes (none of which is written), so this is every change a read could see and nothing else: a read (`touch`, `get`,
279
280
  * `list`) never fires it. Returns the unsubscribe.
280
281
  */
281
282
  onChange(listener) {
@@ -581,7 +582,8 @@ export class RoomLobby {
581
582
  /**
582
583
  * Call `caller.id` by `caller.name` in every room they are a member of. The name was copied once, at
583
584
  * join, so a key renamed in the registry kept its first name in every room it had joined
584
- * (2026-10-26). Writes only the rooms whose copy moved.
585
+ * (2026-10-26). Tells only the rooms whose copy moved, and writes none: a name is never stored (R12),
586
+ * so a rename is not a change to the room and leaves its age alone.
585
587
  */
586
588
  rename(caller) {
587
589
  for (const room of this.#rooms.values()) {
@@ -589,7 +591,7 @@ export class RoomLobby {
589
591
  if (member === undefined || member.name === caller.name)
590
592
  continue;
591
593
  member.name = caller.name;
592
- this.persist(room);
594
+ this.changed(room.id);
593
595
  }
594
596
  }
595
597
  /** false when there is no such room or the caller is not a member. Persisted. */
@@ -610,7 +612,7 @@ export class RoomLobby {
610
612
  * The member's OWN avatar, and nobody else's: `memberId` is always a derived participant id, so a
611
613
  * browser cannot name a row that is not its own. Three states in, two stored: a non-blank string
612
614
  * sets it; `null`, `''`, whitespace, an over-long value and a non-string clear it (`avatarIdOf`).
613
- * `false` only when there is no such room or member. Written only when it changed.
615
+ * `false` only when there is no such room or member. Never written (R12): told to the room when it changed.
614
616
  */
615
617
  setAvatar(roomId, memberId, avatarId) {
616
618
  const room = this.#rooms.get(roomId);
@@ -618,7 +620,7 @@ export class RoomLobby {
618
620
  if (room === undefined || member === undefined)
619
621
  return false;
620
622
  const next = avatarIdOf(avatarId);
621
- // Idempotent, and no write when nothing changed: a member's account is written onto the room
623
+ // Idempotent, and nothing told when nothing changed: a member's account is set on the room
622
624
  // on every arrival and every change to it. `true` either way: the room is in the asked state.
623
625
  if (next === member.avatarId)
624
626
  return true;
@@ -626,7 +628,7 @@ export class RoomLobby {
626
628
  delete member.avatarId;
627
629
  else
628
630
  member.avatarId = next;
629
- this.persist(room);
631
+ this.changed(roomId);
630
632
  this.#announceAvatar(roomId, member.id, member.avatarId ?? null);
631
633
  return true;
632
634
  }
@@ -44,7 +44,7 @@
44
44
  * the holder was live or stale.
45
45
  */
46
46
  import type { Server as SocketIOServer, Socket } from 'socket.io';
47
- import { type JsonLog } from '../log.js';
47
+ import { type JsonLog } from '@wolffm/service-kernel/log';
48
48
  /** How often a holding session socket is asked whether it is alive. */
49
49
  export declare const BEAT_MS = 5000;
50
50
  /** A holder that has answered nothing for this long is stale: two missed beats and change. */
@@ -46,7 +46,7 @@
46
46
  import { SessionClaims } from '@wolffm/rooms-server';
47
47
  import { SESSION_EVENTS, parseSessionClaim } from '@wolffm/rooms-protocol';
48
48
  import { identityFromHeaders } from './identity.js';
49
- import { jsonLogger } from '../log.js';
49
+ import { jsonLogger } from '@wolffm/service-kernel/log';
50
50
  import { callerContext, report } from '../telemetry.js';
51
51
  /** How often a holding session socket is asked whether it is alive. */
52
52
  export const BEAT_MS = 5_000;
@@ -13,6 +13,11 @@
13
13
  * - **Presence.** A hidden screen reads nothing, so the watch itself keeps a room offered on the
14
14
  * front page: every watched room is touched for its member when the watch arrives and every
15
15
  * `FEED_TOUCH_MS` after, for as long as the socket stays.
16
+ * - **A room's other keys.** A watch key that is not a room id names one of the room's facets
17
+ * (`roomOf` says which room): its watcher hears every change of the room, and also what is told
18
+ * for that key alone (`changed(key)`), which the room's own watchers never hear. A game's turn is
19
+ * one: the screen that draws it follows that key, and a screen that only needs the room does not
20
+ * read it again on every turn. Membership and presence are the room's.
16
21
  *
17
22
  * No gate of its own: `attachDraftSocket` and `attachPlay` already put the edge's provenance and the
18
23
  * tier floor in front of every handshake on the default namespace (`room/socket.ts`'s header).
@@ -28,6 +33,8 @@ interface RoomFeedLobby {
28
33
  interface RoomFeedDeps {
29
34
  lobby: RoomFeedLobby;
30
35
  log?: (line: Record<string, unknown>) => void;
36
+ /** The room a watch key names (this file's header). Default: every key is a room id. */
37
+ roomOf?: (key: string) => string;
31
38
  /** Injected for tests. */
32
39
  touchEveryMs?: number;
33
40
  }
@@ -13,6 +13,11 @@
13
13
  * - **Presence.** A hidden screen reads nothing, so the watch itself keeps a room offered on the
14
14
  * front page: every watched room is touched for its member when the watch arrives and every
15
15
  * `FEED_TOUCH_MS` after, for as long as the socket stays.
16
+ * - **A room's other keys.** A watch key that is not a room id names one of the room's facets
17
+ * (`roomOf` says which room): its watcher hears every change of the room, and also what is told
18
+ * for that key alone (`changed(key)`), which the room's own watchers never hear. A game's turn is
19
+ * one: the screen that draws it follows that key, and a screen that only needs the room does not
20
+ * read it again on every turn. Membership and presence are the room's.
16
21
  *
17
22
  * No gate of its own: `attachDraftSocket` and `attachPlay` already put the edge's provenance and the
18
23
  * tier floor in front of every handshake on the default namespace (`room/socket.ts`'s header).
@@ -27,11 +32,27 @@ const FEED_TOUCH_MS = ROOM_STALE_MS / 6;
27
32
  const participantOf = (socket) => identityFromHeaders(socket.request.headers, socket.id);
28
33
  export function attachRoomFeed(io, deps) {
29
34
  const { lobby } = deps;
30
- /** Each watching socket's participant and rooms, for the touch. */
35
+ const roomOf = deps.roomOf ?? ((key) => key);
36
+ /** Each watching socket's participant and keys, for the touch and a room's other keys. */
31
37
  const watching = new Map();
32
38
  const touch = ({ participantId, rooms }) => {
33
- for (const room of rooms)
34
- lobby.touch(room, participantId);
39
+ for (const key of rooms)
40
+ lobby.touch(roomOf(key), participantId);
41
+ };
42
+ /** Every watched key of `roomId`'s other than its id. */
43
+ const otherKeysOf = (roomId) => {
44
+ const keys = new Set();
45
+ for (const { rooms } of watching.values())
46
+ for (const key of rooms)
47
+ if (key !== roomId && roomOf(key) === roomId)
48
+ keys.add(key);
49
+ return keys;
50
+ };
51
+ /** `roomId`'s change, said to its own key and every other key of it. */
52
+ const withOtherKeys = (say) => (roomId) => {
53
+ say(roomId);
54
+ for (const key of otherKeysOf(roomId))
55
+ say(key);
35
56
  };
36
57
  const touching = setInterval(() => {
37
58
  for (const watcher of watching.values())
@@ -56,13 +77,14 @@ export function attachRoomFeed(io, deps) {
56
77
  });
57
78
  const feed = attachRoomBookFeed(io, {
58
79
  wire: { channel: ROOM_FEED_CHANNEL, parseWatch: parseRoomWatch },
59
- onChange: (listener) => lobby.onChange(listener),
60
- isMember: (room, participantId) => lobby.isMember(room, participantId),
80
+ onChange: (listener) => lobby.onChange(withOtherKeys(listener)),
81
+ isMember: (key, participantId) => lobby.isMember(roomOf(key), participantId),
61
82
  participantId: participantOf,
62
83
  log: deps.log ?? jsonLogger('cardtable-feed'),
63
84
  });
85
+ const changedRoom = withOtherKeys(feed.changed);
64
86
  return {
65
- changed: feed.changed,
87
+ changed: (key) => (roomOf(key) === key ? changedRoom(key) : feed.changed(key)),
66
88
  close() {
67
89
  feed.close();
68
90
  clearInterval(touching);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.179",
4
- "gitHead": "989dce1f6edefb23bc21cdde27935b9e8ef9d35a",
3
+ "version": "0.1.180",
4
+ "gitHead": "08b6dd41a5babb752b2291cdfbc1566758032125",
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.179",
21
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.179",
22
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.212",
20
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.180",
21
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.180",
22
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.213",
23
23
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.2",
24
24
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.145",
25
25
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.145",