rpg-engine 0.16.0 → 0.17.1

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.
Files changed (116) hide show
  1. package/dist/adventures/source.d.ts +3 -1
  2. package/dist/adventures/source.js +31 -1
  3. package/dist/campaign/find-campaign.js +8 -4
  4. package/dist/commands/import/import.js +23 -1
  5. package/dist/commands/package/package.js +43 -9
  6. package/dist/commands/package/result.d.ts +3 -4
  7. package/dist/commands/save.d.ts +1 -1
  8. package/dist/commands/shared/books/adventure-layers.d.ts +89 -0
  9. package/dist/commands/shared/books/adventure-layers.js +201 -0
  10. package/dist/commands/shared/books/adventures.d.ts +25 -8
  11. package/dist/commands/shared/books/adventures.js +60 -22
  12. package/dist/commands/shared/characters/thoughts.d.ts +7 -4
  13. package/dist/commands/shared/characters/thoughts.js +15 -5
  14. package/dist/commands/shared/scene/scene.js +3 -1
  15. package/dist/commands/shared/sheet/sheet.d.ts +2 -0
  16. package/dist/commands/show.d.ts +6 -0
  17. package/dist/commands/show.js +16 -2
  18. package/dist/commands/thoughts.d.ts +1 -0
  19. package/dist/commands/thoughts.js +23 -15
  20. package/dist/commands/view/layers.d.ts +36 -0
  21. package/dist/commands/view/layers.js +53 -0
  22. package/dist/commands/view/memory.js +12 -6
  23. package/dist/commands/view/page.js +4 -1
  24. package/dist/commands/view/result.d.ts +8 -1
  25. package/dist/commands/view/view.d.ts +3 -0
  26. package/dist/commands/view/view.js +5 -2
  27. package/dist/commands/write/inheritance.js +1 -5
  28. package/dist/commands/write/layer.d.ts +11 -3
  29. package/dist/commands/write/layer.js +27 -7
  30. package/dist/commands/write/write.d.ts +1 -0
  31. package/dist/commands/write/write.js +6 -2
  32. package/dist/core/length.d.ts +4 -1
  33. package/dist/core/length.js +13 -2
  34. package/dist/core/text.d.ts +2 -0
  35. package/dist/core/text.js +4 -0
  36. package/dist/dashboard/api.js +18 -0
  37. package/dist/dashboard/app.js +5 -1
  38. package/dist/dashboard/guide/ar.md +3 -1
  39. package/dist/dashboard/guide/cs.md +5 -3
  40. package/dist/dashboard/guide/da.md +5 -3
  41. package/dist/dashboard/guide/de.md +3 -1
  42. package/dist/dashboard/guide/el.md +5 -3
  43. package/dist/dashboard/guide/en.md +2 -0
  44. package/dist/dashboard/guide/es.md +5 -3
  45. package/dist/dashboard/guide/fi.md +5 -3
  46. package/dist/dashboard/guide/fr.md +5 -3
  47. package/dist/dashboard/guide/he.md +4 -2
  48. package/dist/dashboard/guide/hi.md +5 -3
  49. package/dist/dashboard/guide/hu.md +5 -3
  50. package/dist/dashboard/guide/is.md +5 -3
  51. package/dist/dashboard/guide/it.md +3 -1
  52. package/dist/dashboard/guide/ja.md +3 -1
  53. package/dist/dashboard/guide/ko.md +3 -1
  54. package/dist/dashboard/guide/nb.md +5 -3
  55. package/dist/dashboard/guide/nl.md +4 -2
  56. package/dist/dashboard/guide/pl.md +5 -3
  57. package/dist/dashboard/guide/pt.md +4 -2
  58. package/dist/dashboard/guide/ro.md +5 -3
  59. package/dist/dashboard/guide/ru.md +4 -2
  60. package/dist/dashboard/guide/sv.md +5 -3
  61. package/dist/dashboard/guide/tr.md +4 -2
  62. package/dist/dashboard/guide/uk.md +5 -3
  63. package/dist/dashboard/guide/zh.md +4 -2
  64. package/dist/dashboard/i18n/ar.json +25 -0
  65. package/dist/dashboard/i18n/cs.json +25 -0
  66. package/dist/dashboard/i18n/da.json +25 -0
  67. package/dist/dashboard/i18n/de.json +25 -0
  68. package/dist/dashboard/i18n/el.json +25 -0
  69. package/dist/dashboard/i18n/es.json +25 -0
  70. package/dist/dashboard/i18n/fi.json +25 -0
  71. package/dist/dashboard/i18n/fr.json +25 -0
  72. package/dist/dashboard/i18n/he.json +25 -0
  73. package/dist/dashboard/i18n/hi.json +25 -0
  74. package/dist/dashboard/i18n/hu.json +25 -0
  75. package/dist/dashboard/i18n/is.json +25 -0
  76. package/dist/dashboard/i18n/it.json +25 -0
  77. package/dist/dashboard/i18n/ja.json +25 -0
  78. package/dist/dashboard/i18n/ko.json +25 -0
  79. package/dist/dashboard/i18n/nb.json +25 -0
  80. package/dist/dashboard/i18n/nl.json +25 -0
  81. package/dist/dashboard/i18n/pl.json +25 -0
  82. package/dist/dashboard/i18n/pt.json +25 -0
  83. package/dist/dashboard/i18n/ro.json +25 -0
  84. package/dist/dashboard/i18n/ru.json +25 -0
  85. package/dist/dashboard/i18n/sv.json +25 -0
  86. package/dist/dashboard/i18n/tr.json +25 -0
  87. package/dist/dashboard/i18n/uk.json +25 -0
  88. package/dist/dashboard/i18n/zh.json +25 -0
  89. package/dist/dashboard/packages/changes.js +111 -0
  90. package/dist/dashboard/packages/installed.js +2 -2
  91. package/dist/dashboard/packages/packages.js +12 -7
  92. package/dist/dashboard/parts/changed.js +37 -0
  93. package/dist/dashboard/parts/refusals.js +4 -0
  94. package/dist/dashboard/parts/widgets.js +3 -1
  95. package/dist/dashboard/play/choices.js +3 -2
  96. package/dist/dashboard/sheet/sheet.js +10 -7
  97. package/dist/dashboard/shell/shown.js +1 -0
  98. package/dist/dashboard/style.css +214 -1
  99. package/dist/dashboard/world/lore.js +5 -1
  100. package/dist/pages/index.d.ts +3 -3
  101. package/dist/pages/index.js +3 -3
  102. package/dist/pages/layers.d.ts +5 -3
  103. package/dist/pages/layers.js +15 -7
  104. package/dist/pages/load.d.ts +2 -0
  105. package/dist/pages/load.js +16 -3
  106. package/dist/pages/markdown.d.ts +5 -1
  107. package/dist/pages/markdown.js +13 -1
  108. package/dist/pages/write-layer.d.ts +5 -0
  109. package/dist/pages/write-layer.js +59 -5
  110. package/dist/server/dashboard/choices.d.ts +2 -0
  111. package/dist/server/dashboard/choices.js +4 -2
  112. package/dist/server/dashboard/index.js +5 -1
  113. package/dist/server/dashboard/writes.d.ts +7 -0
  114. package/dist/server/dashboard/writes.js +39 -0
  115. package/instructions/voices.md +1 -1
  116. package/package.json +1 -1
