rpg-engine 0.4.1 → 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.
Files changed (231) hide show
  1. package/README.md +1 -1
  2. package/dist/campaign/settings.d.ts +1 -0
  3. package/dist/campaign/settings.js +6 -5
  4. package/dist/command-line.js +12 -5
  5. package/dist/commands/advance.js +1 -1
  6. package/dist/commands/ask.d.ts +37 -2
  7. package/dist/commands/ask.js +106 -35
  8. package/dist/commands/change.js +28 -17
  9. package/dist/commands/config.d.ts +1 -1
  10. package/dist/commands/config.js +69 -21
  11. package/dist/commands/effect.js +42 -23
  12. package/dist/commands/find.js +1 -1
  13. package/dist/commands/game-adventures.js +2 -2
  14. package/dist/commands/game-catalog.d.ts +13 -0
  15. package/dist/commands/game-catalog.js +26 -2
  16. package/dist/commands/game-checks.js +23 -21
  17. package/dist/commands/{game-hit-dice.d.ts → game-dice-pool.d.ts} +3 -3
  18. package/dist/commands/{game-hit-dice.js → game-dice-pool.js} +7 -7
  19. package/dist/commands/game-effects.d.ts +12 -4
  20. package/dist/commands/game-effects.js +21 -13
  21. package/dist/commands/game-examples.d.ts +22 -0
  22. package/dist/commands/game-examples.js +72 -0
  23. package/dist/commands/game-extension-list.d.ts +31 -3
  24. package/dist/commands/game-extension-list.js +94 -6
  25. package/dist/commands/game-images.d.ts +1 -1
  26. package/dist/commands/game-images.js +3 -2
  27. package/dist/commands/game-library.d.ts +3 -0
  28. package/dist/commands/game-library.js +2 -2
  29. package/dist/commands/game-mode.js +1 -1
  30. package/dist/commands/game-package-code.d.ts +87 -0
  31. package/dist/commands/game-package-code.js +648 -0
  32. package/dist/commands/game-package-hooks.d.ts +37 -0
  33. package/dist/commands/game-package-hooks.js +116 -0
  34. package/dist/commands/game-recipes.js +2 -2
  35. package/dist/commands/game-reminders.d.ts +5 -2
  36. package/dist/commands/game-reminders.js +7 -4
  37. package/dist/commands/game-rules.d.ts +4 -1
  38. package/dist/commands/game-rules.js +7 -1
  39. package/dist/commands/game-upkeep.js +3 -3
  40. package/dist/commands/game-view-place.d.ts +8 -20
  41. package/dist/commands/game-view-place.js +16 -35
  42. package/dist/commands/game-widgets.d.ts +42 -8
  43. package/dist/commands/game-widgets.js +182 -31
  44. package/dist/commands/game-world.d.ts +32 -3
  45. package/dist/commands/game-world.js +113 -24
  46. package/dist/commands/image.js +1 -1
  47. package/dist/commands/init.js +2 -2
  48. package/dist/commands/mode.d.ts +1 -1
  49. package/dist/commands/package.d.ts +15 -1
  50. package/dist/commands/package.js +96 -24
  51. package/dist/commands/progression.d.ts +2 -0
  52. package/dist/commands/progression.js +7 -2
  53. package/dist/commands/roll.d.ts +2 -0
  54. package/dist/commands/roll.js +50 -24
  55. package/dist/commands/rules.js +6 -4
  56. package/dist/commands/scene.d.ts +2 -2
  57. package/dist/commands/scene.js +13 -10
  58. package/dist/commands/sheet.js +10 -1
  59. package/dist/commands/status.d.ts +1 -0
  60. package/dist/commands/status.js +14 -4
  61. package/dist/commands/trade.js +6 -4
  62. package/dist/commands/translate.js +2 -2
  63. package/dist/commands/travel.js +1 -1
  64. package/dist/commands/validate.js +2 -2
  65. package/dist/commands/view.d.ts +12 -4
  66. package/dist/commands/view.js +11 -8
  67. package/dist/commands/worked-out.js +11 -5
  68. package/dist/commands/write.d.ts +8 -1
  69. package/dist/commands/write.js +84 -25
  70. package/dist/core/command-reference.js +3 -2
  71. package/dist/core/consents.d.ts +18 -0
  72. package/dist/core/consents.js +93 -0
  73. package/dist/core/context.d.ts +68 -27
  74. package/dist/core/context.js +3 -1
  75. package/dist/core/examples.d.ts +25 -0
  76. package/dist/core/examples.js +19 -0
  77. package/dist/core/files.d.ts +2 -0
  78. package/dist/core/files.js +10 -1
  79. package/dist/core/package-api.d.ts +388 -0
  80. package/dist/core/package-declarations.d.ts +102 -0
  81. package/dist/core/package-declarations.js +81 -0
  82. package/dist/core/registry.d.ts +4 -0
  83. package/dist/core/text.d.ts +5 -0
  84. package/dist/core/text.js +12 -0
  85. package/dist/dashboard/acts.js +31 -24
  86. package/dist/dashboard/address.js +4 -1
  87. package/dist/dashboard/api.js +65 -3
  88. package/dist/dashboard/app.js +222 -98
  89. package/dist/dashboard/ask-card.js +169 -0
  90. package/dist/dashboard/asked-dice.js +57 -0
  91. package/dist/dashboard/dice/tray.js +6 -4
  92. package/dist/dashboard/extension-widgets.js +136 -16
  93. package/dist/dashboard/faces.js +6 -4
  94. package/dist/dashboard/help.js +10 -3
  95. package/dist/dashboard/i18n/ar.json +34 -29
  96. package/dist/dashboard/i18n/cs.json +34 -29
  97. package/dist/dashboard/i18n/da.json +34 -29
  98. package/dist/dashboard/i18n/de.json +34 -29
  99. package/dist/dashboard/i18n/el.json +34 -29
  100. package/dist/dashboard/i18n/es.json +34 -29
  101. package/dist/dashboard/i18n/fi.json +34 -29
  102. package/dist/dashboard/i18n/fr.json +34 -29
  103. package/dist/dashboard/i18n/he.json +34 -29
  104. package/dist/dashboard/i18n/hi.json +34 -29
  105. package/dist/dashboard/i18n/hu.json +34 -29
  106. package/dist/dashboard/i18n/is.json +34 -29
  107. package/dist/dashboard/i18n/it.json +34 -29
  108. package/dist/dashboard/i18n/ja.json +34 -29
  109. package/dist/dashboard/i18n/ko.json +34 -29
  110. package/dist/dashboard/i18n/nb.json +34 -29
  111. package/dist/dashboard/i18n/nl.json +34 -29
  112. package/dist/dashboard/i18n/pl.json +34 -29
  113. package/dist/dashboard/i18n/pt.json +34 -29
  114. package/dist/dashboard/i18n/ro.json +34 -29
  115. package/dist/dashboard/i18n/ru.json +34 -29
  116. package/dist/dashboard/i18n/sv.json +34 -29
  117. package/dist/dashboard/i18n/tr.json +34 -29
  118. package/dist/dashboard/i18n/uk.json +34 -29
  119. package/dist/dashboard/i18n/zh.json +34 -29
  120. package/dist/dashboard/icons.js +4 -1
  121. package/dist/dashboard/kit.js +35 -0
  122. package/dist/dashboard/labels.js +1 -0
  123. package/dist/dashboard/pack-card.js +97 -0
  124. package/dist/dashboard/package-page.js +50 -0
  125. package/dist/dashboard/packages.js +180 -24
  126. package/dist/dashboard/page-code.js +248 -0
  127. package/dist/dashboard/party.js +13 -14
  128. package/dist/dashboard/people.js +4 -0
  129. package/dist/dashboard/person-panel.js +110 -0
  130. package/dist/dashboard/place-map-view.js +11 -6
  131. package/dist/dashboard/rolls.js +2 -2
  132. package/dist/dashboard/scene.js +12 -9
  133. package/dist/dashboard/settings.js +55 -9
  134. package/dist/dashboard/sheet.js +21 -13
  135. package/dist/dashboard/status.js +69 -15
  136. package/dist/dashboard/style.css +859 -574
  137. package/dist/dashboard/top.js +75 -25
  138. package/dist/dashboard/turns.js +34 -13
  139. package/dist/dashboard/widgets.js +9 -2
  140. package/dist/dashboard-server.js +246 -11
  141. package/dist/dice/roll-dice.js +1 -1
  142. package/dist/effects/effect.d.ts +7 -4
  143. package/dist/effects/effect.js +12 -9
  144. package/dist/extensions/engine.d.ts +4 -0
  145. package/dist/extensions/engine.js +74 -0
  146. package/dist/extensions/extension.d.ts +28 -33
  147. package/dist/extensions/extension.js +37 -46
  148. package/dist/extensions/index.d.ts +4 -3
  149. package/dist/extensions/index.js +5 -2
  150. package/dist/extensions/packages.d.ts +14 -0
  151. package/dist/extensions/packages.js +148 -12
  152. package/dist/extensions/words.d.ts +2 -1
  153. package/dist/extensions/words.js +5 -4
  154. package/dist/index.d.ts +1 -3
  155. package/dist/mcp-server.js +52 -15
  156. package/dist/page-words.d.ts +4 -0
  157. package/dist/page-words.js +4 -4
  158. package/dist/page.d.ts +218 -0
  159. package/dist/page.js +2 -0
  160. package/dist/pages/index.d.ts +1 -1
  161. package/dist/pages/index.js +1 -1
  162. package/dist/pages/load.d.ts +7 -1
  163. package/dist/pages/load.js +10 -5
  164. package/dist/pages/page-file.js +1 -1
  165. package/dist/pages/stack-writes.js +5 -4
  166. package/dist/pages/standard-fields.d.ts +2 -0
  167. package/dist/pages/standard-fields.js +8 -1
  168. package/dist/pages/world.d.ts +8 -0
  169. package/dist/pages/world.js +9 -2
  170. package/dist/systems/game-system.d.ts +61 -30
  171. package/dist/systems/game-system.js +159 -50
  172. package/dist/systems/index.d.ts +3 -3
  173. package/dist/systems/index.js +3 -3
  174. package/dist/systems/system-code.d.ts +29 -88
  175. package/dist/systems/system-code.js +68 -23
  176. package/dist/systems/words.d.ts +2 -0
  177. package/dist/systems/words.js +11 -0
  178. package/dist/testing.d.ts +76 -0
  179. package/dist/testing.js +197 -0
  180. package/instructions/combat.md +2 -2
  181. package/instructions/core.md +2 -2
  182. package/instructions/dashboard.md +1 -1
  183. package/instructions/mentor-tutorial.md +1 -1
  184. package/instructions/play.md +2 -2
  185. package/instructions/setup.md +1 -1
  186. package/package.json +15 -2
  187. package/systems/5e/LICENSE.md +13 -3
  188. package/systems/5e/dist/dying.js +83 -16
  189. package/systems/5e/dist/index.js +2 -2
  190. package/systems/5e/dist/page-style.js +634 -0
  191. package/systems/5e/dist/page.js +166 -0
  192. package/systems/5e/dist/shown.js +23 -0
  193. package/systems/5e/instructions/combat.md +1 -0
  194. package/systems/5e/package.json +4 -2
  195. package/systems/5e/system.yaml +35 -7
  196. package/systems/5e/versions/srd-5.1/system.yaml +2 -0
  197. package/systems/5e/words/ar.yaml +30 -0
  198. package/systems/5e/words/cs.yaml +30 -0
  199. package/systems/5e/words/da.yaml +30 -0
  200. package/systems/5e/words/de.yaml +30 -0
  201. package/systems/5e/words/el.yaml +30 -0
  202. package/systems/5e/words/es.yaml +30 -0
  203. package/systems/5e/words/fi.yaml +30 -0
  204. package/systems/5e/words/fr.yaml +30 -0
  205. package/systems/5e/words/he.yaml +30 -0
  206. package/systems/5e/words/hi.yaml +30 -0
  207. package/systems/5e/words/hu.yaml +30 -0
  208. package/systems/5e/words/is.yaml +30 -0
  209. package/systems/5e/words/it.yaml +30 -0
  210. package/systems/5e/words/ja.yaml +30 -0
  211. package/systems/5e/words/ko.yaml +30 -0
  212. package/systems/5e/words/nb.yaml +30 -0
  213. package/systems/5e/words/nl.yaml +30 -0
  214. package/systems/5e/words/pl.yaml +30 -0
  215. package/systems/5e/words/pt.yaml +30 -0
  216. package/systems/5e/words/ro.yaml +30 -0
  217. package/systems/5e/words/ru.yaml +30 -0
  218. package/systems/5e/words/sv.yaml +30 -0
  219. package/systems/5e/words/tr.yaml +30 -0
  220. package/systems/5e/words/uk.yaml +30 -0
  221. package/systems/5e/words/zh.yaml +30 -0
  222. package/dist/commands/game-extensions.d.ts +0 -34
  223. package/dist/commands/game-extensions.js +0 -182
  224. package/dist/commands/game-system-code.d.ts +0 -25
  225. package/dist/commands/game-system-code.js +0 -172
  226. package/dist/dashboard/death-save.js +0 -67
  227. package/dist/dashboard/dying.js +0 -46
  228. package/dist/dashboard/save-outcome.js +0 -42
  229. package/dist/dashboard/spend.js +0 -115
  230. package/dist/extensions/api.d.ts +0 -155
  231. /package/dist/{extensions/api.js → core/package-api.js} +0 -0
