@hadoku/game-server 0.1.253 → 0.1.255

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.
@@ -285,6 +285,18 @@ export type RoomBody<V extends RoomViewCore, G = never> = V & {
285
285
  };
286
286
  /** A route's refusal: `status` with a body of exactly `{ error }`. A handler ends on it: `if (…) return refuse(res, 409, '…');`. */
287
287
  export declare function refuse(res: Response, status: number, error: string): void;
288
+ /** A route's refusals by the lobby's reason: a status and the words, or no words to say the reason itself; `'*'` answers any reason not named. */
289
+ export type Refusals = {
290
+ readonly '*': readonly [number, string?];
291
+ readonly [reason: string]: readonly [number, string?];
292
+ };
293
+ /**
294
+ * A route's refusal answerer from its table, ending the request with `{ error }` (and `reason`, when `sayReason`).
295
+ * A room that does not exist, or that the caller or the member they name is not in, is {@link NO_MATCH} whatever the table says.
296
+ */
297
+ export declare function refusalsOf(table: Refusals, opts?: {
298
+ sayReason?: boolean;
299
+ }): (res: Response, reason: string) => void;
288
300
  export declare function badRequest(res: Response, error: string): void;
289
301
  export declare function noMatch(res: Response): void;
290
302
  export declare function notDurable(res: Response, verb?: string): void;