@@ -1,11 +1,13 @@
1
1
  import { type AdventureManifest } from "./manifest.ts";
2
2
  /** The folders an adventure has, as a campaign has them (D-372). */
3
3
  export declare const adventureFolders: readonly ["world", "images", "rules"];
4
- /** An adventure as a package gives it (ADV-1): its manifest, its files by their path, and what was left out at its top. */
4
+ /** An adventure as a package gives it (ADV-1): its manifest, its files by their path, its layers, and what was left out at its top. */
5
5
  export interface AdventureFiles {
6
6
  manifest: AdventureManifest;
7
7
  /** By their path in the package, with forward slashes, such as world/places/Pozzo.md. */
8
8
  files: Record<string, Uint8Array>;
9
+ /** Its layers over the pages of the game system it needs (ADV-8), by their path under layers/<system>/, the page's own in the system, such as world/creatures/Goblin.md. */
10
+ layers: Record<string, Uint8Array>;
9
11
  /** The files and folders at its top that are no part of it, such as a README. */
10
12
  ignored: string[];
11
13
  }
@@ -4,9 +4,12 @@ import { archivePathParts, refuseTwice } from "../core/archive-paths.js";
4
4
  import { filesIn } from "../core/files.js";
5
5
  import { Refusal } from "../core/refusal.js";
6
6
  import { files } from "../core/store.js";
7
+ import { comparableText } from "../core/text.js";
7
8
  import { manifestFileName, readManifest, writeManifest } from "./manifest.js";
8
9
  /** The folders an adventure has, as a campaign has them (D-372). */
9
10
  export const adventureFolders = ["world", "images", "rules"];
11
+ // The folder of its layers over the pages of the game system it needs, as a campaign has it (ADV-8).
12
+ const layersFolder = "layers";
10
13
  /** The most an adventure may unpack to, as a package's .tgz (D-452). */
11
14
  export const largestAdventure = 512 * 1024 * 1024;
12
15
  // What each folder may hold: pages and rules are markdown, pictures as the engine shows them.