@@ -1,24 +1,29 @@
1
- // The widgets of the campaign's extensions on the dashboard (D-502): where each stands, as its extension.yaml puts it and campaign.yaml moves it, and what its
2
- // code's hook shows now, checked and kept short, in the campaign's language. The extension's state never reaches the page: only what its hook gives.
1
+ // The widgets of the campaign's packages on the dashboard (D-502, D-514), its game system's and its extensions': where each stands, as its extension.yaml or
2
+ // system.yaml puts it and campaign.yaml moves it, and what its code's hook shows now, checked and kept short, in the campaign's language. The package's state
3
+ // never reaches the page: only what its hook gives.
3
4
  import { basename } from "node:path";
4
5
  import { z } from "zod";
5
6
  import { campaignFileName, readOneSetting, readTextSetting } from "../campaign/index.js";
7
+ import { widgetPlaces } from "../core/package-declarations.js";
6
8
  import { files } from "../core/store.js";
7
9
  import { comparableText } from "../core/text.js";
8
- import { extensionWords, widgetPages, widgetPlaces, wordIn, } from "../extensions/index.js";
10
+ import { wordIn } from "../extensions/index.js";
9
11
  import { findImage } from "../images/index.js";
10
- import { campaignExtensions } from "./game-extension-list.js";
11
- import { extensionCode, gameViewOf } from "./game-extensions.js";
12
+ import { cardPages } from "../page.js";
13
+ import { isInParty } from "../pages/index.js";
14
+ import { campaignExtensions, packageWords, pageAddress, runsCode } from "./game-extension-list.js";
15
+ import { campaignPackages, gameViewOf, packageCode } from "./game-package-code.js";
16
+ import { campaignWorld } from "./game-world.js";
12
17
  /** The places a campaign may put a widget in, in campaign.yaml: top, side, a page, which is a card in that page, or off. */
