@altnovatrail/widgets 0.2.0 → 0.2.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 (62) hide show
  1. package/README.md +1 -1
  2. package/kinds/line/line.handles.json +195 -0
  3. package/kinds/line/line.schema.json +321 -0
  4. package/kinds/line/vectors/cases.json +213 -0
  5. package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
  6. package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
  7. package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
  8. package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
  9. package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
  10. package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
  11. package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
  12. package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
  13. package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
  14. package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
  15. package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
  16. package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
  17. package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
  18. package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
  19. package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
  20. package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
  21. package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
  22. package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
  23. package/kinds/line/vectors/invalid-line.json +34 -0
  24. package/kinds/line/vectors/invalid-ring.json +34 -0
  25. package/kinds/line/vectors/invalid.json +43 -0
  26. package/kinds/line/vectors/jump-unreachable.json +104 -0
  27. package/kinds/line/vectors/limit.json +40 -0
  28. package/kinds/line/vectors/op-not-in-scene.json +47 -0
  29. package/kinds/line/vectors/ring-cut.json +104 -0
  30. package/kinds/line/vectors/ring-every.json +107 -0
  31. package/kinds/line/vectors/ring-jump.json +135 -0
  32. package/lib/cores/arith.d.ts +62 -0
  33. package/lib/cores/arith.js +197 -0
  34. package/lib/cores/kind.d.ts +10 -0
  35. package/lib/cores/registry.d.ts +43 -0
  36. package/lib/cores/registry.js +87 -0
  37. package/lib/cores/search.js +36 -8
  38. package/lib/kinds/cells/kind.js +11 -64
  39. package/lib/kinds/cells/operation.d.ts +6 -0
  40. package/lib/kinds/cells/polyomino.d.ts +14 -0
  41. package/lib/kinds/cells/polyomino.js +100 -24
  42. package/lib/kinds/line/cut.d.ts +2 -0
  43. package/lib/kinds/line/cut.js +63 -0
  44. package/lib/kinds/line/goals.d.ts +22 -0
  45. package/lib/kinds/line/goals.js +137 -0
  46. package/lib/kinds/line/index.d.ts +13 -0
  47. package/lib/kinds/line/index.js +17 -0
  48. package/lib/kinds/line/jump.d.ts +10 -0
  49. package/lib/kinds/line/jump.js +95 -0
  50. package/lib/kinds/line/kind.d.ts +25 -0
  51. package/lib/kinds/line/kind.js +125 -0
  52. package/lib/kinds/line/line.d.ts +63 -0
  53. package/lib/kinds/line/line.js +157 -0
  54. package/lib/kinds/line/operation.d.ts +69 -0
  55. package/lib/kinds/line/operation.js +1 -0
  56. package/lib/kinds/line/posts.d.ts +11 -0
  57. package/lib/kinds/line/posts.js +85 -0
  58. package/lib/kinds/line/registry.d.ts +2 -0
  59. package/lib/kinds/line/registry.js +11 -0
  60. package/lib/kinds/line/schema.d.ts +4 -0
  61. package/lib/kinds/line/schema.js +157 -0
  62. package/package.json +9 -2
