@phantomblip/game-core 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -15,9 +15,11 @@ In a module made with `npm create @phantomblip/module`, it is already a dependen
15
15
  ## What is in it
16
16
 
17
17
  - Contract types: `GameDefinition`, `Bot`, `BotContext`, `ModuleManifest`, `ModuleRules`, `BotFactories`.
18
+ - `GameInfo`: the catalog facts a manifest may give under `info`.
19
+ - `PlayerPanel`: what `ModuleRules.panels` returns for each seat. With `ModuleRules.progression`, it feeds the site's table frame. Both are optional; see "The table frame" in the `@phantomblip/module-kit` README.
18
20
  - The turn engine: `composeTurn`, `validateTurn`, `playGame`, `runSeries`.
19
21
  - Seeded random numbers: `createRng`, and a `randomBot` to start from.
20
- - `checkModule`, the conformance check `module-kit check` runs, and `MODULE_PROTOCOL`.
22
+ - `checkModule`, the conformance check `module-kit check` runs (it tests `panels` and `progression` when a module has them), and `MODULE_PROTOCOL`.
21
23
  - Bot helpers (`@phantomblip/game-core/bots`) and the harness (`@phantomblip/game-core/harness`).
22
24
 
23
25
  ## Smallest useful example
@@ -36,7 +38,7 @@ console.log(result.winner, result.reason, result.turns);
36
38
 
37
39
  ## Before 1.0
38
40
 
39
- This is version 0.1.x. Expect the SDK to change between releases. `MODULE_PROTOCOL` is the promise of compatibility between a module and the site that runs it.
41
+ This is version 0.x. Expect the SDK to change between releases. `MODULE_PROTOCOL` is the promise of compatibility between a module and the site that runs it.
40
42
 
41
43
  ## Documentation
42
44
 
package/dist/index.d.ts CHANGED
@@ -9,6 +9,6 @@ export type { PlannerHooks, PlannerProfile, PlanOptions, RankedLine } from "./bo
9
9
  export { seedState, xoshiroNext, xoshiroFloat } from "./xoshiro.js";
10
10
  export type { XoshiroState } from "./xoshiro.js";
11
11
  export { MODULE_PROTOCOL, SEAT_FILLS, playerCountsOf } from "./module.js";
12
- export type { BotFactories, GameModule, ModuleManifest, ModuleRules, OptionField } from "./module.js";
12
+ export type { BotFactories, GameInfo, GameModule, ModuleManifest, ModuleRules, OptionField, PlayerPanel } from "./module.js";
13
13
  export { checkManifest, checkModule } from "./module-check.js";
14
14
  export type { CheckOptions } from "./module-check.js";
@@ -79,6 +79,35 @@ export function checkManifest(manifest) {
79
79
  if (!isPlain(r) || Object.values(r).some((v) => typeof v !== "string"))
80
80
  out.push("reasons must map each reason code to a string");
81
81
  }
82
+ if (mf.info !== undefined)
83
+ out.push(...checkInfo(mf.info));
84
+ return out;
85
+ }
86
+ const RATINGS = ["complexity", "luck", "strategy", "interaction"];
87
+ const CREDITS = ["designer", "artist", "publisher"];
88
+ /** The catalog facts: every field optional, but one that is there must be the shape the game's page draws. */
89
+ function checkInfo(info) {
90
+ if (!isPlain(info))
91
+ return ["info must be an object of catalog facts"];
92
+ const i = info;
93
+ const out = [];
94
+ const whole = (v) => typeof v === "number" && Number.isInteger(v);
95
+ if (i.minutes !== undefined) {
96
+ const m = i.minutes;
97
+ if (!Array.isArray(m) || m.length !== 2 || !m.every((n) => whole(n) && n > 0) || m[0] > m[1])
98
+ out.push("info.minutes must be [shortest, longest], whole minutes above zero");
99
+ }
100
+ for (const k of RATINGS)
101
+ if (i[k] !== undefined && !(whole(i[k]) && i[k] >= 1 && i[k] <= 5))
102
+ out.push(`info.${k} must be a whole number from 1 to 5`);
103
+ for (const k of CREDITS)
104
+ if (i[k] !== undefined && (typeof i[k] !== "string" || i[k].trim() === ""))
105
+ out.push(`info.${k} must be a name`);
106
+ if (i.year !== undefined && !whole(i.year))
107
+ out.push("info.year must be a whole number");
108
+ if (i.tags !== undefined && !(Array.isArray(i.tags) && i.tags.every((t) => typeof t === "string" && /^[a-z0-9]+(-[a-z0-9]+)*$/.test(t) && t.length <= 24))) {
109
+ out.push('info.tags must be short lowercase words ("dice", "area-control")');
110
+ }
82
111
  return out;
83
112
  }