13
- export const widgetSpots = [...widgetPlaces.filter((place) => place !== "page"), ...widgetPages, "off"];
18
+ export const widgetSpots = [...widgetPlaces.filter((place) => place !== "page" && place !== "card"), ...cardPages, "off"];
14
19
  // How long the hooks may take, all together, before the page goes on without those still at work, and the most a widget may give, as JSON.
15
20
  const hookTime = 2000;
16
21
  const mostShown = 12_000;
17
22
  // The hooks still at work, by campaign, extension and widget: a reading that comes meanwhile waits on the same call, never calls again.
18
23
  const working = new Map();
19
24
  /**
20
- * The widgets of the campaign's extensions, in the order of the extensions and of their widgets, each where it stands and with what its hook shows now. The
21
- * hooks run together, within one time for all; one that takes longer shows nothing this time, and is not called again while it works.
25
+ * The widgets of the campaign's packages, its game system's first, in the order of the packages and of their widgets, each where it stands and with what its
26
+ * hook shows now. The hooks run together, within one time for all; one that takes longer shows nothing this time, and is not called again while it works.
22
27
  */
23
28
  export async function campaignWidgets(root, context) {
24
29
  const spots = campaignSpots(root);
@@ -28,9 +33,9 @@ export async function campaignWidgets(root, context) {
28
33
  timer = setTimeout(() => done("late"), hookTime).unref();
29
34
  });
