@openbooknetwork/hesap 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +112 -2
- package/package.json +45 -4
- package/scripts/assemble.mjs +40 -0
- package/src/client.css +186 -0
- package/src/client.js +111 -0
- package/src/families/counters/bonds.js +414 -0
- package/src/families/counters/client.css +96 -0
- package/src/families/counters/client.js +21 -0
- package/src/families/counters/counter.js +32 -0
- package/src/families/counters/driver.js +16 -0
- package/src/families/counters/figures.js +172 -0
- package/src/families/counters/frame.js +28 -0
- package/src/families/counters/goals.js +75 -0
- package/src/families/counters/i18n/en.yaml +52 -0
- package/src/families/counters/i18n/tr.yaml +52 -0
- package/src/families/counters/index.mjs +103 -0
- package/src/families/counters/model.js +86 -0
- package/src/families/counters/move.js +42 -0
- package/src/families/counters/places.js +45 -0
- package/src/families.mjs +5 -0
- package/src/i18n/en.yaml +67 -0
- package/src/i18n/tr.yaml +67 -0
- package/src/index.mjs +82 -0
- package/src/kit/dom.js +32 -0
- package/src/kit/drag.js +40 -0
- package/src/kit/format.js +34 -0
- package/src/kit/goal.js +49 -0
- package/src/kit/i18n/en.yaml +15 -0
- package/src/kit/i18n/tr.yaml +15 -0
- package/src/kit/kit.css +89 -0
- package/src/kit/motion.js +44 -0
- package/src/kit/panel.js +70 -0
- package/src/kit/play.js +26 -0
- package/src/kit/random.js +35 -0
- package/src/kit/text.js +25 -0
- package/src/node.mjs +43 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// The blocks a bonds figure is built from: the part-whole diagram, the hands, the list of splits, the lane of pins,
|
|
2
|
+
// the car park, and the trays of the challenges. A block offers one or more ways to be set out, each with its size
|
|
3
|
+
// and a draw that puts it at a corner. draw gives the block's nodes, its places for counters and the parts the
|
|
4
|
+
// scene updates.
|
|
5
|
+
import { svg as el } from '../../kit/dom.js';
|
|
6
|
+
import { R } from './counter.js';
|
|
7
|
+
import { cluster, reach, grid, heap, HEAP } from './places.js';
|
|
8
|
+
|
|
9
|
+
export const GAP = 38; // Counters in a part or a tray stand this far apart.
|
|
10
|
+
const RW = 34; // The radius of the whole's circle.
|
|
11
|
+
const DROP = 40; // The gap between the whole's circle and the parts' circles.
|
|
12
|
+
const BETWEEN = 20; // The gap between the two parts.
|
|
13
|
+
|
|
14
|
+
export const txt = (cls, x, y, s = '') => { const t = el('text', { class: cls, x, y }); t.textContent = s; return t; };
|
|
15
|
+
const at = (c, p) => ({ x: c.x + p.x, y: c.y + p.y });
|
|
16
|
+
const near = (c, rad) => p => Math.hypot(p.x - c.x, p.y - c.y) < rad;
|
|
17
|
+
|
|
18
|
+
// cup draws a cup that covers a circle of radius r round 0, 0 and the number under it.
|
|
19
|
+
function cup(r, foot) {
|
|
20
|
+
const top = -r - 6, w = r * 1.25;
|
|
21
|
+
return el('g', { class: 'counters-cup' }, [
|
|
22
|
+
el('path', { class: 'counters-cup-body', d: `M ${-w} ${foot} L ${-w * 0.78} ${top} Q 0 ${top - 10} ${w * 0.78} ${top} L ${w} ${foot} Z` }),
|
|
23
|
+
el('rect', { class: 'counters-cup-rim', x: -w - 4, y: foot - 10, width: 2 * w + 8, height: 12, rx: 5 }),
|
|
24
|
+
el('rect', { class: 'counters-cup-knob', x: -10, y: top - 18, width: 20, height: 14, rx: 5 }),
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// parts is the diagram whose two part circles hold counters. max gives the most counters each part holds. A part
|
|
29
|
+
// with a cup is a small circle whose number shows when the cup lifts.
|
|
30
|
+
export function parts({ max: [ml, mr], cupped = false, gap = GAP, r = R }) {
|
|
31
|
+
const radius = m => Math.max(50, reach(m, gap) + r + 6), rl = radius(ml), rr = cupped ? 50 : radius(mr), big = Math.max(rl, rr);
|
|
32
|
+
const w = 2 * rl + BETWEEN + 2 * rr, py = 2 * RW + DROP + big, h = py + big + 30;
|
|
33
|
+
return [{ w, h, draw(x, y) {
|
|
34
|
+
const W = { x: x + w / 2, y: y + RW }, L = { x: x + rl, y: y + py }, Rt = { x: x + 2 * rl + BETWEEN + rr, y: y + py };
|
|
35
|
+
const ring = (c, rad, cls, ref) => el('circle', { class: cls, cx: c.x, cy: c.y, r: rad, 'data-ref': ref });
|
|
36
|
+
const whole = txt('counters-big', W.x, W.y), left = txt('counters-badge', L.x, L.y + rl + 18), right = txt('counters-badge', Rt.x, Rt.y + rr + 18);
|
|
37
|
+
const hidden = txt('counters-big', Rt.x, Rt.y);
|
|
38
|
+
const nodes = [el('line', { class: 'counters-link', x1: W.x, y1: W.y, x2: L.x, y2: L.y }), el('line', { class: 'counters-link', x1: W.x, y1: W.y, x2: Rt.x, y2: Rt.y }),
|
|
39
|
+
ring(W, RW, 'counters-whole', 'whole'), ring(L, rl, 'counters-part', 'first'), ring(Rt, rr, 'counters-part', 'second'), whole, left, ...(cupped ? [hidden] : [right])];
|
|
40
|
+
const place = (c, rad) => ({ pos: (i, n) => at(c, cluster(n, gap)[i]), r, hit: near(c, rad + 30), c, rad });
|
|
41
|
+
const lid = cupped ? cup(rr, rr + 30) : null;
|
|
42
|
+
return { nodes, places: { left: place(L, rl), right: place(Rt, rr) }, refs: { whole, badges: [left, cupped ? null : right], hidden, cup: lid && { g: lid, home: Rt, r: rr } } };
|
|
43
|
+
} }];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// numerals is the small diagram over the frames, with a number in each circle. The frames hold the counters.
|
|
47
|
+
export function numerals({ cupped = false }) {
|
|
48
|
+
const rp = 32, w = 260, h = 2 * RW + 30 + 2 * rp + 4;
|
|
49
|
+
return [{ w, h, draw(x, y) {
|
|
50
|
+
const W = { x: x + w / 2, y: y + RW }, L = { x: x + rp + 24, y: y + h - rp - 4 }, Rt = { x: x + w - rp - 24, y: L.y };
|
|
51
|
+
const whole = txt('counters-big', W.x, W.y), left = txt('counters-big', L.x, L.y), right = txt('counters-big', Rt.x, Rt.y);
|
|
52
|
+
const nodes = [el('line', { class: 'counters-link', x1: W.x, y1: W.y, x2: L.x, y2: L.y }), el('line', { class: 'counters-link', x1: W.x, y1: W.y, x2: Rt.x, y2: Rt.y }),
|
|
53
|
+
el('circle', { class: 'counters-whole', cx: W.x, cy: W.y, r: RW, 'data-ref': 'whole' }), el('circle', { class: 'counters-part counters-a-ring', cx: L.x, cy: L.y, r: rp, 'data-ref': 'first' }),
|
|
54
|
+
el('circle', { class: 'counters-part counters-b-ring', cx: Rt.x, cy: Rt.y, r: rp, 'data-ref': 'second' }), whole, left, right];
|
|
55
|
+
return { nodes, places: {}, refs: { whole, badges: [left, cupped ? null : right], hidden: right, cup: cupped ? { g: cup(rp, rp + 8), home: Rt, r: rp } : null } };
|
|
56
|
+
} }];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// hands are two open hands, each holding one part.
|
|
60
|
+
export function hands({ max, gap = 34, r = R }) {
|
|
61
|
+
const rx = Math.max(72, reach(max, gap) + r + 16), ry = rx * 0.76, fingers = 56, apart = 40, side = 26, w = 4 * rx + apart + 2 * side, h = fingers + 2 * ry + 30;
|
|
62
|
+
return [{ w, h, draw(x, y) {
|
|
63
|
+
const one = (cx, out) => {
|
|
64
|
+
const cy = y + fingers + ry, parts = [];
|
|
65
|
+
[-0.56, -0.19, 0.19, 0.56].forEach((k, i) => {
|
|
66
|
+
const fx = cx + k * rx, len = [60, 72, 74, 62][out < 0 ? i : 3 - i];
|
|
67
|
+
parts.push(el('rect', { class: 'counters-hand', x: fx - 15, y: cy - ry - len + 28, width: 30, height: len + 20, rx: 15, transform: `rotate(${k * 14} ${fx} ${cy - ry + 30})` }));
|
|
68
|
+
});
|
|
69
|
+
const tx = cx + out * rx * 0.88, ty = cy + ry * 0.1;
|
|
70
|
+
parts.push(el('ellipse', { class: 'counters-hand', cx: tx, cy: ty, rx: 17, ry: 38, transform: `rotate(${out * 48} ${tx} ${ty + 22})` }));
|
|
71
|
+
parts.push(el('ellipse', { class: 'counters-hand counters-palm', cx, cy, rx, ry }));
|
|
72
|
+
return { g: el('g', {}, parts), c: { x: cx, y: cy + 4 }, cy };
|
|
73
|
+
};
|
|
74
|
+
// Open hands hold their palms up, so the left hand has its thumb on the left and the right hand is its mirror.
|
|
75
|
+
const L = one(x + side + rx, -1), Rt = one(x + side + 3 * rx + apart, 1);
|
|
76
|
+
const left = txt('counters-badge', L.c.x, L.cy + ry + 18), right = txt('counters-badge', Rt.c.x, Rt.cy + ry + 18);
|
|
77
|
+
const place = c => ({ pos: (i, n) => at(c, cluster(n, gap)[i]), r, hit: near(c, rx + 20), c, rad: rx });
|
|
78
|
+
return { nodes: [L.g, Rt.g, left, right], places: { left: place(L.c), right: place(Rt.c) }, refs: { badges: [left, right] } };
|
|
79
|
+
} }];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// list holds a slot for every split, from 0 and the whole to the whole and 0, as a column or as rows of four.
|
|
83
|
+
export function splits(ways) {
|
|
84
|
+
const sw = 80, sh = 30, step = sh + 6, head = 30;
|
|
85
|
+
const way = (cols) => {
|
|
86
|
+
const rows = Math.ceil(ways / cols), w = cols * sw + (cols - 1) * 8;
|
|
87
|
+
return { w, h: head + rows * step, draw(x, y) {
|
|
88
|
+
const slots = Array.from({ length: ways }, (_, i) => {
|
|
89
|
+
const sx = x + (i % cols) * (sw + 8), sy = y + head + Math.floor(i / cols) * step;
|
|
90
|
+
const rect = el('rect', { class: 'counters-slot-box', x: sx, y: sy, width: sw, height: sh, rx: 6 }), text = txt('counters-slot-text', sx + sw / 2, sy + sh / 2);
|
|
91
|
+
return { g: el('g', { class: 'counters-slot a' }, [rect, text]), text };
|
|
92
|
+
});
|
|
93
|
+
const title = txt('counters-list-title', x + w / 2, y + 10);
|
|
94
|
+
return { nodes: [title, el('g', { 'data-ref': 'found' }, slots.map(s => s.g))], places: {}, refs: { slots, title } };
|
|
95
|
+
} };
|
|
96
|
+
};
|
|
97
|
+
return [way(1), way(ways > 8 ? 4 : 3)];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// A bowling pin's outline, standing on 0, 0.
|
|
101
|
+
const PIN = 'M -7 0 C -13 -12 -12 -26 -6 -36 C -4 -40 -5 -44 -5 -48 C -6 -56 -3 -62 0 -62 C 3 -62 6 -56 5 -48 C 5 -44 4 -40 6 -36 C 12 -26 13 -12 7 0 Z';
|
|
102
|
+
// pins stand at the end of a lane in rows of 4, 3, 2 and 1. Cell 0 is the head pin at the front.
|
|
103
|
+
export function pins() {
|
|
104
|
+
const dx = 58, dy = 46, w = 4 * dx + 56, h = 70 + 3 * dy + 40;
|
|
105
|
+
return [{ w, h, draw(x, y) {
|
|
106
|
+
const spots = [], order = [[3, 1], [2, 2], [1, 3], [0, 4]];
|
|
107
|
+
for (const [row, n] of order) for (let k = 0; k < n; k++) spots.push({ x: x + w / 2 + (k - (n - 1) / 2) * dx, y: y + 70 + row * dy });
|
|
108
|
+
const lane = el('rect', { class: 'counters-lane', x, y: y + 2, width: w, height: h - 4, rx: 10 });
|
|
109
|
+
const pinsOf = spots.map((p, i) => {
|
|
110
|
+
const body = el('g', { class: 'counters-pin-body' }, [el('path', { class: 'counters-pin-shape', d: PIN }), el('rect', { class: 'counters-pin-stripe', x: -5, y: -44, width: 10, height: 3 }), el('rect', { class: 'counters-pin-stripe', x: -5, y: -39, width: 10, height: 3 })]);
|
|
111
|
+
const g = el('g', { class: 'counters-pin', transform: `translate(${p.x} ${p.y})`, tabindex: 0, role: 'button', 'data-cell': i }, [el('rect', { class: 'counters-hit', x: -27, y: -48, width: 54, height: 54 }), el('ellipse', { class: 'counters-spot', rx: 10, ry: 4 }), body]);
|
|
112
|
+
return { g, body, tilt: { a: 0 } };
|
|
113
|
+
});
|
|
114
|
+
// The pins at the back are drawn first, so the front pins stand before them.
|
|
115
|
+
return { nodes: [lane, ...[...pinsOf].reverse().map(p => p.g)], places: {}, refs: { pins: pinsOf, lane } };
|
|
116
|
+
} }];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The car park has two rows of ten spaces, an aisle between them, and an entrance on the left with a sign.
|
|
120
|
+
export function park() {
|
|
121
|
+
const sw = 42, sh = 64, aisle = 76, left = 82, w = left + 10 * sw, h = 2 * sh + aisle;
|
|
122
|
+
return [{ w, h, draw(x, y) {
|
|
123
|
+
const cells = Array.from({ length: 20 }, (_, i) => ({ x: x + left + (i % 10 + 0.5) * sw, y: y + (i < 10 ? sh / 2 : sh + aisle + sh / 2) }));
|
|
124
|
+
const lines = [];
|
|
125
|
+
for (const row of [0, 1]) for (let k = 0; k <= 10; k++) {
|
|
126
|
+
const lx = x + left + k * sw, ly = y + row * (sh + aisle);
|
|
127
|
+
lines.push(el('line', { class: 'counters-bay', x1: lx, y1: ly, x2: lx, y2: ly + sh }));
|
|
128
|
+
}
|
|
129
|
+
const spaces = cells.map((c, i) => el('rect', { class: 'counters-space', x: c.x - sw / 2, y: c.y - sh / 2, width: sw, height: sh, tabindex: 0, role: 'button', 'data-cell': i }));
|
|
130
|
+
const ground = el('rect', { class: 'counters-ground', x, y, width: w, height: h, rx: 10 });
|
|
131
|
+
const road = el('line', { class: 'counters-road', x1: x + 12, y1: y + sh + aisle / 2, x2: x + w - 12, y2: y + sh + aisle / 2 });
|
|
132
|
+
const gate = { x: x + left / 2, y: y + sh + aisle / 2 };
|
|
133
|
+
const word = txt('counters-sign-word', x + left / 2, y + 16), num = txt('counters-sign-num', x + left / 2, y + 40);
|
|
134
|
+
const sign = el('g', { class: 'counters-sign', 'data-ref': 'empty' }, [el('rect', { class: 'counters-sign-box', x: x + 8, y: y + 2, width: left - 16, height: 56, rx: 6 }), word, num]);
|
|
135
|
+
return { nodes: [ground, road, ...spaces, ...lines, sign], places: { cells: { pos: i => cells[i], r: R, all: cells }, gate: { pos: () => gate, r: R } },
|
|
136
|
+
refs: { spaces, sign: { word, num }, cells, gate, aisle: p => p.y > y + sh - 4 && p.y < y + sh + aisle + 4 } };
|
|
137
|
+
} }];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// trays holds the red counters of a challenge above the blue ones the reader sets. A tray holds rows of ten with a
|
|
141
|
+
// gap after five, the way a ten frame groups them.
|
|
142
|
+
const TG = 36, TEN = 10 * TG + 8;
|
|
143
|
+
const inRows = i => ({ x: (i % 10 + 0.5) * TG + (i % 10 >= 5 ? 8 : 0), y: (Math.floor(i / 10) + 0.5) * TG });
|
|
144
|
+
export function trays({ red, blue, labels }) {
|
|
145
|
+
const tray = n => Math.ceil(n / 10) * TG + 16, a = tray(red), b = tray(blue), w = TEN + 16;
|
|
146
|
+
return [{ w, h: 24 + a + 16 + 24 + b, draw(x, y) {
|
|
147
|
+
const one = (oy, h, label) => ({
|
|
148
|
+
nodes: [txt('counters-tray-label', x + w / 2, oy + 8, label), el('rect', { class: 'counters-tray', x, y: oy + 24, width: w, height: h, rx: 8 })],
|
|
149
|
+
place: { pos: i => at({ x: x + 8, y: oy + 32 }, inRows(i)), r: R },
|
|
150
|
+
});
|
|
151
|
+
const A = one(y, a, labels[0]), B = one(y + 24 + a + 16, b, labels[1]);
|
|
152
|
+
return { nodes: [...A.nodes, ...B.nodes], places: { red: A.place, blue: B.place }, refs: {} };
|
|
153
|
+
} }];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// tray and pile: the reader builds a part in the tray from a pile of counters.
|
|
157
|
+
export function trayPile({ size, labels }) {
|
|
158
|
+
const rows = Math.ceil(size / 5), t = { w: 5 * TG + 16, h: rows * TG + 16 }, p = { w: HEAP[0] * TG * 0.86 + 2 * R + 8, h: (HEAP.length - 1) * TG * 0.64 + 2 * R + 12 };
|
|
159
|
+
const make = (side) => ({ w: side ? t.w + 24 + p.w : Math.max(t.w, p.w), h: 24 + (side ? Math.max(t.h, p.h) : t.h + 44 + p.h), draw(x, y) {
|
|
160
|
+
const tx = side ? x : x + (this.w - t.w) / 2, ty = y + 24, px = side ? x + t.w + 24 : x + (this.w - p.w) / 2, py = side ? y + 24 + Math.max(t.h, p.h) - p.h : ty + t.h + 44;
|
|
161
|
+
const box = { x: tx, y: ty, w: t.w, h: t.h }, base = { x: px + p.w / 2, y: py + p.h - R - 6 };
|
|
162
|
+
const inBox = (b, q) => q.x > b.x - 16 && q.x < b.x + b.w + 16 && q.y > b.y - 16 && q.y < b.y + b.h + 16;
|
|
163
|
+
// A tap on the tray or the pile anywhere moves one counter, so a phone needs no aim at a single counter.
|
|
164
|
+
const tray = el('rect', { class: 'counters-tray counters-press', x: tx, y: ty, width: t.w, height: t.h, rx: 8, tabindex: 0, role: 'button', 'data-place': 'tray', 'aria-label': labels[0] });
|
|
165
|
+
const pile = el('rect', { class: 'counters-press', x: px, y: py, width: p.w, height: p.h, tabindex: 0, role: 'button', 'data-place': 'pile', 'aria-label': labels[1] });
|
|
166
|
+
const nodes = [txt('counters-tray-label', tx + t.w / 2, y + 8, labels[0]), tray,
|
|
167
|
+
txt('counters-tray-label', px + p.w / 2, side ? y + 8 : py - 16, labels[1]), el('ellipse', { class: 'counters-heap-floor', cx: base.x, cy: base.y + R + 2, rx: p.w / 2, ry: 7 }), pile];
|
|
168
|
+
return { nodes, places: { tray: { pos: i => at({ x: tx + 8, y: ty + 8 }, grid(i, 5, TG)), r: R, hit: q => inBox(box, q) },
|
|
169
|
+
pile: { pos: i => at(base, heap(i, TG)), r: R, hit: q => inBox({ x: px, y: py, w: p.w, h: p.h }, q) } }, refs: { press: [tray, pile] } };
|
|
170
|
+
} });
|
|
171
|
+
return [make(true), make(false)];
|
|
172
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Ten frames: two rows of five cells. A frame's cells are places for counters, and each cell is a target to tap.
|
|
2
|
+
import { svg as el } from '../../kit/dom.js';
|
|
3
|
+
|
|
4
|
+
// A cell is this wide in the figure's units, about 44px on a phone.
|
|
5
|
+
export const CELL = 44;
|
|
6
|
+
// Two frames side by side or one above the other stand this far apart.
|
|
7
|
+
const BETWEEN = 16;
|
|
8
|
+
|
|
9
|
+
// tenFrame draws one frame with its top left corner at x, y and gives the middle of each cell.
|
|
10
|
+
export function tenFrame(x, y, cell = CELL) {
|
|
11
|
+
const lines = [];
|
|
12
|
+
for (let i = 1; i < 5; i++) lines.push(el('line', { x1: x + i * cell, y1: y, x2: x + i * cell, y2: y + 2 * cell }));
|
|
13
|
+
lines.push(el('line', { x1: x, y1: y + cell, x2: x + 5 * cell, y2: y + cell }));
|
|
14
|
+
const g = el('g', { class: 'counters-frame' }, [el('rect', { class: 'counters-frame-box', x, y, width: 5 * cell, height: 2 * cell, rx: 6 }), ...lines]);
|
|
15
|
+
const cells = Array.from({ length: 10 }, (_, i) => ({ x: x + (i % 5 + 0.5) * cell, y: y + (Math.floor(i / 5) + 0.5) * cell }));
|
|
16
|
+
return { g, cells };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// frames gives the ways to set n frames out: side by side, and for two frames also one above the other. Each way has
|
|
20
|
+
// its size and draws the frames at a corner, giving the middle of every cell in order.
|
|
21
|
+
export function frames(n, cell = CELL) {
|
|
22
|
+
const ways = [{ w: n * 5 * cell + (n - 1) * BETWEEN, h: 2 * cell, at: i => ({ x: i * (5 * cell + BETWEEN), y: 0 }) }];
|
|
23
|
+
if (n > 1) ways.push({ w: 5 * cell, h: n * 2 * cell + (n - 1) * BETWEEN, at: i => ({ x: 0, y: i * (2 * cell + BETWEEN) }) });
|
|
24
|
+
return ways.map(way => ({ w: way.w, h: way.h, draw(x, y) {
|
|
25
|
+
const fs = Array.from({ length: n }, (_, i) => { const p = way.at(i); return tenFrame(x + p.x, y + p.y, cell); });
|
|
26
|
+
return { nodes: fs.map(f => f.g), cells: fs.flatMap(f => f.cells) };
|
|
27
|
+
} }));
|
|
28
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// The goals a challenge page can name. The page declares ranges, and the goal draws a target from them each time the
|
|
2
|
+
// page opens and each time the reader asks for another one. The build and the browser both use this file.
|
|
3
|
+
import { capOf } from './model.js';
|
|
4
|
+
|
|
5
|
+
// A pile holds this many counters for the reader to take from.
|
|
6
|
+
export const PILE = 20;
|
|
7
|
+
const span = (lo, hi) => Array.from({ length: Math.max(0, hi - lo + 1) }, (_, i) => lo + i);
|
|
8
|
+
const range = r => Array.isArray(r) && r.length === 2 && r.every(Number.isInteger) && r[0] <= r[1];
|
|
9
|
+
|
|
10
|
+
export const GOALS = {
|
|
11
|
+
// Red counters wait beside the frames. The reader sets how many blue counters go with them and presses Check,
|
|
12
|
+
// which drops them all into the frames. A whole ten of red is never drawn, because its partner is a whole ten too.
|
|
13
|
+
'make-whole': {
|
|
14
|
+
scenes: ['bonds'],
|
|
15
|
+
looks: ['frames'],
|
|
16
|
+
problem(c, spec) {
|
|
17
|
+
if (!Number.isInteger(c.whole)) return 'whole is missing, write the number the frames must hold, such as 10';
|
|
18
|
+
if (c.whole < 2 || c.whole > capOf(spec)) return `whole is ${c.whole}, ${spec.frames} frame${spec.frames > 1 ? 's hold' : ' holds'} from 2 to ${capOf(spec)} counters`;
|
|
19
|
+
if (!range(c.part)) return 'part is missing, write the range of red counters as [low, high], such as [1, 9]';
|
|
20
|
+
return null;
|
|
21
|
+
},
|
|
22
|
+
draw(c, spec, dice) {
|
|
23
|
+
const parts = span(c.part[0], c.part[1]).filter(p => p > 0 && p < c.whole && p % 10);
|
|
24
|
+
return parts.length ? { whole: c.whole, part: dice.pick(parts) } : null;
|
|
25
|
+
},
|
|
26
|
+
// The goal holds once the reader has checked a number of blue counters that fills the frames to the whole.
|
|
27
|
+
holds(state, spec, c, t) {
|
|
28
|
+
if (!state.checked) return { done: false };
|
|
29
|
+
const n = t.part + state.blue;
|
|
30
|
+
return n === t.whole ? { done: true } : { done: false, note: n < t.whole ? 'short' : 'over' };
|
|
31
|
+
},
|
|
32
|
+
reach(spec, c, t) {
|
|
33
|
+
if (t.whole - t.part > c.whole) return `the blue slider stops at ${c.whole}, below the ${t.whole - t.part} blue counters it needs`;
|
|
34
|
+
return null;
|
|
35
|
+
},
|
|
36
|
+
words: (c, t, f) => ({ part: f.int(t.part), whole: f.int(t.whole) }),
|
|
37
|
+
},
|
|
38
|
+
// The diagram shows the whole and one part as counters, and a cup hides the other part. The reader builds the
|
|
39
|
+
// hidden part in a tray, then lifts the cup to compare.
|
|
40
|
+
'hidden-part': {
|
|
41
|
+
scenes: ['bonds'],
|
|
42
|
+
looks: ['diagram'],
|
|
43
|
+
problem(c) {
|
|
44
|
+
if (!range(c.whole) || c.whole[0] < 2 || c.whole[1] > PILE) return `whole is ${JSON.stringify(c.whole)}, write the range of the whole as [low, high] from 2 to ${PILE}, such as [5, 20]`;
|
|
45
|
+
if (c.part !== undefined && !range(c.part)) return 'part is not a range, write the part in sight as [low, high], or leave it out for 1 to one less than the whole';
|
|
46
|
+
return null;
|
|
47
|
+
},
|
|
48
|
+
draw(c, spec, dice) {
|
|
49
|
+
const pairs = span(c.whole[0], c.whole[1]).flatMap(w => span(Math.max(1, c.part?.[0] ?? 1), Math.min(w - 1, c.part?.[1] ?? w - 1)).map(p => ({ whole: w, part: p })));
|
|
50
|
+
return pairs.length ? dice.pick(pairs) : null;
|
|
51
|
+
},
|
|
52
|
+
holds(state, spec, c, t) {
|
|
53
|
+
if (!state.lifted) return { done: false };
|
|
54
|
+
const want = t.whole - t.part;
|
|
55
|
+
return state.tray === want ? { done: true } : { done: false, note: state.tray > want ? 'more' : 'fewer' };
|
|
56
|
+
},
|
|
57
|
+
reach(spec, c, t) {
|
|
58
|
+
if (t.whole - t.part > PILE) return `the pile holds ${PILE} counters, fewer than the ${t.whole - t.part} the tray needs`;
|
|
59
|
+
return null;
|
|
60
|
+
},
|
|
61
|
+
words: (c, t, f) => ({ part: f.int(t.part), whole: f.int(t.whole) }),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// The reader's state as a challenge opens: no blue counters checked, an empty tray and the cup down.
|
|
66
|
+
export const challengeStart = () => ({ blue: 0, checked: false, tray: 0, lifted: false });
|
|
67
|
+
|
|
68
|
+
// another draws a target that differs from the last one, so Another one always changes the figure.
|
|
69
|
+
export function another(goal, c, spec, dice, last) {
|
|
70
|
+
for (let i = 0; i < 40; i++) {
|
|
71
|
+
const t = goal.draw(c, spec, dice);
|
|
72
|
+
if (!last || JSON.stringify(t) !== JSON.stringify(last)) return t;
|
|
73
|
+
}
|
|
74
|
+
return goal.draw(c, spec, dice);
|
|
75
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
counters_lead: "Drag the counters, tap the cells and watch the two parts make the whole."
|
|
2
|
+
counters_whole: Whole
|
|
3
|
+
counters_first: First part
|
|
4
|
+
counters_second: Second part
|
|
5
|
+
counters_empty: Empty cells
|
|
6
|
+
counters_added: Blue added
|
|
7
|
+
counters_found: Splits found
|
|
8
|
+
counters_ways: Splits in all
|
|
9
|
+
counters_hands-first: Left hand
|
|
10
|
+
counters_hands-second: Right hand
|
|
11
|
+
counters_hands-whole: Beads
|
|
12
|
+
counters_one-first: Counters
|
|
13
|
+
counters_one-found: Pairs found
|
|
14
|
+
counters_one-ways: Pairs in all
|
|
15
|
+
counters_two-first: Red
|
|
16
|
+
counters_two-second: Blue
|
|
17
|
+
counters_cup-first: Part in sight
|
|
18
|
+
counters_cup-second: Hidden part
|
|
19
|
+
counters_cup-added: Blue added
|
|
20
|
+
counters_pins-first: Standing
|
|
21
|
+
counters_pins-empty: Knocked down
|
|
22
|
+
counters_cars-first: Parked
|
|
23
|
+
counters_cars-empty: Free spaces
|
|
24
|
+
counters_ctl-whole: Whole
|
|
25
|
+
counters_swap: Swap
|
|
26
|
+
counters_fill: Fill the second frame
|
|
27
|
+
counters_unfill: Empty the second frame
|
|
28
|
+
counters_lift: Lift the cup
|
|
29
|
+
counters_lower: Put the cup back
|
|
30
|
+
counters_check: Check
|
|
31
|
+
counters_blue: Blue counters
|
|
32
|
+
counters_tray: Tray
|
|
33
|
+
counters_pile: Pile
|
|
34
|
+
counters_cup: Cup
|
|
35
|
+
counters_counter: Counter
|
|
36
|
+
counters_car: Car
|
|
37
|
+
counters_cell: "Cell {x}"
|
|
38
|
+
counters_pin-up: "Pin {x}, standing"
|
|
39
|
+
counters_pin-down: "Pin {x}, knocked down"
|
|
40
|
+
counters_free: Free
|
|
41
|
+
counters_pair: "{a} and {b}"
|
|
42
|
+
counters_list-splits: "Splits of {x}"
|
|
43
|
+
counters_list-pairs: "Pairs that make {x}"
|
|
44
|
+
counters_goal-make-whole: "The red tray holds {part}. Set the blue counters so that the frame holds {whole}, then press Check."
|
|
45
|
+
counters_goal-make-whole-two: "The red tray holds {part}. Set the blue counters so that the two frames hold {whole}, then press Check."
|
|
46
|
+
counters_goal-hidden-part: "The whole is {whole}, and the part in sight is {part}. Build the part under the cup in the tray, then lift the cup."
|
|
47
|
+
counters_note-short: Some cells are still empty.
|
|
48
|
+
counters_note-over: Some counters found no cell.
|
|
49
|
+
counters_note-more: The tray holds more counters than the cup.
|
|
50
|
+
counters_note-fewer: The tray holds fewer counters than the cup.
|
|
51
|
+
counters_hint-make-whole: "Count on from {part} to {whole}. Each number you say after {part} is one blue counter."
|
|
52
|
+
counters_hint-hidden-part: "Count on from {part} to {whole}. Each number you say after {part} is one counter under the cup."
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
counters_lead: "Sayaçları sürükleyin, hücrelere dokunun ve iki parçanın bütünü nasıl oluşturduğunu izleyin."
|
|
2
|
+
counters_whole: Bütün
|
|
3
|
+
counters_first: Birinci parça
|
|
4
|
+
counters_second: İkinci parça
|
|
5
|
+
counters_empty: Boş hücreler
|
|
6
|
+
counters_added: Eklenen mavi
|
|
7
|
+
counters_found: Bulunan ayrımlar
|
|
8
|
+
counters_ways: Tüm ayrımlar
|
|
9
|
+
counters_hands-first: Sol el
|
|
10
|
+
counters_hands-second: Sağ el
|
|
11
|
+
counters_hands-whole: Boncuklar
|
|
12
|
+
counters_one-first: Sayaçlar
|
|
13
|
+
counters_one-found: Bulunan ikililer
|
|
14
|
+
counters_one-ways: Tüm ikililer
|
|
15
|
+
counters_two-first: Kırmızı
|
|
16
|
+
counters_two-second: Mavi
|
|
17
|
+
counters_cup-first: Görünen parça
|
|
18
|
+
counters_cup-second: Gizli parça
|
|
19
|
+
counters_cup-added: Eklenen mavi
|
|
20
|
+
counters_pins-first: Ayakta
|
|
21
|
+
counters_pins-empty: Devrilen
|
|
22
|
+
counters_cars-first: Park eden
|
|
23
|
+
counters_cars-empty: Boş yerler
|
|
24
|
+
counters_ctl-whole: Bütün
|
|
25
|
+
counters_swap: Yer değiştir
|
|
26
|
+
counters_fill: İkinci çerçeveyi doldur
|
|
27
|
+
counters_unfill: İkinci çerçeveyi boşalt
|
|
28
|
+
counters_lift: Bardağı kaldır
|
|
29
|
+
counters_lower: Bardağı geri koy
|
|
30
|
+
counters_check: Kontrol et
|
|
31
|
+
counters_blue: Mavi sayaçlar
|
|
32
|
+
counters_tray: Tepsi
|
|
33
|
+
counters_pile: Yığın
|
|
34
|
+
counters_cup: Bardak
|
|
35
|
+
counters_counter: Sayaç
|
|
36
|
+
counters_car: Araba
|
|
37
|
+
counters_cell: "Hücre {x}"
|
|
38
|
+
counters_pin-up: "Lobut {x}, ayakta"
|
|
39
|
+
counters_pin-down: "Lobut {x}, devrik"
|
|
40
|
+
counters_free: Boş
|
|
41
|
+
counters_pair: "{a} ve {b}"
|
|
42
|
+
counters_list-splits: "{x} sayısının ayrımları"
|
|
43
|
+
counters_list-pairs: "{x} yapan ikililer"
|
|
44
|
+
counters_goal-make-whole: "Kırmızı tepside {part} sayaç var. Çerçeve {whole} sayaç tutacak biçimde mavi sayaçları ayarlayın, sonra Kontrol et düğmesine basın."
|
|
45
|
+
counters_goal-make-whole-two: "Kırmızı tepside {part} sayaç var. İki çerçeve {whole} sayaç tutacak biçimde mavi sayaçları ayarlayın, sonra Kontrol et düğmesine basın."
|
|
46
|
+
counters_goal-hidden-part: "Bütün {whole}, görünen parça {part}. Bardağın altındaki parçayı tepside kurun, sonra bardağı kaldırın."
|
|
47
|
+
counters_note-short: Bazı hücreler hâlâ boş.
|
|
48
|
+
counters_note-over: Bazı sayaçlara hücre kalmadı.
|
|
49
|
+
counters_note-more: Tepside bardaktakinden çok sayaç var.
|
|
50
|
+
counters_note-fewer: Tepside bardaktakinden az sayaç var.
|
|
51
|
+
counters_hint-make-whole: "{part} sayısından {whole} sayısına kadar ileri sayın. {part} sayısından sonra söylediğiniz her sayı bir mavi sayaçtır."
|
|
52
|
+
counters_hint-hidden-part: "{part} sayısından {whole} sayısına kadar ileri sayın. {part} sayısından sonra söylediğiniz her sayı bardağın altındaki bir sayaçtır."
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// The Node half of the counters family: counters a reader moves between parts, cells of ten frames, and the skins of
|
|
2
|
+
// the same figure for the world pages. It checks a page's fields against its scene and says what each link shows
|
|
3
|
+
// when the page opens.
|
|
4
|
+
import { schema } from '@openbooknetwork/kitap';
|
|
5
|
+
import { LOOKS, modeOf, capOf, opening, counts, measuresOf } from './model.js';
|
|
6
|
+
import { GOALS, challengeStart } from './goals.js';
|
|
7
|
+
import { numbers } from '../../kit/format.js';
|
|
8
|
+
import { list, challenge, drawn } from '../../node.mjs';
|
|
9
|
+
|
|
10
|
+
const { z } = schema;
|
|
11
|
+
const Int = z.number().int();
|
|
12
|
+
const Range = z.tuple([Int, Int]);
|
|
13
|
+
const Slider = z.object({ slider: Range, start: Int.optional() }).strict();
|
|
14
|
+
const Stepper = z.object({ stepper: Range, start: Int.optional() }).strict();
|
|
15
|
+
const Fields = z.object({
|
|
16
|
+
look: z.enum(LOOKS).default('diagram'), // The diagram, ten frames alone, or the skin of a world page.
|
|
17
|
+
whole: z.union([Int, Stepper]).optional(), // The whole as a number, or on a stepper.
|
|
18
|
+
first: z.union([Int, Slider, Stepper]).optional(), // The first part, or the red counters, as the page opens.
|
|
19
|
+
second: Int.optional(), // The blue counters of the frames as the page opens.
|
|
20
|
+
frames: Int.min(0).max(2).optional(), // The ten frames under the diagram, or of the frames look.
|
|
21
|
+
tap: z.enum(['first', 'second']).optional(), // A tap on a cell adds or takes away a counter of this part.
|
|
22
|
+
swap: z.boolean().default(false), // A Swap button trades the two parts.
|
|
23
|
+
fill: z.boolean().default(false), // A button fills the second frame with red counters and empties it again.
|
|
24
|
+
cup: z.boolean().default(false), // A cup hides the second part until the reader lifts it.
|
|
25
|
+
list: z.boolean().default(false), // A list logs each split the reader finds.
|
|
26
|
+
readout: z.array(z.string()).default([]),
|
|
27
|
+
// A challenge names a goal of goals.js and the ranges it draws its targets from.
|
|
28
|
+
challenge: z.object({ goal: z.string(), whole: z.union([Int, Range]), part: Range.optional() }).strict().optional(),
|
|
29
|
+
}).strict();
|
|
30
|
+
|
|
31
|
+
// value reads a number, a slider or a stepper as its starting value and its range.
|
|
32
|
+
function value(v) {
|
|
33
|
+
if (v == null) return { n: null, ctl: null };
|
|
34
|
+
if (typeof v === 'number') return { n: v, ctl: null };
|
|
35
|
+
const kind = v.slider ? 'slider' : 'stepper', [lo, hi] = v[kind];
|
|
36
|
+
return { n: v.start ?? lo, ctl: { kind, lo, hi } };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function read(pg, { at }, ctx) {
|
|
40
|
+
const fail = words => ctx.fail(at, words);
|
|
41
|
+
const look = pg.look, whole = value(pg.whole), first = value(pg.first);
|
|
42
|
+
if (pg.frames !== undefined && !['diagram', 'frames'].includes(look)) fail(`frames belongs to the diagram and frames looks, the ${look} look has its own places`);
|
|
43
|
+
const frames = look === 'frames' ? pg.frames ?? 1 : look === 'diagram' ? pg.frames ?? 0 : 0;
|
|
44
|
+
if (look === 'frames' && !frames) fail('frames is 0, the frames look shows 1 or 2 ten frames');
|
|
45
|
+
const spec = { ...pg, frames, whole: whole.n, wholeCtl: whole.ctl, first: first.n ?? 0, firstCtl: first.ctl, second: pg.second ?? null };
|
|
46
|
+
const mode = modeOf(spec), cap = capOf(spec);
|
|
47
|
+
for (const [ctl, name] of [[whole.ctl, 'whole'], [first.ctl, 'first']]) if (ctl && (ctl.lo > ctl.hi || spec[name] < ctl.lo || spec[name] > ctl.hi)) fail(`${name} has the range [${ctl.lo}, ${ctl.hi}] and starts at ${spec[name]}, write [low, high] and a start between them`);
|
|
48
|
+
|
|
49
|
+
if (pg.challenge) readChallenge(pg, spec, mode, at, ctx);
|
|
50
|
+
else if (mode === 'parts') {
|
|
51
|
+
if (spec.whole == null) fail('whole is missing, write the number the two parts make, such as 7, or { stepper: [2, 10], start: 7 }');
|
|
52
|
+
const hi = whole.ctl?.hi ?? spec.whole ?? 0;
|
|
53
|
+
if ((whole.ctl?.lo ?? spec.whole) < 1 || hi > 20) fail(`whole reaches ${whole.ctl ? `[${whole.ctl.lo}, ${hi}]` : hi}, the parts hold a whole from 1 to 20`);
|
|
54
|
+
if (first.ctl) fail('first is a number on this page, the reader moves the counters between the parts');
|
|
55
|
+
if (spec.first > (spec.whole ?? 0)) fail(`first is ${spec.first}, more than the whole ${spec.whole}`);
|
|
56
|
+
for (const f of ['second', 'tap']) if (pg[f] !== undefined) fail(`${f} belongs to pages with frames, pins or cars, this page moves counters between parts`);
|
|
57
|
+
for (const f of ['fill', 'cup']) if (pg[f]) fail(`${f} belongs to pages with frames, this page moves counters between parts`);
|
|
58
|
+
if (pg.swap && look === 'hands') fail('swap belongs to the diagram, leave it out of the hands');
|
|
59
|
+
} else {
|
|
60
|
+
if (pg.swap) fail('swap belongs to the diagram without frames, where the parts hold the counters');
|
|
61
|
+
if (spec.second != null && (whole.n != null || pg.cup)) fail('second is set by the whole on this page, leave it out');
|
|
62
|
+
const red = first.ctl?.hi ?? spec.first, blue = spec.second ?? 0;
|
|
63
|
+
if (red + blue > cap || (whole.ctl?.hi ?? spec.whole ?? 0) > cap) fail(`the counters reach ${Math.max(red + blue, whole.ctl?.hi ?? spec.whole ?? 0)}, and the ${look === 'frames' || look === 'diagram' ? `${frames} frame${frames > 1 ? 's hold' : ' holds'}` : `${look} hold`} ${cap}`);
|
|
64
|
+
if (spec.whole != null && (first.ctl?.hi ?? spec.first) > spec.whole && !whole.ctl) fail(`first reaches ${first.ctl?.hi ?? spec.first}, more than the whole ${spec.whole}`);
|
|
65
|
+
if (whole.ctl && whole.ctl.lo < spec.first) fail(`whole starts its range at ${whole.ctl.lo}, below the ${spec.first} red counters`);
|
|
66
|
+
if (whole.ctl && first.ctl) fail('whole and first both have a control, give one of them a number');
|
|
67
|
+
if (pg.cup && (spec.whole == null || whole.ctl || pg.tap !== 'second' || !frames)) fail('cup hides the second part of a declared whole, write whole as a number, tap: second and frames: 1 or 2 with look: diagram');
|
|
68
|
+
if (pg.fill && (frames !== 2 || spec.first + (spec.second ?? 0) > 10)) fail('fill fills the second of two frames, write frames: 2 and keep the first counters in the first frame');
|
|
69
|
+
if (pg.list && (pg.tap !== 'first')) fail('list logs the counters and the empty cells, write tap: first');
|
|
70
|
+
if (pg.tap && first.ctl) fail('tap and a control on first both change the red counters, keep one');
|
|
71
|
+
}
|
|
72
|
+
// A page with blue counters, a whole or a fill button shows two colours, and its readouts name red and blue.
|
|
73
|
+
spec.two = mode === 'cells' && (spec.second != null || spec.whole != null || !!pg.fill || pg.tap === 'second');
|
|
74
|
+
const ids = measuresOf(spec);
|
|
75
|
+
for (const id of pg.readout) if (!ids.includes(id)) fail(`readout names "${id}", this page reads out ${list(ids) || 'nothing'}`);
|
|
76
|
+
return { data: spec, has: id => ids.includes(id), offers: ids.length ? `one of ${list(ids)}` : 'nothing, a challenge page draws its numbers as it opens' };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// A challenge page names its goal, which draws from the page's ranges. Every target it can draw must be open as the
|
|
80
|
+
// page starts and in reach of the page's controls.
|
|
81
|
+
function readChallenge(pg, spec, mode, at, ctx) {
|
|
82
|
+
const c = pg.challenge, goal = challenge(c, GOALS, { scene: pg.scene, at, ctx });
|
|
83
|
+
if (!goal) return;
|
|
84
|
+
if (!goal.looks.includes(pg.look)) return ctx.fail(at, `challenge.goal ${c.goal} works on the ${list(goal.looks)} look, this page shows ${pg.look}`);
|
|
85
|
+
for (const f of ['whole', 'first', 'second', 'tap']) if (pg[f] !== undefined) ctx.fail(at, `${f} is drawn by challenge.goal ${c.goal}, leave it out`);
|
|
86
|
+
for (const f of ['swap', 'fill', 'cup', 'list']) if (pg[f]) ctx.fail(at, `${f} belongs to pages without a challenge, leave it out`);
|
|
87
|
+
if (c.goal === 'hidden-part' && spec.frames) ctx.fail(at, 'frames would show the empty cells of the hidden part, leave frames out');
|
|
88
|
+
const problem = goal.problem(c, spec);
|
|
89
|
+
if (problem) return ctx.fail(at, `challenge.${problem}`);
|
|
90
|
+
drawn(goal, c, spec, { at, ctx, starts: () => challengeStart() });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A link shows a count as the page opens.
|
|
94
|
+
function start(spec, lang, id) {
|
|
95
|
+
if (!measuresOf(spec).includes(id)) return null;
|
|
96
|
+
return numbers(lang).int(counts(spec, opening(spec))[id]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// The two colours of the counters, a red and a blue that readers with any colour vision tell apart, each with a step
|
|
100
|
+
// for the dark themes.
|
|
101
|
+
const bundle = () => ({ families: { 'counters-red': { color: '#d1453b', color_dark: '#f0776d' }, 'counters-blue': { color: '#2a6bd1', color_dark: '#6fa4f5' } } });
|
|
102
|
+
|
|
103
|
+
export default { id: 'counters', scenes: ['bonds'], schema: Fields, read, start, bundle };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// What a counters page holds and what it counts. The build and the browser both use this file, so a page's text
|
|
2
|
+
// and its live values agree.
|
|
3
|
+
|
|
4
|
+
// A page shows its counters in one of these looks. hands, pins and cars are skins for the world pages.
|
|
5
|
+
export const LOOKS = ['diagram', 'frames', 'hands', 'pins', 'cars'];
|
|
6
|
+
// The pins of a lane and the spaces of the car park.
|
|
7
|
+
const CAP = { pins: 10, cars: 20 };
|
|
8
|
+
// The cars fill the car park in this order as a page opens, so the parked cars look spread out.
|
|
9
|
+
const CAR_ORDER = [2, 13, 7, 16, 0, 10, 5, 18, 11, 3, 8, 15, 19, 1, 12, 6, 17, 4, 9, 14];
|
|
10
|
+
|
|
11
|
+
// A page in the parts mode moves counters between two parts, in hands or in the diagram's circles. A page in the
|
|
12
|
+
// cells mode fills the cells of ten frames, the pins of a lane or the spaces of a car park.
|
|
13
|
+
export const modeOf = spec => spec.look === 'hands' || (spec.look === 'diagram' && !spec.frames) ? 'parts' : 'cells';
|
|
14
|
+
export const capOf = spec => CAP[spec.look] ?? spec.frames * 10;
|
|
15
|
+
|
|
16
|
+
// blueOf gives the blue counters that go with red ones: the rest of a declared whole, or the page's own number.
|
|
17
|
+
export function blueOf(spec, red, whole) {
|
|
18
|
+
if (spec.cup) return 0;
|
|
19
|
+
if (whole != null) return Math.max(0, whole - red);
|
|
20
|
+
return spec.second ?? 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// lay puts red counters in the first cells and blue ones after them. keep gives the counter that already sits in a
|
|
24
|
+
// cell, so a counter that stays where it was keeps its id.
|
|
25
|
+
export function lay(spec, red, blue, keep = () => null, fresh = () => null) {
|
|
26
|
+
const cap = capOf(spec), order = spec.look === 'cars' ? CAR_ORDER : [...Array(cap).keys()], cells = Array(cap).fill(null);
|
|
27
|
+
order.forEach((cell, i) => {
|
|
28
|
+
const colour = i < red ? 'a' : i < red + blue ? 'b' : null;
|
|
29
|
+
if (!colour) return;
|
|
30
|
+
const was = keep(cell);
|
|
31
|
+
cells[cell] = was?.colour === colour ? was : { id: fresh() ?? `${colour}${cell}`, colour };
|
|
32
|
+
});
|
|
33
|
+
return cells;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// opening gives the counters as the page opens. A parts page holds the ids of each part, and a cells page holds
|
|
37
|
+
// each cell's counter or null. found lists the first parts of the splits the list has logged.
|
|
38
|
+
export function opening(spec) {
|
|
39
|
+
const whole = spec.whole;
|
|
40
|
+
if (modeOf(spec) === 'parts') {
|
|
41
|
+
const ids = [...Array(whole ?? 0).keys()].map(i => `p${i}`), first = Math.min(spec.first, whole ?? 0);
|
|
42
|
+
const colour = Object.fromEntries(ids.map((id, i) => [id, spec.look === 'hands' || i < first ? 'a' : 'b']));
|
|
43
|
+
const s = { left: ids.slice(0, first), right: ids.slice(first), colour, found: [], lifted: false, extra: false };
|
|
44
|
+
if (spec.list) s.found = [first];
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
const s = { cells: lay(spec, spec.first, blueOf(spec, spec.first, whole)), found: [], lifted: false, extra: false };
|
|
48
|
+
if (spec.list) s.found = [spec.first];
|
|
49
|
+
return s;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// counts gives every number a page can show for its counters.
|
|
53
|
+
export function counts(spec, s) {
|
|
54
|
+
if (modeOf(spec) === 'parts') {
|
|
55
|
+
const first = s.left.length, second = s.right.length;
|
|
56
|
+
return { first, second, whole: first + second, found: s.found.length, ways: first + second + 1 };
|
|
57
|
+
}
|
|
58
|
+
const cap = capOf(spec), red = s.cells.filter(c => c?.colour === 'a').length, blue = s.cells.filter(c => c?.colour === 'b').length;
|
|
59
|
+
const whole = spec.cup ? spec.whole : red + blue;
|
|
60
|
+
return { first: red, second: spec.cup ? whole - red : blue, whole, empty: cap - red - blue, added: blue, found: s.found.length, ways: cap + 1 };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The list logs a split as its first part. Its partner is the second part, or the empty cells of a frame.
|
|
64
|
+
export const partnerOf = (spec, st) => modeOf(spec) === 'parts' ? st.second : st.empty;
|
|
65
|
+
|
|
66
|
+
// The context of a page picks the words of its readouts, such as "Left hand" for the first part of the hands.
|
|
67
|
+
export function contextOf(spec) {
|
|
68
|
+
if (['hands', 'pins', 'cars'].includes(spec.look)) return spec.look;
|
|
69
|
+
if (modeOf(spec) === 'parts') return 'parts';
|
|
70
|
+
if (spec.cup) return 'cup';
|
|
71
|
+
return spec.two ? 'two' : 'one';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The measures a page can read out and link in its text.
|
|
75
|
+
export function measuresOf(spec) {
|
|
76
|
+
if (spec.challenge) return [];
|
|
77
|
+
const ids = ['first'];
|
|
78
|
+
if (modeOf(spec) === 'parts') ids.push('second', 'whole');
|
|
79
|
+
else {
|
|
80
|
+
ids.push('empty');
|
|
81
|
+
if (spec.two || spec.cup) ids.push('second', 'whole');
|
|
82
|
+
if (spec.cup) ids.push('added');
|
|
83
|
+
}
|
|
84
|
+
if (spec.list) ids.push('found', 'ways');
|
|
85
|
+
return ids;
|
|
86
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Carrying a counter from one place to another. A press that hardly moves is a tap, and Enter or Space on a focused
|
|
2
|
+
// counter is a tap too, so every move works by touch, by mouse and by keys.
|
|
3
|
+
import { drag } from '../../kit/drag.js';
|
|
4
|
+
|
|
5
|
+
// A pointer that travels less than this many pixels makes a tap.
|
|
6
|
+
const STILL = 6;
|
|
7
|
+
// A touch sends its click after the counter it carried has moved, so the click lands on whatever lies under the
|
|
8
|
+
// finger then. A click this soon after a carry belongs to the carry and is dropped.
|
|
9
|
+
const AFTER = 500;
|
|
10
|
+
let carried = 0;
|
|
11
|
+
|
|
12
|
+
// carry lets the reader drag node. start may refuse with false. drop gets the point where the counter was let go,
|
|
13
|
+
// or null when the drag was cancelled, and tap runs for a press that did not move.
|
|
14
|
+
export function carry(node, { point, start, move, drop, tap, raise }) {
|
|
15
|
+
let from = null, far = false;
|
|
16
|
+
drag(node, {
|
|
17
|
+
point, raise,
|
|
18
|
+
start(p, e) { from = { x: e.clientX, y: e.clientY }; far = false; return start?.(p, e); },
|
|
19
|
+
move(p, e) {
|
|
20
|
+
if (!far && Math.hypot(e.clientX - from.x, e.clientY - from.y) < STILL) return;
|
|
21
|
+
far = true; move?.(p, e);
|
|
22
|
+
},
|
|
23
|
+
drop(e) {
|
|
24
|
+
carried = performance.now();
|
|
25
|
+
if (!far) return e?.type === 'pointercancel' ? drop?.(null) : tap ? tap() : drop?.(null);
|
|
26
|
+
drop?.(e?.type === 'pointercancel' ? null : point(e));
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
node.addEventListener('keydown', e => {
|
|
30
|
+
if (e.key !== 'Enter' && e.key !== ' ') return;
|
|
31
|
+
e.preventDefault(); e.stopPropagation(); tap?.();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// press makes node a button in the figure: a click, a tap, Enter or Space runs act.
|
|
36
|
+
export function press(node, act) {
|
|
37
|
+
node.addEventListener('click', () => { if (performance.now() - carried > AFTER) act(); });
|
|
38
|
+
node.addEventListener('keydown', e => {
|
|
39
|
+
if (e.key !== 'Enter' && e.key !== ' ') return;
|
|
40
|
+
e.preventDefault(); e.stopPropagation(); act();
|
|
41
|
+
});
|
|
42
|
+
}
|