rpg-engine 0.5.0 → 0.6.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/dist/campaign/settings.d.ts +1 -0
- package/dist/campaign/settings.js +6 -5
- package/dist/command-line.js +12 -5
- package/dist/commands/advance.js +1 -1
- package/dist/commands/ask.d.ts +1 -1
- package/dist/commands/ask.js +66 -24
- package/dist/commands/change.js +28 -17
- package/dist/commands/config.d.ts +1 -1
- package/dist/commands/config.js +67 -21
- package/dist/commands/effect.js +42 -23
- package/dist/commands/find.js +1 -1
- package/dist/commands/game-adventures.js +2 -2
- package/dist/commands/game-catalog.d.ts +13 -0
- package/dist/commands/game-catalog.js +26 -2
- package/dist/commands/game-checks.js +23 -21
- package/dist/commands/{game-hit-dice.d.ts → game-dice-pool.d.ts} +3 -3
- package/dist/commands/{game-hit-dice.js → game-dice-pool.js} +7 -7
- package/dist/commands/game-effects.d.ts +12 -4
- package/dist/commands/game-effects.js +21 -13
- package/dist/commands/game-examples.d.ts +22 -0
- package/dist/commands/game-examples.js +72 -0
- package/dist/commands/game-extension-list.d.ts +31 -3
- package/dist/commands/game-extension-list.js +83 -7
- package/dist/commands/game-images.d.ts +1 -1
- package/dist/commands/game-images.js +3 -2
- package/dist/commands/game-library.d.ts +3 -0
- package/dist/commands/game-library.js +2 -2
- package/dist/commands/game-mode.js +1 -1
- package/dist/commands/game-package-code.d.ts +87 -0
- package/dist/commands/game-package-code.js +648 -0
- package/dist/commands/game-package-hooks.d.ts +37 -0
- package/dist/commands/game-package-hooks.js +116 -0
- package/dist/commands/game-recipes.js +2 -2
- package/dist/commands/game-reminders.d.ts +5 -2
- package/dist/commands/game-reminders.js +7 -4
- package/dist/commands/game-rules.d.ts +4 -1
- package/dist/commands/game-rules.js +7 -1
- package/dist/commands/game-upkeep.js +3 -3
- package/dist/commands/game-view-place.d.ts +8 -20
- package/dist/commands/game-view-place.js +16 -35
- package/dist/commands/game-widgets.d.ts +39 -10
- package/dist/commands/game-widgets.js +138 -35
- package/dist/commands/game-world.d.ts +17 -0
- package/dist/commands/game-world.js +70 -12
- package/dist/commands/image.js +1 -1
- package/dist/commands/init.js +2 -2
- package/dist/commands/mode.d.ts +1 -1
- package/dist/commands/package.d.ts +8 -0
- package/dist/commands/package.js +81 -24
- package/dist/commands/progression.d.ts +2 -0
- package/dist/commands/progression.js +7 -2
- package/dist/commands/roll.d.ts +2 -0
- package/dist/commands/roll.js +50 -24
- package/dist/commands/rules.js +6 -4
- package/dist/commands/scene.d.ts +1 -1
- package/dist/commands/scene.js +13 -10
- package/dist/commands/sheet.js +10 -1
- package/dist/commands/status.d.ts +1 -0
- package/dist/commands/status.js +14 -4
- package/dist/commands/trade.js +6 -4
- package/dist/commands/translate.js +2 -2
- package/dist/commands/travel.js +1 -1
- package/dist/commands/validate.js +2 -2
- package/dist/commands/view.d.ts +8 -4
- package/dist/commands/view.js +7 -6
- package/dist/commands/worked-out.js +11 -5
- package/dist/commands/write.d.ts +8 -1
- package/dist/commands/write.js +84 -25
- package/dist/core/command-reference.js +3 -2
- package/dist/core/consents.d.ts +18 -0
- package/dist/core/consents.js +93 -0
- package/dist/core/context.d.ts +51 -29
- package/dist/core/context.js +3 -1
- package/dist/core/examples.d.ts +25 -0
- package/dist/core/examples.js +19 -0
- package/dist/{extensions/api.d.ts → core/package-api.d.ts} +134 -32
- package/dist/core/package-declarations.d.ts +102 -0
- package/dist/core/package-declarations.js +81 -0
- package/dist/core/registry.d.ts +2 -0
- package/dist/core/text.d.ts +5 -0
- package/dist/core/text.js +12 -0
- package/dist/dashboard/acts.js +31 -24
- package/dist/dashboard/address.js +4 -1
- package/dist/dashboard/api.js +54 -0
- package/dist/dashboard/app.js +140 -96
- package/dist/dashboard/ask-card.js +76 -25
- package/dist/dashboard/asked-dice.js +57 -0
- package/dist/dashboard/dice/tray.js +3 -3
- package/dist/dashboard/extension-widgets.js +104 -10
- package/dist/dashboard/i18n/ar.json +25 -29
- package/dist/dashboard/i18n/cs.json +25 -29
- package/dist/dashboard/i18n/da.json +25 -29
- package/dist/dashboard/i18n/de.json +25 -29
- package/dist/dashboard/i18n/el.json +25 -29
- package/dist/dashboard/i18n/es.json +25 -29
- package/dist/dashboard/i18n/fi.json +25 -29
- package/dist/dashboard/i18n/fr.json +25 -29
- package/dist/dashboard/i18n/he.json +25 -29
- package/dist/dashboard/i18n/hi.json +25 -29
- package/dist/dashboard/i18n/hu.json +25 -29
- package/dist/dashboard/i18n/is.json +25 -29
- package/dist/dashboard/i18n/it.json +25 -29
- package/dist/dashboard/i18n/ja.json +25 -29
- package/dist/dashboard/i18n/ko.json +25 -29
- package/dist/dashboard/i18n/nb.json +25 -29
- package/dist/dashboard/i18n/nl.json +25 -29
- package/dist/dashboard/i18n/pl.json +25 -29
- package/dist/dashboard/i18n/pt.json +25 -29
- package/dist/dashboard/i18n/ro.json +25 -29
- package/dist/dashboard/i18n/ru.json +25 -29
- package/dist/dashboard/i18n/sv.json +25 -29
- package/dist/dashboard/i18n/tr.json +25 -29
- package/dist/dashboard/i18n/uk.json +25 -29
- package/dist/dashboard/i18n/zh.json +25 -29
- package/dist/dashboard/icons.js +4 -1
- package/dist/dashboard/kit.js +35 -0
- package/dist/dashboard/labels.js +1 -0
- package/dist/dashboard/pack-card.js +7 -2
- package/dist/dashboard/package-page.js +50 -0
- package/dist/dashboard/packages.js +172 -23
- package/dist/dashboard/page-code.js +248 -0
- package/dist/dashboard/party.js +13 -14
- package/dist/dashboard/people.js +4 -0
- package/dist/dashboard/person-panel.js +110 -0
- package/dist/dashboard/place-map-view.js +11 -6
- package/dist/dashboard/scene.js +12 -9
- package/dist/dashboard/settings.js +55 -9
- package/dist/dashboard/sheet.js +21 -13
- package/dist/dashboard/status.js +69 -15
- package/dist/dashboard/style.css +592 -825
- package/dist/dashboard/top.js +75 -25
- package/dist/dashboard/turns.js +34 -13
- package/dist/dashboard/widgets.js +9 -2
- package/dist/dashboard-server.js +189 -10
- package/dist/dice/roll-dice.js +1 -1
- package/dist/effects/effect.d.ts +7 -4
- package/dist/effects/effect.js +12 -9
- package/dist/extensions/engine.d.ts +4 -0
- package/dist/extensions/engine.js +74 -0
- package/dist/extensions/extension.d.ts +25 -37
- package/dist/extensions/extension.js +26 -48
- package/dist/extensions/index.d.ts +3 -2
- package/dist/extensions/index.js +4 -1
- package/dist/extensions/packages.d.ts +6 -0
- package/dist/extensions/packages.js +100 -11
- package/dist/index.d.ts +1 -3
- package/dist/mcp-server.js +51 -14
- package/dist/page-words.d.ts +4 -0
- package/dist/page-words.js +4 -4
- package/dist/page.d.ts +218 -0
- package/dist/page.js +2 -0
- package/dist/pages/load.d.ts +7 -1
- package/dist/pages/load.js +10 -5
- package/dist/pages/page-file.js +1 -1
- package/dist/pages/stack-writes.js +5 -4
- package/dist/pages/world.d.ts +8 -0
- package/dist/pages/world.js +9 -2
- package/dist/systems/game-system.d.ts +52 -26
- package/dist/systems/game-system.js +141 -42
- package/dist/systems/index.d.ts +3 -3
- package/dist/systems/index.js +3 -3
- package/dist/systems/system-code.d.ts +29 -88
- package/dist/systems/system-code.js +68 -23
- package/dist/systems/words.d.ts +2 -0
- package/dist/systems/words.js +11 -0
- package/dist/testing.d.ts +20 -5
- package/dist/testing.js +49 -14
- package/instructions/combat.md +2 -2
- package/instructions/core.md +2 -2
- package/instructions/dashboard.md +1 -1
- package/instructions/mentor-tutorial.md +1 -1
- package/instructions/play.md +2 -2
- package/instructions/setup.md +1 -1
- package/package.json +9 -1
- package/systems/5e/LICENSE.md +13 -3
- package/systems/5e/dist/dying.js +83 -16
- package/systems/5e/dist/index.js +2 -2
- package/systems/5e/dist/page-style.js +634 -0
- package/systems/5e/dist/page.js +166 -0
- package/systems/5e/dist/shown.js +23 -0
- package/systems/5e/instructions/combat.md +1 -0
- package/systems/5e/package.json +4 -2
- package/systems/5e/system.yaml +35 -7
- package/systems/5e/versions/srd-5.1/system.yaml +2 -0
- package/systems/5e/words/ar.yaml +30 -0
- package/systems/5e/words/cs.yaml +30 -0
- package/systems/5e/words/da.yaml +30 -0
- package/systems/5e/words/de.yaml +30 -0
- package/systems/5e/words/el.yaml +30 -0
- package/systems/5e/words/es.yaml +30 -0
- package/systems/5e/words/fi.yaml +30 -0
- package/systems/5e/words/fr.yaml +30 -0
- package/systems/5e/words/he.yaml +30 -0
- package/systems/5e/words/hi.yaml +30 -0
- package/systems/5e/words/hu.yaml +30 -0
- package/systems/5e/words/is.yaml +30 -0
- package/systems/5e/words/it.yaml +30 -0
- package/systems/5e/words/ja.yaml +30 -0
- package/systems/5e/words/ko.yaml +30 -0
- package/systems/5e/words/nb.yaml +30 -0
- package/systems/5e/words/nl.yaml +30 -0
- package/systems/5e/words/pl.yaml +30 -0
- package/systems/5e/words/pt.yaml +30 -0
- package/systems/5e/words/ro.yaml +30 -0
- package/systems/5e/words/ru.yaml +30 -0
- package/systems/5e/words/sv.yaml +30 -0
- package/systems/5e/words/tr.yaml +30 -0
- package/systems/5e/words/uk.yaml +30 -0
- package/systems/5e/words/zh.yaml +30 -0
- package/dist/commands/game-extensions.d.ts +0 -34
- package/dist/commands/game-extensions.js +0 -407
- package/dist/commands/game-system-code.d.ts +0 -33
- package/dist/commands/game-system-code.js +0 -188
- package/dist/dashboard/death-save.js +0 -67
- package/dist/dashboard/dying.js +0 -46
- package/dist/dashboard/save-outcome.js +0 -42
- package/dist/dashboard/spend.js +0 -115
- /package/dist/{extensions/api.js → core/package-api.js} +0 -0
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
// The code of the campaign's packages, its game system's and its extensions', with one contract (D-63, D-428, D-436, D-514): their commands, what they do
|
|
10
|
+
// when time passes, and the game they reach it through.
|
|
11
|
+
import { join, resolve } from "node:path";
|
|
12
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
13
|
+
import { parse, stringify } from "yaml";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
import { campaignFileName, findCampaign, readTextSetting, requireCampaign } from "../campaign/index.js";
|
|
16
|
+
import { engineSystemsFolder } from "../core/context.js";
|
|
17
|
+
import { installedFromFolder, realPath } from "../core/files.js";
|
|
18
|
+
import { parseFormula } from "../core/formula.js";
|
|
19
|
+
import { appendLogEntry } from "../core/log.js";
|
|
20
|
+
import { defineCommand } from "../core/registry.js";
|
|
21
|
+
import { files, isInside } from "../core/store.js";
|
|
22
|
+
import { sameName, sameWord, valueNamed } from "../core/text.js";
|
|
23
|
+
import { getVersion } from "../core/version.js";
|
|
24
|
+
import { readCounter } from "../counters/index.js";
|
|
25
|
+
import { rollDice } from "../dice/index.js";
|
|
26
|
+
import { withEffect } from "../effects/index.js";
|
|
27
|
+
import { wordIn } from "../extensions/index.js";
|
|
28
|
+
import { concernsPlayer, environmentAt, isInParty, isPartyMember, isPlayerCharacter, locate, pagesWithNameOrAlias, playerCharacters } from "../pages/index.js";
|
|
29
|
+
import { givenCode, loadSystemCode, systemHasCode, systemPackage, systemPageModule } from "../systems/index.js";
|
|
30
|
+
import { formatGameDate } from "../time/index.js";
|
|
31
|
+
import { askedRoll, isCheckDice, tonesOf } from "./ask.js";
|
|
32
|
+
import { change } from "./change.js";
|
|
33
|
+
import { effectiveFields, effectsOf } from "./effect-layer.js";
|
|
34
|
+
import { checkEffectsReadable, saveEffects } from "./game-effects.js";
|
|
35
|
+
import { campaignExtensions, packageWords, runsCode } from "./game-extension-list.js";
|
|
36
|
+
import { behindTheScreen, sealHiddenRoll } from "./game-master-rolls.js";
|
|
37
|
+
import { keepingConditionNote } from "./game-reminders.js";
|
|
38
|
+
import { readOpenScene } from "./game-scene.js";
|
|
39
|
+
import { calendarDayOf, newDatedLogEntry, presentDate } from "./game-time.js";
|
|
40
|
+
import { widgetCard } from "./game-widgets.js";
|
|
41
|
+
import { campaignSystem, campaignSystemWords, campaignWorld } from "./game-world.js";
|
|
42
|
+
import { formulaValue } from "./worked-out.js";
|
|
43
|
+
import { writePackageFields } from "./write.js";
|
|
44
|
+
/** The folder of the campaign where a package keeps its own files, its state among them (D-433, D-436): an extension's, or its game system's. */
|
|
45
|
+
export const extensionsFolderName = "extensions";
|
|
46
|
+
/** The longest description a command of a package may give the master, in words (D-441): it is read in every chat. */
|
|
47
|
+
const descriptionWords = 150;
|
|
48
|
+
/** The packages that bring code to the campaign (D-514): its game system first, then its extensions in the order campaign.yaml lists them. */
|
|
49
|
+
export function campaignPackages(root, context) {
|
|
50
|
+
const system = campaignSystemPackage(root, context);
|
|
51
|
+
return [...(system === undefined ? [] : [system]), ...campaignExtensions(root, context).extensions];
|
|
52
|
+
}
|
|
53
|
+
/** The campaign's game system as a package of the campaign (D-514): its package.json's name and version, or, for one that is data alone, its name and the engine's version. */
|
|
54
|
+
export function campaignSystemPackage(root, context) {
|
|
55
|
+
const system = campaignSystem(root, context);
|
|
56
|
+
return system === undefined ? undefined : systemCodePackage(system);
|
|
57
|
+
}
|
|
58
|
+
/** A package as messages name it: the game system 5e, the extension weather. */
|
|
59
|
+
export function packageLabel(code) {
|
|
60
|
+
return `${code.kind === "system" ? "game system" : "extension"} ${code.name}`;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The commands of the campaign's packages whose code may run (D-63, D-428, D-441), as commands of rpg-engine: each runs with the game of D-436 and gives the
|
|
64
|
+
* master its text; by name, the package and version that give it, so that a server knows when another takes its place.
|
|
65
|
+
*/
|
|
66
|
+
export async function packageCommands(root, context, taken) {
|
|
67
|
+
const commands = [];
|
|
68
|
+
const sources = {};
|
|
69
|
+
const problems = [];
|
|
70
|
+
for (const code of campaignPackages(root, context)) {
|
|
71
|
+
try {
|
|
72
|
+
const own = [];
|
|
73
|
+
const given = await packageCode(code, context);
|
|
74
|
+
for (const command of given?.commands ?? []) {
|
|
75
|
+
own.push(asCommand(code, command, [...taken, ...commands.map((other) => other.name), ...own.map((other) => other.name)]));
|
|
76
|
+
}
|
|
77
|
+
commands.push(...own);
|
|
78
|
+
own.forEach((command) => (sources[command.name] = `${code.package}@${code.version}#${installs}`));
|
|
79
|
+
refused.delete(codeKey(code));
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
problems.push(codeProblem(code, error));
|
|
83
|
+
refused.set(codeKey(code), firstLine(error));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return { commands, sources, problems };
|
|
87
|
+
}
|
|
88
|
+
/** The names of a package's commands, its code loaded and checked as the campaign would use it; it throws what keeps it from being used. */
|
|
89
|
+
export async function packageCommandNames(code, context, taken) {
|
|
90
|
+
const names = [];
|
|
91
|
+
for (const command of (await packageCode(code, context))?.commands ?? []) {
|
|
92
|
+
names.push(asCommand(code, command, [...taken, ...names]).name);
|
|
93
|
+
}
|
|
94
|
+
return names;
|
|
95
|
+
}
|
|
96
|
+
/** Tells the loader npm has just installed: code installed again at the same version is read anew. */
|
|
97
|
+
export function packagesInstalled() {
|
|
98
|
+
installs += 1;
|
|
99
|
+
}
|
|
100
|
+
/** What keeps the code of the campaign's packages from being used, for validate and status: it loads them. */
|
|
101
|
+
export async function packageCodeProblems(root, context, taken) {
|
|
102
|
+
return (await packageCommands(root, context, taken)).problems;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* What the campaign's packages say after time passed from one date to another, or as the game starts, when both are the same (D-436). The time has passed
|
|
106
|
+
* whatever they do: one that fails says so in its line, never in place of the output.
|
|
107
|
+
*/
|
|
108
|
+
export async function timePassedLines(root, context, from, to) {
|
|
109
|
+
const lines = [];
|
|
110
|
+
for (const code of campaignPackages(root, context)) {
|
|
111
|
+
try {
|
|
112
|
+
const given = await packageCode(code, context);
|
|
113
|
+
if (given?.timePassed !== undefined) {
|
|
114
|
+
lines.push(...cappedLines(code, await given.timePassed({ from: momentOf(root, context, from), to: momentOf(root, context, to) }, gameOf(root, context, code))));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
// One line: the master reads it among others.
|
|
119
|
+
lines.push(`The ${packageLabel(code)} failed after the time passed: ${firstLine(error)}. The time did pass: do not advance again; validate says how to fix it.`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return lines;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The code of a package, when it may run (D-441, D-514): an extension's module's default export, given the kit, loaded as it is first needed; a game system's
|
|
126
|
+
* as the engine loaded it when it started. It throws why a system's code is not there.
|
|
127
|
+
*/
|
|
128
|
+
export async function packageCode(code, context) {
|
|
129
|
+
if (isExtension(code)) {
|
|
130
|
+
return await extensionCode(code);
|
|
131
|
+
}
|
|
132
|
+
const loaded = context.systemCode?.[code.name];
|
|
133
|
+
// What broke it, without the fix the master reads after a change: the problem says its own.
|
|
134
|
+
if (loaded !== undefined && "problem" in loaded) {
|
|
135
|
+
throw new Error(loaded.problem);
|
|
136
|
+
}
|
|
137
|
+
const state = systemCode(code, context);
|
|
138
|
+
if (typeof state === "string") {
|
|
139
|
+
throw new Error(`it ${state}`);
|
|
140
|
+
}
|
|
141
|
+
return state;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* A package's code as loaded already, for the hooks the commands call at once (D-448, D-514): its code, or why it is not there; undefined where it brings none
|
|
145
|
+
* or does not run. An extension's is there once its import has ended, as an entry starts or as its commands were read; one whose commands are refused is not.
|
|
146
|
+
*/
|
|
147
|
+
export function loadedCode(code, context) {
|
|
148
|
+
if (!isExtension(code)) {
|
|
149
|
+
return systemCode(code, context);
|
|
150
|
+
}
|
|
151
|
+
const url = extensionAddress(code);
|
|
152
|
+
const left = refused.get(codeKey(code));
|
|
153
|
+
if (url === undefined) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
return left !== undefined ? `is left out (${left}): validate says how to fix it` : ready.get(url) ?? "is not loaded yet: start the MCP server again (/mcp in Claude Code), or open a new chat";
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Loads the code of the campaign's packages, as an entry that calls their hooks starts (D-514), and again after package installs one: their hooks are called at
|
|
160
|
+
* once, never waiting for an import. What keeps one from being used is kept for its hooks, and left to validate.
|
|
161
|
+
*/
|
|
162
|
+
export async function loadCampaignCode(context) {
|
|
163
|
+
try {
|
|
164
|
+
const root = findCampaign(context.folder);
|
|
165
|
+
if (root !== undefined) {
|
|
166
|
+
await packageCommands(root, context, []);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
// A campaign that does not read loads nothing: the command says why.
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* The code of the campaign's game system when this process has not loaded it, as one installed in the chat that plays it (D-513 (1)): new to the process, it
|
|
175
|
+
* is imported now, with the commands of the campaign's packages; a system loaded already, refused or broken, stays as it was.
|
|
176
|
+
*/
|
|
177
|
+
export async function loadNewSystemCode(context) {
|
|
178
|
+
let system;
|
|
179
|
+
// A campaign or a system that does not read loads nothing: the commands say why.
|
|
180
|
+
try {
|
|
181
|
+
const root = findCampaign(context.folder);
|
|
182
|
+
system = root === undefined ? undefined : campaignSystem(root, context);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (system === undefined || context.systemCode === undefined || context.systemCode[system.name] !== undefined || !systemHasCode(system.folder)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const loaded = (await loadSystemCode(context.systemFolders))[system.name];
|
|
191
|
+
if (loaded !== undefined) {
|
|
192
|
+
context.systemCode[system.name] = loaded;
|
|
193
|
+
await loadCampaignCode(context);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/** The most a package says in one call, after the engine's writes or as time passes, in lines and in characters; what is more is cut, with a line. */
|
|
197
|
+
const mostLines = 5;
|
|
198
|
+
const mostCharacters = 600;
|
|
199
|
+
/** A package's lines for the master, cut at the most it may say in one call, with a line that says so. */
|
|
200
|
+
export function cappedLines(code, lines) {
|
|
201
|
+
const kept = [];
|
|
202
|
+
let length = 0;
|
|
203
|
+
for (const line of lines) {
|
|
204
|
+
if (kept.length === mostLines || length + line.length > mostCharacters) {
|
|
205
|
+
return [...kept, `The ${packageLabel(code)} said more: cut.`];
|
|
206
|
+
}
|
|
207
|
+
kept.push(line);
|
|
208
|
+
length += line.length;
|
|
209
|
+
}
|
|
210
|
+
return kept;
|
|
211
|
+
}
|
|
212
|
+
/** A package that is an extension. */
|
|
213
|
+
export function isExtension(code) {
|
|
214
|
+
return code.kind === "extension";
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The game as a package's code reaches it (D-436, D-505, D-514): what a widget reads, the dice, asking the player, its own state, the fields it may write and
|
|
218
|
+
* the log; nothing else is written. A command's or time's game writes nothing once the call is stopped (ASK-9); a hook's finishes what the engine began.
|
|
219
|
+
*/
|
|
220
|
+
export function gameOf(root, context, code, options = {}) {
|
|
221
|
+
const view = gameViewOf(root, context, code);
|
|
222
|
+
const asked = options.asked ?? {};
|
|
223
|
+
const live = () => {
|
|
224
|
+
if (options.hook !== true && context.signal?.aborted === true) {
|
|
225
|
+
throw new Error("The call was stopped: nothing more is written.");
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return {
|
|
229
|
+
...view,
|
|
230
|
+
roll: (notation, by) => {
|
|
231
|
+
if (by !== undefined) {
|
|
232
|
+
live();
|
|
233
|
+
return rollFor(root, context, notation, by, (text) => packageText(root, code, text));
|
|
234
|
+
}
|
|
235
|
+
const rolled = rollDice(notation, { random: context.random });
|
|
236
|
+
return { notation: rolled.notation, total: rolled.total, faces: rolled.faces, dice: rolled.dice, text: rolled.text };
|
|
237
|
+
},
|
|
238
|
+
set: (page, values, why) => {
|
|
239
|
+
live();
|
|
240
|
+
setPackageFields(root, context, code, page, values, why);
|
|
241
|
+
},
|
|
242
|
+
addEffect: (page, effect, why) => {
|
|
243
|
+
live();
|
|
244
|
+
return addEffect(root, context, page, effect, why);
|
|
245
|
+
},
|
|
246
|
+
endEffect: (page, effect, why) => {
|
|
247
|
+
live();
|
|
248
|
+
return endEffect(root, context, page, effect, why);
|
|
249
|
+
},
|
|
250
|
+
change: (page, field, amount, why) => {
|
|
251
|
+
live();
|
|
252
|
+
const result = change.run(change.input.parse({ page, field, ...amount, why }), context);
|
|
253
|
+
// The change of a counter is made at once; only a rest's dice wait, which change is not given here.
|
|
254
|
+
if (result instanceof Promise) {
|
|
255
|
+
throw new Error("game.change changes a counter or money at once: give it by, to or spend, never a recipe. Nothing changed.");
|
|
256
|
+
}
|
|
257
|
+
return change.format(result);
|
|
258
|
+
},
|
|
259
|
+
ask: (wanted) => {
|
|
260
|
+
// Refused at once, never as a promise a hook lets go.
|
|
261
|
+
if (options.hook === true) {
|
|
262
|
+
throw new Error("ask waits for the player, and a hook is called at once, never waiting: ask from a command of the package instead.");
|
|
263
|
+
}
|
|
264
|
+
return (async () => {
|
|
265
|
+
live();
|
|
266
|
+
const { who, question, check, notation } = wanted;
|
|
267
|
+
const told = toldOf(root, context, code, wanted, who);
|
|
268
|
+
asked.card = told.card ?? asked.card;
|
|
269
|
+
return askedOf(await askedRoll(root, context, askedQuestion(packageText(root, code, question), check, notation), [{ label: check.trim(), notation }], who, told));
|
|
270
|
+
})();
|
|
271
|
+
},
|
|
272
|
+
state: {
|
|
273
|
+
read: view.state.read,
|
|
274
|
+
write: (value) => {
|
|
275
|
+
live();
|
|
276
|
+
files().write(statePath(root, code), `# The state of the ${code.kind === "system" ? "game system" : "extension"} ${code.package}, kept by its code.\n${stringify(value, { lineWidth: 0 })}`);
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
log: (text, visibility = "hidden") => {
|
|
280
|
+
live();
|
|
281
|
+
// An extension's line as it always was; a system's of its own kind.
|
|
282
|
+
const entry = code.kind === "system" ? newDatedLogEntry(root, context, "system", visibility, { system: code.name, text }) : newDatedLogEntry(root, context, "extension", visibility, { extension: code.name, text });
|
|
283
|
+
appendLogEntry(root, entry);
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* The game as a widget of a package reads it (D-502, D-505): the time, where the party is and who is in it, a page, the scene, the campaign's settings and the
|
|
289
|
+
* package's state.
|
|
290
|
+
*/
|
|
291
|
+
export function gameViewOf(root, context, code) {
|
|
292
|
+
return {
|
|
293
|
+
now: () => {
|
|
294
|
+
const now = presentDate(root);
|
|
295
|
+
return now === undefined ? undefined : momentOf(root, context, now);
|
|
296
|
+
},
|
|
297
|
+
calendarDay: (day) => calendarDayOf(root, context, day),
|
|
298
|
+
units: () => (readTextSetting(root, "units") === "imperial" ? "imperial" : "metric"),
|
|
299
|
+
party: () => {
|
|
300
|
+
const world = campaignWorld(root, context);
|
|
301
|
+
const player = playerCharacters(world).find(isInParty);
|
|
302
|
+
return player === undefined
|
|
303
|
+
? undefined
|
|
304
|
+
: {
|
|
305
|
+
places: locate(world, player).chain.filter((page) => page.type === "place").map((page) => page.name),
|
|
306
|
+
environment: Object.fromEntries(environmentAt(world, player).map(({ entry }) => [entry.name, entry.value])),
|
|
307
|
+
members: world.pages.filter(isInParty).map((page) => ({ name: page.name, pc: isPlayerCharacter(page) })),
|
|
308
|
+
};
|
|
309
|
+
},
|
|
310
|
+
page: (name) => {
|
|
311
|
+
const world = campaignWorld(root, context);
|
|
312
|
+
const found = pagesWithNameOrAlias(world, name);
|
|
313
|
+
return found.length === 1 ? gamePage(world, found[0], campaignSystemWords(root, context)) : undefined;
|
|
314
|
+
},
|
|
315
|
+
scene: () => {
|
|
316
|
+
const scene = readOpenScene(root).scene;
|
|
317
|
+
return scene === undefined ? undefined : {
|
|
318
|
+
title: scene.title,
|
|
319
|
+
...(scene.opened === undefined ? {} : { opened: scene.opened }),
|
|
320
|
+
...(scene.round === undefined ? {} : { round: scene.round }),
|
|
321
|
+
...(scene.turn === undefined ? {} : { turn: scene.turn }),
|
|
322
|
+
};
|
|
323
|
+
},
|
|
324
|
+
setting: (name) => readTextSetting(root, name),
|
|
325
|
+
// A name in the campaign's language is the system's by its words, Prono its Prone (D-403).
|
|
326
|
+
sameName: (one, other) => sameWord(one, other, campaignSystemWords(root, context)),
|
|
327
|
+
state: { read: () => parse(files().text(statePath(root, code)) ?? "null") },
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* How a package's code words a roll it asks (D-507): its buttons and its rule translated, what its faces mean and how they turned out, and the card of its
|
|
332
|
+
* widget, read for its key and whose roll it is, refused when the package has no widget by that name.
|
|
333
|
+
*/
|
|
334
|
+
export function toldOf(root, context, code, wanted, who) {
|
|
335
|
+
const words = (text) => packageText(root, code, text);
|
|
336
|
+
const button = (given) => (typeof given === "object" && "label" in given ? { name: words(given.label), note: words(given.note) } : { name: words(given) });
|
|
337
|
+
const { roll, decline, rule, card, says, tones } = wanted;
|
|
338
|
+
if (card !== undefined && !code.widgets.some((widget) => widget.name === card.widget)) {
|
|
339
|
+
const its = code.widgets.length === 0 ? "it declares none" : `its own are ${code.widgets.map((widget) => widget.name).join(", ")}`;
|
|
340
|
+
throw new Error(`The ${packageLabel(code)} asks in the card of a widget ${card.widget} it does not have: ${its}. Nothing was asked.`);
|
|
341
|
+
}
|
|
342
|
+
return {
|
|
343
|
+
...(roll === undefined ? {} : { roll: button(roll) }),
|
|
344
|
+
...(decline === undefined ? {} : { decline: decline === false ? false : button(decline) }),
|
|
345
|
+
...(rule === undefined ? {} : { rule: words(rule) }),
|
|
346
|
+
...(card === undefined ? {} : { card: { widget: `${code.name}.${card.widget}`, key: card.key, shows: () => widgetCard(root, context, code, card.widget, card.key, who) } }),
|
|
347
|
+
...(says === undefined ? {} : { says: (faces) => words(says(faces)) }),
|
|
348
|
+
...(tones === undefined ? {} : { tones }),
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
/** The first line of what went wrong, without its last stop, for a line among others. */
|
|
352
|
+
export function firstLine(error) {
|
|
353
|
+
return (error instanceof Error ? error.message : String(error)).split("\n")[0].replace(/[:.]$/, "");
|
|
354
|
+
}
|
|
355
|
+
function systemCodePackage(system) {
|
|
356
|
+
const own = systemPackage(system.folder);
|
|
357
|
+
const page = systemPageModule(system.folder);
|
|
358
|
+
return {
|
|
359
|
+
kind: "system",
|
|
360
|
+
package: own === undefined || own.package === "" ? system.name : own.package,
|
|
361
|
+
name: system.name,
|
|
362
|
+
version: own?.version ?? getVersion(),
|
|
363
|
+
folder: system.folder,
|
|
364
|
+
title: system.title,
|
|
365
|
+
language: system.language,
|
|
366
|
+
icons: system.icons,
|
|
367
|
+
widgets: system.widgets,
|
|
368
|
+
figures: system.figures,
|
|
369
|
+
...(page === undefined ? {} : { page: page.file }),
|
|
370
|
+
linked: installedFromFolder(system.folder),
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/** A command of a package as a command of rpg-engine, refused when its name is not the package's or is taken, or its description is too long. */
|
|
374
|
+
function asCommand(code, command, taken) {
|
|
375
|
+
const name = command.name.trim();
|
|
376
|
+
const words = command.description.split(/\s+/).length;
|
|
377
|
+
if (name !== code.name && !name.startsWith(`${code.name}-`)) {
|
|
378
|
+
throw new Error(`its command ${name} must be named ${code.name}, or start with ${code.name}-`);
|
|
379
|
+
}
|
|
380
|
+
if (taken.includes(name)) {
|
|
381
|
+
throw new Error(`its command ${name} has the name of another command`);
|
|
382
|
+
}
|
|
383
|
+
if (words > descriptionWords) {
|
|
384
|
+
throw new Error(`its command ${name} has a description of ${words} words, and ${descriptionWords} at most are read`);
|
|
385
|
+
}
|
|
386
|
+
return defineCommand({
|
|
387
|
+
name,
|
|
388
|
+
description: command.description,
|
|
389
|
+
group: "game",
|
|
390
|
+
input: command.input,
|
|
391
|
+
readOnly: command.readOnly,
|
|
392
|
+
positionals: command.positionals,
|
|
393
|
+
waitsForPlayer: command.needsPlayer,
|
|
394
|
+
forPlayer: command.player === true,
|
|
395
|
+
async run(input, called) {
|
|
396
|
+
const campaign = requireCampaign(called.folder);
|
|
397
|
+
const asked = {};
|
|
398
|
+
try {
|
|
399
|
+
return { text: await command.run(input, gameOf(campaign, called, code, { asked })) };
|
|
400
|
+
}
|
|
401
|
+
finally {
|
|
402
|
+
// The card of its last question, as it is after the dice, read once the call ends, however it ends (D-507).
|
|
403
|
+
if (asked.card !== undefined) {
|
|
404
|
+
called.page?.cardEnded?.(asked.card);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
},
|
|
408
|
+
format: (result) => result.text,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
// The code of each extension, by its file and version, loaded once for the process: an import is never undone, a new version is another. What each gave,
|
|
412
|
+
// once loaded, is kept apart too, for the hooks the commands call at once.
|
|
413
|
+
const loaded = new Map();
|
|
414
|
+
const ready = new Map();
|
|
415
|
+
// Why the code of a package at a version and its installs is left out, as its commands are refused: its hooks are not called.
|
|
416
|
+
const refused = new Map();
|
|
417
|
+
let installs = 0;
|
|
418
|
+
/** An extension's code, when it may run (D-441): its module's default export, given the kit; where the engine runs from its source, as in the tests, from its own. */
|
|
419
|
+
function extensionCode(extension) {
|
|
420
|
+
const url = extensionAddress(extension);
|
|
421
|
+
if (url === undefined) {
|
|
422
|
+
return Promise.resolve(undefined);
|
|
423
|
+
}
|
|
424
|
+
const code = loaded.get(url) ?? import(__rewriteRelativeImportExtension(url)).then((module) => {
|
|
425
|
+
if (typeof module.default !== "function") {
|
|
426
|
+
throw new Error(`its code, ${url.replace(/\?.*$/, "")}, has no default export that gives its commands`);
|
|
427
|
+
}
|
|
428
|
+
const given = givenCode(module.default({ z }), fileURLToPath(url.replace(/\?.*$/, "")));
|
|
429
|
+
ready.set(url, given);
|
|
430
|
+
return given;
|
|
431
|
+
});
|
|
432
|
+
loaded.set(url, code);
|
|
433
|
+
return code;
|
|
434
|
+
}
|
|
435
|
+
/** A package at its version and the installs so far, as what is kept of its code is keyed. */
|
|
436
|
+
function codeKey(code) {
|
|
437
|
+
return `${code.package}@${code.version}#${installs}`;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* A system's code as the engine loaded it, or why it is not there: not loaded, loaded at another version than the one installed now, refused or broken; undefined
|
|
441
|
+
* when it brings none.
|
|
442
|
+
*/
|
|
443
|
+
/** Whether a package's code is refused, as another's without the player's yes, one made for another API or with commands refused: said once, not at each call. */
|
|
444
|
+
export function codeRefused(code, context) {
|
|
445
|
+
const loaded = context.systemCode?.[code.name];
|
|
446
|
+
return isExtension(code) ? refused.has(codeKey(code)) : loaded !== undefined && "refused" in loaded;
|
|
447
|
+
}
|
|
448
|
+
function systemCode(code, context) {
|
|
449
|
+
if (!systemHasCode(code.folder)) {
|
|
450
|
+
return undefined;
|
|
451
|
+
}
|
|
452
|
+
const loaded = context.systemCode?.[code.name];
|
|
453
|
+
const again = "start the MCP server again (/mcp in Claude Code), or open a new chat";
|
|
454
|
+
if (loaded === undefined) {
|
|
455
|
+
return `is not loaded: ${again}`;
|
|
456
|
+
}
|
|
457
|
+
if (loaded.version !== undefined && loaded.version !== code.version) {
|
|
458
|
+
return `is loaded at version ${loaded.version}, and ${code.version} is installed now: ${again}`;
|
|
459
|
+
}
|
|
460
|
+
if ("refused" in loaded) {
|
|
461
|
+
return loaded.refused;
|
|
462
|
+
}
|
|
463
|
+
return "problem" in loaded ? `cannot load (${loaded.problem}): install rpg-engine again, or run npm run build in its repository, then start the MCP server again` : loaded.code;
|
|
464
|
+
}
|
|
465
|
+
/** The address an extension's code is imported from, for its version and the installs so far; undefined when its code does not run. */
|
|
466
|
+
function extensionAddress(extension) {
|
|
467
|
+
const source = join(extension.folder, "src", "index.ts");
|
|
468
|
+
const entry = !runsCode(extension) ? undefined : import.meta.url.endsWith(".ts") && files().exists(source) ? source : extension.code;
|
|
469
|
+
// 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.
|
|
470
|
+
return entry === undefined ? undefined : `${pathToFileURL(realPath(entry)).href}?v=${encodeURIComponent(extension.version)}&installs=${installs}`;
|
|
471
|
+
}
|
|
472
|
+
/** A game date as the API gives it: as written, its day and minute, and its day of the calendar. */
|
|
473
|
+
function momentOf(root, context, date) {
|
|
474
|
+
const calendar = calendarDayOf(root, context, date.day);
|
|
475
|
+
return { date: formatGameDate(date), day: date.day, ...(date.minute === undefined ? {} : { minute: date.minute }), ...(calendar === undefined ? {} : { calendar }) };
|
|
476
|
+
}
|
|
477
|
+
function statePath(root, code) {
|
|
478
|
+
return join(root, extensionsFolderName, code.name, "state.yaml");
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* A page as a package reads it (D-505): in the party or not, the player's or not, its fields as show gives them, effects and what the system works out included;
|
|
482
|
+
* an effect it has by the system's name or its word in the campaign's language (D-403).
|
|
483
|
+
*/
|
|
484
|
+
function gamePage(world, page, words) {
|
|
485
|
+
const fields = effectiveFields(world, page);
|
|
486
|
+
return {
|
|
487
|
+
name: page.name,
|
|
488
|
+
party: isPartyMember(page),
|
|
489
|
+
pc: isPlayerCharacter(page),
|
|
490
|
+
value: (field) => plainValue(valueNamed(fields, field)),
|
|
491
|
+
counter: (field) => {
|
|
492
|
+
const counter = readCounter(valueNamed(fields, field));
|
|
493
|
+
return counter === undefined ? undefined : { value: counter.value, ...(counter.max === undefined ? {} : { max: counter.max }) };
|
|
494
|
+
},
|
|
495
|
+
has: (effect) => effectsOf(page).some((one) => sameWord(effect, one.name, words)),
|
|
496
|
+
formula: (text) => {
|
|
497
|
+
let formula;
|
|
498
|
+
try {
|
|
499
|
+
formula = parseFormula(text);
|
|
500
|
+
}
|
|
501
|
+
catch (error) {
|
|
502
|
+
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.`);
|
|
503
|
+
}
|
|
504
|
+
return formulaValue(world, fields, formula);
|
|
505
|
+
},
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
/** A field's value as the API gives it: a number, text, or a list as text; undefined for anything else, as a map. */
|
|
509
|
+
function plainValue(value) {
|
|
510
|
+
if (typeof value === "number" || typeof value === "string") {
|
|
511
|
+
return value;
|
|
512
|
+
}
|
|
513
|
+
if (typeof value === "boolean") {
|
|
514
|
+
return String(value);
|
|
515
|
+
}
|
|
516
|
+
return Array.isArray(value) ? value.map((item) => (typeof item === "object" && item !== null ? JSON.stringify(item) : String(item))) : undefined;
|
|
517
|
+
}
|
|
518
|
+
/** The page a name, or an alias, gives alone; it throws, with nothing changed, when none or more than one has it. */
|
|
519
|
+
function onePage(world, name) {
|
|
520
|
+
const found = pagesWithNameOrAlias(world, name);
|
|
521
|
+
if (found.length !== 1) {
|
|
522
|
+
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.`);
|
|
523
|
+
}
|
|
524
|
+
return found[0];
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Sets fields on a page for a package's code, any field of any page (D-505, D-511), as write --set does: each under the name the game system with its additions
|
|
528
|
+
* gives it, whatever the case it came in.
|
|
529
|
+
*/
|
|
530
|
+
function setPackageFields(root, context, code, name, values, why) {
|
|
531
|
+
const odd = Object.entries(values).find(([, value]) => typeof value !== "string" && (typeof value !== "number" || !Number.isFinite(value)));
|
|
532
|
+
if (odd !== undefined) {
|
|
533
|
+
throw new Error(`The ${packageLabel(code)} gave ${odd[0]} the value ${String(odd[1])}: give a number or text. Nothing changed.`);
|
|
534
|
+
}
|
|
535
|
+
const world = campaignWorld(root, context);
|
|
536
|
+
const declared = Object.keys(world.system?.fields ?? {});
|
|
537
|
+
writePackageFields(root, context, world, onePage(world, name), Object.fromEntries(Object.entries(values).map(([field, value]) => [sameName(declared, field) ?? field, value])), why);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Adds an effect without end, for a package's code: the engine's notes on what it breaks of what one keeps; undefined when the page has it already, maybe in the
|
|
541
|
+
* campaign's language (D-403).
|
|
542
|
+
*/
|
|
543
|
+
function addEffect(root, context, name, effect, why) {
|
|
544
|
+
const world = campaignWorld(root, context);
|
|
545
|
+
const page = onePage(world, name);
|
|
546
|
+
const words = campaignSystemWords(root, context);
|
|
547
|
+
// Effects written by hand that do not read are never written over (EFF-12).
|
|
548
|
+
checkEffectsReadable(page);
|
|
549
|
+
if (effectsOf(page).some((one) => sameWord(effect, one.name, words))) {
|
|
550
|
+
return undefined;
|
|
551
|
+
}
|
|
552
|
+
const keeping = keepingConditionNote(world, readOpenScene(root).scene, campaignSystem(root, context), page.name, effect, words);
|
|
553
|
+
saveEffects(root, context, world, { page, copy: false }, withEffect(effectsOf(page), { name: effect, values: [] }, words));
|
|
554
|
+
appendLogEntry(root, newDatedLogEntry(root, context, "effect-added", concernsPlayer(world, page) ? "public" : "hidden", { page: page.name, effect, why }));
|
|
555
|
+
return keeping === "" ? [] : [keeping];
|
|
556
|
+
}
|
|
557
|
+
/** Ends an effect, for a package's code, by its name or the system's word for it in the campaign's language (D-403); whether the page had it. */
|
|
558
|
+
function endEffect(root, context, name, effect, why) {
|
|
559
|
+
const world = campaignWorld(root, context);
|
|
560
|
+
const page = onePage(world, name);
|
|
561
|
+
const words = campaignSystemWords(root, context);
|
|
562
|
+
checkEffectsReadable(page);
|
|
563
|
+
const effects = effectsOf(page);
|
|
564
|
+
const ended = effects.filter((one) => sameWord(effect, one.name, words));
|
|
565
|
+
if (ended.length === 0) {
|
|
566
|
+
return false;
|
|
567
|
+
}
|
|
568
|
+
saveEffects(root, context, world, { page, copy: false }, effects.filter((one) => !ended.includes(one)));
|
|
569
|
+
// The log names what the page had, as it was written.
|
|
570
|
+
const visibility = concernsPlayer(world, page) ? "public" : "hidden";
|
|
571
|
+
ended.forEach((one) => appendLogEntry(root, newDatedLogEntry(root, context, "effect-ended", visibility, { page: page.name, effect: one.name, why })));
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* 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;
|
|
576
|
+
* the player's physical dice, given as their faces, always in view.
|
|
577
|
+
*/
|
|
578
|
+
function rollFor(root, context, notation, by, words) {
|
|
579
|
+
const rolled = rollDice(notation, by.values === undefined ? { random: context.random } : { values: by.values });
|
|
580
|
+
const physical = by.values !== undefined;
|
|
581
|
+
const hidden = !physical && behindTheScreen(root, context, by.who);
|
|
582
|
+
const checked = by.check === undefined ? {} : { check: by.check };
|
|
583
|
+
const says = by.says === undefined ? {} : { says: words(by.says(rolled.faces)) };
|
|
584
|
+
const tones = tonesOf(by.tones?.(rolled.faces), rolled.faces);
|
|
585
|
+
const entry = newDatedLogEntry(root, context, "roll", hidden ? "hidden" : "public", {
|
|
586
|
+
notation: rolled.notation,
|
|
587
|
+
total: rolled.total,
|
|
588
|
+
result: rolled.text,
|
|
589
|
+
...says,
|
|
590
|
+
...tones,
|
|
591
|
+
why: by.why,
|
|
592
|
+
who: by.who,
|
|
593
|
+
...checked,
|
|
594
|
+
physical,
|
|
595
|
+
dice: rolled.dice,
|
|
596
|
+
});
|
|
597
|
+
if (hidden) {
|
|
598
|
+
sealHiddenRoll(entry, rolled.text, context);
|
|
599
|
+
}
|
|
600
|
+
appendLogEntry(root, entry);
|
|
601
|
+
return { notation: rolled.notation, total: rolled.total, faces: rolled.faces, dice: rolled.dice, text: rolled.text, id: entry.id, hidden };
|
|
602
|
+
}
|
|
603
|
+
/** Words of a package in the campaign's language, by its words as a widget's are, with their {names} filled; as written where its words lack them. */
|
|
604
|
+
function packageText(root, code, text) {
|
|
605
|
+
let words;
|
|
606
|
+
try {
|
|
607
|
+
words = packageWords(code, readTextSetting(root, "language") ?? code.language);
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
// Words that do not read leave the text as written, as validate says.
|
|
611
|
+
}
|
|
612
|
+
const { text: written, values } = typeof text === "string" ? { text, values: {} } : text;
|
|
613
|
+
return wordIn(words, written).replace(/\{(\w+)\}/g, (whole, name) => (Object.hasOwn(values, name) ? valueText(values[name]) : whole));
|
|
614
|
+
}
|
|
615
|
+
/** A value as plain text in a line of the log. */
|
|
616
|
+
function valueText(value) {
|
|
617
|
+
const unit = (given) => (given === undefined ? "" : ` ${given}`);
|
|
618
|
+
return typeof value === "number" || typeof value === "string" ? String(value) : "number" in value ? `${value.number}${unit(value.unit)}` : `${value.from}–${value.to}${unit(value.unit)}`;
|
|
619
|
+
}
|
|
620
|
+
/** The question of a roll asked, refused without words, a check's name or dice (ASK-2). */
|
|
621
|
+
function askedQuestion(question, check, notation) {
|
|
622
|
+
if (question.trim() === "" || check.trim() === "" || !isCheckDice(notation)) {
|
|
623
|
+
const dice = isCheckDice(notation) ? "" : `: ${notation} are no dice`;
|
|
624
|
+
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 2d6+1${dice}. Nothing was asked.`);
|
|
625
|
+
}
|
|
626
|
+
return question.trim();
|
|
627
|
+
}
|
|
628
|
+
/** What ask answered, as a package's code gets it. */
|
|
629
|
+
function askedOf(result) {
|
|
630
|
+
switch (result.kind) {
|
|
631
|
+
case "roll":
|
|
632
|
+
return { rolled: { id: result.id, total: result.total, faces: result.faces, text: result.text } };
|
|
633
|
+
case "changed":
|
|
634
|
+
return { declined: true };
|
|
635
|
+
case "said":
|
|
636
|
+
return { said: result.words };
|
|
637
|
+
default:
|
|
638
|
+
return { inWords: "asked" in result ? result.asked : "", physical: result.kind === "physical" || result.kind === "dice in words" };
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
function codeProblem(code, error) {
|
|
642
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
643
|
+
const kind = code.kind === "system" ? "game system" : "extension";
|
|
644
|
+
// A system the engine brings comes back with the engine, never with package.
|
|
645
|
+
const shipped = code.kind === "system" && isInside(resolve(engineSystemsFolder), resolve(code.folder));
|
|
646
|
+
const fix = shipped ? "install rpg-engine again, or run npm run build in its repository, then start the game's server again" : `install it again, or another version, with package install ${code.name}`;
|
|
647
|
+
return { path: campaignFileName, message: `The code of the ${kind} ${code.package} cannot be used: ${why}. Its commands are left out.`, fix };
|
|
648
|
+
}
|