30
35
  const reading = [];
31
- for (const extension of campaignExtensions(root, context).extensions) {
36
+ for (const extension of campaignPackages(root, context)) {
32
37
  const words = wordsOf(extension, language);
33
- const code = extension.widgets.length === 0 ? undefined : extensionCode(extension).then((loaded) => ({ loaded }), (error) => ({ error: messageOf(error) }));
38
+ const code = extension.widgets.length === 0 ? undefined : packageCode(extension, context).then((loaded) => ({ loaded }), (error) => ({ error: messageOf(error) }));
34
39
  for (const widget of extension.widgets) {
35
40
  const id = `${extension.name}.${widget.name}`;
36
41
  const own = ownSpot(widget);
@@ -45,12 +50,17 @@ export async function campaignWidgets(root, context) {
45
50
  own,
46
51
  ...(widget.icon === undefined ? {} : { icon: extension.icons[widget.icon] }),
47
52
  ...(words.problem === undefined ? {} : { problem: words.problem }),
53
+ ...pageOf(extension),
54
+ // Its words for its page code's own words, in the campaign's language (D-510).
55
+ ...(extension.page === undefined || words.read === undefined || Object.keys(words.read.words).length === 0 ? {} : { words: words.read.words }),
48
56
  };
49
- if (spot.place === "off" || code === undefined) {
57
+ // A widget only of the cards of questions is read when one asks in it, never here.
58
+ if (spot.place === "off" || spot.place === "card" || code === undefined) {
50
59
  reading.push(Promise.resolve(base));
51
60
  continue;
52
61
  }
53
- reading.push(code.then((loaded) => widgetNow(root, context, extension, widget.name, base, loaded, words.read, late)));
62
+ const party = spot.place === "person" ? partyNames(root, context) : undefined;
63
+ reading.push(code.then((loaded) => widgetNow(root, context, extension, widget.name, base, loaded, words.read, late, party)));
54
64
  }
55
65
  }
56
66
  try {
@@ -69,12 +79,15 @@ export function extensionWordsProblems(root, context) {
69
79
  });
70
80
  }
71
81
  /** A widget with what its hook shows now, or why it shows nothing: its code, a hook it lacks, a hook that failed or was late, what it gave. */
72
- async function widgetNow(root, context, extension, name, base, loaded, words, late) {
82
+ async function widgetNow(root, context, extension, name, base, loaded, words, late, party) {
73
83
  const hook = "error" in loaded ? undefined : loaded.loaded?.widgets?.[name];
74
84
  if (hook === undefined) {
75
85
  const problem = "error" in loaded ? `its code cannot be used: ${loaded.error}` : loaded.loaded === undefined ? "its code does not run in this campaign" : `its code gives nothing for the widget ${name}`;
76
86
  return { ...base, problem };
77
87
  }
88
+ if (party !== undefined) {
89
+ return await peopleNow(root, context, extension, name, base, hook, words, late, party);
90
+ }
78
91
  try {
79
92
  const answer = await Promise.race([hookCall(root, extension, name, () => hook(gameViewOf(root, context, extension))), late]);
80
93
  if (answer === "late") {
@@ -87,6 +100,32 @@ async function widgetNow(root, context, extension, name, base, loaded, words, la
87
100
  return { ...base, problem: messageOf(error) };
88
101
  }
89
102
  }
103
+ /**
104
+ * A widget in place person with what its hook shows on each of the party, by name (D-514): one called for each, together; one that fails or is late shows
105
+ * nothing on them, and the first problem says why.
106
+ */
107
+ async function peopleNow(root, context, extension, name, base, hook, words, late, party) {
108
+ const shown = await Promise.all(party.map(async (who) => {
109
+ try {
110
+ const answer = await Promise.race([hookCall(root, extension, `${name}#person#${who}`, () => hook(gameViewOf(root, context, extension), { who })), late]);
111
+ if (answer === "late") {
112
+ return { problem: `it gave nothing in ${hookTime / 1000} seconds` };
113
+ }
114
+ return answer === undefined ? {} : { shows: [who, shownContent(extension, words, answer)] };
115
+ }
116
+ catch (error) {
117
+ return { problem: messageOf(error) };
118
+ }
119
+ }));
120
+ const people = Object.fromEntries(shown.flatMap((one) => (one.shows === undefined ? [] : [one.shows])));
121
+ // The first in the party's order, whichever answered first.
122
+ const problem = shown.find((one) => one.problem !== undefined)?.problem;
123
+ return { ...base, ...(Object.keys(people).length === 0 ? {} : { people }), ...(problem === undefined ? {} : { problem }) };
124
+ }
125
+ /** The names of the party, the player's characters and their companions, in the order of the pages. */
126
+ function partyNames(root, context) {
127
+ return campaignWorld(root, context).pages.filter(isInParty).map((page) => page.name);
128
+ }
90
129
  /** A hook's call, or the one already at work for the same widget of the same campaign. */
91
130
  function hookCall(root, extension, widget, call) {
92
131
  const key = `${root}|${extension.package}@${extension.version}|${widget}`;
@@ -98,21 +137,31 @@ function hookCall(root, extension, widget, call) {
98
137
  working.set(key, started);
99
138
  return started;
100
139
  }
101
- /** The widgets of the campaign's extensions by the names campaign.yaml gives them, as weather.today, with where each extension puts it. */
140
+ /** The widgets of the campaign's packages by the names campaign.yaml gives them, as weather.today, with where each package puts it. */
102
141
  export function campaignWidgetNames(root, context) {
103
- return Object.fromEntries(campaignExtensions(root, context).extensions.flatMap((extension) => extension.widgets.map((widget) => [`${extension.name}.${widget.name}`, ownSpot(widget)])));
142
+ return Object.fromEntries(campaignPackages(root, context).flatMap((extension) => extension.widgets.map((widget) => [`${extension.name}.${widget.name}`, ownSpot(widget)])));
104
143
  }
105
144
  /**
106
145
  * Where campaign.yaml would put the widgets it names, checked before it is written: each a widget of the campaign's extensions, each place one of those the
107
146
  * dashboard has. It throws what is wrong, and how to write it.
108
147
  */
109
148
  export function checkWidgetSpots(root, context, entries) {
110
- const known = Object.keys(campaignWidgetNames(root, context));
149
+ const spots = campaignWidgetNames(root, context);
150
+ const known = Object.keys(spots);
111
151
  for (const [name, value] of Object.entries(entries)) {
112
- if (!known.some((one) => comparableText(one) === comparableText(name))) {
113
- const names = known.length === 0 ? "the campaign's extensions have none" : `the campaign's are ${known.join(", ")}`;
152
+ const own = Object.entries(spots).find(([one]) => comparableText(one) === comparableText(name))?.[1];
153
+ if (own === undefined) {
154
+ const names = known.length === 0 ? "the campaign's packages have none" : `the campaign's are ${known.join(", ")}`;
114
155
  throw new Error(`There is no widget ${name}: ${names}. Write one as ${known[0] ?? "weather.today"}: side. Nothing changed.`);
115
156
  }
157
+ // A widget made for each of the party shows only there, or nowhere, as its hook needs who (D-514); another may come there too (D-516).
158
+ const place = typeof value === "string" ? value : value !== null && typeof value === "object" && "place" in value ? value.place : undefined;
159
+ if (own.place === "person" && place !== "person" && place !== "off") {
160
+ throw new Error(`${name} stands on each of the party: write ${name}: off, or ${name}: person. Nothing changed.`);
161
+ }
162
+ if (own.place === "card") {
163
+ throw new Error(`${name} shows only in the card its package asks a roll in, so it has no place to choose: take ${name} out of widgets. Nothing changed.`);
164
+ }
116
165
  if (!spotSchema.safeParse(value).success) {
117
166
  // Of a place written as its parts, what is wrong with them, as an order that is no whole number.
118
167
  const issue = value !== null && typeof value === "object" ? spotObject.safeParse(value).error?.issues[0] : undefined;
@@ -121,12 +170,14 @@ export function checkWidgetSpots(root, context, entries) {
121
170
  }
122
171
  }
123
172
  }
173
+ // The places campaign.yaml may give a widget.
174
+ const spotPlaces = [...widgetPlaces.filter((place) => place !== "card"), "off"];
124
175
  // The most and least order of a card among those of its place.
125
176
  const orderWords = "is a whole number from -100 to 100";
126
177
  /** A widget's place in campaign.yaml as its place, page and order. */
127
178
  const spotObject = z.object({
128
- place: z.enum([...widgetPlaces, "off"], { error: `is ${[...widgetPlaces, "off"].join(", ")}` }).optional(),
129
- page: z.enum(widgetPages, { error: `is ${widgetPages.join(", ")}` }).optional(),
179
+ place: z.enum(spotPlaces, { error: `is ${spotPlaces.join(", ")}` }).optional(),
180
+ page: z.enum(cardPages, { error: `is ${cardPages.join(", ")}` }).optional(),
130
181
  order: z.int({ error: orderWords }).min(-100, { error: orderWords }).max(100, { error: orderWords }).optional(),
131
182
  }, { error: (issue) => (issue.code === "unrecognized_keys" ? `has no ${issue.keys.join(", ")}: only place, page and order` : undefined) }).strict().refine((spot) => spot.place !== "page" || spot.page !== undefined, { error: "a card in place page names its page: scene, sheet, world or journal" });
132
183
  /** A widget's place in campaign.yaml: a word, or its place, page and order. */
@@ -151,20 +202,28 @@ function ownSpot(widget) {
151
202
  }
152
203
  /** Where the campaign puts a widget: a page's name is a card in that page; a place without its page keeps the extension's page, else the Scene. */
153
204
  function spotOf(given, own) {
205
+ // A widget made for each of the party is there or off, as its hook needs who; another may come there too, a small card on each (D-516).
206
+ const named = typeof given === "string" ? given : given.place;
207
+ if (own.place === "person") {
208
+ return named === "off" ? { place: "off", order: own.order } : own;
209
+ }
210
+ if (own.place === "card") {
211
+ return own;
212
+ }
154
213
  if (typeof given === "string") {
155
- return widgetPages.includes(given) ? { place: "page", page: given, order: own.order } : { place: given, order: own.order };
214
+ return cardPages.includes(given) ? { place: "page", page: given, order: own.order } : { place: given, order: own.order };
156
215
  }
157
216
  const place = given.place ?? (given.page === undefined ? own.place : "page");
158
217
  const page = place === "page" ? given.page ?? own.page ?? "scene" : undefined;
159
218
  return { place, ...(page === undefined ? {} : { page }), order: given.order ?? own.order };
160
219
  }
161
- /** An extension's words in the campaign's language, none where it is written in it; or why they cannot be read, with their file. */
220
+ /** A package's words in the campaign's language, none where it is written in it; or why they cannot be read, with their file. */
162
221
  function wordsOf(extension, language) {
163
222
  if (language === undefined || language.split("-")[0] === extension.language.split("-")[0]) {
164
223
  return {};
165
224
  }
166
225
  try {
167
- const read = extensionWords(extension.folder, language);
226
+ const read = packageWords(extension, language);
168
227
  return read === undefined ? {} : { read };
169
228
  }
170
229
  catch (error) {
@@ -184,24 +243,45 @@ const textSchema = z.union([
184
243
  z.object({ text: text(240), values: z.record(z.string().regex(/^\w+$/), valueSchema).refine((values) => Object.keys(values).length <= 8, "at most 8 values") }).strict(),
185
244
  ]);
186
245
  const iconName = z.string().regex(/^[a-z][a-z0-9-]*$/);
246
+ const toneSchema = z.enum(["good", "bad"]);
187
247
  const partSchema = z.union([
188
- z.object({ lead: textSchema, icon: iconName.optional(), value: valueSchema.optional() }).strict(),
248
+ z.object({ lead: textSchema, icon: iconName.optional(), value: valueSchema.optional(), tone: toneSchema.optional(), figure: iconName.optional() }).strict(),
249
+ z.object({ who: text(80), does: textSchema.optional() }).strict(),
189
250
  z.object({ text: textSchema }).strict(),
190
251
  z.object({ facts: z.array(textSchema).max(12) }).strict(),
191
252
  z.object({ values: z.array(z.object({ label: textSchema, value: valueSchema }).strict()).max(12) }).strict(),
192
- z.object({ track: z.object({ label: textSchema, value: z.int().min(0), max: z.int().min(1).max(100), look: z.enum(["pips", "bar"]).optional() }).strict() }).strict(),
253
+ z.object({
254
+ track: z.object({
255
+ label: textSchema,
256
+ value: z.int().min(0),
257
+ max: z.int().min(1).max(100),
258
+ look: z.enum(["pips", "bar", "steps"]).optional(),
259
+ held: z.int().min(0).optional(),
260
+ marked: z.int().min(0).optional(),
261
+ coming: z.int().min(0).optional(),
262
+ stage: z.enum(["start", "center", "end"]).optional(),
263
+ tone: toneSchema.optional(),
264
+ figure: iconName.optional(),
265
+ field: z.string().min(1).max(60).optional(),
266
+ }).strict(),
267
+ }).strict(),
193
268
  z.object({ rows: z.array(z.object({ label: textSchema, value: valueSchema.optional(), note: textSchema.optional() }).strict()).max(24) }).strict(),
194
269
  z.object({ table: z.object({ head: z.array(textSchema).min(1).max(6), rows: z.array(z.array(valueSchema).max(6)).max(24) }).strict() }).strict(),
195
270
  z.object({ image: z.string().regex(/^[\w .-]+\.(png|jpe?g|webp|svg)$/i), alt: textSchema }).strict(),
196
271
  ]);
197
272
  const widgetSchema = z.object({
273
+ // JSON for the page code alone, its size held by the most a widget gives.
274
+ data: z.json().optional(),
275
+ body: iconName.optional(),
198
276
  icon: iconName.optional(),
199
277
  chip: textSchema.optional(),
278
+ dead: z.boolean().optional(),
279
+ tint: z.enum(["low", "ally", "gold", "muted"], { error: "a card's tint is low, ally, gold or muted" }).optional(),
200
280
  says: textSchema.optional(),
201
281
  note: textSchema.optional(),
202
282
  parts: z.array(partSchema).max(12).optional(),
203
283
  }).strict();
204
- /** What a hook gave, checked, translated, its icons and pictures found; it throws what the dashboard cannot show, for whoever made the extension. */
284
+ /** What a hook gave, checked, translated, its icons and pictures found; it throws what the dashboard cannot show, for whoever made the package. */
205
285
  function shownContent(extension, words, given) {
206
286
  const parsed = widgetSchema.safeParse(given);
207
287
  if (!parsed.success) {
@@ -213,24 +293,40 @@ function shownContent(extension, words, given) {
213
293
  }
214
294
  const icon = (name) => {
215
295
  if (name !== undefined && extension.icons[name] === undefined) {
216
- throw new Error(`it names the icon ${name}, which its extension.yaml does not have`);
296
+ throw new Error(`it names the icon ${name}, which its ${fileOf(extension)} does not have`);
217
297
  }
218
298
  return name === undefined ? undefined : extension.icons[name];
219
299
  };
220
300
  const said = (one) => (typeof one === "string" ? wordIn(words, one) : { text: wordIn(words, one.text), values: Object.fromEntries(Object.entries(one.values).map(([name, value]) => [name, valueIn(value)])) });
221
301
  const valueIn = (value) => typeof value === "number" ? value : typeof value === "string" ? wordIn(words, value) : value.unit === undefined ? value : { ...value, unit: wordIn(words, value.unit) };
222
- const { icon: ownIcon, chip, says, note, parts } = parsed.data;
302
+ // A figure it names is one its package declares, for its page code to draw.
303
+ const figure = (name) => {
304
+ if (name !== undefined && extension.figures[name] === undefined) {
305
+ throw new Error(`it names the figure ${name}, which its ${fileOf(extension)} does not declare in figures`);
306
+ }
307
+ return name === undefined ? {} : { figure: name };
308
+ };
309
+ const { icon: ownIcon, chip, dead, tint, says, note, parts, data, body } = parsed.data;
223
310
  const shownIcon = icon(ownIcon);
311
+ const inside = body === undefined ? {} : { body: figure(body).figure };
224
312
  return {
313
+ ...(data === undefined ? {} : { data }),
314
+ ...inside,
225
315
  ...(shownIcon === undefined ? {} : { icon: shownIcon }),
226
316
  ...(chip === undefined ? {} : { chip: said(chip) }),
317
+ ...(dead === true ? { dead: true } : {}),
318
+ ...(tint === undefined ? {} : { tint }),
227
319
  ...(says === undefined ? {} : { says: said(says) }),
228
320
  ...(note === undefined ? {} : { note: said(note) }),
229
321
  ...(parts === undefined ? {} : {
230
322
  parts: parts.map((part) => {
231
323
  if ("lead" in part) {
232
324
  const leadIcon = icon(part.icon);
233
- return { lead: said(part.lead), ...(leadIcon === undefined ? {} : { icon: leadIcon }), ...(part.value === undefined ? {} : { value: valueIn(part.value) }) };
325
+ const tone = part.tone === undefined ? {} : { tone: part.tone };
326
+ return { lead: said(part.lead), ...(leadIcon === undefined ? {} : { icon: leadIcon }), ...(part.value === undefined ? {} : { value: valueIn(part.value) }), ...tone, ...figure(part.figure) };
327
+ }
328
+ if ("who" in part) {
329
+ return { who: part.who, ...(part.does === undefined ? {} : { does: said(part.does) }) };
234
330
  }
235
331
  if ("text" in part) {
236
332
  return { text: said(part.text) };
@@ -242,7 +338,7 @@ function shownContent(extension, words, given) {
242
338
  return { values: part.values.map((one) => ({ label: said(one.label), value: valueIn(one.value) })) };
243
339
  }
244
340
  if ("track" in part) {
245
- return { track: { ...part.track, label: said(part.track.label), value: Math.min(part.track.value, part.track.max) } };
341
+ return { track: shownTrack({ ...part.track, label: said(part.track.label), ...figure(part.track.figure) }) };
246
342
  }
247
343
  if ("rows" in part) {
248
344
  return {
@@ -261,7 +357,58 @@ function shownContent(extension, words, given) {
261
357
  }),
262
358
  };
263
359
  }
264
- /** A picture of the extension's images/, as the dashboard serves it at an address of the extension's own; it throws when the extension has none by that name. */
360
+ /** A count as the page draws it: the lit ones, then those held, marked and coming, each cut to what is left of its maximum. */
361
+ function shownTrack(track) {
362
+ let left = track.max;
363
+ const fit = (count) => {
364
+ const kept = count === undefined ? undefined : Math.min(count, left);
365
+ left -= kept ?? 0;
366
+ return kept;
367
+ };
368
+ const value = fit(track.value) ?? 0;
369
+ const [held, marked, coming] = [fit(track.held), fit(track.marked), fit(track.coming)];
370
+ return { ...track, value, ...(held === undefined ? {} : { held }), ...(marked === undefined ? {} : { marked }), ...(coming === undefined ? {} : { coming }) };
371
+ }
372
+ /**
373
+ * What one widget of a package shows now, for the card of a question (D-507): read as the dashboard reads it, in the campaign's language; nothing when its
374
+ * hook is not there, fails, gives what no part draws, or is late.
375
+ */
376
+ export async function widgetCard(root, context, extension, name, key, who) {
377
+ return await widgetFor(root, context, extension, name, { key, ...(who === undefined ? {} : { who }) });
378
+ }
379
+ /**
380
+ * What one widget of a package shows for a card of its page code, for the character the card shows (D-514): the package by its npm name, the widget declared
381
+ * by it; nothing when either is not the campaign's, or as for a card of a question.
382
+ */
383
+ export async function packageWidget(root, context, packageName, name, who) {
384
+ const owner = campaignPackages(root, context).find((one) => one.package === packageName);
385
+ return owner === undefined || !owner.widgets.some((widget) => widget.name === name) ? undefined : await widgetFor(root, context, owner, name, who === undefined ? {} : { who });
386
+ }
387
+ /** What a widget shows for a card: for a question, by its key, the attempt; for a card of the page code, by who it shows. */
388
+ async function widgetFor(root, context, extension, name, asked) {
389
+ try {
390
+ const hook = (await packageCode(extension, context))?.widgets?.[name];
391
+ if (hook === undefined) {
392
+ return undefined;
393
+ }
394
+ let timer;
395
+ const late = new Promise((done) => {
396
+ timer = setTimeout(() => done("late"), hookTime).unref();
397
+ });
398
+ // Its own call for its key or who, never one at work for the column or another card.
399
+ const answer = await Promise.race([hookCall(root, extension, `${name}#${asked.key ?? ""}#${asked.who ?? ""}`, () => hook(gameViewOf(root, context, extension), asked)), late]).finally(() => clearTimeout(timer));
400
+ return answer === "late" || answer === undefined ? undefined : shownContent(extension, wordsOf(extension, readTextSetting(root, "language")).read, answer);
401
+ }
402
+ catch {
403
+ return undefined;
404
+ }
405
+ }
406
+ /** Where a widget's page code is, when its package has some and its code runs (D-510): its module's address, its version, its figures. */
407
+ function pageOf(extension) {
408
+ const module = extension.page === undefined || !runsCode(extension) ? {} : { module: pageAddress(extension.package, extension.version, extension.page) };
409
+ return { version: extension.version, ...module, ...(Object.keys(extension.figures).length === 0 ? {} : { figures: extension.figures }) };
410
+ }
411
+ /** A picture of the package's images/, as the dashboard serves it at an address of the package's own; it throws when the package has none by that name. */
265
412
  function pictureOf(extension, name) {
266
413
  const file = findImage([extension.folder], name);
267
414
  if (file === undefined) {
@@ -269,6 +416,10 @@ function pictureOf(extension, name) {
269
416
  }
270
417
  return { extension: extension.name, file: basename(file), version: Math.trunc(files().modified(file) ?? 0) };
271
418
  }
419
+ /** The file a package declares its widgets, icons and figures in. */
420
+ function fileOf(extension) {
421
+ return extension.kind === "system" ? "system.yaml" : "extension.yaml";
422
+ }
272
423
  function messageOf(error) {
273
424
  return (error instanceof Error ? error.message : String(error)).split("\n")[0];
274
425
  }
@@ -2,18 +2,42 @@ import { type CountedByGame } from "../campaign/index.ts";
2
2
  import type { Context } from "../core/context.ts";
3
3
  import type { Problem } from "../core/problem.ts";
4
4
  import { type PageType, type World } from "../pages/index.ts";
5
- import { type GameSystem, type SystemSetting, type SystemWords } from "../systems/index.ts";
5
+ import { type CampaignData, type GameSystem, type SystemLayer, type SystemSetting, type SystemWords } from "../systems/index.ts";
6
6
  /** The world of the campaign in `root`, with the pages of its game system when campaign.yaml names one that is installed (SYS-1, SYS-3). */
7
7
  export declare function campaignWorld(root: string, context: Context): World;
8
- /** The game system's words in the campaign's language (D-403): its own, else those translated on the fly on this computer (D-406). */
9
- export declare function systemWordsOf(system: GameSystem, language: string, context: Context): SystemWords | undefined;
8
+ /**
9
+ * The game system's words in the campaign's language (D-403): its own, else those translated on the fly on this computer (D-406); with the names of the
10
+ * fields the campaign's extensions add, from their words, layered as their additions are (CAM-2, D-505).
11
+ */
12
+ export declare function systemWordsOf(root: string, system: GameSystem, language: string, context: Context): SystemWords | undefined;
13
+ /**
14
+ * The game system's words in the campaign's language, by their English (D-403), by which a name written in it is the system's, Prono its Prone; none without a
15
+ * language, or when they do not read. Kept until campaign.yaml, an extension or a translation of this computer changes.
16
+ */
17
+ export declare function campaignSystemWords(root: string, context: Context): Record<string, string> | undefined;
10
18
  /**
11
19
  * The installed game system that campaign.yaml names, with what the campaign adds to it (CAM-1, CAM-2): alone when the additions do not read (CAM-3). Undefined
12
20
  * when it names none, or one that is not installed (SYS-1).
13
21
  */
14
22
  export declare function campaignSystem(root: string, context: Context): GameSystem | undefined;
23
+ /** What the campaign's extensions add to its game system, in the order campaign.yaml lists them, then the campaign's own additions, which win (CAM-2). */
24
+ export declare function systemLayers(root: string, context: Context, campaign: CampaignData): SystemLayer[];
25
+ /** The layers over the campaign's system that it plays with, in order, and by name those left out because the system would not read with them (CAM-3). */
26
+ export declare function playedLayers(root: string, context: Context): {
27
+ kept: SystemLayer[];
28
+ refused: string[];
29
+ };
30
+ /** The name of an extension's layer of additions, as messages give it. */
31
+ export declare function extensionLayer(name: string): string;
15
32
  /** A setting the campaign's game system adds to the setup (DAT-10), by its name in any case; undefined when the system has none by that name. */
16
33
  export declare function systemSetting(root: string, context: Context, name: string): SystemSetting | undefined;
34
+ /**
35
+ * The value a setting of the system has in the campaign (DAT-10): the one campaign.yaml gives, when the system has it; for the version of the rules, once the game
36
+ * has begun, the one it plays unwritten, the first (VER-2). Undefined while it is still to be chosen.
37
+ */
38
+ export declare function playedSettingValue(root: string, context: Context, setting: SystemSetting): string | undefined;
39
+ /** The names of the player characters the game has begun with (D-519): from the first, the campaign keeps its system and the version of its rules (SYS-9, VER-4). */
40
+ export declare function playersOfTheGame(root: string, context: Context): string[];
17
41
  /** The value campaign.yaml gives a setting of the system, when it is one the system has (DAT-10). */
18
42
  export declare function systemSettingValue(root: string, setting: SystemSetting): string | undefined;
19
43
  /** "xp is not kept with advancement milestones": said when a field the campaign's settings leave out is written or changed (DAT-10); undefined for any other. */
@@ -61,6 +85,11 @@ export declare function missingWithSystems(root: string, missing: string[], cont
61
85
  * one that is not installed says so, with the fix (D-167). Undefined when campaign.yaml names none.
62
86
  */
63
87
  export declare function systemInStatus(root: string, context: Context): string | undefined;
88
+ /**
89
+ * ", the second rules": the version of its rules a system is played with, when it has more than one to choose from (VER-1), or that it is still to ask, so
90
+ * that the first, read meanwhile, is never taken as chosen; empty otherwise.
91
+ */
92
+ export declare function playedRules(root: string, context: Context, system: GameSystem): string;
64
93
  /** A language as messages name it, such as English for en. */
65
94
  export declare function languageName(code: string): string;
66
95
  /** What the game system counts itself, for a rigor's meaning (UPK-1, UPK-3): the day's food, and what burns, by its first name. */