@altnovatrail/widgets 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cores/vector.schema.json +22 -1
- package/kinds/cards/cards.handles.json +109 -0
- package/kinds/cards/cards.schema.json +483 -0
- package/kinds/cards/vectors/any.json +255 -0
- package/kinds/cards/vectors/box.json +325 -0
- package/kinds/cards/vectors/fork-taps.json +305 -0
- package/kinds/cards/vectors/fork.json +254 -0
- package/kinds/cards/vectors/invalid-goal.json +70 -0
- package/kinds/cards/vectors/invalid-ids.json +113 -0
- package/kinds/cards/vectors/invalid-schema.json +88 -0
- package/kinds/cards/vectors/invalid-taps.json +214 -0
- package/kinds/cards/vectors/key.json +264 -0
- package/kinds/cards/vectors/limit.json +274 -0
- package/kinds/cards/vectors/no-goal.json +205 -0
- package/kinds/cards/vectors/none.json +239 -0
- package/kinds/cards/vectors/recog.json +113 -0
- package/kinds/cards/vectors/riddle.json +397 -0
- package/kinds/cards/vectors/solo.json +218 -0
- package/kinds/cells/cells.schema.json +17 -2
- package/kinds/line/line.schema.json +17 -2
- package/lib/cores/kind.d.ts +21 -3
- package/lib/cores/search.d.ts +22 -6
- package/lib/cores/search.js +34 -6
- package/lib/kinds/cards/goal.d.ts +25 -0
- package/lib/kinds/cards/goal.js +72 -0
- package/lib/kinds/cards/index.d.ts +14 -0
- package/lib/kinds/cards/index.js +17 -0
- package/lib/kinds/cards/kind.d.ts +23 -0
- package/lib/kinds/cards/kind.js +101 -0
- package/lib/kinds/cards/parts.d.ts +36 -0
- package/lib/kinds/cards/parts.js +34 -0
- package/lib/kinds/cards/scene.d.ts +102 -0
- package/lib/kinds/cards/scene.js +356 -0
- package/lib/kinds/cards/schema.d.ts +10 -0
- package/lib/kinds/cards/schema.js +285 -0
- package/lib/kinds/cells/registry.js +1 -1
- package/lib/kinds/cells/schema.js +3 -1
- package/lib/kinds/line/registry.js +1 -1
- package/lib/kinds/line/schema.js +3 -1
- package/package.json +8 -3
- package/cores/handles.schema.json +0 -63
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// The kind cards: a scene of cards in groups, options and a table of signs, a tap that chooses a
|
|
2
|
+
// card or an option, a state of what is chosen, and a goal whose answer is the cards that meet it,
|
|
3
|
+
// read off the scene by the solver with no search. Its state graph is the sets a learner's taps
|
|
4
|
+
// can choose, which the search core still lays and holds within the kind's limits, so that a
|
|
5
|
+
// lesson's walkthrough leads to the answer by taps.
|
|
6
|
+
import { isObject } from "../../cores/json.js";
|
|
7
|
+
import { answer } from "./goal.js";
|
|
8
|
+
import { readScene } from "./scene.js";
|
|
9
|
+
const byCodeUnits = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
10
|
+
/**
|
|
11
|
+
* What a state holds of what may be chosen, each once and sorted: a state reported from outside the
|
|
12
|
+
* kind may hold an ID twice, out of order, or one no tap chooses, and the rules count no such item.
|
|
13
|
+
*/
|
|
14
|
+
function chosen(scene, state) {
|
|
15
|
+
const held = Array.isArray(state.chosen) ? state.chosen : [];
|
|
16
|
+
return scene.choosable.filter((id) => held.includes(id)).sort(byCodeUnits);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The kind cards with its limits of states and of work. A move refuses what is no tap, not-a-move;
|
|
20
|
+
* an ID of no card and no option of the scene, no-part; and one the scene does not let a tap choose,
|
|
21
|
+
* not-choosable.
|
|
22
|
+
*/
|
|
23
|
+
export function cardsKind(limit, workLimit) {
|
|
24
|
+
return {
|
|
25
|
+
limit,
|
|
26
|
+
workLimit,
|
|
27
|
+
read(input) {
|
|
28
|
+
const read = readScene(input);
|
|
29
|
+
if (!read.ok)
|
|
30
|
+
return read;
|
|
31
|
+
const scene = read.scene;
|
|
32
|
+
const from = scene.groups.filter((group) => scene.choose?.from.includes(group.id));
|
|
33
|
+
const choosable = scene.choose === null
|
|
34
|
+
? []
|
|
35
|
+
: [
|
|
36
|
+
...from.flatMap((group) => group.cards.map((card) => card.id)),
|
|
37
|
+
...scene.options.map((option) => option.id),
|
|
38
|
+
];
|
|
39
|
+
const found = answer(scene);
|
|
40
|
+
// A scene of taps is met by taps alone: its answer is cards a tap chooses, one where a tap
|
|
41
|
+
// chooses one, and never none, which the scene's own state would meet before any tap.
|
|
42
|
+
if (found !== null &&
|
|
43
|
+
scene.choose !== null &&
|
|
44
|
+
(found.parts.length === 0 ||
|
|
45
|
+
(!scene.choose.many && found.parts.length > 1) ||
|
|
46
|
+
found.parts.some((id) => !choosable.includes(id)))) {
|
|
47
|
+
return {
|
|
48
|
+
ok: false,
|
|
49
|
+
errors: [
|
|
50
|
+
{
|
|
51
|
+
path: "/goal",
|
|
52
|
+
message: scene.choose.many
|
|
53
|
+
? "a goal whose answer is cards a tap chooses, at least one"
|
|
54
|
+
: "a goal whose answer is one card a tap chooses",
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return { ok: true, scene: { ...scene, choosable, answer: found } };
|
|
60
|
+
},
|
|
61
|
+
start: () => ({ chosen: [] }),
|
|
62
|
+
// The kind's moves draw nothing at random, so it reads nothing of the version or the seed.
|
|
63
|
+
play(scene, state, move) {
|
|
64
|
+
const m = move;
|
|
65
|
+
if (!isObject(m) || Object.keys(m).length !== 1 || typeof m.choose !== "string") {
|
|
66
|
+
return { ok: false, reason: "not-a-move" };
|
|
67
|
+
}
|
|
68
|
+
const id = m.choose;
|
|
69
|
+
const known = scene.options.some((option) => option.id === id) ||
|
|
70
|
+
scene.groups.some((group) => group.cards.some((card) => card.id === id));
|
|
71
|
+
if (!known)
|
|
72
|
+
return { ok: false, reason: "no-part" };
|
|
73
|
+
if (!scene.choosable.includes(id))
|
|
74
|
+
return { ok: false, reason: "not-choosable" };
|
|
75
|
+
const now = chosen(scene, state);
|
|
76
|
+
// One choice leaves whatever else a state from outside the kind held.
|
|
77
|
+
const many = scene.choose?.many === true;
|
|
78
|
+
const next = now.includes(id)
|
|
79
|
+
? many
|
|
80
|
+
? now.filter((held) => held !== id)
|
|
81
|
+
: []
|
|
82
|
+
: many
|
|
83
|
+
? [...now, id].sort(byCodeUnits)
|
|
84
|
+
: [id];
|
|
85
|
+
return { ok: true, state: { chosen: next } };
|
|
86
|
+
},
|
|
87
|
+
moves: (scene) => scene.choosable.map((id) => ({ choose: id })),
|
|
88
|
+
hasGoal: (scene) => scene.goal !== null,
|
|
89
|
+
meets(scene, state) {
|
|
90
|
+
// A scene only for the pen has nothing a state could hold chosen.
|
|
91
|
+
if (scene.answer === null || scene.choosable.length === 0)
|
|
92
|
+
return false;
|
|
93
|
+
const now = chosen(scene, state);
|
|
94
|
+
return (now.length === scene.answer.parts.length &&
|
|
95
|
+
now.every((id, i) => id === scene.answer?.parts[i]));
|
|
96
|
+
},
|
|
97
|
+
asks: (scene) => scene.answer === null
|
|
98
|
+
? { key: "fewest" }
|
|
99
|
+
: { key: "parts", parts: scene.answer.parts, witness: scene.answer.witness },
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Scene } from "./scene.js";
|
|
2
|
+
export type Part = {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly of: "group";
|
|
5
|
+
readonly group: string;
|
|
6
|
+
} | {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly of: "card";
|
|
9
|
+
readonly card: string;
|
|
10
|
+
readonly group: string;
|
|
11
|
+
} | {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly of: "option";
|
|
14
|
+
} | {
|
|
15
|
+
readonly id: string;
|
|
16
|
+
readonly of: "row";
|
|
17
|
+
readonly card: string;
|
|
18
|
+
} | {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly of: "column";
|
|
21
|
+
readonly column: string;
|
|
22
|
+
} | {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly of: "cell";
|
|
25
|
+
readonly card: string;
|
|
26
|
+
readonly column: string;
|
|
27
|
+
};
|
|
28
|
+
export declare const rowId: (card: string) => string;
|
|
29
|
+
export declare const columnId: (column: string) => string;
|
|
30
|
+
export declare const cellId: (card: string, column: string) => string;
|
|
31
|
+
/**
|
|
32
|
+
* Every part of the scene, in its order: each group and its cards, the options, then the table's
|
|
33
|
+
* rows, columns and cells, row by row. The rows go in the order the table names their groups,
|
|
34
|
+
* as the panel lays them.
|
|
35
|
+
*/
|
|
36
|
+
export declare function parts(scene: Scene): Part[];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const rowId = (card) => `row:${card}`;
|
|
2
|
+
export const columnId = (column) => `col:${column}`;
|
|
3
|
+
export const cellId = (card, column) => `cell:${card}:${column}`;
|
|
4
|
+
/**
|
|
5
|
+
* Every part of the scene, in its order: each group and its cards, the options, then the table's
|
|
6
|
+
* rows, columns and cells, row by row. The rows go in the order the table names their groups,
|
|
7
|
+
* as the panel lays them.
|
|
8
|
+
*/
|
|
9
|
+
export function parts(scene) {
|
|
10
|
+
const all = [];
|
|
11
|
+
for (const group of scene.groups) {
|
|
12
|
+
all.push({ id: group.id, of: "group", group: group.id });
|
|
13
|
+
for (const card of group.cards) {
|
|
14
|
+
all.push({ id: card.id, of: "card", card: card.id, group: group.id });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
for (const option of scene.options)
|
|
18
|
+
all.push({ id: option.id, of: "option" });
|
|
19
|
+
if (scene.table !== null) {
|
|
20
|
+
const { rows, columns } = scene.table;
|
|
21
|
+
const cards = rows.flatMap((id) => scene.groups.find((g) => g.id === id)?.cards ?? []);
|
|
22
|
+
for (const card of cards)
|
|
23
|
+
all.push({ id: rowId(card.id), of: "row", card: card.id });
|
|
24
|
+
for (const column of columns) {
|
|
25
|
+
all.push({ id: columnId(column.id), of: "column", column: column.id });
|
|
26
|
+
}
|
|
27
|
+
for (const card of cards) {
|
|
28
|
+
for (const column of columns) {
|
|
29
|
+
all.push({ id: cellId(card.id, column.id), of: "cell", card: card.id, column: column.id });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return all;
|
|
34
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { Read } from "../../cores/kind.js";
|
|
2
|
+
/** The ID of a card, a group or an option: a part's ID, as the widget gives it, by itself. */
|
|
3
|
+
export declare const partId: RegExp;
|
|
4
|
+
/** The ID of a sign, a value or a column: kebab-case, with no colon, so a part's ID holds it. */
|
|
5
|
+
export declare const nameId: RegExp;
|
|
6
|
+
export declare const maxSigns = 10;
|
|
7
|
+
export declare const maxValues = 12;
|
|
8
|
+
export declare const maxGroups = 6;
|
|
9
|
+
export declare const maxCards = 12;
|
|
10
|
+
export declare const maxOptions = 6;
|
|
11
|
+
export declare const maxColumns = 8;
|
|
12
|
+
export declare const maxLabel = 80;
|
|
13
|
+
export declare const maxText = 300;
|
|
14
|
+
/** The most conditions an all or an any joins, and the deepest they nest, a literal at depth 1. */
|
|
15
|
+
export declare const maxJoined = 8;
|
|
16
|
+
export declare const maxDepth = 4;
|
|
17
|
+
export interface Value {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly label: string;
|
|
20
|
+
}
|
|
21
|
+
export interface Sign {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
readonly label: string;
|
|
24
|
+
readonly values: readonly Value[];
|
|
25
|
+
}
|
|
26
|
+
export interface Card {
|
|
27
|
+
readonly id: string;
|
|
28
|
+
/** A card of a task: the text of its statement, drawn in place of a picture; null for a creature. */
|
|
29
|
+
readonly text: string | null;
|
|
30
|
+
/** The value the card gives each sign of the scene, by the sign's ID. */
|
|
31
|
+
readonly signs: ReadonlyMap<string, string>;
|
|
32
|
+
}
|
|
33
|
+
export interface Group {
|
|
34
|
+
readonly id: string;
|
|
35
|
+
readonly label: string | null;
|
|
36
|
+
readonly cards: readonly Card[];
|
|
37
|
+
}
|
|
38
|
+
export interface Option {
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly label: string;
|
|
41
|
+
}
|
|
42
|
+
/** A sign that has a value, or that has any other. */
|
|
43
|
+
export type Literal = {
|
|
44
|
+
readonly sign: string;
|
|
45
|
+
readonly is: string;
|
|
46
|
+
} | {
|
|
47
|
+
readonly sign: string;
|
|
48
|
+
readonly not: string;
|
|
49
|
+
};
|
|
50
|
+
export type Condition = Literal | {
|
|
51
|
+
readonly all: readonly Condition[];
|
|
52
|
+
} | {
|
|
53
|
+
readonly any: readonly Condition[];
|
|
54
|
+
};
|
|
55
|
+
export interface Column {
|
|
56
|
+
readonly id: string;
|
|
57
|
+
readonly label: string;
|
|
58
|
+
readonly literal: Literal;
|
|
59
|
+
}
|
|
60
|
+
export interface Table {
|
|
61
|
+
/** The groups whose cards are the table's rows, in the order the scene names them. */
|
|
62
|
+
readonly rows: readonly string[];
|
|
63
|
+
readonly columns: readonly Column[];
|
|
64
|
+
}
|
|
65
|
+
export interface Choose {
|
|
66
|
+
readonly many: boolean;
|
|
67
|
+
/** The groups whose cards a tap chooses, as the scene names them; options are always chosen so. */
|
|
68
|
+
readonly from: readonly string[];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* What a state is to hold: the cards of the groups it ranges over that meet a condition of signs;
|
|
72
|
+
* that have every value the cards of a group share; or that differ from every other card of the
|
|
73
|
+
* range by at least one of the signs named.
|
|
74
|
+
*/
|
|
75
|
+
export type Goal = {
|
|
76
|
+
readonly signs: Condition;
|
|
77
|
+
readonly among: readonly string[];
|
|
78
|
+
} | {
|
|
79
|
+
readonly like: string;
|
|
80
|
+
readonly among: readonly string[];
|
|
81
|
+
} | {
|
|
82
|
+
readonly differs: readonly string[];
|
|
83
|
+
readonly among: readonly string[];
|
|
84
|
+
};
|
|
85
|
+
export interface Scene {
|
|
86
|
+
readonly signs: readonly Sign[];
|
|
87
|
+
readonly groups: readonly Group[];
|
|
88
|
+
readonly options: readonly Option[];
|
|
89
|
+
readonly choose: Choose | null;
|
|
90
|
+
readonly table: Table | null;
|
|
91
|
+
readonly goal: Goal | null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The values every card of a group gives, as literals in the order of the scene's signs: the rule
|
|
95
|
+
* the group teaches.
|
|
96
|
+
*/
|
|
97
|
+
export declare function shared(group: Group, signs: readonly Sign[]): Literal[];
|
|
98
|
+
/**
|
|
99
|
+
* A scene read by the kind's schema, or every error it names: the taps, the table and the goal are
|
|
100
|
+
* read once the signs, the groups and the options are, against what they name.
|
|
101
|
+
*/
|
|
102
|
+
export declare function readScene(input: unknown): Read<Scene>;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// A scene of the kind cards, read from its JSON by hand as cards.schema.json has it: the signs the
|
|
2
|
+
// scene names with their values, the cards in groups, each giving a value to every sign, the
|
|
3
|
+
// options to choose among, how a tap chooses, the table of signs and at most one goal. The reading
|
|
4
|
+
// refuses what the schema cannot say — an ID given twice, a sign or a value the scene does not
|
|
5
|
+
// name, a card that leaves a sign out — and never takes what the schema refuses.
|
|
6
|
+
import { array, isObject, map, object, pointer, string } from "../../cores/json.js";
|
|
7
|
+
/** The ID of a card, a group or an option: a part's ID, as the widget gives it, by itself. */
|
|
8
|
+
export const partId = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,23}$/;
|
|
9
|
+
/** The ID of a sign, a value or a column: kebab-case, with no colon, so a part's ID holds it. */
|
|
10
|
+
export const nameId = /^[a-z0-9][a-z0-9-]{0,23}$/;
|
|
11
|
+
export const maxSigns = 10;
|
|
12
|
+
export const maxValues = 12;
|
|
13
|
+
export const maxGroups = 6;
|
|
14
|
+
export const maxCards = 12;
|
|
15
|
+
export const maxOptions = 6;
|
|
16
|
+
export const maxColumns = 8;
|
|
17
|
+
export const maxLabel = 80;
|
|
18
|
+
export const maxText = 300;
|
|
19
|
+
/** The most conditions an all or an any joins, and the deepest they nest, a literal at depth 1. */
|
|
20
|
+
export const maxJoined = 8;
|
|
21
|
+
export const maxDepth = 4;
|
|
22
|
+
// A text's length is in characters, as the schema's maxLength counts them, not UTF-16 code units.
|
|
23
|
+
const label = (value, path, errors, max = maxLabel) => {
|
|
24
|
+
const length = typeof value === "string" ? [...value].length : 0;
|
|
25
|
+
if (typeof value !== "string" || length < 1 || length > max) {
|
|
26
|
+
errors.push({ path, message: `a text of 1 to ${max} characters` });
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
};
|
|
31
|
+
/** An ID not given before in its set; one given twice is named and not taken again. */
|
|
32
|
+
function fresh(value, path, errors, pattern, what, taken) {
|
|
33
|
+
const id = string(value, path, errors, pattern, what);
|
|
34
|
+
if (id === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
if (taken.has(id)) {
|
|
37
|
+
errors.push({ path, message: "this ID is given twice" });
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
taken.add(id);
|
|
41
|
+
return id;
|
|
42
|
+
}
|
|
43
|
+
/** A list of IDs, each once and each one of those known. */
|
|
44
|
+
function ids(value, path, errors, known, what, max) {
|
|
45
|
+
const items = array(value, path, errors, 1, max);
|
|
46
|
+
if (items === undefined)
|
|
47
|
+
return undefined;
|
|
48
|
+
const read = [];
|
|
49
|
+
let ok = true;
|
|
50
|
+
items.forEach((item, i) => {
|
|
51
|
+
const at = pointer(path, i);
|
|
52
|
+
if (typeof item !== "string" || !known.has(item)) {
|
|
53
|
+
errors.push({ path: at, message: what });
|
|
54
|
+
ok = false;
|
|
55
|
+
}
|
|
56
|
+
else if (read.includes(item)) {
|
|
57
|
+
errors.push({ path: at, message: "this ID is given twice" });
|
|
58
|
+
ok = false;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
read.push(item);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return ok ? read : undefined;
|
|
65
|
+
}
|
|
66
|
+
function readSigns(value, errors) {
|
|
67
|
+
const items = array(value, "/signs", errors, 1, maxSigns);
|
|
68
|
+
if (items === undefined)
|
|
69
|
+
return undefined;
|
|
70
|
+
const taken = new Set();
|
|
71
|
+
const signs = [];
|
|
72
|
+
items.forEach((item, i) => {
|
|
73
|
+
const at = pointer("/signs", i);
|
|
74
|
+
const o = object(item, at, errors, ["id", "label", "values"]);
|
|
75
|
+
if (o === undefined)
|
|
76
|
+
return;
|
|
77
|
+
const id = fresh(o.id, pointer(at, "id"), errors, nameId, "a sign's ID", taken);
|
|
78
|
+
const named = label(o.label, pointer(at, "label"), errors);
|
|
79
|
+
const listed = array(o.values, pointer(at, "values"), errors, 1, maxValues);
|
|
80
|
+
const valueIds = new Set();
|
|
81
|
+
const values = [];
|
|
82
|
+
listed?.forEach((v, j) => {
|
|
83
|
+
const vat = pointer(pointer(at, "values"), j);
|
|
84
|
+
const vo = object(v, vat, errors, ["id", "label"]);
|
|
85
|
+
if (vo === undefined)
|
|
86
|
+
return;
|
|
87
|
+
const vid = fresh(vo.id, pointer(vat, "id"), errors, nameId, "a value's ID", valueIds);
|
|
88
|
+
const vlabel = label(vo.label, pointer(vat, "label"), errors);
|
|
89
|
+
if (vid !== undefined && vlabel !== undefined)
|
|
90
|
+
values.push({ id: vid, label: vlabel });
|
|
91
|
+
});
|
|
92
|
+
if (id !== undefined && named !== undefined && listed !== undefined) {
|
|
93
|
+
signs.push({ id, label: named, values });
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return signs;
|
|
97
|
+
}
|
|
98
|
+
/** The value a sign has, or that a literal names, read against the scene's signs. */
|
|
99
|
+
function signValue(value, sign, path, errors) {
|
|
100
|
+
if (sign === undefined)
|
|
101
|
+
return undefined;
|
|
102
|
+
const id = typeof value === "string" ? value : undefined;
|
|
103
|
+
if (id === undefined || !sign.values.some((v) => v.id === id)) {
|
|
104
|
+
errors.push({ path, message: `a value of the sign "${sign.id}"` });
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
return id;
|
|
108
|
+
}
|
|
109
|
+
function readCard(value, path, errors, signs, taken) {
|
|
110
|
+
const o = object(value, path, errors, ["id", "signs"], ["text"]);
|
|
111
|
+
if (o === undefined)
|
|
112
|
+
return undefined;
|
|
113
|
+
const id = fresh(o.id, pointer(path, "id"), errors, partId, "a card's ID", taken);
|
|
114
|
+
const text = o.text === undefined ? null : label(o.text, pointer(path, "text"), errors, maxText);
|
|
115
|
+
const at = pointer(path, "signs");
|
|
116
|
+
const given = map(o.signs, at, errors);
|
|
117
|
+
const values = new Map();
|
|
118
|
+
if (given !== undefined && signs !== undefined) {
|
|
119
|
+
for (const name of Object.keys(given)) {
|
|
120
|
+
if (!signs.some((sign) => sign.id === name)) {
|
|
121
|
+
errors.push({ path: pointer(at, name), message: "no sign of the scene has this ID" });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
for (const sign of signs) {
|
|
125
|
+
if (!Object.hasOwn(given, sign.id)) {
|
|
126
|
+
errors.push({ path: at, message: `the sign "${sign.id}" is missing` });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const v = signValue(given[sign.id], sign, pointer(at, sign.id), errors);
|
|
130
|
+
if (v !== undefined)
|
|
131
|
+
values.set(sign.id, v);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (id === undefined || text === undefined || given === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
return { id, text, signs: values };
|
|
137
|
+
}
|
|
138
|
+
function readGroups(value, errors, signs, taken) {
|
|
139
|
+
const items = array(value, "/groups", errors, 1, maxGroups);
|
|
140
|
+
if (items === undefined)
|
|
141
|
+
return undefined;
|
|
142
|
+
const groups = [];
|
|
143
|
+
items.forEach((item, i) => {
|
|
144
|
+
const at = pointer("/groups", i);
|
|
145
|
+
const o = object(item, at, errors, ["id", "cards"], ["label"]);
|
|
146
|
+
if (o === undefined)
|
|
147
|
+
return;
|
|
148
|
+
const id = fresh(o.id, pointer(at, "id"), errors, partId, "a group's ID", taken);
|
|
149
|
+
const named = o.label === undefined ? null : label(o.label, pointer(at, "label"), errors);
|
|
150
|
+
const listed = array(o.cards, pointer(at, "cards"), errors, 1, maxCards);
|
|
151
|
+
const cards = (listed ?? []).map((card, j) => readCard(card, pointer(pointer(at, "cards"), j), errors, signs, taken));
|
|
152
|
+
if (id !== undefined && named !== undefined && listed !== undefined) {
|
|
153
|
+
groups.push({ id, label: named, cards: cards.filter((card) => card !== undefined) });
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
return groups;
|
|
157
|
+
}
|
|
158
|
+
function readOptions(value, errors, taken) {
|
|
159
|
+
if (value === undefined)
|
|
160
|
+
return [];
|
|
161
|
+
const items = array(value, "/options", errors, 1, maxOptions);
|
|
162
|
+
const options = [];
|
|
163
|
+
items?.forEach((item, i) => {
|
|
164
|
+
const at = pointer("/options", i);
|
|
165
|
+
const o = object(item, at, errors, ["id", "label"]);
|
|
166
|
+
if (o === undefined)
|
|
167
|
+
return;
|
|
168
|
+
const id = fresh(o.id, pointer(at, "id"), errors, partId, "an option's ID", taken);
|
|
169
|
+
const named = label(o.label, pointer(at, "label"), errors);
|
|
170
|
+
if (id !== undefined && named !== undefined)
|
|
171
|
+
options.push({ id, label: named });
|
|
172
|
+
});
|
|
173
|
+
return options;
|
|
174
|
+
}
|
|
175
|
+
/** A sign and one of its values, as a literal and a column give them. */
|
|
176
|
+
function readLiteral(o, path, errors, signs) {
|
|
177
|
+
const name = typeof o.sign === "string" ? o.sign : undefined;
|
|
178
|
+
const sign = signs.find((s) => s.id === name);
|
|
179
|
+
if (sign === undefined) {
|
|
180
|
+
errors.push({ path: pointer(path, "sign"), message: "a sign of the scene" });
|
|
181
|
+
}
|
|
182
|
+
const has = Object.hasOwn(o, "is");
|
|
183
|
+
if (has === Object.hasOwn(o, "not")) {
|
|
184
|
+
errors.push({ path, message: 'one of the members "is" and "not"' });
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
const member = has ? "is" : "not";
|
|
188
|
+
const v = signValue(o[member], sign, pointer(path, member), errors);
|
|
189
|
+
if (sign === undefined || v === undefined)
|
|
190
|
+
return undefined;
|
|
191
|
+
return has ? { sign: sign.id, is: v } : { sign: sign.id, not: v };
|
|
192
|
+
}
|
|
193
|
+
function readCondition(value, path, errors, signs, depth) {
|
|
194
|
+
if (!isObject(value)) {
|
|
195
|
+
errors.push({ path, message: "an object" });
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
const join = Object.hasOwn(value, "all") ? "all" : Object.hasOwn(value, "any") ? "any" : null;
|
|
199
|
+
if (join === null) {
|
|
200
|
+
const o = object(value, path, errors, ["sign"], ["is", "not"]);
|
|
201
|
+
return o === undefined ? undefined : readLiteral(o, path, errors, signs);
|
|
202
|
+
}
|
|
203
|
+
const o = object(value, path, errors, [join]);
|
|
204
|
+
if (o === undefined)
|
|
205
|
+
return undefined;
|
|
206
|
+
if (depth >= maxDepth) {
|
|
207
|
+
errors.push({ path, message: `conditions nested at most ${maxDepth} deep` });
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
const at = pointer(path, join);
|
|
211
|
+
const items = array(o[join], at, errors, 1, maxJoined);
|
|
212
|
+
if (items === undefined)
|
|
213
|
+
return undefined;
|
|
214
|
+
const read = items.map((item, i) => readCondition(item, pointer(at, i), errors, signs, depth + 1));
|
|
215
|
+
if (read.some((c) => c === undefined))
|
|
216
|
+
return undefined;
|
|
217
|
+
const conditions = read;
|
|
218
|
+
return join === "all" ? { all: conditions } : { any: conditions };
|
|
219
|
+
}
|
|
220
|
+
function readChoose(value, errors, groups) {
|
|
221
|
+
if (value === undefined)
|
|
222
|
+
return null;
|
|
223
|
+
const o = object(value, "/choose", errors, [], ["many", "from"]);
|
|
224
|
+
if (o === undefined)
|
|
225
|
+
return undefined;
|
|
226
|
+
if (o.many !== undefined && typeof o.many !== "boolean") {
|
|
227
|
+
errors.push({ path: "/choose/many", message: "true or false" });
|
|
228
|
+
}
|
|
229
|
+
const from = o.from === undefined
|
|
230
|
+
? [...groups]
|
|
231
|
+
: ids(o.from, "/choose/from", errors, groups, "a group of the scene", maxGroups);
|
|
232
|
+
if (from === undefined || (o.many !== undefined && typeof o.many !== "boolean")) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
return { many: o.many === true, from };
|
|
236
|
+
}
|
|
237
|
+
function readTable(value, errors, signs, groups) {
|
|
238
|
+
if (value === undefined)
|
|
239
|
+
return null;
|
|
240
|
+
const o = object(value, "/table", errors, ["columns"], ["rows"]);
|
|
241
|
+
if (o === undefined)
|
|
242
|
+
return undefined;
|
|
243
|
+
const rows = o.rows === undefined
|
|
244
|
+
? [...groups]
|
|
245
|
+
: ids(o.rows, "/table/rows", errors, groups, "a group of the scene", maxGroups);
|
|
246
|
+
const items = array(o.columns, "/table/columns", errors, 1, maxColumns);
|
|
247
|
+
const taken = new Set();
|
|
248
|
+
const columns = [];
|
|
249
|
+
items?.forEach((item, i) => {
|
|
250
|
+
const at = pointer("/table/columns", i);
|
|
251
|
+
const c = object(item, at, errors, ["id", "label", "sign"], ["is", "not"]);
|
|
252
|
+
if (c === undefined)
|
|
253
|
+
return;
|
|
254
|
+
const id = fresh(c.id, pointer(at, "id"), errors, nameId, "a column's ID", taken);
|
|
255
|
+
const named = label(c.label, pointer(at, "label"), errors);
|
|
256
|
+
const literal = readLiteral(c, at, errors, signs);
|
|
257
|
+
if (id !== undefined && named !== undefined && literal !== undefined) {
|
|
258
|
+
columns.push({ id, label: named, literal });
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
if (rows === undefined || items === undefined || columns.length !== items.length) {
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
return { rows, columns };
|
|
265
|
+
}
|
|
266
|
+
function readGoal(value, errors, signs, groups) {
|
|
267
|
+
if (value === undefined)
|
|
268
|
+
return null;
|
|
269
|
+
if (!isObject(value)) {
|
|
270
|
+
errors.push({ path: "/goal", message: "an object" });
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
const forms = ["signs", "like", "differs"].filter((form) => Object.hasOwn(value, form));
|
|
274
|
+
if (forms.length !== 1) {
|
|
275
|
+
errors.push({ path: "/goal", message: 'one of the members "signs", "like" and "differs"' });
|
|
276
|
+
return undefined;
|
|
277
|
+
}
|
|
278
|
+
const form = forms[0];
|
|
279
|
+
const o = object(value, "/goal", errors, [form], ["among"]);
|
|
280
|
+
if (o === undefined)
|
|
281
|
+
return undefined;
|
|
282
|
+
const known = new Set(groups.map((group) => group.id));
|
|
283
|
+
const at = pointer("/goal", form);
|
|
284
|
+
if (form === "signs") {
|
|
285
|
+
const condition = readCondition(o.signs, at, errors, signs, 1);
|
|
286
|
+
const among = readAmong(o.among, errors, known, undefined);
|
|
287
|
+
return condition === undefined || among === undefined ? undefined : { signs: condition, among };
|
|
288
|
+
}
|
|
289
|
+
if (form === "like") {
|
|
290
|
+
const group = groups.find((g) => g.id === o.like);
|
|
291
|
+
if (group === undefined) {
|
|
292
|
+
errors.push({ path: at, message: "a group of the scene" });
|
|
293
|
+
return undefined;
|
|
294
|
+
}
|
|
295
|
+
if (shared(group, signs).length === 0) {
|
|
296
|
+
errors.push({ path: at, message: "a group whose cards share a value of some sign" });
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
const among = readAmong(o.among, errors, known, group.id);
|
|
300
|
+
return among === undefined ? undefined : { like: group.id, among };
|
|
301
|
+
}
|
|
302
|
+
const named = ids(o.differs, at, errors, new Set(signs.map((sign) => sign.id)), "a sign of the scene", maxSigns);
|
|
303
|
+
const among = readAmong(o.among, errors, known, undefined);
|
|
304
|
+
return named === undefined || among === undefined ? undefined : { differs: named, among };
|
|
305
|
+
}
|
|
306
|
+
/** The groups a goal ranges over: those named, or every group but the one it learns from. */
|
|
307
|
+
function readAmong(value, errors, known, learnsFrom) {
|
|
308
|
+
if (value === undefined) {
|
|
309
|
+
const others = [...known].filter((id) => id !== learnsFrom);
|
|
310
|
+
if (others.length === 0) {
|
|
311
|
+
errors.push({ path: "/goal", message: "another group for the goal to range over" });
|
|
312
|
+
return undefined;
|
|
313
|
+
}
|
|
314
|
+
return others;
|
|
315
|
+
}
|
|
316
|
+
return ids(value, "/goal/among", errors, known, "a group of the scene", maxGroups);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* The values every card of a group gives, as literals in the order of the scene's signs: the rule
|
|
320
|
+
* the group teaches.
|
|
321
|
+
*/
|
|
322
|
+
export function shared(group, signs) {
|
|
323
|
+
const rule = [];
|
|
324
|
+
for (const sign of signs) {
|
|
325
|
+
const first = group.cards[0]?.signs.get(sign.id);
|
|
326
|
+
if (first !== undefined && group.cards.every((card) => card.signs.get(sign.id) === first)) {
|
|
327
|
+
rule.push({ sign: sign.id, is: first });
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return rule;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* A scene read by the kind's schema, or every error it names: the taps, the table and the goal are
|
|
334
|
+
* read once the signs, the groups and the options are, against what they name.
|
|
335
|
+
*/
|
|
336
|
+
export function readScene(input) {
|
|
337
|
+
const errors = [];
|
|
338
|
+
const o = object(input, "", errors, ["signs", "groups"], ["options", "choose", "table", "goal"]);
|
|
339
|
+
if (o === undefined)
|
|
340
|
+
return { ok: false, errors };
|
|
341
|
+
const signs = readSigns(o.signs, errors);
|
|
342
|
+
const taken = new Set();
|
|
343
|
+
const groups = readGroups(o.groups, errors, signs, taken);
|
|
344
|
+
const options = readOptions(o.options, errors, taken);
|
|
345
|
+
if (signs === undefined || groups === undefined || errors.length > 0) {
|
|
346
|
+
return { ok: false, errors };
|
|
347
|
+
}
|
|
348
|
+
const known = new Set(groups.map((group) => group.id));
|
|
349
|
+
const choose = readChoose(o.choose, errors, known);
|
|
350
|
+
const table = readTable(o.table, errors, signs, known);
|
|
351
|
+
const goal = readGoal(o.goal, errors, signs, groups);
|
|
352
|
+
if (errors.length > 0 || choose === undefined || table === undefined || goal === undefined) {
|
|
353
|
+
return { ok: false, errors };
|
|
354
|
+
}
|
|
355
|
+
return { ok: true, scene: { signs, groups, options, choose, table, goal } };
|
|
356
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Json } from "../../cores/kind.js";
|
|
2
|
+
/** The version of the kind's schema: a new member a scene may give is its next minor version. */
|
|
3
|
+
export declare const version = "1.0";
|
|
4
|
+
/** The $id of the kind's schema at a version: a name, which need not resolve. */
|
|
5
|
+
export declare function schemaId(at: string): string;
|
|
6
|
+
type Schema = {
|
|
7
|
+
readonly [keyword: string]: Json;
|
|
8
|
+
};
|
|
9
|
+
export declare function cardsSchema(): Schema;
|
|
10
|
+
export {};
|