@hadoku/game-server 0.1.79 → 0.1.81

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.
@@ -203,6 +203,8 @@ export interface RoomCoreDeps<V extends RoomViewCore, C, P extends object, G = n
203
203
  forfeited?: (roomId: string, tableId: string, loser: string, matches: readonly string[]) => void;
204
204
  /** Whether a member has a screen open (`undefined`), or since when they have not (ms, or `null` when not seen since boot). Absent: no `away` is sent. */
205
205
  away?: (memberId: string) => number | null | undefined;
206
+ /** Who has a live room connection to `roomId` (`room/serve.ts`'s `here`). Absent, or a room watchparty draws (its roster is watchparty's): no `here` is sent. */
207
+ here?: (roomId: string) => ReadonlySet<string>;
206
208
  /** "Reset = Close": runs AFTER `closed`, same as `index.ts`'s own `seedTestingRooms` boot call. */
207
209
  reseedTesting?: () => void;
208
210
  arrived?: (roomId: string, caller: CallerLike) => void;
@@ -226,6 +228,8 @@ export type RoomBody<V extends RoomViewCore, G = never> = V & {
226
228
  tableGames?: Record<string, G>;
227
229
  /** The members with no screen open now, each with when they were last seen (ISO), or `null` when this server has not seen them since it started. */
228
230
  away?: Record<string, string | null>;
231
+ /** The members in the room now: those with a screen open on it, its lobby or any game of it. The spectator row lists only these. Absent where this server cannot say: every member is listed. */
232
+ here?: string[];
229
233
  };
230
234
  export declare function badRequest(res: Response, error: string): void;
231
235
  export declare function noMatch(res: Response): void;
@@ -25,7 +25,7 @@ import { MultiRateLimiter } from '@wolffm/rooms-server';
25
25
  import { isDurableParticipantId } from '@wolffm/rooms-protocol';
26
26
  import { isAdmin } from '../edge-auth.js';
27
27
  import { bodyErrors, callerOf, parseBody, perCallerLimit } from './request.js';
28
- import { watchpartyRoomId } from '../room/watchparty-link.js';
28
+ import { isWatchpartyRoomId, watchpartyRoomId } from '../room/watchparty-link.js';
29
29
  import { jsonLogger } from '../log.js';
30
30
  /** A `BodySchema<T>` with no dependency the way `RoomCoreWire`'s injected ones have — this file imports no zod, no `@hadoku/shared`. */
31
31
  function field(check, message) {
@@ -137,7 +137,15 @@ export function attachRoomCore(api, deps, opts = {}) {
137
137
  const mine = room.members.find((m) => m.id === callerId);
138
138
  const games = deps.tableGames?.(room);
139
139
  const away = deps.away === undefined ? undefined : awayOf(room.members, deps.away);
140
- return { ...room, you: { id: callerId, leader: room.leader === callerId, ready: mine?.ready === true }, ...(games === undefined ? {} : { tableGames: games }), ...(away === undefined ? {} : { away }) };
140
+ const inRoom = isWatchpartyRoomId(room.id) ? undefined : deps.here?.(room.id);
141
+ const here = inRoom === undefined ? undefined : room.members.filter((m) => inRoom.has(m.id)).map((m) => m.id);
142
+ return {
143
+ ...room,
144
+ you: { id: callerId, leader: room.leader === callerId, ready: mine?.ready === true },
145
+ ...(games === undefined ? {} : { tableGames: games }),
146
+ ...(away === undefined ? {} : { away }),
147
+ ...(here === undefined ? {} : { here }),
148
+ };
141
149
  };
142
150
  const rooms = Router();
143
151
  rooms.param('id', hideTestingRoomsParam(lobby));
@@ -81,6 +81,11 @@ export interface ShellSeat {
81
81
  * 2026-10-19). A seat nobody has joined yet is not away — it is starting.
82
82
  */
83
83
  away: boolean;
84
+ /**
85
+ * The person on this seat has been at this game: a connection of theirs reached it, once. Never
86
+ * cleared. A seat is held from the match's making (`claim`), so holding one says nothing of this.
87
+ */
88
+ entered: boolean;
84
89
  }
85
90
  /** What the shell is handed: the log, and the graces and hooks of a seat's presence, a restart and an ending. */