@@ -83,7 +86,10 @@ function adventureOf(label, entries) {
83
86
  if (manifest === undefined) {
84
87
  throw new Refusal(`${label} has no ${manifestFileName} at its top: it is not an adventure. A document or a folder of notes is imported by the master, who writes its pages. Nothing changed.`, "not-adventure", { name: label });
85
88
  }
89
+ const read = readManifest(new TextDecoder().decode(manifest[1]), `${label}, ${manifestFileName}`);
86
90
  const held = {};
91
+ const layers = {};
92
+ const layerPaths = [];
87
93
  const ignored = new Set();
88
94
  for (const [parts, data] of inner) {
89
95
  const path = parts.join("/");
@@ -91,6 +97,13 @@ function adventureOf(label, entries) {
91
97
  if (parts.length === 1 && parts[0].toLowerCase() === manifestFileName) {
92
98
  continue;
93
99
  }
100
+ if (parts.length > 1 && parts[0].toLowerCase() === layersFolder) {
101
+ const under = layerPath(label, parts, read.needs.system);
102
+ layers[under] = data;
103
+ // Kept one by one: the system's folder written in two cases gives the same layer twice.
104
+ layerPaths.push(`${layersFolder}/${read.needs.system ?? ""}/${under}`);
105
+ continue;
106
+ }
94
107
  if (folder === undefined || parts.length === 1) {
95
108
  // What a repository has beside an adventure, its readme and licence, goes unsaid.
96
109
  if (!/^(readme|licen[cs]e|copying|changelog)(\..*)?$/i.test(parts[0])) {
@@ -105,5 +118,22 @@ function adventureOf(label, entries) {
105
118
  held[[folder, ...parts.slice(1)].join("/")] = data;
106
119
  }
107
120
  refuseTwice(label, Object.keys(held));
108
- return { manifest: readManifest(new TextDecoder().decode(manifest[1]), `${label}, ${manifestFileName}`), files: held, ignored: [...ignored].sort() };
121
+ refuseTwice(label, layerPaths);
122
+ return { manifest: read, files: held, layers, ignored: [...ignored].sort() };
123
+ }
124
+ /**
125
+ * Where a file under layers/ lies over the pages of the game system (ADV-8): only in layers/<the system the adventure needs>/world/, in markdown. Another folder
126
+ * there, a file of another kind, or layers in an adventure that needs no system refuse it whole.
127
+ */
128
+ function layerPath(label, parts, system) {
129
+ const path = parts.join("/");
130
+ if (system === undefined) {
131
+ throw new Refusal(`${label} holds ${path}, a layer over a page of a game system, and its ${manifestFileName} names none: write there the system its layers are for, as needs: {system: its name}, `
132
+ + `or take ${layersFolder}/ out, and give it again. It is refused whole.`, "adventure-file", { name: label, file: path });
133
+ }
134
+ if (comparableText(parts[1]) !== comparableText(system) || parts.length < 4 || parts[2].toLowerCase() !== "world" || !/\.md$/i.test(path)) {
135
+ throw new Refusal(`${label} holds ${path}, and ${layersFolder}/ holds only ${system}/world/ with markdown files, its layers over the pages of the game system it needs: it is refused whole. `
136
+ + "Take it out, or move it there, and give it again.", "adventure-file", { name: label, file: path });
137
+ }
138
+ return ["world", ...parts.slice(3)].join("/");
109
139
  }
@@ -1,5 +1,5 @@
1
1
  import { dirname, join, resolve } from "node:path";
2
- import { files } from "../core/store.js";
2
+ import { files, isInside } from "../core/store.js";
3
3
  /** The file that marks a folder as a campaign and holds its settings. */
4
4
  export const campaignFileName = "campaign.yaml";
5
5
  /** The folder of a campaign with its house rules, a markdown file for each (D-178). */
@@ -27,10 +27,12 @@ export function requireCampaign(folder) {
27
27
  files().track(root);
28
28
  return root;
29
29
  }
30
+ // The campaign's layers over its game system, which lie a folder further down than its pages: layers/<system>/world/<type>.
31
+ const layersFolder = "layers";
30
32
  // The folders the engine writes in: a link among them, a junction that came with a folder someone else made, would carry its writes outside the campaign. A file
31
33
  // that is a link the store refuses as it writes it.
32
- const writtenFolders = ["world", "images", "rules", "sessions", ".saves", "exports", ".player", ".imports", ".claude", ".codex"];
33
- /** Throws when a folder the engine writes in, or one in it two levels deep, is a link: a symbolic link or a junction. */
34
+ const writtenFolders = ["world", "images", "rules", "sessions", ".saves", "exports", ".player", ".imports", ".claude", ".codex", layersFolder];
35
+ /** Throws when a folder the engine writes in, or one in it two levels deep, three for the layers, is a link: a symbolic link or a junction. */
34
36
  function refuseLinksOut(root) {
35
37
  const store = files();
36
38
  const folders = writtenFolders.map((name) => join(root, name));
@@ -39,7 +41,9 @@ function refuseLinksOut(root) {
39
41
  ...store.entries(folder).filter((entry) => entry.isFolder).map((entry) => join(folder, entry.name)),
40
42
  ];
41
43
  const types = folders.flatMap(inside);
42
- const linked = [...folders, ...types, ...types.flatMap(inside)].find((path) => store.isLink(path));
44
+ const deeper = types.flatMap(inside);
45
+ const layerTypes = deeper.filter((path) => isInside(join(root, layersFolder), path)).flatMap(inside);
46
+ const linked = [...folders, ...types, ...deeper, ...layerTypes].find((path) => store.isLink(path));
43
47
  if (linked !== undefined) {
44
48
  throw new Error(`${linked} is a link to somewhere else, and what the engine writes there would leave the campaign: replace it with a real folder, with what it holds copied in.`);
45
49
  }
@@ -1,9 +1,11 @@
1
1
  import { basename, extname, join, relative, resolve, sep } from "node:path";
2
2
  import { z } from "zod";
3
- import { adventureFolderName } from "../../adventures/index.js";
3
+ import { adventureFolderName, isAdventureFolder, readAdventureZip } from "../../adventures/index.js";
4
4
  import { changeSetting, readSettings, requireCampaign, storyWays } from "../../campaign/index.js";
5
5
  import { appendLogEntry } from "../../core/log.js";
6
+ import { Refusal } from "../../core/refusal.js";
6
7
  import { defineCommand } from "../../core/registry.js";
8
+ import { files } from "../../core/store.js";
7
9
  import { documentAt, readDocument } from "../../documents/index.js";
8
10
  import { stopProcess } from "../../helpers/index.js";
9
11
  import { beginOpening, clearOpening, doneImports, dropDocumentText, dropImport, hasImport, importAge, importDate, importedPages, importFile, importFolder, importPiece, importStarts, importStyle, isOwnOpening, markOpening, openImport, openingImport, prepareImport, readImportState, runElsewhere, writeImportState, } from "../../importer/index.js";
@@ -174,6 +176,9 @@ async function startImport(root, context, document, title) {
174
176
  if (found === undefined) {
175
177
  throw new Error(`There is no ${document}: give the full path of the player's document. Nothing changed.`);
176
178
  }
179
+ if (isAdventure(document, found)) {
180
+ throw new Error(`${basename(document)} is an adventure already, not a document to read: install it with package, action install, name ${document}. Nothing changed.`);
181
+ }
177
182
  const name = title === undefined || title === "" ? basename(document, found === "folder" ? "" : extname(document)) : title;
178
183
  const folder = importFolder(root, adventureFolderName(name));
179
184
  if (hasImport(folder)) {
@@ -222,6 +227,23 @@ async function startImport(root, context, document, title) {
222
227
  }
223
228
  return { ...resultOf(root, folder, readImportState(folder) ?? state), pictures: text.pictures.length, ...recognized, helper: program };
224
229
  }
230
+ /** Whether what was given as a document is an adventure made already: a folder with adventure.yaml, or a .zip that holds one, which package install reads and checks. */
231
+ function isAdventure(path, found) {
232
+ if (found === "folder") {
233
+ return isAdventureFolder(path);
234
+ }
235
+ if (!/\.zip$/i.test(path)) {
236
+ return false;
237
+ }
238
+ try {
239
+ readAdventureZip(files().bytes(path) ?? new Uint8Array(), basename(path));
240
+ return true;
241
+ }
242
+ catch (error) {
243
+ // Any other .zip is no document either, and is told so as one; an adventure with a mistake in it is still one.
244
+ return !(error instanceof Refusal && ["not-adventure", "archive-unread"].includes(error.code));
245
+ }
246
+ }
225
247
  /** The next piece for the master to read in the chat, with the importer's instructions once in a chat; the last one ends the import. */
226
248
  function nextPiece(root, context, folder, state) {
227
249
  const text = importPiece(folder, state.done);
@@ -7,7 +7,7 @@ import { comparableText } from "../../core/text.js";
7
7
  import { isNewerVersion } from "../../core/version.js";
8
8
  import { installedExtensions, readExtension, shortName } from "../../extensions/index.js";
9
9
  import { isVerified } from "../../packages/index.js";
10
- import { exportAdventure, installAdventure, installedAdventures, isAdventureSpec, readAdventureSpec, removeAdventure, storyTouching } from "../shared/books/adventures.js";
10
+ import { exportAdventure, installAdventure, installedAdventures, isAdventureSpec, readAdventureSpec, removeAdventure, storyTouching, } from "../shared/books/adventures.js";
11
11
  import { listedExtensions } from "../shared/packages/extensions.js";
12
12
  import { issuesOf, languagesHere, systemsHere, usable } from "./here.js";
13
13
  import { installed } from "./install.js";
@@ -50,8 +50,7 @@ export const packageCommand = defineCommand({
50
50
  }
51
51
  const adventure = installedAdventures(root).find((one) => comparableText(one.title) === comparableText(name));
52
52
  if (input.action === "remove" && adventure !== undefined) {
53
- removeAdventure(root, context, adventure);
54
- return { kind: "adventure-removed", title: adventure.title };
53
+ return { kind: "adventure-removed", ...removeAdventure(root, context, adventure) };
55
54
  }
56
55
  return input.action === "remove" ? removed(root, context, name) : await uninstalled(root, context, name);
57
56
  },
@@ -60,7 +59,7 @@ export const packageCommand = defineCommand({
60
59
  return adventureText(result);
61
60
  }
62
61
  if (result.kind === "adventure-removed") {
63
- return `${result.title} is out of the campaign, with the files it wrote.`;
62
+ return removedText(result);
64
63
  }
65
64
  if (result.kind === "exported") {
66
65
  return exportText(result);
@@ -152,9 +151,36 @@ export function adventureText(result) {
152
151
  result.shadows.length === 0 ? "" : `Its pages take the place of the game system's: ${result.shadows.join(", ")}.`,
153
152
  result.covers.length === 0 ? "" : `Its rules take the place of the game system's: ${result.covers.join(", ")}.`,
154
153
  result.ignored.length === 0 ? "" : `Left out, no part of an adventure: ${result.ignored.join(", ")}.`,
154
+ ...layersText(result.layers),
155
155
  `${read} before you narrate.${start} Bring a hook into the story: the adventure begins when the player takes it.`,
156
156
  ].filter((line) => line !== "").join(" ");
157
157
  }
158
+ /** What the master hears of an adventure's layers (ADV-8): the pages of the game system they change, with their type, and those left out, with why. */
159
+ function layersText(layers) {
160
+ // With the values, so that the master can write any of them into the campaign's own layer.
161
+ const kept = layers.kept.map((one) => {
162
+ const sections = one.sections.length === 0 ? [] : [`the section${one.sections.length === 1 ? "" : "s"} ${one.sections.join(", ")}`];
163
+ const gave = [...Object.entries(one.fields).map(([field, value]) => `${field} ${value}`), ...sections, ...(one.introduction ? ["the introduction"] : [])];
164
+ return `The campaign's own layer over ${one.page} stays: the adventure's is left out (it gave ${gave.join(", ")}).`;
165
+ });
166
+ return [
167
+ layers.changed.length === 0 ? "" : `Its layers change pages of the game system for the whole campaign: ${typedPages(layers.changed)}; show says what each changes.`,
168
+ ...kept,
169
+ layers.stray.length === 0 ? "" : `Left out, as the rules the campaign plays have no such page: ${layers.stray.join(", ")}.`,
170
+ ];
171
+ }
172
+ /** What the master hears of an adventure taken out (ADV-6, ADV-8): its files gone, the pages of the game system its layers change no more, and the layers that stay. */
173
+ function removedText(result) {
174
+ return [
175
+ `${result.title} is out of the campaign, with the files it wrote.`,
176
+ result.layers.length === 0 ? "" : `Its layers are off ${typedPages(result.layers)}: each reads as the game system writes it again.`,
177
+ result.layersKept.length === 0 ? "" : `What it layered over ${result.layersKept.join(", ")} was changed since it came, and stays the campaign's: write the page with layer and reset takes it off.`,
178
+ ].filter((line) => line !== "").join(" ");
179
+ }
180
+ /** "Bog Hound (creature), Hardtack (item)": pages of the game system, each with its type. */
181
+ function typedPages(pages) {
182
+ return pages.map((one) => (one.type === undefined ? one.page : `${one.page} (${one.type})`)).join(", ");
183
+ }
158
184
  /** The packages on this computer, and those the campaign lists and does not find there: at once, without npm (D-442). */
159
185
  function listed(root, context) {
160
186
  const used = Object.keys(listedExtensions(root));
@@ -215,22 +241,30 @@ async function adventureInstalled(root, context, spec) {
215
241
  const adventure = await readAdventureSpec(spec, context);
216
242
  return { kind: "adventure-installed", ...installAdventure(root, context, adventure, spec) };
217
243
  }
218
- /** What the master hears of an export (ADV-7): the file, what it came from and what stayed out. */
244
+ /** What the master hears of an export (ADV-7, ADV-8): the file, what it came from, the layers it carries and what stayed out. */
219
245
  function exportText(result) {
246
+ const layersOut = (why, what) => {
247
+ const names = result.leftOut.layers.filter((one) => one.why === why).map((one) => one.name);
248
+ return names.length === 0 ? "" : `${what} (${names.join(", ")})`;
249
+ };
220
250
  const out = [
221
251
  result.leftOut.characters.length === 0 ? "" : `the player characters (${result.leftOut.characters.join(", ")})`,
222
252
  result.leftOut.adventures.length === 0 ? "" : `the adventures it installed (${result.leftOut.adventures.join(", ")})`,
223
- // An adventure plays the system as it ships (SYS-11).
224
- result.leftOut.layers.length === 0 ? "" : `the campaign's layers over pages of the system (${result.leftOut.layers.join(", ")}), which play as the system writes them`,
253
+ layersOut("adventure", "the layers an installed adventure brought, not changed since"),
254
+ layersOut("no-page", "the layers over no page of the game system, which validate names"),
255
+ layersOut("forbidden", "the layers that write what a layer cannot change, which validate names with the fields to take out"),
256
+ layersOut("unread", "the layers that do not read, which validate names with what to fix"),
225
257
  ].filter((part) => part !== "");
258
+ const kinds = result.layers.length === 0 ? "pages" : "pages or layers";
226
259
  return [
227
260
  `Wrote ${result.file}: ${result.pages} pages, from ${result.from}${out.length === 0 ? "" : `, without ${out.join(" and ")}`}.`,
261
+ result.layers.length === 0 ? "" : `It carries the campaign's layers over pages of the game system, as they are now: ${typedPages(result.layers)}.`,
228
262
  result.opening === undefined
229
263
  ? "It has no opening page: one tagged opening, with the situation, the hooks, what happens if no one steps in and the pages to read first, and its start, makes it ready."
230
264
  : `Its opening is [[${result.opening}]].`,
231
265
  result.sourced === undefined
232
266
  ? "The player shares it as a file, and only what they have the right to share."
233
- : `${result.sourced.length} of its pages come from the player's own documents (source:): ${result.sourced.slice(0, 5).join(", ")}${result.sourced.length > 5 ? "…" : ""}. `
267
+ : `${result.sourced.length} of its ${kinds} come from the player's own documents (source:): ${result.sourced.slice(0, 5).join(", ")}${result.sourced.length > 5 ? "…" : ""}. `
234
268
  + "So it says personal use only. Ask the player whether they may share it: only with the document's own license, or their rights to it; if so, export again with that license.",
235
- ].join(" ");
269
+ ].filter((line) => line !== "").join(" ");
236
270
  }
@@ -1,5 +1,5 @@
1
1
  import type { Extension } from "../../extensions/index.ts";
2
- import type { AdventureExported, AdventureInstalled } from "../shared/books/adventures.ts";
2
+ import type { AdventureExported, AdventureInstalled, AdventureRemoved } from "../shared/books/adventures.ts";
3
3
  import type { InstalledLanguage, InstalledSystem } from "./here.ts";
4
4
  export type PackageResult = {
5
5
  kind: "list";
@@ -50,10 +50,9 @@ export type PackageResult = {
50
50
  missing: number;
51
51
  } | ({
52
52
  kind: "adventure-installed";
53
- } & AdventureInstalled) | {
53
+ } & AdventureInstalled) | ({
54
54
  kind: "adventure-removed";
55
- title: string;
56
- } | ({
55
+ } & AdventureRemoved) | ({
57
56
  kind: "exported";
58
57
  } & AdventureExported);
59
58
  /** A package on this computer, as list gives it to the master and to the dashboard; broken when it does not read (D-442); with where its problems are reported. */
@@ -30,8 +30,8 @@ export declare const save: import("../core/registry.ts").Command<z.ZodObject<{
30
30
  action: z.ZodDefault<z.ZodEnum<{
31
31
  load: "load";
32
32
  list: "list";
33
- make: "make";
34
33
  reset: "reset";
34
+ make: "make";
35
35
  "new chat": "new chat";
36
36
  }>>;
37
37
  label: z.ZodOptional<z.ZodString>;
@@ -0,0 +1,89 @@
1
+ import type { AdventureFiles } from "../../../adventures/index.ts";
2
+ import type { Context } from "../../../core/context.ts";
3
+ import { type Page, type PageType, type World } from "../../../pages/index.ts";
4
+ /** A page of the game system a layer changed, or changes no more, as the master hears it. */
5
+ export interface LayeredPage {
6
+ page: string;
7
+ type?: PageType;
8
+ }
9
+ /** A layer of the adventure that did not come in, since the campaign has its own over the page: the fields it gave, each with its value, and the text it wrote. */
10
+ export interface LayerKept {
11
+ page: string;
12
+ fields: Record<string, string>;
13
+ sections: string[];
14
+ introduction: boolean;
15
+ }
16
+ /** What an adventure's layers did in the campaign (ADV-8). */
17
+ export interface LayersInstalled {
18
+ /** The pages of the game system they change now. */
19
+ changed: LayeredPage[];
20
+ /** Those left out, over a page the campaign has its own layer on. */
21
+ kept: LayerKept[];
22
+ /** Those left out, over no page of the system in the rules the campaign plays, by the name of the page each is for. */
23
+ stray: string[];
24
+ }
25
+ /** Why a layer of the campaign stays out of an export: over no page, not read, writing what a layer never changes, or brought by an adventure and not changed since. */
26
+ export interface LayerLeftOut {
27
+ name: string;
28
+ why: "no-page" | "unread" | "forbidden" | "adventure";
29
+ }
30
+ /** The campaign's layers an export carries, by their path in the .zip, with the pages they change, those from the player's documents, and those that stay out. */
31
+ export interface LayersExported {
32
+ carried: Record<string, Uint8Array>;
33
+ pages: LayeredPage[];
34
+ sourced: string[];
35
+ leftOut: LayerLeftOut[];
36
+ }
37
+ /** The layers of an install worked out before a file is written. */
38
+ export interface LayersPlan {
39
+ writes: {
40
+ path: string;
41
+ bytes: Uint8Array;
42
+ }[];
43
+ /** Every layer the adventure has in the campaign, written now or already there the same, by its file, with the print of its text: what taking it out compares. */
44
+ prints: Record<string, string>;
45
+ /** The pages of the system the layers written now change, as they were, each with the fields its layer gives. */
46
+ changed: {
47
+ page: Page;
48
+ fields: string[];
49
+ }[];
50
+ kept: LayerKept[];
51
+ stray: string[];
52
+ problems: string[];
53
+ }
54
+ /** The layers that go with an adventure taken out, the pages of the system they lay over as they were, and those changed since, which stay. */
55
+ export interface LayersRemoval {
56
+ paths: string[];
57
+ changed: Page[];
58
+ kept: string[];
59
+ }
60
+ /** The layers a line of the log says an adventure brought, by their file, each with the print of its text. */
61
+ export declare function layerPrints(value: unknown): Record<string, string>;
62
+ /**
63
+ * The campaign's layers as they are now, for an export (ADV-8): each over a page of the system goes, at the page's path; one over no page, one that does not read or
64
+ * writes what a layer never changes, and one an installed adventure brought and no one changed since stay out, by name. `brought` are the layers of the adventures installed.
65
+ */
66
+ export declare function layersToExport(root: string, world: World, system: string, brought: Record<string, string>[]): LayersExported;
67
+ /**
68
+ * Where each layer of an adventure goes (ADV-8): at its page's path in the campaign's layers. One over a page the campaign has its own layer on stays out, with what it
69
+ * would have given, unless the two are the same; one over no page of the rules the campaign plays stays out; one that does not read, or writes what a layer never
70
+ * changes, is a problem.
71
+ */
72
+ export declare function planLayers(root: string, world: World, adventure: AdventureFiles): LayersPlan;
73
+ /**
74
+ * The layers that go with an adventure taken out (ADV-8): those still as it wrote them. One changed since, with write or by hand, stays the campaign's, by the name of
75
+ * its page; one already gone is skipped; one another adventure of the campaign brought the same stays with that one. `others` are the layers of the other adventures.
76
+ */
77
+ export declare function layersToRemove(root: string, world: World, brought: Record<string, string>, others: Record<string, string>[]): LayersRemoval;
78
+ /**
79
+ * Refuses to bring an adventure in, or take it out, while the open scene holds copies of a page whose fields its layers change (INH-10), as write with layer does.
80
+ * Said without the page and its copies: the player reads it too, adding an adventure on the dashboard, and the remedy is the same.
81
+ */
82
+ export declare function checkLayersNotCopied(root: string, world: World, title: string, doing: "installed" | "taken out", pages: Page[]): void;
83
+ /**
84
+ * One public line for each counter of the player's party that changed with the pages of the system an adventure's layers came over, or went from (INH-11), as write
85
+ * with layer writes them. `before` is the world as it was, and `pages` those pages in it; a page that was not there then, as one the adventure brings, changed nothing.
86
+ */
87
+ export declare function logLayerCounters(root: string, context: Context, before: World, pages: Page[]): void;
88
+ /** The pages of the system as the master hears them changed: each by its name, with its type. */
89
+ export declare function changedPages(pages: Page[]): LayeredPage[];
@@ -0,0 +1,201 @@
1
+ // The layers an adventure carries over the pages of the game system it needs (ADV-8, SYS-11): which of the campaign's an export takes, where an install puts them and
2
+ // which it leaves out, and which go with the adventure when it is taken out.
3
+ import { basename, join, relative, sep } from "node:path";
4
+ import { Refusal } from "../../../core/refusal.js";
5
+ import { files } from "../../../core/store.js";
6
+ import { comparableText, textPrint, valueText } from "../../../core/text.js";
7
+ import { introductionText, layerFolder, layerParts, layersFolderName, layerTakes, pageWithPath, readFields, splitPageFile, systemOriginal, worldFolderName } from "../../../pages/index.js";
8
+ import { followersOfWrite, logCounterChanges } from "../../write/inheritance.js";
9
+ import { campaignCoins } from "../campaign/money.js";
10
+ import { campaignWorld } from "../campaign/world.js";
11
+ import { checkNotCopied, readOpenScene } from "../scene/scene.js";
12
+ /** The layers a line of the log says an adventure brought, by their file, each with the print of its text. */
13
+ export function layerPrints(value) {
14
+ return value === null || typeof value !== "object" || Array.isArray(value) ? {} : Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string"));
15
+ }
16
+ /**
17
+ * The campaign's layers as they are now, for an export (ADV-8): each over a page of the system goes, at the page's path; one over no page, one that does not read or
18
+ * writes what a layer never changes, and one an installed adventure brought and no one changed since stay out, by name. `brought` are the layers of the adventures installed.
19
+ */
20
+ export function layersToExport(root, world, system, brought) {
21
+ const byLayer = new Map((world.system?.pages ?? []).flatMap((page) => (page.layer === undefined ? [] : [[page.layer.path, page]])));
22
+ const exported = { carried: {}, pages: [], sourced: [], leftOut: [] };
23
+ for (const [path, bytes] of layerFiles(root, system)) {
24
+ const file = `${layersFolderName}/${system}/${path}`;
25
+ const page = byLayer.get(file);
26
+ const name = page?.name ?? basename(path, ".md");
27
+ const layer = readOrProblem(file, bytes);
28
+ if (page === undefined || typeof layer === "string") {
29
+ exported.leftOut.push({ name, why: typeof layer === "string" ? "unread" : "no-page" });
30
+ }
31
+ else if ((page.layer?.ignored.length ?? 0) > 0) {
32
+ exported.leftOut.push({ name, why: "forbidden" });
33
+ }
34
+ else if (isBrought(brought, file, bytes)) {
35
+ exported.leftOut.push({ name, why: "adventure" });
36
+ }
37
+ else if (!givesNothing(layer)) {
38
+ exported.carried[`${layersFolderName}/${system}/${systemOriginal(page).path}`] = bytes;
39
+ exported.pages.push(...changedPages([page]));
40
+ // From the player's documents when the layer says so, or the page under it does.
41
+ exported.sourced.push(...(layer.fields.source !== undefined || systemOriginal(page).fields.source !== undefined ? [name] : []));
42
+ }
43
+ }
44
+ return exported;
45
+ }
46
+ /**
47
+ * Where each layer of an adventure goes (ADV-8): at its page's path in the campaign's layers. One over a page the campaign has its own layer on stays out, with what it
48
+ * would have given, unless the two are the same; one over no page of the rules the campaign plays stays out; one that does not read, or writes what a layer never
49
+ * changes, is a problem.
50
+ */
51
+ export function planLayers(root, world, adventure) {
52
+ const system = world.system?.name ?? adventure.manifest.needs.system ?? "";
53
+ const there = layerFiles(root, system);
54
+ const own = new Map([...there.keys()].map((path) => [comparableText(path), path]));
55
+ // As the world lays a layer over a page: by their paths, compared as names are.
56
+ const under = new Map((world.system?.pages ?? []).map((page) => [comparableText(page.path), page]));
57
+ const plan = { writes: [], prints: {}, changed: [], kept: [], stray: [], problems: [] };
58
+ const targets = new Set();
59
+ for (const path of Object.keys(adventure.layers).sort()) {
60
+ const bytes = adventure.layers[path];
61
+ const file = `${layersFolderName}/${system}/${path}`;
62
+ const layer = readOrProblem(file, bytes);
63
+ const page = under.get(comparableText(path));
64
+ if (typeof layer === "string") {
65
+ plan.problems.push(`its layer over ${page?.name ?? basename(path, ".md")} does not read: ${layer}`);
66
+ continue;
67
+ }
68
+ // A file that gives nothing is no layer, as write leaves none.
69
+ if (givesNothing(layer)) {
70
+ continue;
71
+ }
72
+ const forbidden = Object.entries(layer.fields).filter(([field, value]) => !layerTakes(page === undefined ? pageless : systemOriginal(page), field, value, world.system?.fields)).map(([field]) => field);
73
+ if (forbidden.length > 0) {
74
+ plan.problems.push(`its layer over ${page?.name ?? basename(path, ".md")} writes ${forbidden.join(", ")}`);
75
+ continue;
76
+ }
77
+ if (page === undefined) {
78
+ plan.stray.push(basename(path, ".md"));
79
+ continue;
80
+ }
81
+ const held = own.get(comparableText(path));
82
+ const target = `${layersFolderName}/${system}/${held ?? systemOriginal(page).path}`;
83
+ // Two files whose names differ only as names are compared, such as by an apostrophe.
84
+ if (targets.has(target)) {
85
+ plan.problems.push(`two of its layers lie over ${page.name}`);
86
+ continue;
87
+ }
88
+ targets.add(target);
89
+ // The same as the campaign's by its print, which is what taking the adventure out compares.
90
+ if (held !== undefined && printOf(there.get(held) ?? new Uint8Array()) !== printOf(bytes)) {
91
+ const fields = Object.fromEntries(Object.entries(layer.fields).map(([field, value]) => [field, valueText(value)]));
92
+ plan.kept.push({ page: page.name, fields, sections: layer.sections, introduction: layer.introduction });
93
+ continue;
94
+ }
95
+ plan.prints[target] = printOf(bytes);
96
+ if (held === undefined) {
97
+ plan.writes.push({ path: target, bytes });
98
+ plan.changed.push({ page, fields: Object.keys(layer.fields) });
99
+ }
100
+ }
101
+ return plan;
102
+ }
103
+ /**
104
+ * The layers that go with an adventure taken out (ADV-8): those still as it wrote them. One changed since, with write or by hand, stays the campaign's, by the name of
105
+ * its page; one already gone is skipped; one another adventure of the campaign brought the same stays with that one. `others` are the layers of the other adventures.
106
+ */
107
+ export function layersToRemove(root, world, brought, others) {
108
+ const byLayer = new Map((world.system?.pages ?? []).flatMap((page) => (page.layer === undefined ? [] : [[comparableText(page.layer.path), page]])));
109
+ const removal = { paths: [], changed: [], kept: [] };
110
+ for (const [file, print] of Object.entries(brought)) {
111
+ const bytes = files().bytes(join(root, ...file.split("/")));
112
+ const page = byLayer.get(comparableText(file));
113
+ if (bytes === undefined) {
114
+ continue;
115
+ }
116
+ if (printOf(bytes) !== print) {
117
+ removal.kept.push(page?.name ?? basename(file, ".md"));
118
+ }
119
+ else if (!isBrought(others, file, bytes)) {
120
+ removal.paths.push(file);
121
+ removal.changed.push(...(page === undefined ? [] : [page]));
122
+ }
123
+ }
124
+ return removal;
125
+ }
126
+ /**
127
+ * Refuses to bring an adventure in, or take it out, while the open scene holds copies of a page whose fields its layers change (INH-10), as write with layer does.
128
+ * Said without the page and its copies: the player reads it too, adding an adventure on the dashboard, and the remedy is the same.
129
+ */
130
+ export function checkLayersNotCopied(root, world, title, doing, pages) {
131
+ const open = readOpenScene(root);
132
+ for (const page of pages) {
133
+ try {
134
+ checkNotCopied(world, open, page);
135
+ }
136
+ catch (error) {
137
+ if (!(error instanceof Refusal) || error.code !== "copies-in-scene") {
138
+ throw error;
139
+ }
140
+ throw new Refusal(`${title} cannot be ${doing} now: its layers change a page of the game system that has copies in the open scene, which read from it what they do not change. `
141
+ + `Close the scene first, then give the same command again. Nothing changed.`, "adventure-copies", { name: title });
142
+ }
143
+ }
144
+ }
145
+ /**
146
+ * One public line for each counter of the player's party that changed with the pages of the system an adventure's layers came over, or went from (INH-11), as write
147
+ * with layer writes them. `before` is the world as it was, and `pages` those pages in it; a page that was not there then, as one the adventure brings, changed nothing.
148
+ */
149
+ export function logLayerCounters(root, context, before, pages) {
150
+ if (pages.length === 0) {
151
+ return;
152
+ }
153
+ const now = campaignWorld(root, context);
154
+ const followers = new Set(pages.flatMap((page) => {
155
+ const after = now.system?.pages.find((one) => one.path === page.path);
156
+ return after === undefined ? [] : followersOfWrite(now, page, after).pages;
157
+ }));
158
+ const changed = now.pages.filter((page) => followers.has(page)).flatMap((page) => {
159
+ const was = pageWithPath(before, page.path);
160
+ return was === undefined ? [] : [[was, page]];
161
+ });
162
+ logCounterChanges(root, context, before, now, changed, campaignCoins(root, context).coins);
163
+ }
164
+ /** The pages of the system as the master hears them changed: each by its name, with its type. */
165
+ export function changedPages(pages) {
166
+ return pages.map((page) => ({ page: page.name, ...(page.type === undefined ? {} : { type: page.type }) }));
167
+ }
168
+ // A page of no type with no fields, for a layer over no page: only what no layer ever changes is refused of it.
169
+ const pageless = { name: "", path: "", fields: {}, body: "" };
170
+ /** The campaign's layers over the pages of a game system as their files are now, in order, by their path from the system's layers' folder, such as world/creatures/Goblin.md. */
171
+ function layerFiles(root, system) {
172
+ const folder = layerFolder(root, system);
173
+ const found = files().files(join(folder, worldFolderName), (name) => /\.md$/i.test(name)).map((file) => [relative(folder, file).split(sep).join("/"), files().bytes(file) ?? new Uint8Array()]);
174
+ return new Map(found.sort(([first], [second]) => (first < second ? -1 : 1)));
175
+ }
176
+ /** What a layer's file gives; it throws, naming the file, when its frontmatter does not read. */
177
+ function readLayer(file, bytes) {
178
+ const text = splitPageFile(new TextDecoder().decode(bytes));
179
+ return { fields: readFields(text.frontmatter, file), sections: layerParts(text.body).filter((part) => part.content !== "").map((part) => part.name), introduction: introductionText(text.body) !== "" };
180
+ }
181
+ /** What a layer's file gives, or why it does not read. */
182
+ function readOrProblem(file, bytes) {
183
+ try {
184
+ return readLayer(file, bytes);
185
+ }
186
+ catch (error) {
187
+ return error instanceof Error ? error.message : String(error);
188
+ }
189
+ }
190
+ function givesNothing(layer) {
191
+ return Object.keys(layer.fields).length === 0 && layer.sections.length === 0 && !layer.introduction;
192
+ }
193
+ /** Whether one of these adventures brought the layer in this file as it is now: the same file, as names are compared, with the same print. */
194
+ function isBrought(brought, file, bytes) {
195
+ const print = printOf(bytes);
196
+ return brought.some((layers) => Object.entries(layers).some(([path, known]) => comparableText(path) === comparableText(file) && known === print));
197
+ }
198
+ /** The print of a layer's file, by which one changed since it was written is told. */
199
+ function printOf(bytes) {
200
+ return textPrint(new TextDecoder().decode(bytes));
201
+ }