rpg-engine 0.4.1 → 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/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-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/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 +9 -0
- 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 +9 -0
- 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 +9 -0
- package/dist/dashboard/i18n/hi.json +9 -0
- 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 +9 -0
- package/dist/dashboard/i18n/ko.json +9 -0
- 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 +9 -0
- 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/style.css +521 -3
- 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
package/dist/systems/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { type ActVerb, actVerbs, type CampaignData, campaignDataNames, type CheckAtom, type CheckOperator, type DamageRule, findCampaignSystem, findSystem, type GameSystem, type GridRules, installedSystems, type Lifestyle, type Recipe, type Reward, rollName, type SheetEntry, type SheetGroup, type SystemCheck, systemFileName, type SystemSetting, type Threshold, type Travel, type TravelKind, type TurnUse, withSettings, } from "./game-system.ts";
|
|
1
|
+
export { type ActVerb, actVerbs, type CampaignData, campaignDataNames, type CheckAtom, type CheckOperator, type DamageRule, findCampaignSystem, findSystem, type GameSystem, type GridRules, installedSystems, type Lifestyle, type Recipe, type Reward, rollName, type SheetEntry, type SheetGroup, type SystemCheck, systemFileName, type SystemLayer, type SystemSetting, type Threshold, type Travel, type TravelKind, type TurnUse, withSettings, } from "./game-system.ts";
|
|
2
2
|
export { type CounterChange, type LoadedSystemCode, loadSystemCode, type RollMade, type SystemEntry, type SystemGame, systemHasCode, type SystemHooks, type SystemPage, type SystemReader } from "./system-code.ts";
|
|
3
3
|
export { type SystemFolder, systemFolders } from "./system-folders.ts";
|
|
4
4
|
export { type SystemWords, systemWords } from "./words.ts";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type ShownWidget } from "./commands/game-widgets.ts";
|
|
2
|
+
import type { Context, Player } from "./core/context.ts";
|
|
3
|
+
import type { LogEntry } from "./core/log.ts";
|
|
4
|
+
export type { ShownWidget } from "./commands/game-widgets.ts";
|
|
5
|
+
export type { CardAsked, Context, Player, Rows, Said } from "./core/context.ts";
|
|
6
|
+
export type { LogEntry } from "./core/log.ts";
|
|
7
|
+
/** What a command printed, and its exit code: 0 when it did what it was asked. */
|
|
8
|
+
export interface Ran {
|
|
9
|
+
output: string;
|
|
10
|
+
exitCode: number;
|
|
11
|
+
}
|
|
12
|
+
/** A campaign for an extension's tests, made by testCampaign. */
|
|
13
|
+
export interface TestCampaign {
|
|
14
|
+
/** Its folder, a temporary one of its own. */
|
|
15
|
+
folder: string;
|
|
16
|
+
/** What its commands run with: change its `page` to answer the player's questions, its `signal` to stop a call. */
|
|
17
|
+
context: Context;
|
|
18
|
+
/** Runs a command as the command line does, as `run("lockpicking", "Crypt door", "--who", "Kira")`. */
|
|
19
|
+
run(...args: string[]): Promise<Ran>;
|
|
20
|
+
/** Runs a command that must succeed, and gives what it printed; it throws with that when it fails. */
|
|
21
|
+
ok(...args: string[]): Promise<string>;
|
|
22
|
+
/** The dice show these faces in turn, each one die of any size; when they run out, a roll throws. */
|
|
23
|
+
dice(...faces: number[]): void;
|
|
24
|
+
/** The lines of the campaign's log, in order. */
|
|
25
|
+
log(): LogEntry[];
|
|
26
|
+
/** A file of the campaign, as text, by its path in it; undefined when there is none. */
|
|
27
|
+
read(path: string): string | undefined;
|
|
28
|
+
/** The widgets of the campaign's extensions, as the dashboard shows them. */
|
|
29
|
+
widgets(): Promise<ShownWidget[]>;
|
|
30
|
+
/** The dashboard of the campaign, served on a port of this computer, with a page open on it. */
|
|
31
|
+
dashboard(): Promise<TestDashboard>;
|
|
32
|
+
/** Takes the campaign's folder away, and anything it started. */
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
/** A campaign's dashboard in a test: the player through its page, what it answers, and what it is sent. */
|
|
36
|
+
export interface TestDashboard {
|
|
37
|
+
/** The player through the page: give it as the campaign's context.page for the questions to reach the page. */
|
|
38
|
+
player: Player;
|
|
39
|
+
/** What the dashboard answers at an address, as JSON, as /api/asked. */
|
|
40
|
+
get(path: string): Promise<any>;
|
|
41
|
+
/** Sends the dashboard what the page sends, as an answer to /api/answer; its status and what it answered. */
|
|
42
|
+
post(path: string, body: unknown): Promise<{
|
|
43
|
+
status: number;
|
|
44
|
+
body: any;
|
|
45
|
+
}>;
|
|
46
|
+
close(): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
/** How a test campaign is made: its game system, the folders of the extensions it uses, its language, and whether its setup is done (by default it is). */
|
|
49
|
+
export interface TestCampaignOptions {
|
|
50
|
+
system: string;
|
|
51
|
+
extensions?: string[];
|
|
52
|
+
language?: string;
|
|
53
|
+
play?: boolean;
|
|
54
|
+
/** Settings written in campaign.yaml as by hand, as {"calendar-start": "14 April"}. */
|
|
55
|
+
settings?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A campaign in play for a test (D-506): made with init in a temporary folder, its setup done, using these extensions, each linked from its folder as the
|
|
59
|
+
* player's own, never through npm, so that its code runs. Its dice show 4 until told otherwise, its time is a fixed day, its ids count up.
|
|
60
|
+
*/
|
|
61
|
+
export declare function testCampaign(options: TestCampaignOptions): Promise<TestCampaign>;
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
// The test kit for whoever writes an extension (D-506): a campaign of a game system in play, with the extension installed from its folder as the player's own,
|
|
2
|
+
// its commands run as the command line runs them, with the dice and the time decided; its log, its widgets and the dashboard read as the page reads them.
|
|
3
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmdirSync, rmSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { request } from "node:http";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { basename, join, resolve } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { stringify } from "yaml";
|
|
9
|
+
import { runCommandLine } from "./command-line.js";
|
|
10
|
+
import { campaignWidgets } from "./commands/game-widgets.js";
|
|
11
|
+
import { SessionStore } from "./core/session-store.js";
|
|
12
|
+
import { startDashboard } from "./dashboard-server.js";
|
|
13
|
+
import { loadSystemCode } from "./systems/index.js";
|
|
14
|
+
/**
|
|
15
|
+
* A campaign in play for a test (D-506): made with init in a temporary folder, its setup done, using these extensions, each linked from its folder as the
|
|
16
|
+
* player's own, never through npm, so that its code runs. Its dice show 4 until told otherwise, its time is a fixed day, its ids count up.
|
|
17
|
+
*/
|
|
18
|
+
export async function testCampaign(options) {
|
|
19
|
+
const base = mkdtempSync(join(tmpdir(), "rpg-extension-test-"));
|
|
20
|
+
const folder = join(base, "campaign");
|
|
21
|
+
const packages = join(base, "packages");
|
|
22
|
+
const linked = (options.extensions ?? []).map((extension) => linkPackage(packages, resolve(extension)));
|
|
23
|
+
let ids = 0;
|
|
24
|
+
let faces;
|
|
25
|
+
const context = {
|
|
26
|
+
folder,
|
|
27
|
+
random: (sides) => {
|
|
28
|
+
if (faces === undefined) {
|
|
29
|
+
return Math.min(4, sides);
|
|
30
|
+
}
|
|
31
|
+
const face = faces.shift();
|
|
32
|
+
if (face === undefined) {
|
|
33
|
+
throw new Error("The test ran out of dice: give more faces with dice().");
|
|
34
|
+
}
|
|
35
|
+
return face;
|
|
36
|
+
},
|
|
37
|
+
now: () => new Date("2026-10-02T10:00:00Z"),
|
|
38
|
+
newId: () => `id${++ids}`,
|
|
39
|
+
newSalt: () => "salt",
|
|
40
|
+
actor: "master",
|
|
41
|
+
platform: process.platform === "win32" || process.platform === "darwin" ? process.platform : "linux",
|
|
42
|
+
systemFolders: [enginePath("../systems/")],
|
|
43
|
+
systemCode: await loadSystemCode([enginePath("../systems/")]),
|
|
44
|
+
extensionFolders: [packages],
|
|
45
|
+
};
|
|
46
|
+
const started = [];
|
|
47
|
+
// Each call reads the campaign's context as the test left it, a page or a signal of its own included.
|
|
48
|
+
const campaign = {
|
|
49
|
+
folder,
|
|
50
|
+
context,
|
|
51
|
+
run: (...args) => runCommandLine(args, campaign.context),
|
|
52
|
+
ok: async (...args) => {
|
|
53
|
+
const ran = await campaign.run(...args);
|
|
54
|
+
if (ran.exitCode !== 0) {
|
|
55
|
+
throw new Error(`${args.join(" ")} failed: ${ran.output}`);
|
|
56
|
+
}
|
|
57
|
+
return ran.output;
|
|
58
|
+
},
|
|
59
|
+
dice: (...given) => (faces = [...given]),
|
|
60
|
+
log: () => (readText(join(folder, "log.jsonl")) ?? "").split("\n").filter((line) => line.trim() !== "").map((line) => JSON.parse(line)),
|
|
61
|
+
read: (path) => readText(join(folder, path)),
|
|
62
|
+
widgets: () => campaignWidgets(folder, campaign.context),
|
|
63
|
+
dashboard: async () => {
|
|
64
|
+
const dashboard = await testDashboard(campaign.context);
|
|
65
|
+
started.push(dashboard);
|
|
66
|
+
return dashboard;
|
|
67
|
+
},
|
|
68
|
+
close: async () => {
|
|
69
|
+
await Promise.all(started.map((dashboard) => dashboard.close()));
|
|
70
|
+
// The links first, alone: never through them, into the extension's own folder.
|
|
71
|
+
linked.forEach((one) => unlink(one.link));
|
|
72
|
+
rmSync(base, { recursive: true, force: true });
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
await campaign.ok("init", "--name", "Test", "--language", options.language ?? "en", "--system", options.system, "--difficulty", "normal", "--lethality", "dice");
|
|
76
|
+
if (options.play !== false) {
|
|
77
|
+
inPlay(folder);
|
|
78
|
+
}
|
|
79
|
+
const extensions = Object.fromEntries(linked.map((one) => [one.name, one.version]));
|
|
80
|
+
const added = { ...options.settings, ...(linked.length === 0 ? {} : { extensions }) };
|
|
81
|
+
const written = readFileSync(join(folder, "campaign.yaml"), "utf8");
|
|
82
|
+
writeFileSync(join(folder, "campaign.yaml"), Object.keys(added).length === 0 ? written : `${written}${stringify(added)}`);
|
|
83
|
+
return campaign;
|
|
84
|
+
}
|
|
85
|
+
/** An extension's folder linked into the packages a test campaign reads, as one the player installed from a folder: its name and version. */
|
|
86
|
+
function linkPackage(packages, folder) {
|
|
87
|
+
const manifest = JSON.parse(readFileSync(join(folder, "package.json"), "utf8"));
|
|
88
|
+
if (manifest.name === undefined || manifest.version === undefined) {
|
|
89
|
+
throw new Error(`${folder} has no package.json with a name and a version: give the folder of an extension.`);
|
|
90
|
+
}
|
|
91
|
+
const scope = manifest.name.includes("/") ? manifest.name.split("/")[0] : "";
|
|
92
|
+
mkdirSync(join(packages, scope), { recursive: true });
|
|
93
|
+
// A junction on Windows, which needs no rights; a link elsewhere.
|
|
94
|
+
const link = join(packages, scope, basename(manifest.name));
|
|
95
|
+
symlinkSync(folder, link, "junction");
|
|
96
|
+
return { name: manifest.name, version: manifest.version, link };
|
|
97
|
+
}
|
|
98
|
+
/** Takes a link away, never what it leads to: a junction of Windows goes as an empty folder does. */
|
|
99
|
+
function unlink(link) {
|
|
100
|
+
try {
|
|
101
|
+
unlinkSync(link);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
rmdirSync(link);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** A campaign that init made in play, as after its setup (MOD-1): the settings of play that are not there take their usual values. */
|
|
108
|
+
function inPlay(folder) {
|
|
109
|
+
const state = join(folder, "state.yaml");
|
|
110
|
+
writeFileSync(state, readFileSync(state, "utf8").replace(/^mode: setup$/m, "mode: play"));
|
|
111
|
+
const settings = join(folder, "campaign.yaml");
|
|
112
|
+
const written = readFileSync(settings, "utf8");
|
|
113
|
+
const usual = ["saves: checkpoint", "rigor: smooth", "dice: digital", "units: metric"].filter((line) => !new RegExp(`^${line.split(":")[0]}:`, "m").test(written));
|
|
114
|
+
writeFileSync(settings, `${written}${usual.map((line) => `${line}\n`).join("")}`);
|
|
115
|
+
}
|
|
116
|
+
/** The dashboard of a test campaign, with a page open on it that the server has heard from, so that questions go to the page. */
|
|
117
|
+
async function testDashboard(context) {
|
|
118
|
+
const session = new SessionStore({ systemFolders: context.systemFolders, watchFolder: () => ({ close: () => undefined }) });
|
|
119
|
+
const dashboard = await startDashboard({ ...context, files: session }, session, { ports: [0], page: context.folder });
|
|
120
|
+
if (dashboard === undefined) {
|
|
121
|
+
session.close();
|
|
122
|
+
throw new Error("The test's dashboard did not start.");
|
|
123
|
+
}
|
|
124
|
+
const port = Number(new URL(dashboard.url).port);
|
|
125
|
+
const call = (method, path, body) => new Promise((done, fail) => {
|
|
126
|
+
const headers = body === undefined ? {} : { "origin": `http://127.0.0.1:${port}`, "content-type": "application/json" };
|
|
127
|
+
const sent = request({ host: "127.0.0.1", port, path, method, headers }, (response) => {
|
|
128
|
+
let text = "";
|
|
129
|
+
response.setEncoding("utf8");
|
|
130
|
+
response.on("data", (chunk) => (text += chunk));
|
|
131
|
+
response.on("end", () => done({ status: response.statusCode ?? 0, body: text.trim() === "" ? {} : JSON.parse(text) }));
|
|
132
|
+
});
|
|
133
|
+
sent.on("error", fail);
|
|
134
|
+
sent.end(body === undefined ? undefined : JSON.stringify(body));
|
|
135
|
+
});
|
|
136
|
+
const page = request({ host: "127.0.0.1", port, path: "/events" });
|
|
137
|
+
const opened = new Promise((done) => page.on("response", (response) => response.once("data", done)));
|
|
138
|
+
page.end();
|
|
139
|
+
await opened;
|
|
140
|
+
return {
|
|
141
|
+
player: dashboard.player,
|
|
142
|
+
get: async (path) => (await call("GET", path)).body,
|
|
143
|
+
post: (path, body) => call("POST", path, body),
|
|
144
|
+
close: async () => {
|
|
145
|
+
page.destroy();
|
|
146
|
+
await dashboard.close();
|
|
147
|
+
session.close();
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/** A path of the engine's package, from this file in src/ or dist/ alike. */
|
|
152
|
+
function enginePath(relative) {
|
|
153
|
+
return fileURLToPath(new URL(relative, import.meta.url));
|
|
154
|
+
}
|
|
155
|
+
function readText(path) {
|
|
156
|
+
try {
|
|
157
|
+
return readFileSync(path, "utf8");
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rpg-engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Play a tabletop role-playing game with an AI as your game master: the AI tells the story; RPG Engine remembers the world, rolls the dice and keeps the accounts.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
".": {
|
|
13
13
|
"types": "./dist/index.d.ts",
|
|
14
14
|
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./testing": {
|
|
17
|
+
"types": "./dist/testing.d.ts",
|
|
18
|
+
"default": "./dist/testing.js"
|
|
15
19
|
}
|
|
16
20
|
},
|
|
17
21
|
"files": [
|
|
@@ -25,11 +29,12 @@
|
|
|
25
29
|
"instructions"
|
|
26
30
|
],
|
|
27
31
|
"scripts": {
|
|
28
|
-
"build": "node -e \"for (const folder of ['dist', 'systems/5e/dist'
|
|
32
|
+
"build": "node -e \"for (const folder of ['dist', 'systems/5e/dist']) require('node:fs').rmSync(folder, { recursive: true, force: true })\" && tsc && tsc -p src/dashboard && node -e \"require('node:fs').cpSync('src/dashboard', 'dist/dashboard', { recursive: true, filter: (path) => !/[.](tsx?|py)$|tsconfig[.]json$/.test(path) })\" && tsc -p systems/5e",
|
|
29
33
|
"dev": "tsc --watch",
|
|
30
34
|
"demo": "npm run build && node --experimental-strip-types --no-warnings test/demo/dashboard.ts --ask --port 5760",
|
|
31
35
|
"betasim": "npm run build && node --experimental-strip-types --no-warnings test/betasim/run.ts",
|
|
32
36
|
"translate": "node --experimental-strip-types --no-warnings test/i18n/translate.ts",
|
|
37
|
+
"test:downstream": "node --experimental-strip-types --no-warnings test/downstream/run.ts",
|
|
33
38
|
"test": "vitest run",
|
|
34
39
|
"format": "eslint --fix . && dprint fmt",
|
|
35
40
|
"lint": "eslint . && dprint check && tsc -p tsconfig.test.json && tsc -p src/dashboard --noEmit",
|