rpg-engine 0.4.0 → 0.5.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 +1 -1
- package/dist/commands/ask.d.ts +37 -2
- package/dist/commands/ask.js +46 -17
- package/dist/commands/change.d.ts +7 -0
- package/dist/commands/change.js +34 -8
- package/dist/commands/config.js +4 -2
- package/dist/commands/game-extension-list.js +12 -0
- package/dist/commands/game-extensions.d.ts +1 -1
- package/dist/commands/game-extensions.js +242 -17
- package/dist/commands/game-master-rolls.d.ts +2 -0
- package/dist/commands/game-master-rolls.js +2 -0
- package/dist/commands/game-system-code.d.ts +9 -1
- package/dist/commands/game-system-code.js +27 -11
- package/dist/commands/game-widgets.d.ts +6 -1
- package/dist/commands/game-widgets.js +52 -4
- package/dist/commands/game-world.d.ts +15 -3
- package/dist/commands/game-world.js +45 -14
- package/dist/commands/package.d.ts +7 -1
- package/dist/commands/package.js +17 -2
- package/dist/commands/roll.js +2 -2
- package/dist/commands/scene.d.ts +1 -1
- package/dist/commands/view.d.ts +4 -0
- package/dist/commands/view.js +5 -3
- package/dist/core/context.d.ts +19 -0
- package/dist/core/files.d.ts +2 -0
- package/dist/core/files.js +10 -1
- package/dist/core/registry.d.ts +2 -0
- package/dist/dashboard/api.js +11 -3
- package/dist/dashboard/app.js +88 -8
- package/dist/dashboard/ask-card.js +118 -0
- package/dist/dashboard/dice/tray.js +5 -3
- package/dist/dashboard/extension-widgets.js +35 -9
- package/dist/dashboard/faces.js +6 -4
- package/dist/dashboard/help.js +10 -3
- package/dist/dashboard/i18n/ar.json +10 -1
- package/dist/dashboard/i18n/cs.json +9 -0
- package/dist/dashboard/i18n/da.json +9 -0
- package/dist/dashboard/i18n/de.json +9 -0
- package/dist/dashboard/i18n/el.json +10 -1
- package/dist/dashboard/i18n/es.json +9 -0
- package/dist/dashboard/i18n/fi.json +9 -0
- package/dist/dashboard/i18n/fr.json +9 -0
- package/dist/dashboard/i18n/he.json +10 -1
- package/dist/dashboard/i18n/hi.json +10 -1
- package/dist/dashboard/i18n/hu.json +9 -0
- package/dist/dashboard/i18n/is.json +9 -0
- package/dist/dashboard/i18n/it.json +9 -0
- package/dist/dashboard/i18n/ja.json +10 -1
- package/dist/dashboard/i18n/ko.json +10 -1
- package/dist/dashboard/i18n/nb.json +9 -0
- package/dist/dashboard/i18n/nl.json +9 -0
- package/dist/dashboard/i18n/pl.json +9 -0
- package/dist/dashboard/i18n/pt.json +9 -0
- package/dist/dashboard/i18n/ro.json +9 -0
- package/dist/dashboard/i18n/ru.json +9 -0
- package/dist/dashboard/i18n/sv.json +9 -0
- package/dist/dashboard/i18n/tr.json +9 -0
- package/dist/dashboard/i18n/uk.json +9 -0
- package/dist/dashboard/i18n/zh.json +11 -2
- package/dist/dashboard/markdown.js +15 -4
- package/dist/dashboard/pack-card.js +92 -0
- package/dist/dashboard/packages.js +11 -4
- package/dist/dashboard/rolls.js +2 -2
- package/dist/dashboard/scene.js +21 -10
- package/dist/dashboard/style.css +524 -4
- package/dist/dashboard-server.js +57 -1
- package/dist/extensions/api.d.ts +148 -17
- package/dist/extensions/extension.d.ts +10 -3
- package/dist/extensions/extension.js +16 -3
- package/dist/extensions/index.d.ts +3 -3
- package/dist/extensions/index.js +2 -2
- package/dist/extensions/packages.d.ts +8 -0
- package/dist/extensions/packages.js +48 -1
- package/dist/extensions/words.d.ts +2 -1
- package/dist/extensions/words.js +5 -4
- package/dist/index.d.ts +1 -1
- package/dist/mcp-server.js +1 -1
- package/dist/pages/index.d.ts +1 -1
- package/dist/pages/index.js +1 -1
- package/dist/pages/standard-fields.d.ts +2 -0
- package/dist/pages/standard-fields.js +8 -1
- package/dist/systems/game-system.d.ts +14 -9
- package/dist/systems/game-system.js +29 -19
- package/dist/systems/index.d.ts +1 -1
- package/dist/testing.d.ts +61 -0
- package/dist/testing.js +162 -0
- package/package.json +7 -2
|
@@ -12,15 +12,27 @@ import { pathToFileURL } from "node:url";
|
|
|
12
12
|
import { parse, stringify } from "yaml";
|
|
13
13
|
import { z } from "zod";
|
|
14
14
|
import { campaignFileName, readTextSetting, requireCampaign } from "../campaign/index.js";
|
|
15
|
+
import { realPath } from "../core/files.js";
|
|
16
|
+
import { parseFormula } from "../core/formula.js";
|
|
15
17
|
import { appendLogEntry } from "../core/log.js";
|
|
16
18
|
import { defineCommand } from "../core/registry.js";
|
|
17
19
|
import { files } from "../core/store.js";
|
|
20
|
+
import { sameName, valueNamed } from "../core/text.js";
|
|
18
21
|
import { rollDice } from "../dice/index.js";
|
|
19
|
-
import {
|
|
22
|
+
import { extensionWords, wordIn } from "../extensions/index.js";
|
|
23
|
+
import { environmentAt, isCoreField, isInParty, isPlayerCharacter, locate, pagesWithNameOrAlias, playerCharacters } from "../pages/index.js";
|
|
24
|
+
import { findSystem } from "../systems/index.js";
|
|
20
25
|
import { formatGameDate } from "../time/index.js";
|
|
26
|
+
import { askedRoll, isCheckDice, tonesOf } from "./ask.js";
|
|
27
|
+
import { effectiveFields } from "./effect-layer.js";
|
|
21
28
|
import { campaignExtensions, runsCode } from "./game-extension-list.js";
|
|
29
|
+
import { behindTheScreen, sealHiddenRoll } from "./game-master-rolls.js";
|
|
30
|
+
import { readOpenScene } from "./game-scene.js";
|
|
31
|
+
import { setFields, systemPage } from "./game-system-code.js";
|
|
22
32
|
import { calendarDayOf, newDatedLogEntry, presentDate } from "./game-time.js";
|
|
23
|
-
import {
|
|
33
|
+
import { widgetCard } from "./game-widgets.js";
|
|
34
|
+
import { campaignWorld, extensionLayer, playedLayers } from "./game-world.js";
|
|
35
|
+
import { formulaValue } from "./worked-out.js";
|
|
24
36
|
/** The folder of the campaign where an extension keeps its own files, its state among them (D-433, D-436). */
|
|
25
37
|
export const extensionsFolderName = "extensions";
|
|
26
38
|
/** The longest description a command of an extension may give the master, in words (D-441): it is read in every chat. */
|
|
@@ -105,9 +117,19 @@ function asCommand(extension, command, taken) {
|
|
|
105
117
|
input: command.input,
|
|
106
118
|
readOnly: command.readOnly,
|
|
107
119
|
positionals: command.positionals,
|
|
120
|
+
waitsForPlayer: command.needsPlayer,
|
|
108
121
|
async run(input, called) {
|
|
109
122
|
const campaign = requireCampaign(called.folder);
|
|
110
|
-
|
|
123
|
+
const asked = {};
|
|
124
|
+
try {
|
|
125
|
+
return { text: await command.run(input, gameOf(campaign, called, extension, asked)) };
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
// The card of its last question, as it is after the dice, read once the call ends, however it ends (D-507).
|
|
129
|
+
if (asked.card !== undefined) {
|
|
130
|
+
called.page?.cardEnded?.(asked.card);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
111
133
|
},
|
|
112
134
|
format: (result) => result.text,
|
|
113
135
|
});
|
|
@@ -125,7 +147,8 @@ export function extensionCode(extension) {
|
|
|
125
147
|
if (entry === undefined) {
|
|
126
148
|
return Promise.resolve(undefined);
|
|
127
149
|
}
|
|
128
|
-
|
|
150
|
+
// By its real path, as Node would resolve it: a package linked from a folder of the player's is read from that folder, its own settings beside it.
|
|
151
|
+
const url = `${pathToFileURL(realPath(entry)).href}?v=${encodeURIComponent(extension.version)}&installs=${installs}`;
|
|
129
152
|
const code = loaded.get(url) ?? import(__rewriteRelativeImportExtension(url)).then((module) => {
|
|
130
153
|
if (typeof module.default !== "function") {
|
|
131
154
|
throw new Error(`its code, ${entry}, has no default export that gives its commands`);
|
|
@@ -135,14 +158,54 @@ export function extensionCode(extension) {
|
|
|
135
158
|
loaded.set(url, code);
|
|
136
159
|
return code;
|
|
137
160
|
}
|
|
138
|
-
/**
|
|
139
|
-
|
|
140
|
-
|
|
161
|
+
/**
|
|
162
|
+
* The game as an extension's code reaches it (D-436, D-505): what a widget reads, the dice, asking the player, its own state, the fields it adds and the log;
|
|
163
|
+
* nothing else is written, and nothing once the call is stopped (ASK-9).
|
|
164
|
+
*/
|
|
165
|
+
function gameOf(root, context, extension, asked = {}) {
|
|
166
|
+
const view = gameViewOf(root, context, extension);
|
|
167
|
+
const live = () => {
|
|
168
|
+
if (context.signal?.aborted === true) {
|
|
169
|
+
throw new Error("The call was stopped: nothing more is written.");
|
|
170
|
+
}
|
|
171
|
+
};
|
|
141
172
|
return {
|
|
142
|
-
|
|
173
|
+
...view,
|
|
174
|
+
roll: (notation, by) => {
|
|
175
|
+
if (by !== undefined) {
|
|
176
|
+
live();
|
|
177
|
+
return rollFor(root, context, notation, by, (text) => extensionText(root, extension, text));
|
|
178
|
+
}
|
|
143
179
|
const rolled = rollDice(notation, { random: context.random });
|
|
144
180
|
return { total: rolled.total, faces: rolled.faces, text: rolled.text };
|
|
145
181
|
},
|
|
182
|
+
set: (page, values, why) => {
|
|
183
|
+
live();
|
|
184
|
+
setOwnFields(root, context, extension, page, values, why);
|
|
185
|
+
},
|
|
186
|
+
ask: async (wanted) => {
|
|
187
|
+
live();
|
|
188
|
+
const { who, question, check, notation } = wanted;
|
|
189
|
+
const told = toldOf(root, context, extension, wanted);
|
|
190
|
+
asked.card = told.card ?? asked.card;
|
|
191
|
+
return askedOf(await askedRoll(root, context, askedQuestion(extensionText(root, extension, question), check, notation), [{ label: check.trim(), notation }], who, told));
|
|
192
|
+
},
|
|
193
|
+
state: {
|
|
194
|
+
read: view.state.read,
|
|
195
|
+
write: (value) => {
|
|
196
|
+
live();
|
|
197
|
+
files().write(statePath(root, extension), `# The state of the extension ${extension.package}, kept by its code.\n${stringify(value, { lineWidth: 0 })}`);
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
log: (text, visibility = "hidden") => {
|
|
201
|
+
live();
|
|
202
|
+
appendLogEntry(root, newDatedLogEntry(root, context, "extension", visibility, { extension: extension.name, text }));
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/** The game as a widget of an extension reads it (D-502, D-505): the time, where the party is and who is in it, a page, the scene and the extension's state. */
|
|
207
|
+
export function gameViewOf(root, context, extension) {
|
|
208
|
+
return {
|
|
146
209
|
now: () => {
|
|
147
210
|
const now = presentDate(root);
|
|
148
211
|
return now === undefined ? undefined : momentOf(root, context, now);
|
|
@@ -157,25 +220,187 @@ function gameOf(root, context, extension) {
|
|
|
157
220
|
: {
|
|
158
221
|
places: locate(world, player).chain.filter((page) => page.type === "place").map((page) => page.name),
|
|
159
222
|
environment: Object.fromEntries(environmentAt(world, player).map(({ entry }) => [entry.name, entry.value])),
|
|
223
|
+
members: world.pages.filter(isInParty).map((page) => ({ name: page.name, pc: isPlayerCharacter(page) })),
|
|
160
224
|
};
|
|
161
225
|
},
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
226
|
+
page: (name) => {
|
|
227
|
+
const world = campaignWorld(root, context);
|
|
228
|
+
const found = pagesWithNameOrAlias(world, name);
|
|
229
|
+
return found.length === 1 ? gamePage(world, found[0]) : undefined;
|
|
230
|
+
},
|
|
231
|
+
scene: () => {
|
|
232
|
+
const scene = readOpenScene(root).scene;
|
|
233
|
+
return scene === undefined ? undefined : {
|
|
234
|
+
title: scene.title,
|
|
235
|
+
...(scene.opened === undefined ? {} : { opened: scene.opened }),
|
|
236
|
+
...(scene.round === undefined ? {} : { round: scene.round }),
|
|
237
|
+
...(scene.turn === undefined ? {} : { turn: scene.turn }),
|
|
238
|
+
};
|
|
165
239
|
},
|
|
166
|
-
|
|
240
|
+
state: { read: () => parse(files().text(statePath(root, extension)) ?? "null") },
|
|
167
241
|
};
|
|
168
242
|
}
|
|
169
|
-
/** The game as a widget of an extension reads it (D-502): the game its commands reach, without the dice, the log or writing. */
|
|
170
|
-
export function gameViewOf(root, context, extension) {
|
|
171
|
-
const { now, calendarDay, units, party, state } = gameOf(root, context, extension);
|
|
172
|
-
return { now, calendarDay, units, party, state: { read: state.read } };
|
|
173
|
-
}
|
|
174
243
|
/** A game date as the API gives it: as written, its day and minute, and its day of the calendar. */
|
|
175
244
|
function momentOf(root, context, date) {
|
|
176
245
|
const calendar = calendarDayOf(root, context, date.day);
|
|
177
246
|
return { date: formatGameDate(date), day: date.day, ...(date.minute === undefined ? {} : { minute: date.minute }), ...(calendar === undefined ? {} : { calendar }) };
|
|
178
247
|
}
|
|
248
|
+
function statePath(root, extension) {
|
|
249
|
+
return join(root, extensionsFolderName, extension.name, "state.yaml");
|
|
250
|
+
}
|
|
251
|
+
/** A page as an extension reads it (D-505): as a system's code does, with its fields as show gives them, effects and what the system works out included. */
|
|
252
|
+
function gamePage(world, page) {
|
|
253
|
+
const fields = effectiveFields(world, page);
|
|
254
|
+
return {
|
|
255
|
+
...systemPage(world, page),
|
|
256
|
+
value: (field) => plainValue(valueNamed(fields, field)),
|
|
257
|
+
formula: (text) => {
|
|
258
|
+
let formula;
|
|
259
|
+
try {
|
|
260
|
+
formula = parseFormula(text);
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
throw new Error(`The formula ${text} does not read: ${error instanceof Error ? error.message : String(error)} Write it as the game system's computed fields are written.`);
|
|
264
|
+
}
|
|
265
|
+
return formulaValue(world, fields, formula);
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/** A field's value as the API gives it: a number, text, or a list as text; undefined for anything else, as a map. */
|
|
270
|
+
function plainValue(value) {
|
|
271
|
+
if (typeof value === "number" || typeof value === "string") {
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
if (typeof value === "boolean") {
|
|
275
|
+
return String(value);
|
|
276
|
+
}
|
|
277
|
+
return Array.isArray(value) ? value.map((item) => (typeof item === "object" && item !== null ? JSON.stringify(item) : String(item))) : undefined;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Sets on a page only the fields an extension adds to the game system, as fields or counters, under their own names (D-505): another is refused with the names
|
|
281
|
+
* of its own, as one the core, the system, an extension below it or the campaign declares too, so that no package reaches what another keeps, such as hit
|
|
282
|
+
* points; and all of them while its additions are left out (CAM-3).
|
|
283
|
+
*/
|
|
284
|
+
function setOwnFields(root, context, extension, name, values, why) {
|
|
285
|
+
const { kept, refused } = playedLayers(root, context);
|
|
286
|
+
const layer = extensionLayer(extension.name);
|
|
287
|
+
if (refused.includes(layer)) {
|
|
288
|
+
throw new Error(`What the extension ${extension.name} adds to the game system does not read, so the campaign plays without it and it sets none of its fields: validate says why. Nothing changed.`);
|
|
289
|
+
}
|
|
290
|
+
const below = kept.filter((other, index) => other.from === campaignFileName || index < kept.findIndex((mine) => mine.from === layer));
|
|
291
|
+
const taken = [...Object.keys(findSystem(context.systemFolders, readTextSetting(root, "system") ?? "")?.fields ?? {}), ...below.flatMap((other) => declaredFields(other.data))];
|
|
292
|
+
const declared = declaredFields(extension.adds);
|
|
293
|
+
const own = declared.filter((field, index) => sameName(declared.slice(0, index), field) === undefined && sameName(taken, field) === undefined && !isCoreField(field));
|
|
294
|
+
const others = Object.keys(values).filter((field) => sameName(own, field) === undefined);
|
|
295
|
+
const odd = Object.entries(values).find(([, value]) => typeof value !== "string" && (typeof value !== "number" || !Number.isFinite(value)));
|
|
296
|
+
if (others.length > 0) {
|
|
297
|
+
const its = own.length === 0 ? "it adds none in its extension.yaml" : `its own are ${own.join(", ")}`;
|
|
298
|
+
throw new Error(`The extension ${extension.name} sets only the fields it adds to the game system, and ${others.join(", ")} is not one of them: ${its}. Add it to fields or counters in its extension.yaml. Nothing changed.`);
|
|
299
|
+
}
|
|
300
|
+
if (odd !== undefined) {
|
|
301
|
+
throw new Error(`The extension ${extension.name} gave ${odd[0]} the value ${String(odd[1])}: give a number or text. Nothing changed.`);
|
|
302
|
+
}
|
|
303
|
+
const world = campaignWorld(root, context);
|
|
304
|
+
const found = pagesWithNameOrAlias(world, name);
|
|
305
|
+
if (found.length !== 1) {
|
|
306
|
+
throw new Error(`${found.length === 0 ? `No page alone is named ${name}` : `${found.length} pages are named ${name}`}: give the name of one page of the campaign. Nothing changed.`);
|
|
307
|
+
}
|
|
308
|
+
// Each under the name its extension.yaml gives it, whatever the case it came in.
|
|
309
|
+
setFields(root, context, world, found[0], Object.fromEntries(Object.entries(values).map(([field, value]) => [sameName(own, field) ?? field, value])), why);
|
|
310
|
+
}
|
|
311
|
+
/** The fields and counters a layer of additions declares. */
|
|
312
|
+
function declaredFields(data) {
|
|
313
|
+
const names = (map) => (typeof map === "object" && map !== null && !Array.isArray(map) ? Object.keys(map) : []);
|
|
314
|
+
return [...names(data.fields), ...names(data.counters)];
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* A roll for who, as the master's for them (D-287, D-505): a line in the log, in view for the party and behind the screen for anyone else as master-rolls says;
|
|
318
|
+
* the player's physical dice, given as their faces, always in view.
|
|
319
|
+
*/
|
|
320
|
+
function rollFor(root, context, notation, by, words) {
|
|
321
|
+
const rolled = rollDice(notation, by.values === undefined ? { random: context.random } : { values: by.values });
|
|
322
|
+
const physical = by.values !== undefined;
|
|
323
|
+
const hidden = !physical && behindTheScreen(root, context, by.who);
|
|
324
|
+
const checked = by.check === undefined ? {} : { check: by.check };
|
|
325
|
+
const says = by.says === undefined ? {} : { says: words(by.says(rolled.faces)) };
|
|
326
|
+
const tones = tonesOf(by.tones?.(rolled.faces), rolled.faces);
|
|
327
|
+
const entry = newDatedLogEntry(root, context, "roll", hidden ? "hidden" : "public", {
|
|
328
|
+
notation: rolled.notation,
|
|
329
|
+
total: rolled.total,
|
|
330
|
+
result: rolled.text,
|
|
331
|
+
...says,
|
|
332
|
+
...tones,
|
|
333
|
+
why: by.why,
|
|
334
|
+
who: by.who,
|
|
335
|
+
...checked,
|
|
336
|
+
physical,
|
|
337
|
+
dice: rolled.dice,
|
|
338
|
+
});
|
|
339
|
+
if (hidden) {
|
|
340
|
+
sealHiddenRoll(entry, rolled.text, context);
|
|
341
|
+
}
|
|
342
|
+
appendLogEntry(root, entry);
|
|
343
|
+
return { total: rolled.total, faces: rolled.faces, text: rolled.text, id: entry.id, hidden };
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* What a roll an extension asks tells the page in its own words (D-507): its buttons and its rule translated, what its faces mean and how they turned out, and
|
|
347
|
+
* the card of its widget, refused when the extension has no widget by that name.
|
|
348
|
+
*/
|
|
349
|
+
function toldOf(root, context, extension, wanted) {
|
|
350
|
+
const words = (text) => extensionText(root, extension, text);
|
|
351
|
+
const button = (given) => (typeof given === "object" && "label" in given ? { name: words(given.label), note: words(given.note) } : { name: words(given) });
|
|
352
|
+
const { roll, decline, rule, card, says, tones } = wanted;
|
|
353
|
+
if (card !== undefined && !extension.widgets.some((widget) => widget.name === card.widget)) {
|
|
354
|
+
const its = extension.widgets.length === 0 ? "it declares none in its extension.yaml" : `its own are ${extension.widgets.map((widget) => widget.name).join(", ")}`;
|
|
355
|
+
throw new Error(`The extension ${extension.name} asks in the card of a widget ${card.widget} it does not have: ${its}. Nothing was asked.`);
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
...(roll === undefined ? {} : { roll: button(roll) }),
|
|
359
|
+
...(decline === undefined ? {} : { decline: decline === false ? false : button(decline) }),
|
|
360
|
+
...(rule === undefined ? {} : { rule: words(rule) }),
|
|
361
|
+
...(card === undefined ? {} : { card: { widget: `${extension.name}.${card.widget}`, key: card.key, shows: () => widgetCard(root, context, extension, card.widget, card.key) } }),
|
|
362
|
+
...(says === undefined ? {} : { says: (faces) => words(says(faces)) }),
|
|
363
|
+
...(tones === undefined ? {} : { tones }),
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
/** Words of an extension in the campaign's language, by its words as a widget's are, with their {names} filled; as written where its words lack them. */
|
|
367
|
+
function extensionText(root, extension, text) {
|
|
368
|
+
let words;
|
|
369
|
+
try {
|
|
370
|
+
words = extensionWords(extension.folder, readTextSetting(root, "language") ?? extension.language);
|
|
371
|
+
}
|
|
372
|
+
catch {
|
|
373
|
+
// Words that do not read leave the text as written, as validate says.
|
|
374
|
+
}
|
|
375
|
+
const { text: written, values } = typeof text === "string" ? { text, values: {} } : text;
|
|
376
|
+
return wordIn(words, written).replace(/\{(\w+)\}/g, (whole, name) => (Object.hasOwn(values, name) ? valueText(values[name]) : whole));
|
|
377
|
+
}
|
|
378
|
+
/** A value as plain text in a line of the log. */
|
|
379
|
+
function valueText(value) {
|
|
380
|
+
const unit = (given) => (given === undefined ? "" : ` ${given}`);
|
|
381
|
+
return typeof value === "number" || typeof value === "string" ? String(value) : "number" in value ? `${value.number}${unit(value.unit)}` : `${value.from}–${value.to}${unit(value.unit)}`;
|
|
382
|
+
}
|
|
383
|
+
/** The question of a roll asked, refused without words, a check's name or dice (ASK-2). */
|
|
384
|
+
function askedQuestion(question, check, notation) {
|
|
385
|
+
if (question.trim() === "" || check.trim() === "" || !isCheckDice(notation)) {
|
|
386
|
+
const dice = isCheckDice(notation) ? "" : `: ${notation} are no dice`;
|
|
387
|
+
throw new Error(`Give ask the question, what is at stake either way, the check's name, such as Stealth, and its dice, such as 1d20+5${dice}. Nothing was asked.`);
|
|
388
|
+
}
|
|
389
|
+
return question.trim();
|
|
390
|
+
}
|
|
391
|
+
/** What ask answered, as an extension's code gets it. */
|
|
392
|
+
function askedOf(result) {
|
|
393
|
+
switch (result.kind) {
|
|
394
|
+
case "roll":
|
|
395
|
+
return { rolled: { id: result.id, total: result.total, faces: result.faces, text: result.text } };
|
|
396
|
+
case "changed":
|
|
397
|
+
return { declined: true };
|
|
398
|
+
case "said":
|
|
399
|
+
return { said: result.words };
|
|
400
|
+
default:
|
|
401
|
+
return { inWords: "asked" in result ? result.asked : "", physical: result.kind === "physical" || result.kind === "dice in words" };
|
|
402
|
+
}
|
|
403
|
+
}
|
|
179
404
|
function codeProblem(extension, error) {
|
|
180
405
|
const why = error instanceof Error ? error.message : String(error);
|
|
181
406
|
return { path: campaignFileName, message: `The code of the extension ${extension.package} cannot be used: ${why}. Its commands are left out.`, fix: `install it again, or another version, with package install ${extension.name}` };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Context } from "../core/context.ts";
|
|
2
2
|
import type { LogEntry } from "../core/log.ts";
|
|
3
|
+
/** What the master is told of a roll the setting hid. */
|
|
4
|
+
export declare const screenNote = "Behind the screen, as master-rolls says: tell only the outcome, in the story.";
|
|
3
5
|
/**
|
|
4
6
|
* Whether the master-rolls setting hides a roll of who: one for anyone the party does not hold, by name or alias, as a foe's copy in the scene or a name of
|
|
5
7
|
* no page; never a roll of no one, as an oracle's.
|
|
@@ -5,6 +5,8 @@ import { createProof, fingerprint } from "../dice/index.js";
|
|
|
5
5
|
import { isPartyMember, pagesWithNameOrAlias } from "../pages/index.js";
|
|
6
6
|
import { readOpenScene } from "./game-scene.js";
|
|
7
7
|
import { campaignWorld } from "./game-world.js";
|
|
8
|
+
/** What the master is told of a roll the setting hid. */
|
|
9
|
+
export const screenNote = "Behind the screen, as master-rolls says: tell only the outcome, in the story.";
|
|
8
10
|
/**
|
|
9
11
|
* Whether the master-rolls setting hides a roll of who: one for anyone the party does not hold, by name or alias, as a foe's copy in the scene or a name of
|
|
10
12
|
* no page; never a roll of no one, as an oracle's.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Context, SaveAsked } from "../core/context.ts";
|
|
2
|
-
import type
|
|
2
|
+
import { type Page, type World } from "../pages/index.ts";
|
|
3
|
+
import type { CounterChange, RollMade, SystemPage } from "../systems/index.ts";
|
|
3
4
|
/** What a system's code said after a command's change, and apart why it did not run or failed, which the caller adds to its notes, never deciding by it. */
|
|
4
5
|
export interface SystemSaid {
|
|
5
6
|
lines: string[];
|
|
@@ -23,3 +24,10 @@ export declare function systemAsking(root: string, context: Context, question: {
|
|
|
23
24
|
who: string;
|
|
24
25
|
notations: string[];
|
|
25
26
|
}): SaveAsked | undefined;
|
|
27
|
+
/** A page as a system's code reads it: in the party or not, the player's or not, its counters with what its effects give, its effects. */
|
|
28
|
+
export declare function systemPage(world: World, page: Page): SystemPage;
|
|
29
|
+
/**
|
|
30
|
+
* Sets fields on a page, numbers or text, a counter only numbers, keeping its maximum, each with its line in the log, public when the page concerns the player:
|
|
31
|
+
* for a system's code and an extension's (D-505).
|
|
32
|
+
*/
|
|
33
|
+
export declare function setFields(root: string, context: Context, world: World, page: Page, values: Record<string, number | string>, why: string): void;
|
|
@@ -91,7 +91,13 @@ function reader(root, context) {
|
|
|
91
91
|
function game(root, context) {
|
|
92
92
|
return {
|
|
93
93
|
...reader(root, context),
|
|
94
|
-
set: (name, values, why) =>
|
|
94
|
+
set: (name, values, why) => {
|
|
95
|
+
const world = campaignWorld(root, context);
|
|
96
|
+
const page = uniquePage(world, name);
|
|
97
|
+
if (page !== undefined) {
|
|
98
|
+
setFields(root, context, world, page, values, why);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
95
101
|
addEffect: (name, effect, why) => addEffect(root, context, name, effect, why),
|
|
96
102
|
endEffect: (name, effect, why) => endEffect(root, context, name, effect, why),
|
|
97
103
|
roll: (notation, by) => {
|
|
@@ -107,7 +113,8 @@ function game(root, context) {
|
|
|
107
113
|
},
|
|
108
114
|
};
|
|
109
115
|
}
|
|
110
|
-
|
|
116
|
+
/** A page as a system's code reads it: in the party or not, the player's or not, its counters with what its effects give, its effects. */
|
|
117
|
+
export function systemPage(world, page) {
|
|
111
118
|
const fields = effectiveFields(world, page);
|
|
112
119
|
return {
|
|
113
120
|
name: page.name,
|
|
@@ -125,24 +132,33 @@ function uniquePage(world, name) {
|
|
|
125
132
|
const found = pagesWithName(world, name);
|
|
126
133
|
return found.length === 1 ? found[0] : undefined;
|
|
127
134
|
}
|
|
128
|
-
/**
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Sets fields on a page, numbers or text, a counter only numbers, keeping its maximum, each with its line in the log, public when the page concerns the player:
|
|
137
|
+
* for a system's code and an extension's (D-505).
|
|
138
|
+
*/
|
|
139
|
+
export function setFields(root, context, world, page, values, why) {
|
|
140
|
+
if (Object.keys(values).length === 0) {
|
|
133
141
|
return;
|
|
134
142
|
}
|
|
143
|
+
// A counter takes numbers alone: text in it would read as no counter at all.
|
|
144
|
+
const worded = Object.entries(values).find(([field, value]) => typeof value === "string" && valueNamed(world.system?.fields ?? {}, field) === "counter");
|
|
145
|
+
if (worded !== undefined) {
|
|
146
|
+
throw new Error(`${worded[0]} is a counter: give it a number, not ${worded[1]}. Nothing changed.`);
|
|
147
|
+
}
|
|
135
148
|
const before = effectiveFields(world, page);
|
|
136
149
|
const written = Object.fromEntries(Object.entries(values).map(([field, value]) => {
|
|
137
150
|
const own = readCounter(valueNamed(page.fields, field));
|
|
138
|
-
return [field, own?.max === undefined ? value : formatCounter({ ...own, value })];
|
|
151
|
+
return [field, typeof value !== "number" || own?.max === undefined ? value : formatCounter({ ...own, value })];
|
|
139
152
|
}));
|
|
140
153
|
writePage(world, page.name, undefined, { set: written });
|
|
141
154
|
const visibility = concernsPlayer(world, page) ? "public" : "hidden";
|
|
142
155
|
for (const [field, after] of Object.entries(written)) {
|
|
143
|
-
const was =
|
|
144
|
-
const
|
|
145
|
-
|
|
156
|
+
const was = valueNamed(before, field);
|
|
157
|
+
const counter = readCounter(was);
|
|
158
|
+
const shown = was === undefined ? (typeof after === "number" ? "0" : "none") : counter === undefined ? String(was) : formatCounter(counter);
|
|
159
|
+
// A counter whose maximum the system works out, which the page does not write, reads with it after too.
|
|
160
|
+
const now = typeof after === "number" && counter?.max !== undefined ? formatCounter({ value: after, max: counter.max }) : after;
|
|
161
|
+
appendLogEntry(root, newDatedLogEntry(root, context, "counter-changed", visibility, { page: page.name, field, before: String(shown), after: String(now), why }));
|
|
146
162
|
}
|
|
147
163
|
}
|
|
148
164
|
/** Adds an effect without end: the engine's notes on what it breaks of what one keeps; undefined when the page has it already. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Context } from "../core/context.ts";
|
|
2
2
|
import type { Problem } from "../core/problem.ts";
|
|
3
|
-
import { type Widget, type WidgetPage, type WidgetPlace, type WidgetText, type WidgetValue } from "../extensions/index.ts";
|
|
3
|
+
import { type Extension, type Widget, type WidgetPage, type WidgetPlace, type WidgetText, type WidgetValue } from "../extensions/index.ts";
|
|
4
4
|
/** Where a widget stands: a place of the dashboard, with its page for a card in one, and its order among the cards there; or off. */
|
|
5
5
|
export interface WidgetSpot {
|
|
6
6
|
place: WidgetPlace | "off";
|
|
@@ -64,3 +64,8 @@ export declare function campaignWidgetNames(root: string, context: Context): Rec
|
|
|
64
64
|
* dashboard has. It throws what is wrong, and how to write it.
|
|
65
65
|
*/
|
|
66
66
|
export declare function checkWidgetSpots(root: string, context: Context, entries: Record<string, unknown>): void;
|
|
67
|
+
/**
|
|
68
|
+
* What one widget of an extension shows now, for the card of a question (D-507): read as the dashboard reads it, in the campaign's language; nothing when its
|
|
69
|
+
* hook is not there, fails, gives what no part draws, or is late.
|
|
70
|
+
*/
|
|
71
|
+
export declare function widgetCard(root: string, context: Context, extension: Extension, name: string, key: string): Promise<ShownContent | undefined>;
|
|
@@ -184,12 +184,25 @@ const textSchema = z.union([
|
|
|
184
184
|
z.object({ text: text(240), values: z.record(z.string().regex(/^\w+$/), valueSchema).refine((values) => Object.keys(values).length <= 8, "at most 8 values") }).strict(),
|
|
185
185
|
]);
|
|
186
186
|
const iconName = z.string().regex(/^[a-z][a-z0-9-]*$/);
|
|
187
|
+
const toneSchema = z.enum(["good", "bad"]);
|
|
187
188
|
const partSchema = z.union([
|
|
188
|
-
z.object({ lead: textSchema, icon: iconName.optional(), value: valueSchema.optional() }).strict(),
|
|
189
|
+
z.object({ lead: textSchema, icon: iconName.optional(), value: valueSchema.optional(), tone: toneSchema.optional() }).strict(),
|
|
189
190
|
z.object({ text: textSchema }).strict(),
|
|
190
191
|
z.object({ facts: z.array(textSchema).max(12) }).strict(),
|
|
191
192
|
z.object({ values: z.array(z.object({ label: textSchema, value: valueSchema }).strict()).max(12) }).strict(),
|
|
192
|
-
z.object({
|
|
193
|
+
z.object({
|
|
194
|
+
track: z.object({
|
|
195
|
+
label: textSchema,
|
|
196
|
+
value: z.int().min(0),
|
|
197
|
+
max: z.int().min(1).max(100),
|
|
198
|
+
look: z.enum(["pips", "bar", "steps"]).optional(),
|
|
199
|
+
held: z.int().min(0).optional(),
|
|
200
|
+
marked: z.int().min(0).optional(),
|
|
201
|
+
coming: z.int().min(0).optional(),
|
|
202
|
+
stage: z.enum(["start", "center", "end"]).optional(),
|
|
203
|
+
tone: toneSchema.optional(),
|
|
204
|
+
}).strict(),
|
|
205
|
+
}).strict(),
|
|
193
206
|
z.object({ rows: z.array(z.object({ label: textSchema, value: valueSchema.optional(), note: textSchema.optional() }).strict()).max(24) }).strict(),
|
|
194
207
|
z.object({ table: z.object({ head: z.array(textSchema).min(1).max(6), rows: z.array(z.array(valueSchema).max(6)).max(24) }).strict() }).strict(),
|
|
195
208
|
z.object({ image: z.string().regex(/^[\w .-]+\.(png|jpe?g|webp|svg)$/i), alt: textSchema }).strict(),
|
|
@@ -230,7 +243,8 @@ function shownContent(extension, words, given) {
|
|
|
230
243
|
parts: parts.map((part) => {
|
|
231
244
|
if ("lead" in part) {
|
|
232
245
|
const leadIcon = icon(part.icon);
|
|
233
|
-
|
|
246
|
+
const tone = part.tone === undefined ? {} : { tone: part.tone };
|
|
247
|
+
return { lead: said(part.lead), ...(leadIcon === undefined ? {} : { icon: leadIcon }), ...(part.value === undefined ? {} : { value: valueIn(part.value) }), ...tone };
|
|
234
248
|
}
|
|
235
249
|
if ("text" in part) {
|
|
236
250
|
return { text: said(part.text) };
|
|
@@ -242,7 +256,7 @@ function shownContent(extension, words, given) {
|
|
|
242
256
|
return { values: part.values.map((one) => ({ label: said(one.label), value: valueIn(one.value) })) };
|
|
243
257
|
}
|
|
244
258
|
if ("track" in part) {
|
|
245
|
-
return { track: { ...part.track, label: said(part.track.label)
|
|
259
|
+
return { track: shownTrack({ ...part.track, label: said(part.track.label) }) };
|
|
246
260
|
}
|
|
247
261
|
if ("rows" in part) {
|
|
248
262
|
return {
|
|
@@ -261,6 +275,40 @@ function shownContent(extension, words, given) {
|
|
|
261
275
|
}),
|
|
262
276
|
};
|
|
263
277
|
}
|
|
278
|
+
/** A count as the page draws it: the lit ones, then those held, marked and coming, each cut to what is left of its maximum. */
|
|
279
|
+
function shownTrack(track) {
|
|
280
|
+
let left = track.max;
|
|
281
|
+
const fit = (count) => {
|
|
282
|
+
const kept = count === undefined ? undefined : Math.min(count, left);
|
|
283
|
+
left -= kept ?? 0;
|
|
284
|
+
return kept;
|
|
285
|
+
};
|
|
286
|
+
const value = fit(track.value) ?? 0;
|
|
287
|
+
const [held, marked, coming] = [fit(track.held), fit(track.marked), fit(track.coming)];
|
|
288
|
+
return { ...track, value, ...(held === undefined ? {} : { held }), ...(marked === undefined ? {} : { marked }), ...(coming === undefined ? {} : { coming }) };
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* What one widget of an extension shows now, for the card of a question (D-507): read as the dashboard reads it, in the campaign's language; nothing when its
|
|
292
|
+
* hook is not there, fails, gives what no part draws, or is late.
|
|
293
|
+
*/
|
|
294
|
+
export async function widgetCard(root, context, extension, name, key) {
|
|
295
|
+
try {
|
|
296
|
+
const hook = (await extensionCode(extension))?.widgets?.[name];
|
|
297
|
+
if (hook === undefined) {
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
let timer;
|
|
301
|
+
const late = new Promise((done) => {
|
|
302
|
+
timer = setTimeout(() => done("late"), hookTime).unref();
|
|
303
|
+
});
|
|
304
|
+
// Its own call for its key, never one at work for the column or another card.
|
|
305
|
+
const answer = await Promise.race([hookCall(root, extension, `${name}#${key}`, () => hook(gameViewOf(root, context, extension), { key })), late]).finally(() => clearTimeout(timer));
|
|
306
|
+
return answer === "late" || answer === undefined ? undefined : shownContent(extension, wordsOf(extension, readTextSetting(root, "language")).read, answer);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
264
312
|
/** A picture of the extension's images/, as the dashboard serves it at an address of the extension's own; it throws when the extension has none by that name. */
|
|
265
313
|
function pictureOf(extension, name) {
|
|
266
314
|
const file = findImage([extension.folder], name);
|
|
@@ -2,16 +2,28 @@ import { type CountedByGame } from "../campaign/index.ts";
|
|
|
2
2
|
import type { Context } from "../core/context.ts";
|
|
3
3
|
import type { Problem } from "../core/problem.ts";
|
|
4
4
|
import { type PageType, type World } from "../pages/index.ts";
|
|
5
|
-
import { type GameSystem, type SystemSetting, type SystemWords } from "../systems/index.ts";
|
|
5
|
+
import { type CampaignData, type GameSystem, type SystemLayer, type SystemSetting, type SystemWords } from "../systems/index.ts";
|
|
6
6
|
/** The world of the campaign in `root`, with the pages of its game system when campaign.yaml names one that is installed (SYS-1, SYS-3). */
|
|
7
7
|
export declare function campaignWorld(root: string, context: Context): World;
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The game system's words in the campaign's language (D-403): its own, else those translated on the fly on this computer (D-406); with the names of the
|
|
10
|
+
* fields the campaign's extensions add, from their words, layered as their additions are (CAM-2, D-505).
|
|
11
|
+
*/
|
|
12
|
+
export declare function systemWordsOf(root: string, system: GameSystem, language: string, context: Context): SystemWords | undefined;
|
|
10
13
|
/**
|
|
11
14
|
* The installed game system that campaign.yaml names, with what the campaign adds to it (CAM-1, CAM-2): alone when the additions do not read (CAM-3). Undefined
|
|
12
15
|
* when it names none, or one that is not installed (SYS-1).
|
|
13
16
|
*/
|
|
14
17
|
export declare function campaignSystem(root: string, context: Context): GameSystem | undefined;
|
|
18
|
+
/** What the campaign's extensions add to its game system, in the order campaign.yaml lists them, then the campaign's own additions, which win (CAM-2). */
|
|
19
|
+
export declare function systemLayers(root: string, context: Context, campaign: CampaignData): SystemLayer[];
|
|
20
|
+
/** The layers over the campaign's system that it plays with, in order, and by name those left out because the system would not read with them (CAM-3). */
|
|
21
|
+
export declare function playedLayers(root: string, context: Context): {
|
|
22
|
+
kept: SystemLayer[];
|
|
23
|
+
refused: string[];
|
|
24
|
+
};
|
|
25
|
+
/** The name of an extension's layer of additions, as messages give it. */
|
|
26
|
+
export declare function extensionLayer(name: string): string;
|
|
15
27
|
/** A setting the campaign's game system adds to the setup (DAT-10), by its name in any case; undefined when the system has none by that name. */
|
|
16
28
|
export declare function systemSetting(root: string, context: Context, name: string): SystemSetting | undefined;
|
|
17
29
|
/** The value campaign.yaml gives a setting of the system, when it is one the system has (DAT-10). */
|