84
113
  /** Everything wrong with a module, in words. An empty list means it conforms. */
@@ -122,8 +151,9 @@ export function checkModule(m, opts = {}) {
122
151
  /**
123
152
  * One seeded game of the default bot against itself, then the log replayed
124
153
  * from a fresh setup the way the server would, and from replayStart of the
125
- * finished state the way the replay viewer would. Returns the first thing
126
- * that goes wrong, or null.
154
+ * finished state the way the replay viewer would. On the way through the
155
+ * first replay the table-frame hooks (panels, progression) are read from
156
+ * every seat's view and a spectator's. Returns the first thing that goes wrong, or null.
127
157
  */
128
158
  function playthrough(m, factory, count, seed, turnCap) {
129
159
  const { rules } = m;
@@ -141,11 +171,22 @@ function playthrough(m, factory, count, seed, turnCap) {
141
171
  let state;
142
172
  try {
143
173
  state = def.setup({ options: def.defaultOptions, seed, playerCount: count });
144
- for (const rec of result.log) {
174
+ // The frame hooks are read as the replay goes: at the start, about FRAME_SAMPLES times on the way, and at the end.
175
+ const every = Math.max(1, Math.ceil(result.log.length / FRAME_SAMPLES));
176
+ const atStart = frameProblem(rules, state, count, "at the start");
177
+ if (atStart)
178
+ return atStart;
179
+ for (const [i, rec] of result.log.entries()) {
145
180
  const v = validateTurn(def, state, rec.seat, rec.actions);
146
181
  if (!v.ok)
147
182
  return `replay: turn ${rec.turn} fails validateTurn: ${v.error}`;
148
183
  state = def.applyTurn(state, rec.seat, rec.actions);
184
+ const last = i === result.log.length - 1;
185
+ if (last || (i + 1) % every === 0) {
186
+ const p = frameProblem(rules, state, count, last ? "at the end" : `after turn ${rec.turn}`);
187
+ if (p)
188
+ return p;
189
+ }
149
190
  }
150
191
  if (!def.isTerminal(state).over)
151
192
  return "replay: the log does not end in a terminal state (setup or applyTurn is not deterministic)";
@@ -184,3 +225,76 @@ function playthrough(m, factory, count, seed, turnCap) {
184
225
  return `replayStart: the log replayed from its state ends ${got}, not ${ended} as the game did`;
185
226
  return null;
186
227
  }
228
+ /** How many positions of one playthrough the frame hooks are read at, beyond its start and its end. */
229
+ const FRAME_SAMPLES = 40;
230
+ /** The seat number the host passes `playerView` for someone watching. */
231
+ const SPECTATOR = -1;
232
+ /** How far outside 0 to 1 a progression may land through floating-point rounding (0.1 + 0.2 + 0.7) and still pass. */
233
+ const PROGRESS_SLACK = 1e-9;
234
+ /**
235
+ * The first thing wrong with the module's table-frame hooks (`rules.panels`, `rules.progression`) at one position,
236
+ * or null. Each is read from every seat's `playerView` and from a spectator's (seat -1), as the host's table does; a
237
+ * module with neither is not asked for a view at all. Never throws: a throw from the module is the problem.
238
+ */
239
+ function frameProblem(rules, state, count, when) {
240
+ if (!rules.panels && !rules.progression)
241
+ return null;
242
+ for (const seat of [...Array.from({ length: count }, (_, i) => i), SPECTATOR]) {
243
+ const who = seat === SPECTATOR ? "a spectator" : `seat ${seat}`;
244
+ const where = `(${who}'s view, ${when})`;
245
+ let view;
246
+ try {
247
+ view = rules.def.playerView(state, seat);
248
+ }
249
+ catch (e) {
250
+ return `playerView threw for ${who} ${when}: ${e.message}`;
251
+ }
252
+ if (rules.panels) {
253
+ let panels;
254
+ try {
255
+ panels = rules.panels(view, count);
256
+ }
257
+ catch (e) {
258
+ return `panels threw on ${who}'s view ${when}: ${e.message}`;
259
+ }
260
+ if (!Array.isArray(panels))
261
+ return `panels did not return a list ${where}`;
262
+ if (panels.length !== count)
263
+ return `panels returned a list of ${panels.length} for ${count} players ${where}`;
264
+ for (const [i, panel] of panels.entries()) {
265
+ const wrong = panelProblem(panel);
266
+ if (wrong)
267
+ return `panels: panel ${i}${wrong} ${where}`;
268
+ }
269
+ }
270
+ if (rules.progression) {
271
+ let p;
272
+ try {
273
+ p = rules.progression(view);
274
+ }
275
+ catch (e) {
276
+ return `progression threw on ${who}'s view ${when}: ${e.message}`;
277
+ }
278
+ if (typeof p !== "number" || !Number.isFinite(p) || p < -PROGRESS_SLACK || p > 1 + PROGRESS_SLACK) {
279
+ return `progression returned ${typeof p === "string" ? JSON.stringify(p) : String(p)}, not a number from 0 to 1 ${where}`;
280
+ }
281
+ }
282
+ }
283
+ return null;
284
+ }
285
+ /** What is wrong with one player panel, worded to follow "panel N", or null. Every field is optional. */
286
+ function panelProblem(panel) {
287
+ if (!isObject(panel))
288
+ return " must be an object";
289
+ const scalar = (v) => typeof v === "number" || typeof v === "string";
290
+ if (panel.score !== undefined && !scalar(panel.score))
291
+ return "'s score must be a number or a string";
292
+ for (const k of ["scoreLabel", "note"])
293
+ if (panel[k] !== undefined && typeof panel[k] !== "string")
294
+ return `'s ${k} must be a string`;
295
+ const c = panel.counters;
296
+ if (c !== undefined && !(Array.isArray(c) && c.every((x) => isObject(x) && typeof x.label === "string" && scalar(x.value)))) {
297
+ return "'s counters must be a list of { label: string, value: number or string }";
298
+ }
299
+ return null;
300
+ }
package/dist/module.d.ts CHANGED
@@ -15,6 +15,37 @@ export interface OptionField {
15
15
  /** How the chosen string becomes an engine option. */
16
16
  kind: "number" | "string" | "boolean";
17
17
  }
18
+ /** Catalog facts for the game's own page. Every field is optional; the page shows what is there. */
19
+ export interface GameInfo {
20
+ /** How long one game takes at the table, in minutes: [shortest, longest]. */
21
+ minutes?: [number, number];
22
+ /** 1 (a child can learn it) to 5 (a heavy game). */
23
+ complexity?: 1 | 2 | 3 | 4 | 5;
24
+ /** 1 (almost none) to 5 (mostly). */
25
+ luck?: 1 | 2 | 3 | 4 | 5;
26
+ strategy?: 1 | 2 | 3 | 4 | 5;
27
+ interaction?: 1 | 2 | 3 | 4 | 5;
28
+ designer?: string;
29
+ artist?: string;
30
+ publisher?: string;
31
+ year?: number;
32
+ /** Short lowercase words the catalog filters by ("dice", "cards", "abstract"). */
33
+ tags?: string[];
34
+ }
35
+ /** What the host's player panel shows for one seat, beyond the name and the picture. */
36
+ export interface PlayerPanel {
37
+ /** The seat's score, when the game has one. */
38
+ score?: number | string;
39
+ /** What the score counts ("points", "pieces"). Default: "points". */
40
+ scoreLabel?: string;
41
+ /** A few small counts: cards in hand, coins, ships. Keep it to four. */
42
+ counters?: {
43
+ label: string;
44
+ value: number | string;
45
+ }[];
46
+ /** One short line ("Passed", "First player"). */
47
+ note?: string;
48
+ }
18
49
  /** Plain data describing a module. Must survive JSON.stringify unchanged. */
19
50
  export interface ModuleManifest {
20
51
  id: string;
@@ -57,6 +88,8 @@ export interface ModuleManifest {
57
88
  /** True when the module's UI entry has a standalone replay page (`ui.ReplayPage`, served at /replay/<id>). */
58
89
  replayPage?: boolean;
59
90
  cover?: string;
91
+ /** Catalog facts: playing time, weight, credits, tags. */
92
+ info?: GameInfo;
60
93
  }
61
94
  type AnyDefinition = GameDefinition<any, any, any>;
62
95
  type AnyBotOf = Bot<any, any>;
@@ -68,6 +101,13 @@ export interface ModuleRules {
68
101
  replayStart(state: unknown): unknown | null;
69
102
  /** Partnership games: the team of each seat, or null when everyone plays alone. */
70
103
  teamsFor?(playerCount: number): number[] | null;
104
+ /**
105
+ * One panel per seat for the host's player panels, read from a state the viewer may see (a `playerView`): never
106
+ * put another seat's hidden information here. Left out, the panels show names only.
107
+ */
108
+ panels?(view: unknown, playerCount: number): PlayerPanel[];
109
+ /** How far along the game is, from 0 (just dealt) to 1 (over), read from a `playerView`. */
110
+ progression?(view: unknown): number;
71
111
  }
72
112
  export type BotFactories = Record<string, () => AnyBotOf>;
73
113
  export interface GameModule {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phantomblip/game-core",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",