86
91
  export interface ShellDeps {
@@ -240,6 +245,8 @@ export declare abstract class MatchShell<Line extends {
240
245
  * {@link checkWaitingOn}, and every bar {@link send} puts on the wire.
241
246
  */
242
247
  syncClock(): void;
248
+ /** A seat whose clock a game's own rule holds still while it is waited on. None, unless a game says so. */
249
+ protected clockHeld(_seat: number): boolean;
243
250
  /**
244
251
  * Time the next flag: a timer for the running seat with the least time left. When it fires with time
245
252
  * still on that seat's clock, it is armed again for what is left. Node fires a timer by the event
@@ -288,6 +295,8 @@ export declare abstract class MatchShell<Line extends {
288
295
  * it over, and nobody's game is interrupted (`busy.ts`).
289
296
  */
290
297
  anyoneHere(): boolean;
298
+ /** By seat: whether its person has been at this game (an AI seat has, always). */
299
+ entered(): boolean[];
291
300
  /** A connection for this seat is back: whatever `seatDisconnected` armed is off. */
292
301
  seatConnected(seat: number): void;
293
302
  /**
@@ -171,12 +171,17 @@ export class MatchShell {
171
171
  return;
172
172
  const now = this.now();
173
173
  const running = this.status === 'running' && this.timedOut === null;
174
- const moved = this.clock.set([running && this.game.waitedOn(0), running && this.game.waitedOn(1)], now);
174
+ const runs = (seat) => running && this.game.waitedOn(seat) && !this.clockHeld(seat);
175
+ const moved = this.clock.set([runs(0), runs(1)], now);
175
176
  if (!moved)
176
177
  return;
177
178
  this.armFlag(now);
178
179
  this.game.tellRoster();
179
180
  }
181
+ /** A seat whose clock a game's own rule holds still while it is waited on. None, unless a game says so. */
182
+ clockHeld(_seat) {
183
+ return false;
184
+ }
180
185
  /**
181
186
  * Time the next flag: a timer for the running seat with the least time left. When it fires with time
182
187
  * still on that seat's clock, it is armed again for what is left. Node fires a timer by the event
@@ -350,12 +355,17 @@ export class MatchShell {
350
355
  anyoneHere() {
351
356
  return this.seats.some((s) => s.here);
352
357
  }
358
+ /** By seat: whether its person has been at this game (an AI seat has, always). */
359
+ entered() {
360
+ return this.seats.map((s) => s.ai || s.entered);
361
+ }
353
362
  /** A connection for this seat is back: whatever `seatDisconnected` armed is off. */
354
363
  seatConnected(seat) {
355
364
  const s = this.seats[seat];
356
365
  if (s === undefined)
357
366
  return;
358
367
  s.here = true;
368
+ s.entered = true;
359
369
  if (s.away) {
360
370
  s.away = false;
361
371
  this.game.tellRoster();
@@ -66,7 +66,7 @@ export class RelayMatch extends HostedMatch {
66
66
  hostSeatRef = null;
67
67
  constructor(id, seatCount, deps, restore) {
68
68
  super(id, relaySpec(seatCount), deps, restore);
69
- this.seats = Array.from({ length: seatCount }, (_, seat) => ({ seat, ai: false, owner: null, playerId: seat, departure: null, here: false, away: false }));
69
+ this.seats = Array.from({ length: seatCount }, (_, seat) => ({ seat, ai: false, owner: null, playerId: seat, departure: null, here: false, away: false, entered: false }));
70
70
  for (const [seat, owner] of (restore?.owners ?? []).entries()) {
71
71
  const s = this.seats[seat];
72
72
  if (s !== undefined && owner !== null)
@@ -161,6 +161,8 @@ export async function startRelayGame(options) {
161
161
  // The room feed, from the `sockets` phase on: a pending start is no lobby write, so its route
162
162
  // pings the room's watchers itself, or every page but the starter's missed the countdown.
163
163
  let feedChanged = () => { };
164
+ // The chat's roster by room, once the room socket is up (`socketsPlugin`).
165
+ let roomHere = () => new Set();
164
166
  const roomsPlugin = {
165
167
  name: 'rooms',
166
168
  phase: 'routes',
@@ -170,6 +172,7 @@ export async function startRelayGame(options) {
170
172
  lobby,
171
173
  wire: relayWire(options.gameType, options.roomIdRe),
172
174
  seatRoutes: lobby,
175
+ here: (roomId) => roomHere(roomId),
173
176
  arrived: (roomId, caller) => {
174
177
  if (caller.userId !== null)
175
178
  arrived(roomId, caller.id, caller.userId);
@@ -260,9 +263,10 @@ export async function startRelayGame(options) {
260
263
  // The room's own presence: its grace is not the relay seat's hold.
261
264
  const presence = new PresenceTracker({ idleMs: 2_000_000_000, graceMs: disconnectGraceMs });
262
265
  const roomLog = jsonLogger(`${id}-room`);
263
- await attachServedRoom(ctx.io, {
266
+ const served = await attachServedRoom(ctx.io, {
264
267
  lobby,
265
268
  presence,
269
+ onRoster: (roomId) => feedChanged(roomId),
266
270
  people: ctx.state.people,
267
271
  isMatch: (matchId) => relayMatches.get(matchId) !== undefined,
268
272
  minTier: config.minTier,
@@ -286,6 +290,7 @@ export async function startRelayGame(options) {
286
290
  });
287
291
  const feed = attachRoomFeed(ctx.io, { lobby, log: jsonLogger(`${id}-feed`) });
288
292
  feedChanged = (roomId) => feed.changed(roomId);
293
+ roomHere = served.here;
289
294
  return {
290
295
  shutdown: () => {
291
296
  relay.close();
@@ -41,6 +41,14 @@
41
41
  * what screens built before 2026-11-11 joined during a game, and is gated as its room is. A
42
42
  * refusal sends nothing of the room.
43
43
  *
44
+ * ## Who is in a room now
45
+ *
46
+ * `here(roomId)` is the chat's own roster by participant id: everyone with a live room connection to
47
+ * the room, at its lobby, a table of it or a game at one. A member with no screen on the room is a
48
+ * member still (their seat, their sign-up, their place in a locked room) and is not in it. The
49
+ * spectator row reads this (`http/room-core.ts`'s `here`), so it names who is watching, never who
50
+ * once visited (the operator, 2026-11-19). `onRoster` says a room's answer changed.
51
+ *
44
52
  * `appId` must agree with the UI's own app id; it is passed in rather than imported, since a
45
53
  * server importing UI source is the wrong direction for any app's layering.
46
54
  */
@@ -120,7 +128,14 @@ export interface ServeDeps<V extends ServeRoomView> {
120
128
  /** Default `/room`. */
121
129
  namespace?: string;
122
130
  onEvent?: (level: 'info' | 'warn' | 'debug', event: string, fields: Record<string, unknown>) => void;
131
+ /** A lobby room's `here` changed: someone's room connection opened or closed. */
132
+ onRoster?: (roomId: string) => void;
133
+ }
134
+ /** The served rooms, and who is in each lobby room now. */
135
+ export interface ServedRooms extends ServedRoom<string, RoomChatMessage> {
136
+ /** The participants with a live room connection to `roomId` (see this file's header). */
137
+ here(roomId: string): ReadonlySet<string>;
123
138
  }
124
139
  /** Mount a game's rooms on `io` (see this file's header). */
125
- export declare function attachServedRoom<V extends ServeRoomView>(io: SocketIOServer, deps: ServeDeps<V>): Promise<ServedRoom<string, RoomChatMessage>>;
140
+ export declare function attachServedRoom<V extends ServeRoomView>(io: SocketIOServer, deps: ServeDeps<V>): Promise<ServedRooms>;
126
141
  export {};
@@ -41,6 +41,14 @@
41
41
  * what screens built before 2026-11-11 joined during a game, and is gated as its room is. A
42
42
  * refusal sends nothing of the room.
43
43
  *
44
+ * ## Who is in a room now
45
+ *
46
+ * `here(roomId)` is the chat's own roster by participant id: everyone with a live room connection to
47
+ * the room, at its lobby, a table of it or a game at one. A member with no screen on the room is a
48
+ * member still (their seat, their sign-up, their place in a locked room) and is not in it. The
49
+ * spectator row reads this (`http/room-core.ts`'s `here`), so it names who is watching, never who
50
+ * once visited (the operator, 2026-11-19). `onRoster` says a room's answer changed.
51
+ *
44
52
  * `appId` must agree with the UI's own app id; it is passed in rather than imported, since a
45
53
  * server importing UI source is the wrong direction for any app's layering.
46
54
  */
@@ -121,6 +129,10 @@ function gateRoomHandshake(namespace, minTier, onEvent) {
121
129
  /** Mount a game's rooms on `io` (see this file's header). */
122
130
  export async function attachServedRoom(io, deps) {
123
131
  const { lobby, people } = deps;
132
+ // Each admitted socket's lobby room (a table's and a game's key name their room's), and each
133
+ // lobby room's joined connections by socket id.
134
+ const lobbyRoomOf = new WeakMap();
135
+ const joined = new Map();
124
136
  gateRoomHandshake(io.of(deps.namespace ?? '/room'), deps.minTier, deps.onEvent);
125
137
  const store = createChatHistoryStore({
126
138
  stateDir: deps.stateDir,
@@ -157,6 +169,8 @@ export async function attachServedRoom(io, deps) {
157
169
  if (!mayJoin(lobby, place, participantId))
158
170
  return refuse('not-member');
159
171
  const roomId = place.roomId;
172
+ if (roomId !== null)
173
+ lobbyRoomOf.set(socket, roomId);
160
174
  const accountId = accountOfHeaders(headers);
161
175
  return {
162
176
  roomId: place.served,
@@ -174,6 +188,25 @@ export async function attachServedRoom(io, deps) {
174
188
  },
175
189
  };
176
190
  },
191
+ onJoined(socket, admission) {
192
+ const roomId = lobbyRoomOf.get(socket);
193
+ if (roomId === undefined)
194
+ return;
195
+ let sockets = joined.get(roomId);
196
+ if (sockets === undefined)
197
+ joined.set(roomId, (sockets = new Map()));
198
+ sockets.set(socket.id, admission.key);
199
+ deps.onRoster?.(roomId);
200
+ },
201
+ onLeave(socket) {
202
+ const roomId = lobbyRoomOf.get(socket);
203
+ const sockets = roomId === undefined ? undefined : joined.get(roomId);
204
+ if (roomId === undefined || sockets === undefined || !sockets.delete(socket.id))
205
+ return;
206
+ if (sockets.size === 0)
207
+ joined.delete(roomId);
208
+ deps.onRoster?.(roomId);
209
+ },
177
210
  // Always read fresh. serveRoom asks without `fresh` only for a person it holds no copy of (their
178
211
  // first join or line since this server started), and people's cache may predate a change they
179
212
  // made while their socket was down: a restart's resumed match fills it, and the chrome's
@@ -197,5 +230,14 @@ export async function attachServedRoom(io, deps) {
197
230
  if (lobby.get(roomId) === undefined)
198
231
  void store.clearRoomChat(roomId);
199
232
  });
200
- return served;
233
+ return Object.assign(served, {
234
+ here(roomId) {
235
+ const here = new Set();
236
+ // Live on the tracker too, as the chat's roster reads it: a connection gone silent is not here.
237
+ for (const [socketId, key] of joined.get(roomId) ?? [])
238
+ if (deps.presence.keyOf(socketId) === key)
239
+ here.add(key);
240
+ return here;
241
+ },
242
+ });
201
243
  }
@@ -1 +1,7 @@
1
1
  export declare function watchpartyRoomId(watchpartyRoom: string): string;
2
+ /**
3
+ * Whether `roomId` is one a watchparty room's game slot is bound to. Such a room is drawn inside
4
+ * watchparty's own page, whose chat and roster are watchparty's: nobody at it holds a connection to
5
+ * this server's room socket, so this server cannot say who is in it now.
6
+ */
7
+ export declare function isWatchpartyRoomId(roomId: string): boolean;
@@ -15,3 +15,11 @@ import { createHash } from 'node:crypto';
15
15
  export function watchpartyRoomId(watchpartyRoom) {
16
16
  return `wp-${createHash('sha256').update(watchpartyRoom).digest('hex').slice(0, 20)}`;
17
17
  }
18
+ /**
19
+ * Whether `roomId` is one a watchparty room's game slot is bound to. Such a room is drawn inside
20
+ * watchparty's own page, whose chat and roster are watchparty's: nobody at it holds a connection to
21
+ * this server's room socket, so this server cannot say who is in it now.
22
+ */
23
+ export function isWatchpartyRoomId(roomId) {
24
+ return roomId.startsWith('wp-');
25
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.79",
4
- "gitHead": "79f22a0216157212e7816c595b3dea1d23e98d5f",
3
+ "version": "0.1.81",
4
+ "gitHead": "0639f9cc1c84111638f55d2fe7b7a5903d8ba560",
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.",
@@ -16,9 +16,9 @@
16
16
  "play/remote/"
17
17
  ],
18
18
  "dependencies": {
19
- "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.79",
20
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.79",
21
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.112",
19
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.81",
20
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.81",
21
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.114",
22
22
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.1.31",
23
23
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.124",
24
24
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.124",