@@ -62,32 +62,21 @@ const sitSeatBody = field((v) => isObject(v) && (v.seat === undefined || isSeat(
62
62
  const emptyBody = field((v) => isObject(v) && Object.keys(v).length === 0, 'expected an empty body');
63
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
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. */
66
- function seatOpStatus(res, reason) {
67
- if (reason === 'no-room')
68
- return noMatch(res);
69
- if (reason === 'not-member' || reason === 'target-not-member')
70
- return noMatch(res);
71
- if (reason === 'not-leader')
72
- return refuse(res, 403, 'only the room leader can do that');
73
- if (reason === 'not-draft' || reason === 'not-lobby')
74
- return refuse(res, 409, 'this room does not seat through game seats, or its seats are fixed right now');
75
- if (reason === 'historical')
76
- return refuse(res, 409, 'a historical room’s field is the event’s, not the leader’s to change');
77
- if (reason === 'too-few')
78
- return refuse(res, 400, `a room always keeps at least ${String(MIN_OPEN_SEATS)} seats open`);
79
- if (reason === 'bad-seat')
80
- return refuse(res, 400, 'not a seat this room has');
81
- if (reason === 'no-bot-seat')
82
- return refuse(res, 409, 'this room has no seat the bot can take');
83
- if (reason === 'seat-taken')
84
- return refuse(res, 409, 'that seat is already taken');
85
- if (reason === 'pod-full')
86
- return refuse(res, 409, 'every seat is taken');
87
- if (reason === 'not-open')
88
- return refuse(res, 409, `seats open ${String(SEATS_OPEN_BEFORE_MS / 60_000)} minutes before the event starts`);
89
- res.status(400).json({ error: reason });
90
- }
65
+ /** Every seat op's own refusal, by the lobby's reason; a reason not named is a 400 that says it. */
66
+ const seatOpStatus = refusalsOf({
67
+ 'not-leader': [403, 'only the room leader can do that'],
68
+ 'not-draft': [409, 'this room does not seat through game seats, or its seats are fixed right now'],
69
+ 'not-lobby': [409, 'this room does not seat through game seats, or its seats are fixed right now'],
70
+ historical: [409, 'a historical room’s field is the event’s, not the leader’s to change'],
71
+ 'too-few': [400, `a room always keeps at least ${String(MIN_OPEN_SEATS)} seats open`],
72
+ 'bad-seat': [400, 'not a seat this room has'],
73
+ 'no-bot-seat': [409, 'this room has no seat the bot can take'],
74
+ 'seat-taken': [409, 'that seat is already taken'],
75
+ 'pod-full': [409, 'every seat is taken'],
76
+ 'not-open': [409, `seats open ${String(SEATS_OPEN_BEFORE_MS / 60_000)} minutes before the event starts`],
77
+ '*': [400],
78
+ });
79
+ const closeRefused = refusalsOf({ '*': [403, 'only the room owner can close it'] });
91
80
  /**
92
81
  * The leader's ops that act on another person or on the room's size, per caller: `seats/remove`,
93
82
  * `seats/capacity`, `seats/bot` (the AI replaces a seated human) and `leader`. A burst of 10, then
@@ -128,6 +117,18 @@ function awayOf(members, seen) {
128
117
  export function refuse(res, status, error) {
129
118
  res.status(status).json({ error });
130
119
  }
120
+ /**
121
+ * A route's refusal answerer from its table, ending the request with `{ error }` (and `reason`, when `sayReason`).
122
+ * A room that does not exist, or that the caller or the member they name is not in, is {@link NO_MATCH} whatever the table says.
123
+ */
124
+ export function refusalsOf(table, opts = {}) {
125
+ return (res, reason) => {
126
+ if (reason === 'no-room' || reason === 'not-member' || reason === 'target-not-member')
127
+ return noMatch(res);
128
+ const [status, error = reason] = table[reason] ?? table['*'];
129
+ res.status(status).json(opts.sayReason === true ? { error, reason } : { error });
130
+ };
131
+ }
131
132
  export function badRequest(res, error) {
132
133
  refuse(res, 400, error);
133
134
  }
@@ -337,11 +338,8 @@ export function attachRoomCore(api, deps, opts = {}) {
337
338
  return noMatch(res);
338
339
  const wasTesting = lobby.get(id)?.testing === true;
339
340
  const result = lobby.close(id, caller.id);
340
- if (!result.ok) {
341
- if (result.reason === 'no-room')
342
- return noMatch(res);
343
- return res.status(403).json({ error: 'only the room owner can close it' });
344
- }
341
+ if (!result.ok)
342
+ return closeRefused(res, result.reason);
345
343
  deps.closed?.(id, result.matches);
346
344
  if (wasTesting)
347
345
  deps.reseedTesting?.();
@@ -28,6 +28,7 @@
28
28
  import { existsSync, mkdirSync } from 'node:fs';
29
29
  import { join } from 'node:path';
30
30
  import express from 'express';
31
+ import { z } from 'zod';
31
32
  import { PresenceTracker } from '@wolffm/rooms-server';
32
33
  import { OffLoopRoomStore } from './room/off-loop-store.js';
33
34
  import { jsonLogger } from '@wolffm/service-kernel/log';
@@ -45,6 +46,7 @@ import { leaderSeatOf, wireRelayRooms } from './play/relay-rooms.js';
45
46
  import { relayMatches } from './play/runtimes/relay-match.js';
46
47
  import { configureParticipantIdDomain, identityFromHeaders } from './room/identity.js';
47
48
  import { LIVE } from '@wolffm/platform-wire/room-settings';
49
+ import { createRoomBodyFor, EmptyBodySchema, JoinRoomBodySchema, ReadyBodySchema, roomBody, WatchpartyBindBodySchema } from '@wolffm/platform-wire/room-requests';
48
50
  import { RoomLobby } from './room/lobby.js';
49
51
  import { MATCH_LIVE, plainRoomType } from './room/room-type.js';
50
52
  import { attachServedRoom } from './room/serve.js';
@@ -348,29 +350,17 @@ export async function startRelayGame(options) {
348
350
  });
349
351
  return server;
350
352
  }
351
- /** The room routes' request bodies, for a game with one room type and, maybe, settings. */
353
+ const GAME_TYPE_PATCH = 'gameType must be 1–40 characters';
354
+ /** The room routes' request bodies (every game's, `@hadoku/platform-wire/room-requests`), for a game with one room type and, maybe, settings. */
352
355
  function relayWire(gameType, roomIdRe, settings) {
353
- const object = (fields, required, message) => ({
354
- safeParse(body) {
355
- const ok = typeof body === 'object' &&
356
- body !== null &&
357
- !Array.isArray(body) &&
358
- Object.keys(body).every((k) => k in fields) &&
359
- required.every((k) => k in body) &&
360
- Object.entries(body).every(([k, v]) => fields[k](v));
361
- return ok ? { success: true, data: body } : refusal(message);
362
- },
363
- });
364
- const text = (max, min = 0) => (v) => typeof v === 'string' && v.trim().length >= min && v.length <= max;
365
- const optional = (check) => (v) => v === undefined || check(v);
366
356
  return {
367
357
  roomIdRe,
368
- createRoomBody: object({ name: text(60, 1), gameType: (v) => v === gameType, password: optional(text(100)) }, ['name', 'gameType'], 'expected { name, gameType, password? }'),
369
- joinRoomBody: object({ password: optional(text(100)) }, [], 'expected { password? }'),
370
- readyBody: object({ ready: (v) => typeof v === 'boolean' }, ['ready'], 'expected { ready }'),
371
- emptyBody: object({}, [], 'expected an empty body'),
372
- watchpartyBindBody: object({ room: text(100, 1) }, ['room'], 'expected { room }'),
373
- patchRoomBody: patchBody(object({ gameType: text(40, 1) }, ['gameType'], 'expected { gameType }'), settings),
358
+ createRoomBody: createRoomBodyFor(gameType),
359
+ joinRoomBody: JoinRoomBodySchema,
360
+ readyBody: ReadyBodySchema,
361
+ emptyBody: EmptyBodySchema,
362
+ watchpartyBindBody: WatchpartyBindBodySchema,
363
+ patchRoomBody: patchBody(roomBody({ gameType: z.string({ error: GAME_TYPE_PATCH }).refine((v) => v.trim() !== '' && v.length <= 40, { error: GAME_TYPE_PATCH }) }), settings),
374
364
  };
375
365
  }
376
366
  /** `PATCH /:id`: `{ gameType }`, or, for a game that declares settings, `{ settings }` with the spec's own refusals. */
@@ -34,6 +34,8 @@
34
34
  */
35
35
  import { FileRoomStore, type RoomCodec } from '@wolffm/rooms-server/store';
36
36
  import { type Look } from '@wolffm/platform-wire/look';
37
+ import type { PlatformRoomViewSchema, RoomMemberSchema } from '@wolffm/platform-wire/room-view';
38
+ import type { z } from 'zod';
37
39
  import { type IdMove } from '../erase/anonymise.js';
38
40
  import type { Caller } from './caller.js';
39
41
  import { type Secret } from './provisioning.js';
@@ -68,32 +70,9 @@ export interface RoomCreateInput {
68
70
  minTier?: string;
69
71
  }
70
72
  /** A member as every room's view names them. */
71
- export interface LobbyMemberView {
72
- id: string;
73
- name: string;
74
- joinedAt: string;
75
- ready: boolean;
76
- avatarId?: string;
77
- look?: Look;
78
- }
79
- /** A room's view as every game's room has it (`@hadoku/shared`'s `roomBaseFields`); the game type's part is spread over it. */
80
- export interface PlatformRoomView {
81
- id: string;
82
- name: string;
83
- creator: string;
84
- leader: string;
85
- gameType: string;
86
- /** Whether a password is set: never the password, never any hash of it. */
87
- locked: boolean;
88
- members: LobbyMemberView[];
89
- /** Who holds each game seat, by seat number; a member in none is watching. */
90
- seats: (string | null)[];
91
- createdAt: string;
92
- testing: boolean;
93
- scheduledFor: string | null;
94
- /** The room's own tier floor (`tier-floor.ts`); absent for a room anyone at the app's floor may see. */
95
- minTier?: string;
96
- }
73
+ export type LobbyMemberView = z.output<typeof RoomMemberSchema>;
74
+ /** A room's view as every game's room has it (`@hadoku/platform-wire/room-view`); the game type's part is spread over it. */
75
+ export type PlatformRoomView = z.output<typeof PlatformRoomViewSchema>;
97
76
  /** The room-level `app` blob as the store keeps it: the platform's fields, the game seats, and the game's own part under `game`. */
98
77
  export interface StoredRoomApp {
99
78
  name: string;
@@ -8,10 +8,10 @@
8
8
  *
9
9
  * Split out of `room/lobby.ts`'s `join`/`leave`/`setReady`/`viewOf`/`#persist`.
10
10
  * These wrap `@wolffm/rooms-server`'s `MembershipLedger` functions, which
11
- * do the bookkeeping (who is in, readiness). The lead is permanent (the
12
- * operator, 2026-11-29): whoever leads a room keeps it until they pass it
13
- * (`passRoomLeader`). Leaving never hands it on, not even out of an emptied
14
- * room, so the ledger's own succession on a leave is undone here.
11
+ * do the bookkeeping (who is in, readiness, who leads). The lead is permanent
12
+ * (the operator, 2026-11-29): whoever leads a room keeps it until they pass it
13
+ * (`passRoomLeader`, the ledger's `passLead`). Leaving never hands it on, not
14
+ * even out of an emptied room: the ledger's `memberLeft` keeps it.
15
15
  *
16
16
  * Every mutator mutates in place and returns nothing but what the caller
17
17
  * cannot otherwise observe — the same discipline `lobby.ts`'s own mutators
@@ -8,10 +8,10 @@
8
8
  *
9
9
  * Split out of `room/lobby.ts`'s `join`/`leave`/`setReady`/`viewOf`/`#persist`.
10
10
  * These wrap `@wolffm/rooms-server`'s `MembershipLedger` functions, which
11
- * do the bookkeeping (who is in, readiness). The lead is permanent (the
12
- * operator, 2026-11-29): whoever leads a room keeps it until they pass it
13
- * (`passRoomLeader`). Leaving never hands it on, not even out of an emptied
14
- * room, so the ledger's own succession on a leave is undone here.
11
+ * do the bookkeeping (who is in, readiness, who leads). The lead is permanent
12
+ * (the operator, 2026-11-29): whoever leads a room keeps it until they pass it
13
+ * (`passRoomLeader`, the ledger's `passLead`). Leaving never hands it on, not
14
+ * even out of an emptied room: the ledger's `memberLeft` keeps it.
15
15
  *
16
16
  * Every mutator mutates in place and returns nothing but what the caller
17
17
  * cannot otherwise observe — the same discipline `lobby.ts`'s own mutators
@@ -19,7 +19,7 @@
19
19
  * member's GAME state (a pod seat, a table, a series) — this file only ever
20
20
  * touches `members`, `leader` and `ledger`.
21
21
  */
22
- import { addMember, earliestMember, memberLeft, isReady, setReady as ledgerSetReady } from '@wolffm/rooms-server';
22
+ import { addMember, earliestMember, memberLeft, isReady, passLead, setReady as ledgerSetReady } from '@wolffm/rooms-server';
23
23
  import { passwordMatches } from "./provisioning.js";
24
24
  /** Whether `id` already holds a seat at the table, so to speak — already a member of the room. */
25
25
  export function isRoomMember(room, id) {
@@ -57,9 +57,7 @@ export function removeRoomMember(room, id, handOn = false) {
57
57
  room.members.splice(index, 1);
58
58
  const leaderLeft = room.ledger.leaderId === id;
59
59
  memberLeft(room.ledger, id);
60
- if (!handOn)
61
- room.ledger.leaderId = room.leader;
62
- else if (leaderLeft) {
60
+ if (handOn && leaderLeft) {
63
61
  room.ledger.leaderId = earliestMember(room.ledger);
64
62
  room.leader = room.ledger.leaderId ?? room.leader;
65
63
  }
@@ -70,9 +68,8 @@ export function removeRoomMember(room, id, handOn = false) {
70
68
  * member. The caller has checked the hand-off is the current leader's to make, and persists the room.
71
69
  */
72
70
  export function passRoomLeader(room, to) {
73
- if (!isRoomMember(room, to))
71
+ if (!passLead(room.ledger, to))
74
72
  return false;
75
- room.ledger.leaderId = to;
76
73
  room.leader = to;
77
74
  return true;
78
75
  }
@@ -32,7 +32,7 @@ export interface PlatformRoomRecord<G, M extends PlatformMember = PlatformMember
32
32
  * can name that service's account here, and the lead moves by `passLeader` without it. Read `leader`.
33
33
  */
34
34
  creator: string;
35
- /** Mirrors `ledger.leaderId`, kept non-null; it moves only when the leader hands it on (`membership.ts`), never on a leave. */
35
+ /** Mirrors `ledger.leaderId`, kept non-null. It moves when the leader hands it on (`passRoomLeader`), and on a leave only in a `handOn` room no person owns (`membership.ts`). */
36
36
  leader: string;
37
37
  gameType: string;
38
38
  /** A seeded, admin-only room: never swept (`room-list.ts`). */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.253",
4
- "gitHead": "e62618447255d421cc093fbea9b448f9d87b1e17",
3
+ "version": "0.1.255",
4
+ "gitHead": "bd56441796a344d86f4ec3864896652db6ef01b5",
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.253",
20
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.253",
21
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.286",
19
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.255",
20
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.255",
21
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.288",
22
22
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.4",
23
23
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.158",
24
24
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.158",