@@ -0,0 +1,107 @@
1
+ {
2
+ "description": "A ring of six places with a post on place 3, and the goal a post on every place from 5 round past the last to 2: the stretch goes on round the ring, so 5, 6, 1 and 2 need a post each, 4 moves, and the post on 3 counts for nothing.",
3
+ "scene": {
4
+ "line": {
5
+ "places": 6,
6
+ "first": 1,
7
+ "closed": true,
8
+ "posts": [3]
9
+ },
10
+ "operations": {
11
+ "post": {}
12
+ },
13
+ "goal": {
14
+ "every": {
15
+ "from": 5,
16
+ "to": 2
17
+ }
18
+ }
19
+ },
20
+ "seed": "0",
21
+ "start": {
22
+ "state": {
23
+ "posts": [3]
24
+ },
25
+ "goal": false
26
+ },
27
+ "sequences": [
28
+ [
29
+ {
30
+ "move": {
31
+ "post": {
32
+ "at": 5
33
+ }
34
+ },
35
+ "state": {
36
+ "posts": [3, 5]
37
+ },
38
+ "goal": false
39
+ },
40
+ {
41
+ "move": {
42
+ "post": {
43
+ "at": 6
44
+ }
45
+ },
46
+ "state": {
47
+ "posts": [3, 5, 6]
48
+ },
49
+ "goal": false
50
+ },
51
+ {
52
+ "move": {
53
+ "post": {
54
+ "at": 1
55
+ }
56
+ },
57
+ "state": {
58
+ "posts": [1, 3, 5, 6]
59
+ },
60
+ "goal": false
61
+ },
62
+ {
63
+ "move": {
64
+ "post": {
65
+ "at": 2
66
+ }
67
+ },
68
+ "state": {
69
+ "posts": [1, 2, 3, 5, 6]
70
+ },
71
+ "goal": true
72
+ }
73
+ ]
74
+ ],
75
+ "proof": {
76
+ "key": 4,
77
+ "witness": {
78
+ "path": [
79
+ {
80
+ "post": {
81
+ "at": 1
82
+ }
83
+ },
84
+ {
85
+ "post": {
86
+ "at": 2
87
+ }
88
+ },
89
+ {
90
+ "post": {
91
+ "at": 5
92
+ }
93
+ },
94
+ {
95
+ "post": {
96
+ "at": 6
97
+ }
98
+ }
99
+ ]
100
+ },
101
+ "ends": [
102
+ {
103
+ "posts": [1, 2, 3, 5, 6]
104
+ }
105
+ ]
106
+ }
107
+ }
@@ -0,0 +1,135 @@
1
+ {
2
+ "description": "A jumper on a ring of twelve places from 0, starting on 0 and jumping five places forward or back, never off the ring: past the last place it goes on round from the first. The fewest jumps to place 1 are 5 forward, 0, 5, 10, 3, 8, 1, where back it takes 7, 0, 7, 2 and on; the jumper's distance is the shorter way round to its start.",
3
+ "scene": {
4
+ "line": {
5
+ "places": 12,
6
+ "closed": true
7
+ },
8
+ "operations": {
9
+ "jump": {
10
+ "from": 0,
11
+ "step": 5,
12
+ "back": true
13
+ }
14
+ },
15
+ "goal": {
16
+ "counters": {
17
+ "is": {
18
+ "at": 1
19
+ }
20
+ }
21
+ }
22
+ },
23
+ "seed": "0",
24
+ "start": {
25
+ "state": {
26
+ "posts": [],
27
+ "at": 0
28
+ },
29
+ "goal": false
30
+ },
31
+ "sequences": [
32
+ [
33
+ {
34
+ "move": {
35
+ "jump": "forward"
36
+ },
37
+ "state": {
38
+ "posts": [],
39
+ "at": 5
40
+ },
41
+ "goal": false
42
+ },
43
+ {
44
+ "move": {
45
+ "jump": "forward"
46
+ },
47
+ "state": {
48
+ "posts": [],
49
+ "at": 10
50
+ },
51
+ "goal": false
52
+ },
53
+ {
54
+ "move": {
55
+ "jump": "forward"
56
+ },
57
+ "state": {
58
+ "posts": [],
59
+ "at": 3
60
+ },
61
+ "goal": false
62
+ },
63
+ {
64
+ "move": {
65
+ "jump": "forward"
66
+ },
67
+ "state": {
68
+ "posts": [],
69
+ "at": 8
70
+ },
71
+ "goal": false
72
+ },
73
+ {
74
+ "move": {
75
+ "jump": "forward"
76
+ },
77
+ "state": {
78
+ "posts": [],
79
+ "at": 1
80
+ },
81
+ "goal": true
82
+ }
83
+ ],
84
+ [
85
+ {
86
+ "move": {
87
+ "jump": "back"
88
+ },
89
+ "state": {
90
+ "posts": [],
91
+ "at": 7
92
+ },
93
+ "goal": false
94
+ },
95
+ {
96
+ "move": {
97
+ "jump": "back"
98
+ },
99
+ "state": {
100
+ "posts": [],
101
+ "at": 2
102
+ },
103
+ "goal": false
104
+ }
105
+ ]
106
+ ],
107
+ "proof": {
108
+ "key": 5,
109
+ "witness": {
110
+ "path": [
111
+ {
112
+ "jump": "forward"
113
+ },
114
+ {
115
+ "jump": "forward"
116
+ },
117
+ {
118
+ "jump": "forward"
119
+ },
120
+ {
121
+ "jump": "forward"
122
+ },
123
+ {
124
+ "jump": "forward"
125
+ }
126
+ ]
127
+ },
128
+ "ends": [
129
+ {
130
+ "posts": [],
131
+ "at": 1
132
+ }
133
+ ]
134
+ }
135
+ }
@@ -0,0 +1,62 @@
1
+ import type { SceneError } from "./kind.js";
2
+ declare const lowest: unique symbol;
3
+ /**
4
+ * A fraction in lowest terms: its sign in the numerator, its denominator 1 or more. Only the core
5
+ * makes one, by fraction or parseFraction, so every operation may take both for granted.
6
+ */
7
+ export interface Fraction {
8
+ readonly num: number;
9
+ readonly den: number;
10
+ readonly [lowest]: true;
11
+ }
12
+ /** A whole number with its remainder, the remainder from 0 to the divisor's size less one. */
13
+ export interface Division {
14
+ readonly quotient: number;
15
+ readonly remainder: number;
16
+ }
17
+ /**
18
+ * The pattern of a fraction as JSON writes it in a scene and a state: the one form module-kit's
19
+ * solver answers in, an integer or `p/q` in lowest terms with the sign before p, as "0", "-3" or
20
+ * "7/2". A module's key may write a fraction in any form of its value, but a scene and a state
21
+ * write it in this one alone, and "2/4" or "3/1" there is an error. The pattern cannot see lowest
22
+ * terms or the bounds of a safe integer; parseFraction does.
23
+ */
24
+ export declare const fractionPattern = "^(0|-?[1-9][0-9]{0,15}(/([2-9]|[1-9][0-9]{1,15}))?)$";
25
+ /** The part of a kind's schema for a fraction in a scene or a state. */
26
+ export declare const fractionSchema: {
27
+ readonly type: "string";
28
+ readonly pattern: "^(0|-?[1-9][0-9]{0,15}(/([2-9]|[1-9][0-9]{1,15}))?)$";
29
+ readonly description: 'A number, exact: an integer or a fraction in lowest terms with its sign before the numerator, as "0", "-3" or "7/2".';
30
+ };
31
+ /** The fraction num/den in lowest terms; a denominator of 0 or a part not a safe integer throws. */
32
+ export declare function fraction(num: number, den?: number): Fraction;
33
+ /** a + b. */
34
+ export declare function add(a: Fraction, b: Fraction): Fraction;
35
+ /** a − b. */
36
+ export declare function subtract(a: Fraction, b: Fraction): Fraction;
37
+ /** a · b. */
38
+ export declare function multiply(a: Fraction, b: Fraction): Fraction;
39
+ /** a ÷ b; b of 0 throws. */
40
+ export declare function divide(a: Fraction, b: Fraction): Fraction;
41
+ /** −1, 0 or 1 as a is less than, equal to or greater than b. */
42
+ export declare function compare(a: Fraction, b: Fraction): -1 | 0 | 1;
43
+ /**
44
+ * The whole quotient of a by b and its remainder, from 0 to |b| − 1 whatever the signs: a place on
45
+ * a ring of b, counted on from any whole number. b of 0 throws.
46
+ */
47
+ export declare function divideWithRemainder(a: number, b: number): Division;
48
+ /** The greatest common divisor of a and b, never negative; of 0 and 0, 0. */
49
+ export declare function gcd(a: number, b: number): number;
50
+ /** The least common multiple of a and b, never negative; 0 where either is 0. */
51
+ export declare function lcm(a: number, b: number): number;
52
+ /** A fraction in the notation of fractionPattern, for a scene or a state. */
53
+ export declare function fractionText(value: Fraction): string;
54
+ /**
55
+ * The fraction a text writes in the notation of fractionPattern, or undefined where it writes
56
+ * none: a fraction not in lowest terms, with the denominator 1, or a part past a safe integer, the
57
+ * pattern's matches among them. One value has one text, so a state that holds it keys as one.
58
+ */
59
+ export declare function parseFraction(value: string): Fraction | undefined;
60
+ /** A fraction of a scene by fractionSchema, read as the other readers of a scene's JSON are. */
61
+ export declare function readFraction(value: unknown, path: string, errors: SceneError[]): Fraction | undefined;
62
+ export {};
@@ -0,0 +1,197 @@
1
+ /**
2
+ * The pattern of a fraction as JSON writes it in a scene and a state: the one form module-kit's
3
+ * solver answers in, an integer or `p/q` in lowest terms with the sign before p, as "0", "-3" or
4
+ * "7/2". A module's key may write a fraction in any form of its value, but a scene and a state
5
+ * write it in this one alone, and "2/4" or "3/1" there is an error. The pattern cannot see lowest
6
+ * terms or the bounds of a safe integer; parseFraction does.
7
+ */
8
+ export const fractionPattern = "^(0|-?[1-9][0-9]{0,15}(/([2-9]|[1-9][0-9]{1,15}))?)$";
9
+ /** The part of a kind's schema for a fraction in a scene or a state. */
10
+ export const fractionSchema = {
11
+ type: "string",
12
+ pattern: fractionPattern,
13
+ description: 'A number, exact: an integer or a fraction in lowest terms with its sign before the numerator, as "0", "-3" or "7/2".',
14
+ };
15
+ const NOTATION = /^(-?[1-9][0-9]{0,15})(?:\/([1-9][0-9]{0,15}))?$/;
16
+ /** The fraction num/den in lowest terms; a denominator of 0 or a part not a safe integer throws. */
17
+ export function fraction(num, den = 1) {
18
+ safe(num);
19
+ safe(den);
20
+ if (den === 0) {
21
+ throw new RangeError("a fraction with the denominator 0");
22
+ }
23
+ const divisor = gcd(num, den);
24
+ const sign = den < 0 ? -1 : 1;
25
+ return made((sign * num) / divisor, (sign * den) / divisor);
26
+ }
27
+ /** a + b. */
28
+ export function add(a, b) {
29
+ // Over the least common denominator, then reduced by what the numerator still shares with it.
30
+ const shared = gcd(a.den, b.den);
31
+ const left = product(a.num, b.den / shared);
32
+ const right = product(b.num, a.den / shared);
33
+ const den = product(a.den, b.den / shared);
34
+ const num = left === undefined || right === undefined ? undefined : left + right;
35
+ if (den === undefined || num === undefined || !Number.isSafeInteger(num)) {
36
+ return exact(BigInt(a.num) * BigInt(b.den) + BigInt(b.num) * BigInt(a.den), BigInt(a.den) * BigInt(b.den));
37
+ }
38
+ const divisor = gcd(num, shared);
39
+ return made(num / divisor, den / divisor);
40
+ }
41
+ /** a − b. */
42
+ export function subtract(a, b) {
43
+ return add(a, made(-b.num, b.den));
44
+ }
45
+ /** a · b. */
46
+ export function multiply(a, b) {
47
+ return times(a, b, "·", b);
48
+ }
49
+ /** a ÷ b; b of 0 throws. */
50
+ export function divide(a, b) {
51
+ if (b.num === 0) {
52
+ throw new RangeError("a division by 0");
53
+ }
54
+ const reciprocal = b.num < 0 ? made(-b.den, -b.num) : made(b.den, b.num);
55
+ return times(a, reciprocal, "÷", b);
56
+ }
57
+ // The operation and its right operand as asked only name them in the error, written only if thrown,
58
+ // since the product runs on every edge of a state graph.
59
+ function times(a, b, sign, asked) {
60
+ // Each numerator reduced by the other's denominator first, so the product is in lowest terms
61
+ // and no part of it is larger than the result's own.
62
+ const first = gcd(a.num, b.den);
63
+ const second = gcd(b.num, a.den);
64
+ const num = product(a.num / first, b.num / second);
65
+ const den = product(a.den / second, b.den / first);
66
+ if (num === undefined || den === undefined) {
67
+ throw new RangeError(`not a safe integer in the result of ${fractionText(a)} ${sign} ${fractionText(asked)}`);
68
+ }
69
+ return made(num, den);
70
+ }
71
+ /** −1, 0 or 1 as a is less than, equal to or greater than b. */
72
+ export function compare(a, b) {
73
+ const left = product(a.num, b.den);
74
+ const right = product(b.num, a.den);
75
+ if (left !== undefined && right !== undefined) {
76
+ return left < right ? -1 : left > right ? 1 : 0;
77
+ }
78
+ const difference = BigInt(a.num) * BigInt(b.den) - BigInt(b.num) * BigInt(a.den);
79
+ return difference < 0n ? -1 : difference > 0n ? 1 : 0;
80
+ }
81
+ /**
82
+ * The whole quotient of a by b and its remainder, from 0 to |b| − 1 whatever the signs: a place on
83
+ * a ring of b, counted on from any whole number. b of 0 throws.
84
+ */
85
+ export function divideWithRemainder(a, b) {
86
+ safe(a);
87
+ safe(b);
88
+ if (b === 0) {
89
+ throw new RangeError("a division by 0");
90
+ }
91
+ // % keeps a's sign; a less its remainder is a multiple of b no larger than a, so it divides exactly.
92
+ const remainder = a % b;
93
+ const quotient = (a - remainder) / b;
94
+ if (remainder >= 0) {
95
+ return { quotient: quotient + 0, remainder: remainder + 0 };
96
+ }
97
+ return b > 0
98
+ ? { quotient: quotient - 1, remainder: remainder + b }
99
+ : { quotient: quotient + 1, remainder: remainder - b };
100
+ }
101
+ /** The greatest common divisor of a and b, never negative; of 0 and 0, 0. */
102
+ export function gcd(a, b) {
103
+ safe(a);
104
+ safe(b);
105
+ let x = Math.abs(a);
106
+ let y = Math.abs(b);
107
+ while (y !== 0) {
108
+ const rest = x % y;
109
+ x = y;
110
+ y = rest;
111
+ }
112
+ return x;
113
+ }
114
+ /** The least common multiple of a and b, never negative; 0 where either is 0. */
115
+ export function lcm(a, b) {
116
+ if (a === 0 || b === 0) {
117
+ safe(a);
118
+ safe(b);
119
+ return 0;
120
+ }
121
+ const multiple = product(Math.abs(a) / gcd(a, b), Math.abs(b));
122
+ if (multiple === undefined) {
123
+ throw new RangeError(`not a safe integer: the least common multiple of ${a} and ${b}`);
124
+ }
125
+ return multiple;
126
+ }
127
+ /** A fraction in the notation of fractionPattern, for a scene or a state. */
128
+ export function fractionText(value) {
129
+ return value.den === 1 ? String(value.num) : `${value.num}/${value.den}`;
130
+ }
131
+ /**
132
+ * The fraction a text writes in the notation of fractionPattern, or undefined where it writes
133
+ * none: a fraction not in lowest terms, with the denominator 1, or a part past a safe integer, the
134
+ * pattern's matches among them. One value has one text, so a state that holds it keys as one.
135
+ */
136
+ export function parseFraction(value) {
137
+ if (value === "0") {
138
+ return made(0, 1);
139
+ }
140
+ const match = NOTATION.exec(value);
141
+ if (match === null) {
142
+ return undefined;
143
+ }
144
+ const num = Number(match[1]);
145
+ const den = match[2] === undefined ? 1 : Number(match[2]);
146
+ if (!Number.isSafeInteger(num) || !Number.isSafeInteger(den)) {
147
+ return undefined;
148
+ }
149
+ if (match[2] !== undefined && (den === 1 || gcd(num, den) !== 1)) {
150
+ return undefined;
151
+ }
152
+ return made(num, den);
153
+ }
154
+ /** A fraction of a scene by fractionSchema, read as the other readers of a scene's JSON are. */
155
+ export function readFraction(value, path, errors) {
156
+ const read = typeof value === "string" ? parseFraction(value) : undefined;
157
+ if (read === undefined) {
158
+ errors.push({
159
+ path,
160
+ message: 'a number as "0", "-3" or "7/2": an integer, or a fraction in lowest terms',
161
+ });
162
+ }
163
+ return read;
164
+ }
165
+ function safe(value) {
166
+ if (!Number.isSafeInteger(value)) {
167
+ throw new RangeError(`not a safe integer: ${String(value)}`);
168
+ }
169
+ }
170
+ // The product of two safe integers, or undefined where it is not one. Past 2^53 the floating-point
171
+ // product rounds to 2^53 or more, never back below it, so the check of the result is exact.
172
+ function product(a, b) {
173
+ const result = a * b;
174
+ return Number.isSafeInteger(result) ? result : undefined;
175
+ }
176
+ // The same fraction, reduced in bigint where its parts on the way are past a safe integer.
177
+ function exact(num, den) {
178
+ let x = num < 0n ? -num : num;
179
+ let y = den;
180
+ while (y !== 0n) {
181
+ const rest = x % y;
182
+ x = y;
183
+ y = rest;
184
+ }
185
+ const reducedNum = num / x;
186
+ const reducedDen = den / x;
187
+ const top = BigInt(Number.MAX_SAFE_INTEGER);
188
+ if (reducedNum > top || reducedNum < -top || reducedDen > top) {
189
+ throw new RangeError(`not a safe integer in a fraction: ${reducedNum}/${reducedDen}`);
190
+ }
191
+ return made(Number(reducedNum), Number(reducedDen));
192
+ }
193
+ // -0, which a product or a quotient of 0 by a negative number gives, is 0: JSON writes the two
194
+ // alike, and Object.is would part them.
195
+ function made(num, den) {
196
+ return { num: num + 0, den };
197
+ }
@@ -55,6 +55,16 @@ export interface Kind<Scene, State extends Json, Move extends Json> {
55
55
  * path follows. A move listed here that play refuses is skipped.
56
56
  */
57
57
  moves(scene: Scene, state: State): readonly Move[];
58
+ /**
59
+ * At least how many moves `moves` lists from the state, counted without listing them, so that a
60
+ * state whose moves alone pass the limit of work stops the search before they are made: a kind
61
+ * one of whose states may have tens of thousands of moves gives it. It is a whole number and never
62
+ * more than moves lists: the search throws on a count that is no whole number, and on one above
63
+ * the list where the count alone keeps within the limit, while a count above the list that passes
64
+ * it would stop a scene the list does not. A kind that leaves it out, or gives 0, has its moves
65
+ * counted once listed, and the work and the limit are the same either way.
66
+ */
67
+ leastMoves?(scene: Scene, state: State): number;
58
68
  /**
59
69
  * What a move from the state reads and writes that its canonical key leaves out, in characters as
60
70
  * the key would write it: a place the state keeps by leaving it out, which every move still
@@ -0,0 +1,43 @@
1
+ import type { Json, Played, SceneError } from "./kind.js";
2
+ /** A part of a registry: an operation or a goal, by the name a scene and a move give it. */
3
+ export interface Named {
4
+ readonly name: string;
5
+ }
6
+ /**
7
+ * Throws where the registry's version is not "major.minor" or a name of an operation or a goal is
8
+ * given twice or is not in snake_case.
9
+ */
10
+ export declare function checkRegistry(kind: string, registryVersion: string, parts: readonly Named[]): void;
11
+ /** Adds a name to those taken; throws, naming what it is, where it is taken or not in snake_case. */
12
+ export declare function takeName(taken: Set<string>, named: string, what: string): void;
13
+ /**
14
+ * One part's parameters read at the path, or undefined where they do not read: then an error is
15
+ * there, the part's own or, where it named none, one that the kind cannot read them.
16
+ */
17
+ export declare function readPart<T>(read: (value: unknown, path: string, errors: SceneError[]) => T | undefined, value: unknown, path: string, errors: SceneError[]): {
18
+ readonly options: T;
19
+ } | undefined;
20
+ /**
21
+ * The scene's goal, a map of one member named by the goal, with its parameters read; null where
22
+ * the scene has none, where it does not read, or where `read` is undefined because what the goal
23
+ * reads against — the kind's sheet or line — did not read itself.
24
+ */
25
+ export declare function readGoal<G>(input: unknown, goals: ReadonlyMap<string, G>, errors: SceneError[], read: ((goal: G, value: unknown, path: string, errors: SceneError[]) => unknown) | undefined): {
26
+ readonly goal: G;
27
+ readonly options: unknown;
28
+ } | null;
29
+ /** The chosen operations with their parameters, in the registry's order. */
30
+ export declare function inRegistryOrder<O extends Named>(operations: readonly O[], chosen: ReadonlyMap<string, unknown>): {
31
+ readonly op: O;
32
+ readonly options: unknown;
33
+ }[];
34
+ /**
35
+ * A move dispatched to the scene's operation it names, by its one member: refused as not a move,
36
+ * as an operation the kind has but the scene does not allow, or as one the kind does not have.
37
+ */
38
+ export declare function dispatch<O extends Named, S>(allowed: readonly {
39
+ readonly op: O;
40
+ readonly options: unknown;
41
+ }[], known: ReadonlyMap<string, O>, move: {
42
+ readonly [operation: string]: Json;
43
+ }, play: (op: O, options: unknown, held: Json) => Played<S>): Played<S>;
@@ -0,0 +1,87 @@
1
+ // What every kind assembled from a registry of operations and goals shares: the check of the
2
+ // registry's names, the reading of one part's parameters and of the scene's goal, and the dispatch
3
+ // of a move to the operation it names. Each kind keeps what its own scene and state add.
4
+ import { isObject, map, pointer } from "./json.js";
5
+ const version = /^[1-9][0-9]*\.(0|[1-9][0-9]*)$/;
6
+ const snakeCase = /^[a-z][a-z0-9_]*$/;
7
+ /**
8
+ * Throws where the registry's version is not "major.minor" or a name of an operation or a goal is
9
+ * given twice or is not in snake_case.
10
+ */
11
+ export function checkRegistry(kind, registryVersion, parts) {
12
+ if (!version.test(registryVersion)) {
13
+ throw new Error(`the version of ${kind} is "major.minor": ${registryVersion}`);
14
+ }
15
+ const names = new Set();
16
+ for (const part of parts) {
17
+ takeName(names, part.name, "an operation's or a goal's name");
18
+ }
19
+ }
20
+ /** Adds a name to those taken; throws, naming what it is, where it is taken or not in snake_case. */
21
+ export function takeName(taken, named, what) {
22
+ if (!snakeCase.test(named) || taken.has(named)) {
23
+ throw new Error(`${what} is new and in snake_case: ${named}`);
24
+ }
25
+ taken.add(named);
26
+ }
27
+ /**
28
+ * One part's parameters read at the path, or undefined where they do not read: then an error is
29
+ * there, the part's own or, where it named none, one that the kind cannot read them.
30
+ */
31
+ export function readPart(read, value, path, errors) {
32
+ const before = errors.length;
33
+ const options = read(value, path, errors);
34
+ if (options === undefined && errors.length === before) {
35
+ errors.push({ path, message: "parameters the kind cannot read" });
36
+ }
37
+ return options === undefined ? undefined : { options };
38
+ }
39
+ /**
40
+ * The scene's goal, a map of one member named by the goal, with its parameters read; null where
41
+ * the scene has none, where it does not read, or where `read` is undefined because what the goal
42
+ * reads against — the kind's sheet or line — did not read itself.
43
+ */
44
+ export function readGoal(input, goals, errors, read) {
45
+ const goalMap = input === undefined ? undefined : map(input, "/goal", errors);
46
+ if (goalMap === undefined) {
47
+ return null;
48
+ }
49
+ const entries = Object.entries(goalMap);
50
+ const [named, value] = entries[0] ?? ["", undefined];
51
+ const found = goals.get(named);
52
+ if (entries.length !== 1) {
53
+ errors.push({ path: "/goal", message: "one goal, by its name" });
54
+ return null;
55
+ }
56
+ if (found === undefined) {
57
+ errors.push({ path: pointer("/goal", named), message: "no goal of the kind has this name" });
58
+ return null;
59
+ }
60
+ if (read === undefined) {
61
+ return null;
62
+ }
63
+ const part = readPart((v, path, errs) => read(found, v, path, errs), value, pointer("/goal", named), errors);
64
+ return part === undefined ? null : { goal: found, options: part.options };
65
+ }
66
+ /** The chosen operations with their parameters, in the registry's order. */
67
+ export function inRegistryOrder(operations, chosen) {
68
+ return operations
69
+ .filter((op) => chosen.has(op.name))
70
+ .map((op) => ({ op, options: chosen.get(op.name) }));
71
+ }
72
+ /**
73
+ * A move dispatched to the scene's operation it names, by its one member: refused as not a move,
74
+ * as an operation the kind has but the scene does not allow, or as one the kind does not have.
75
+ */
76
+ export function dispatch(allowed, known, move, play) {
77
+ const named = isObject(move) ? Object.keys(move) : [];
78
+ if (named.length !== 1) {
79
+ return { ok: false, reason: "not-a-move" };
80
+ }
81
+ const opName = named[0];
82
+ const chosen = allowed.find(({ op }) => op.name === opName);
83
+ if (chosen === undefined) {
84
+ return { ok: false, reason: known.has(opName) ? "op-not-in-scene" : "unknown-op" };
85
+ }
86
+ return play(chosen.op, chosen.options, move[opName]);
87
+ }