@openbooknetwork/hesap 0.3.1 → 0.3.3

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.
@@ -4,12 +4,18 @@
4
4
  // sets a group with a stepper or throws two dice. On a take-away page the reader taps counters away, each leaving a
5
5
  // dashed ring, or steps the number taken away. Two step lines show how a calculation across ten makes ten first. The
6
6
  // shelf look sells jars of jam into two crates, and the tray look sows the seeds of a packet in a seed tray. A
7
- // challenge writes its calculation open, and its counters move once the reader's numbers are right.
8
- import { DIE, JARS, opening, counts, lineOf, leftOf, stepsOf, LINES, measuresOf, labelKeys, goalKey } from './add-model.js';
7
+ // challenge writes its calculation open, and its counters move once the reader's numbers are right. A family page
8
+ // writes the four facts of its two groups, and a tap on a fact shows it in the counters or the eggs of the egg box.
9
+ // The domino covers a half with a card, and its line finds the covered half. A page with a whole shows outlined places
10
+ // for the missing part, which taps fill with counters that carry the numbers of the count. The squad seats players on
11
+ // its bench the same way, with no places drawn. An undo page joins the counters of the tray and then takes them back,
12
+ // with a second line. A claim page writes a sum the reader claims and checks it by taking the second part away.
13
+ import { DIE, JARS, CELLS, BENCH, claims, checked, opening, counts, lineOf, leftOf, askOf, backOf, full, fills, stepsOf, LINES, measuresOf, labelKeys, goalKey, family as isFamily, factsListed } from './add-model.js';
9
14
  import { ADD_GOALS, addStart } from './add-goals.js';
10
- import { counter, show, put, R } from './counter.js';
15
+ import { counter, show, put, gone, R } from './counter.js';
11
16
  import { frames, CELL } from './frame.js';
12
- import { waitTray, die, DIE_SIZE, shelfWays, crate, JAR_HIT, seedTray } from './add-figures.js';
17
+ import { waitTray, die, DIE_SIZE, shelfWays, crate, JAR_HIT, seedTray, eggBox, domino, pitch, bench } from './add-figures.js';
18
+ import { factsBlock } from './facts.js';
13
19
  import { carry, press } from './move.js';
14
20
  import { driver } from './driver.js';
15
21
  import { svg as el, svgPoint } from '../../kit/dom.js';
@@ -18,7 +24,7 @@ import { goalRow } from '../../kit/goal.js';
18
24
  import { live } from '../../kit/text.js';
19
25
  import { numbers } from '../../kit/format.js';
20
26
  import { dice, fresh, another } from '../../kit/random.js';
21
- import { writtenLine, words, reading, widthOf, SIZE, STEP_SIZE, DASH } from '../../kit/written.js';
27
+ import { writtenLine, words, reading, widthOf, SIZE, STEP_SIZE, DASH, FACT_IDS, sameFact, takenBy } from '../../kit/written.js';
22
28
 
23
29
  const MAX = 1.7; // A figure grows to at most this scale on a wide stage.
24
30
  const PAD = 14; // The figure keeps this far from the stage's edges, in pixels.
@@ -32,12 +38,21 @@ const firstOf = id => id[0] === 'r';
32
38
  // line makes a written line with the links that mark it whole.
33
39
  const line = (whole, options) => Object.assign(writtenLine(options), { whole });
34
40
  const RED_BLUE = { a: 'counters-a', b: 'counters-b' };
41
+ // The class that colours each number of a line by the group it counts, on the frames and on the eggs.
42
+ const PAINT = { first: 'counters-a', second: 'counters-b', missing: 'counters-b' }, EGG_PAINT = { first: 'counters-egg-a', second: 'counters-egg-b' };
43
+ // The verdict under a claim's check says whether the sum is right. It keeps its own size on the screen, in pixels: the
44
+ // font, a line, and room for two lines, so a narrow stage wraps it and the figure keeps its scale.
45
+ const VERDICT_FONT = 17, VERDICT_LINE = 22, VERDICT_ROOM = 2 * VERDICT_LINE + 14;
46
+ const FINGER = 44; // A fact is at least this tall on the screen, in pixels, so a finger finds it.
47
+ const FACTS_MAX = 1.3; // The facts grow to at most this scale on a wide stage.
35
48
 
36
49
  export default function add({ host, stage, panel, T, lang, moving, motion: fx }) {
37
50
  const go = driver(fx);
38
51
  const back = el('g'), layer = el('g', { class: 'counters-layer' }), front = el('g');
39
52
  const dies = [die('first'), die('waiting')];
40
53
  const svg = el('svg', { class: 'counters-svg' }, [back, layer, front]);
54
+ // marks holds the outlined places of the missing part.
55
+ const marks = el('g');
41
56
  stage.appendChild(svg);
42
57
  // base is the page as written, and spec the page with the steppers a challenge gives it. s holds the ids of the
43
58
  // counters of each place: red in the frames, joined in the frames after them, later in the second frame of a split
@@ -48,17 +63,34 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
48
63
  // figure as drawn, and lines the written lines of the page.
49
64
  let base = null, spec = null, s = null, geo = null, area = { w: 0, h: 0 }, nodes = new Map(), seq = 0, hold = null;
50
65
  let chips = [], goal = null, target = null, solved = false, lines = [], play = 0;
66
+ // block is the facts of a family page, and picked the fact the reader tapped.
67
+ let block = null, picked = null, verdict = null, verdictW = 0;
51
68
  const lit = new Set();
52
69
 
53
70
  const int = n => numbers(lang()).int(n);
54
71
  const ids = (n, k) => Array.from({ length: n }, () => `${k}${seq++}`);
55
72
  // now gives the counts, and whether the figure shows the full ten that a step of making ten needs.
56
- const now = () => ({ first: s.red.length, second: spec.take ? s.taken.size : s.joined.length + s.later.length, waiting: s.tray.length,
57
- ten: spec.take ? s.red.slice(10).every(id => s.taken.has(id)) : s.red.length + s.joined.length >= 10 });
73
+ const now = () => spec.look === 'domino' ? { first: spec.first, second: spec.second, waiting: 0, ten: false, cover: s.cover, found: s.found }
74
+ : { first: s.red.length, second: spec.take ? s.taken.size : s.joined.length + s.later.length, waiting: s.tray.length,
75
+ ten: spec.take ? s.red.slice(10).every(id => s.taken.has(id)) : s.red.length + s.joined.length >= 10, whole: s.whole, done: s.done, claim: s.claim };
58
76
  // A page with waiting shows the tray or the packet, and the reader moves its counters unless a challenge moves them.
59
77
  const waits = () => spec.waiting != null;
60
78
  const moves = () => waits() && !base.challenge;
61
79
  const shelf = () => spec.look === 'shelf';
80
+ const family = () => isFamily(spec);
81
+ // A page with a whole and the missing-part challenge put a counter in on a tap of the frames or the bench, and take
82
+ // the last one out on a tap of a counter put in. A page with a whole numbers them and draws places for them.
83
+ const putting = () => fills(spec) || !!spec.put;
84
+ const squad = () => spec.look === 'squad';
85
+ // The facts of a page that puts counters in show once the places are full, or once a challenge's goal holds. Until
86
+ // then its line asks the subtraction.
87
+ const factsShow = st => !putting() || (fills(spec) ? full(st) : solved);
88
+ // tint gives a line the colours of the groups its numbers count, by the link id of each number.
89
+ const tint = l => {
90
+ if (!l) return l;
91
+ const refs = l.refs ?? { a: 'first', b: 'second', c: 'whole' }, paint = spec.look === 'eggs' ? EGG_PAINT : PAINT;
92
+ return { ...l, refs, cls: Object.fromEntries(Object.entries(refs).filter(([, id]) => paint[id]).map(([r, id]) => [r, paint[id]])) };
93
+ };
62
94
  // A take-away page whose number taken away has no stepper takes taps on its counters.
63
95
  const taps = () => spec.take && !spec.secondCtl && !base.challenge;
64
96
  const goalOf = () => ADD_GOALS[base.challenge.goal];
@@ -69,29 +101,38 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
69
101
  // morning and the afternoon in their crates.
70
102
  function deal() {
71
103
  const c = base.challenge;
72
- spec = base; seq = 0; hold = null; solved = false;
104
+ spec = base; seq = 0; hold = null; solved = false; picked = null;
105
+ marks.replaceChildren(); // The places of another page or target go.
73
106
  if (c) { target = another(ADD_GOALS[c.goal], c, base, dice(fresh()), target); spec = { ...base, ...ADD_GOALS[c.goal].page(target) }; }
74
107
  s = { answer: addStart(), checked: false };
75
108
  place();
76
109
  // The written lines: the line of the calculation, on the shelf the line of the jars left, and with written: ten
77
110
  // the two steps of making ten.
78
- const end = spec.take ? 'left' : 'sum', main = line(['written', 'reading'], { refs: { a: 'first', b: 'second', c: end }, cls: spec.take ? { a: 'counters-a' } : RED_BLUE });
79
- lines = !spec.written ? [] : shelf() ? [main, line(['written-left', 'reading-left'], { refs: { b: 'sum', c: 'left' } })]
111
+ const end = claims(spec) ? 'claim' : spec.take ? 'left' : spec.look === 'domino' ? 'whole' : 'sum';
112
+ const main = putting() ? line(!fills(spec) ? [] : family() ? ['written-minus-first', 'reading-minus-first'] : ['written', 'reading'], { cls: { b: 'counters-a', c: 'counters-b' } })
113
+ : line(['written', 'reading'], { refs: { a: 'first', b: 'second', c: end }, cls: spec.take ? { a: 'counters-a' } : spec.look === 'eggs' ? { a: 'counters-egg-a', b: 'counters-egg-b' } : RED_BLUE });
114
+ block = family() ? factsBlock({ tint, pick }) : null;
115
+ lines = putting() ? (spec.written ? [main] : []) : spec.undo ? [main, line(['written-back', 'reading-back'], { refs: { b: 'back', c: 'left' }, cls: { b: 'counters-b' } })]
116
+ : claims(spec) ? [main, line(['written-back', 'reading-back'], { refs: { a: 'claim', b: 'second' }, cls: { b: 'counters-b' } })]
117
+ : !spec.written || family() ? [] : shelf() ? [main, line(['written-left', 'reading-left'], { refs: { b: 'sum', c: 'left' } })]
80
118
  : spec.written === 'ten' ? [main, line(['written-to-ten', 'reading-to-ten'], { step: true, refs: { a: 'first', b: 'to-ten' }, cls: spec.take ? { a: 'counters-a' } : RED_BLUE }),
81
119
  line(['written-rest', 'reading-rest'], { step: true, refs: { b: 'rest', c: end }, cls: spec.take ? {} : { b: 'counters-b' } })]
82
120
  : [main];
83
- front.replaceChildren(...lines.map(l => l.el), ...dies.map(d => d.g));
121
+ // A claim page says under its check whether the check gives back the first part.
122
+ // Its group takes the place, since the flash's CSS transform on the text would override a transform attribute.
123
+ verdict = claims(spec) ? el('text', { class: 'counters-verdict' }) : null;
124
+ front.replaceChildren(...lines.map(l => l.el), ...(verdict ? [el('g', {}, [verdict])] : []), ...(block ? [block.el] : []), ...dies.map(d => d.g));
84
125
  }
85
126
  // place sets the counters as the page opens, and stops a run of moving counters.
86
127
  function place() {
87
128
  const o = opening(spec);
88
129
  play++;
89
- Object.assign(s, { red: [], joined: [], later: [], tray: [], taken: new Set(), jars: [], crate: s.crate ?? 'first', dice: spec.dice ? [o.first, o.waiting] : null });
130
+ Object.assign(s, { red: [], joined: [], later: [], tray: [], taken: new Set(), jars: [], crate: s.crate ?? 'first', dice: spec.dice ? [o.first, o.waiting] : null, cover: o.cover, found: o.found, whole: o.whole, done: o.done, claim: o.claim });
90
131
  if (shelf()) { s.jars = ids(JARS, 'j'); s.red = s.jars.slice(0, o.first); s.joined = s.jars.slice(o.first, o.first + o.second); }
91
132
  else if (spec.take) { s.red = ids(o.first, 'r'); s.taken = new Set(s.red.slice(s.red.length - o.second)); }
92
133
  else Object.assign(s, { red: ids(o.first, 'r'), joined: ids(o.second, 'b'), tray: ids(o.waiting, 'b') });
93
134
  }
94
- const holds = () => base.challenge ? goalOf().holds({ ...s.answer, checked: s.checked }, spec, base.challenge, target) : { done: false };
135
+ const holds = () => base.challenge ? goalOf().holds({ ...s.answer, put: s.joined.length, checked: s.checked }, spec, base.challenge, target) : { done: false };
95
136
 
96
137
  // framesLayout tries the frames side by side and one above the other, or the seed tray, with the tray or the seed
97
138
  // packet beside them or under them, and keeps the way that fits the stage at the largest scale. Each die stands
@@ -99,7 +140,7 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
99
140
  function framesLayout(fit) {
100
141
  const room = spec.dice ? DIE[1] : spec.second + (spec.waiting ?? 0), tray = waits() ? waitTray(room, spec.look === 'tray') : null, dieH = spec.dice ? DIE_ROOM : 0;
101
142
  let best = null;
102
- for (const fw of spec.look === 'tray' ? [seedTray()] : frames(2)) for (const beside of tray ? [true, false] : [null]) {
143
+ for (const fw of spec.look === 'tray' ? [seedTray()] : spec.look === 'eggs' ? [eggBox()] : frames(2)) for (const beside of tray ? [true, false] : [null]) {
103
144
  let w, h, f, t = null;
104
145
  if (beside === null) { w = fw.w; h = dieH + fw.h; f = { x: 0, y: dieH }; }
105
146
  else if (beside) { const hh = Math.max(fw.h, tray.h); w = fw.w + SPACE + tray.w; h = dieH + hh; f = { x: 0, y: dieH + (hh - fw.h) / 2 }; t = { x: fw.w + SPACE, y: dieH + (hh - tray.h) / 2 }; }
@@ -113,7 +154,10 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
113
154
  // split seed tray it sows the seed in the row of that cell.
114
155
  const row = i => spec.look === 'tray' && spec.split ? (i < 10 ? 'first' : 'second') : true;
115
156
  const targets = moves() ? fd.cells.map((c, i) => { const rect = el('rect', { class: 'counters-cell', x: c.x - CELL / 2, y: c.y - CELL / 2, width: CELL, height: CELL, rx: 4 }); press(rect, () => joinNext(row(i))); return rect; }) : [];
116
- return { nodes: [...fd.nodes, ...(td ? [td.box] : []), ...targets], cells: fd.cells, frames: box, tray: td, targets };
157
+ // A tap anywhere on the frames puts the next counter in, so a phone needs no aim at a single place.
158
+ const area = putting() ? el('rect', { class: 'counters-press', x: box.x - 6, y: box.y - 6, width: box.w + 12, height: box.h + 12, rx: 8, tabindex: 0, role: 'button' }) : null;
159
+ if (area) press(area, putNext);
160
+ return { nodes: [...fd.nodes, ...(td ? [td.box] : []), ...targets, ...(area ? [area] : [])], cells: fd.cells, frames: box, tray: td, targets, area };
117
161
  });
118
162
  }
119
163
  return best;
@@ -138,34 +182,100 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
138
182
  return best;
139
183
  }
140
184
 
141
- // build draws the figure to fit the stage, with the written lines under it, and puts every counter in its place.
185
+ // squadLayout sets the pitch with the bench beside it or under it, the bench in one row, two rows, or one or two
186
+ // columns, and keeps the way that fits the stage at the largest scale, a row first where two ways tie. A tap on the
187
+ // bench seats the next player.
188
+ function squadLayout(fit) {
189
+ // The bench has room for as many players as it may hold, so its size gives nothing away, and it shows no seats.
190
+ const p = pitch(), n = BENCH;
191
+ let best = null;
192
+ for (const cols of new Set([n, Math.ceil(n / 2), 2, 1])) for (const beside of [true, false]) {
193
+ const b = bench(n, Math.min(cols, n));
194
+ const w = beside ? p.w + SPACE + b.w : Math.max(p.w, b.w), h = beside ? Math.max(p.h, b.h) : p.h + SPACE + b.h;
195
+ best = fit(best, w, h, dx => {
196
+ const pd = p.draw(dx + (beside ? 0 : (w - p.w) / 2), beside ? (h - p.h) / 2 : 0);
197
+ const bd = b.draw(dx + (beside ? p.w + SPACE : (w - b.w) / 2), beside ? (h - b.h) / 2 : p.h + SPACE);
198
+ press(bd.area, putNext);
199
+ return { nodes: [...pd.nodes, ...bd.nodes], spots: pd.spots, seats: bd.seats, area: bd.area, targets: [] };
200
+ });
201
+ }
202
+ return best;
203
+ }
204
+
205
+ // dominoLayout sets the domino with its whole above it. A tap on a half covers it, and a tap on the card lifts it.
206
+ function dominoLayout(fit) {
207
+ const d = domino(spec.first, spec.second);
208
+ return fit(null, d.w, d.h, dx => {
209
+ const g = d.draw(dx, 0);
210
+ g.halves.forEach((hf, k) => press(hf.g, () => coverHalf(k ? 'second' : 'first')));
211
+ press(g.card, lift);
212
+ return { nodes: g.nodes, domino: g, targets: [] };
213
+ });
214
+ }
215
+
216
+ // build draws the figure to fit the stage, with the written lines or the facts under it, and puts every counter in
217
+ // its place.
142
218
  function build() {
143
219
  const r = svg.getBoundingClientRect(); area = { w: Math.round(r.width), h: Math.round(r.height) };
144
220
  if (!area.w || !area.h) { geo = null; return; } // A phone is showing the text, so the figure is drawn when the stage shows.
145
221
  const room = k => k && spec.written === 'ten' ? STEP_LINE : LINE;
146
- const lineW = lines.length ? widthOf('00 + 00 = 00') : 0, lineH = lines.length ? SPACE + lines.reduce((sum, l, k) => sum + room(k), 0) : 0;
222
+ // The written lines under the figure. A page that puts counters in writes its subtraction in the room of its facts,
223
+ // which take its place once they show.
224
+ const shared = putting() && !!block;
225
+ const under = () => lines.length && !shared ? [{ w: widthOf('00 + 00 = 00'), h: lines.reduce((sum, l, k) => sum + room(k), 0) }] : [{ w: 0, h: 0 }];
226
+ // The facts under the figure take their size from the stage on their own, so the figure keeps its scale. The
227
+ // block keeps its room while its facts are hidden, so the figure keeps one size.
228
+ const facts = block ? factsSize() : null, factsH = (facts ? facts.h : 0) + (verdict ? VERDICT_ROOM : 0);
147
229
  // fit keeps the way of setting the figure out that shows it at the largest scale.
148
230
  const fit = (best, w, h, draw) => {
149
- const W = Math.max(w, lineW), H = h + lineH, scale = Math.min((area.w - 2 * PAD) / W, (area.h - 2 * PAD) / H);
150
- return !best || scale > best.scale + 1e-6 ? { scale, w: W, h: H, top: h, draw: () => draw((W - w) / 2) } : best;
231
+ for (const u of under()) {
232
+ const W = Math.max(w, u.w), H = h + (u.h ? SPACE + u.h : 0), scale = Math.min((area.w - 2 * PAD) / W, (area.h - 2 * PAD - factsH) / H);
233
+ if (!best || scale > best.scale + 1e-6) best = { scale, w: W, h: H, top: h, u, draw: () => draw((W - w) / 2) };
234
+ }
235
+ return best;
151
236
  };
152
- const L = shelf() ? shelfLayout(fit) : framesLayout(fit), scale = Math.min(MAX, L.scale);
153
- svg.setAttribute('viewBox', `${L.w / 2 - area.w / 2 / scale} ${L.h / 2 - area.h / 2 / scale} ${area.w / scale} ${area.h / scale}`);
237
+ const L = shelf() ? shelfLayout(fit) : spec.look === 'domino' ? dominoLayout(fit) : squad() ? squadLayout(fit) : framesLayout(fit);
238
+ const scale = Math.min(MAX, L.scale), H = L.h + factsH / scale;
239
+ svg.setAttribute('viewBox', `${L.w / 2 - area.w / 2 / scale} ${H / 2 - area.h / 2 / scale} ${area.w / scale} ${area.h / scale}`);
154
240
  geo = L.draw();
155
- back.replaceChildren(...geo.nodes);
241
+ back.replaceChildren(...geo.nodes, marks);
156
242
  for (const d of dies) d.g.setAttribute('display', spec.dice ? 'inline' : 'none');
157
243
  let y = L.top + SPACE;
158
- lines.forEach((l, k) => { l.at(L.w / 2, y + room(k) / 2 - 6); y += room(k); });
244
+ if (facts) {
245
+ block.el.setAttribute('transform', `translate(${L.w / 2 - facts.w / 2 / scale} ${L.h + SPACE / scale}) scale(${facts.k / scale})`);
246
+ block.at(0, 0, 2, facts.row);
247
+ }
248
+ if (shared) lines.forEach(l => l.at(L.w / 2, L.h + factsH / 2 / scale - 6));
249
+ else lines.forEach((l, k) => { l.at(L.w / 2, y + room(k) / 2 - 6); y += room(k); });
250
+ if (verdict) {
251
+ verdict.parentNode.setAttribute('transform', `translate(${L.w / 2} ${L.h + VERDICT_LINE / scale}) scale(${1 / scale})`);
252
+ verdictW = area.w - 2 * PAD; delete verdict.dataset.words; // The verdict wraps again for the new width.
253
+ }
159
254
  for (const n of nodes.values()) n.tween?.pause();
160
255
  layer.replaceChildren(); nodes = new Map();
161
256
  render('jump');
162
257
  }
163
258
 
259
+ // factsSize gives the facts' own scale, as wide as the stage allows and at most FACTS_MAX, the height of a row,
260
+ // tall enough for a finger at that scale, and the room the block takes under the figure, in pixels.
261
+ function factsSize() {
262
+ let row = LINE, k = 1, way = null;
263
+ for (let i = 0; i < 4; i++) { way = block.ways(row)[0]; k = Math.min(FACTS_MAX, (area.w - 2 * PAD) / way.w); if (row * k >= FINGER - 0.5) break; row = FINGER / k; }
264
+ way = block.ways(row)[0];
265
+ return { k, row, w: way.w * k, h: SPACE + way.h * k };
266
+ }
267
+
164
268
  // where gives the place of every counter. The first group and the joined counters fill the cells in order, and
165
269
  // the waiting counters stand in the tray. A counter taken away keeps its cell. A jar on the shelf stands in its
166
270
  // own place, and a jar sold stands in its crate in the order it was sold.
167
271
  function where() {
168
272
  const out = new Map();
273
+ if (spec.look === 'domino') return out;
274
+ if (squad()) {
275
+ s.red.forEach((id, i) => out.set(id, geo.spots[i]));
276
+ s.joined.forEach((id, i) => out.set(id, geo.seats[i]));
277
+ return out;
278
+ }
169
279
  if (shelf()) {
170
280
  s.jars.forEach((id, i) => out.set(id, geo.shelf(i)));
171
281
  [s.red, s.joined].forEach((list, k) => list.forEach((id, i) => out.set(id, geo.crates[k].pos(i))));
@@ -212,11 +322,24 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
212
322
  press(n.g, () => sell(id));
213
323
  return n;
214
324
  }
215
- const kinds = spec.look === 'tray' ? ['seedling', 'sprout'] : ['disc', 'square'], n = counter(kinds[firstOf(id) ? 0 : 1], firstOf(id) ? 'a' : 'b');
325
+ const kinds = spec.look === 'tray' ? ['seedling', 'sprout'] : spec.look === 'eggs' ? ['egg-brown', 'egg-white'] : squad() ? ['shirt', 'shirt'] : ['disc', 'square'];
326
+ const kind = kinds[firstOf(id) ? 0 : 1], n = counter(kind, firstOf(id) ? 'a' : 'b');
327
+ // A counter of a family page leaves a dashed outline while a picked subtraction takes its group away.
328
+ if (family()) n.pop.append(gone(kind));
329
+ // A counter put in carries the number the count reaches, and a player on the squad wears it on the shirt. The red
330
+ // counters let a tap through to the frames under them, and every player lets a tap through to the bench, so each tap
331
+ // on the bench seats the next player.
332
+ if (putting()) {
333
+ if (squad() || (fills(spec) && !firstOf(id))) { n.pop.append(el('text', { class: 'counters-num' })); n.g.classList.add('counters-numbered'); }
334
+ if (firstOf(id) || squad()) { n.g.classList.add('counters-still'); return n; }
335
+ n.g.setAttribute('tabindex', 0); n.g.setAttribute('role', 'button');
336
+ press(n.g, takeLast);
337
+ return n;
338
+ }
216
339
  // A sprout of the tray is a seed while it lies in the packet.
217
340
  if (spec.look === 'tray' && !firstOf(id)) n.pop.append(el('ellipse', { class: 'counters-body counters-seed', rx: 5, ry: 7, transform: 'rotate(20)' }));
218
341
  if (spec.take) {
219
- n.pop.append(el('circle', { class: 'counters-void counters-gone', r: R - 2 }));
342
+ n.pop.append(gone(kind));
220
343
  if (taps()) { n.g.setAttribute('tabindex', 0); n.g.setAttribute('role', 'button'); press(n.g, () => takeAway(id)); }
221
344
  return n;
222
345
  }
@@ -251,11 +374,33 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
251
374
  const into = !to ? s.tray : !spec.split || (to !== 'second' && s.red.length + s.joined.length < 10) ? s.joined : s.later;
252
375
  if (!from || (to && from !== s.tray) || (!to && from === s.tray)) return false;
253
376
  from.splice(from.indexOf(id), 1); into.push(id);
377
+ if (spec.undo && !s.tray.length) s.done = true;
254
378
  changed();
255
379
  return true;
256
380
  }
257
381
  // joinNext joins the next waiting counter, in order or into the row given.
258
382
  function joinNext(to = true) { const id = s.tray.at(-1); if (id) moveTo(id, to); }
383
+ // putNext puts a counter in the next place, or in the next cell on a challenge. takeLast takes the last one out, and a
384
+ // key that took it moves on to the counter before it, or to the frames once none is left.
385
+ function putNext() {
386
+ if (s.joined.length >= (fills(spec) ? s.whole - s.red.length : CELLS - s.red.length)) return;
387
+ s.joined.push(...ids(1, 'b'));
388
+ s.checked = false; picked = null;
389
+ changed();
390
+ }
391
+ function takeLast() {
392
+ if (!s.joined.length) return;
393
+ const keys = document.activeElement?.closest?.('.counters-counter');
394
+ s.joined.pop();
395
+ s.checked = false; picked = null;
396
+ changed();
397
+ if (keys) (nodes.get(s.joined.at(-1))?.g ?? geo?.area)?.focus();
398
+ }
399
+ // setWhole steps the whole, which empties the places.
400
+ function setWhole(v) {
401
+ s.whole = v; s.joined.length = 0; picked = null;
402
+ changed();
403
+ }
259
404
  // takeAway takes a counter away, or puts back the counter of a ring.
260
405
  function takeAway(id) {
261
406
  if (s.taken.has(id)) s.taken.delete(id); else s.taken.add(id);
@@ -283,16 +428,37 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
283
428
  // on or back to stay after the first group's. On a take-away page the first number puts every counter back, and the
284
429
  // number taken away takes counters from the last cell back.
285
430
  function setGroup(k, v) {
431
+ picked = null;
286
432
  if (spec.take && k === 'second') s.taken = new Set(s.red.slice(s.red.length - v));
287
433
  else {
288
434
  const list = k === 'first' ? s.red : s.joined;
289
435
  if (list.length < v) list.push(...ids(v - list.length, k === 'first' ? 'r' : 'b'));
290
436
  list.length = v;
291
437
  if (spec.take) s.taken = new Set();
438
+ if (fills(spec)) s.joined.length = 0;
292
439
  }
293
440
  s.checked = false;
294
441
  changed();
295
442
  }
443
+ // pick picks a fact, or lets it go when it is picked already.
444
+ function pick(id) {
445
+ picked = picked === id ? null : id;
446
+ if (geo) paint(false);
447
+ }
448
+ // coverHalf covers a half of the domino, and the line turns into the calculation that finds it. lift takes the card
449
+ // off, and the line takes its answer. A key that covered a half moves on to the card, and back when it lifts.
450
+ function coverHalf(k) {
451
+ const keys = document.activeElement?.closest?.('.counters-half');
452
+ s.cover = k; s.found = k;
453
+ changed();
454
+ if (keys) geo?.domino.card.focus();
455
+ }
456
+ function lift() {
457
+ const keys = document.activeElement?.closest?.('.counters-domino-card'), k = s.cover;
458
+ s.cover = null;
459
+ changed();
460
+ if (keys && k) geo?.domino.halves[k === 'first' ? 0 : 1].g.focus();
461
+ }
296
462
  function changed() { render('move'); }
297
463
 
298
464
  // paint turns the dice, writes the lines, opens the empty cells to taps and marks the counters taken away and the
@@ -302,8 +468,20 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
302
468
  const st = now(), steps = stepsOf(spec, st), g = base.challenge && goalOf();
303
469
  // A challenge writes its drawn calculation open from the start, and its answer and steps once the goal holds.
304
470
  const of = g ? [() => g.line(target, solved), () => solved && g.steps?.(target)[0], () => solved && g.steps?.(target)[1]]
305
- : [() => lineOf(spec, st), () => shelf() ? leftOf(spec, st) : steps?.lines[0], () => steps?.lines[1]];
306
- lines.forEach((l, k) => { if (l.set(of[k]() || null, int) && anim) go.flash(l.el); });
471
+ : [() => spec.look === 'domino' ? tint(lineOf(spec, st)) : fills(spec) ? tint(askOf(spec, st)) : lineOf(spec, st), () => shelf() ? leftOf(spec, st) : spec.undo || claims(spec) ? backOf(spec, st) : steps?.lines[0], () => steps?.lines[1]];
472
+ const facts = factsShow(st);
473
+ lines.forEach((l, k) => { if (l.set((block && facts ? null : of[k]()) || null, int) && anim) go.flash(l.el); });
474
+ if (block) {
475
+ block.el.setAttribute('display', facts ? 'inline' : 'none');
476
+ for (const changed of block.set({ first: st.first, second: st.second, listed: factsListed(spec), int, T, picked })) if (anim && facts) go.flash(changed);
477
+ }
478
+ if (putting()) paintPut(st);
479
+ if (verdict) {
480
+ const right = checked(st), words = T(right ? 'add-claim-right' : 'add-claim-wrong', { back: int(st.claim - st.second), first: int(st.first) });
481
+ if (verdict.dataset.words !== words) { verdict.dataset.words = words; wrap(verdict, words); if (anim) go.flash(verdict); }
482
+ verdict.classList.toggle('counters-verdict-right', right);
483
+ }
484
+ if (geo.domino) paintDomino(st);
307
485
  const used = new Set([...where().values()].map(p => geo.cells?.indexOf(p)));
308
486
  geo.targets.forEach((t, i) => t.setAttribute('display', !used.has(i) && s.tray.length ? 'inline' : 'none'));
309
487
  for (const [id, n] of nodes) {
@@ -322,18 +500,68 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
322
500
  light();
323
501
  }
324
502
 
503
+ // wrap writes a sentence in lines no wider than the stage, at about 0.55 of the font a letter.
504
+ function wrap(text, words) {
505
+ const most = Math.max(10, Math.floor(verdictW / (VERDICT_FONT * 0.55))), rows = [''];
506
+ for (const w of words.split(' ')) {
507
+ if (rows.at(-1) && `${rows.at(-1)} ${w}`.length > most) rows.push(w);
508
+ else rows[rows.length - 1] = rows.at(-1) ? `${rows.at(-1)} ${w}` : w;
509
+ }
510
+ text.replaceChildren(...rows.map((r, k) => { const t = el('tspan', { x: 0, dy: k ? VERDICT_LINE : 0 }); t.textContent = r; return t; }));
511
+ }
512
+
513
+ // paintPut draws the places of the missing part still empty, numbers the counters and names what a tap does.
514
+ function paintPut(st) {
515
+ const slots = !fills(spec) || squad() ? [] : geo.cells.slice(st.first + st.second, st.whole), side = squad() ? 36 : R * 1.8;
516
+ marks.replaceChildren(...slots.map(p => el('rect', { class: 'counters-gap-place', x: p.x - side / 2, y: p.y - side / 2, width: side, height: side, rx: 5 })));
517
+ geo.area.setAttribute('aria-label', T(squad() ? 'add-squad-put' : fills(spec) ? 'add-whole-put' : 'add-put'));
518
+ for (const [id, n] of nodes) {
519
+ const k = firstOf(id) ? s.red.indexOf(id) + 1 : st.first + s.joined.indexOf(id) + 1, num = n.pop.querySelector('.counters-num');
520
+ if (num) num.textContent = int(k);
521
+ if (squad()) { n.g.setAttribute('role', 'img'); n.g.setAttribute('aria-label', T(firstOf(id) ? 'add-squad-player' : 'add-squad-bench', int(k))); }
522
+ else if (!firstOf(id)) n.g.setAttribute('aria-label', fills(spec) ? T('add-whole-out', int(k)) : T('add-out'));
523
+ }
524
+ }
525
+
526
+ // paintDomino writes the whole above the domino, names each half and puts the card on the covered half.
527
+ function paintDomino(st) {
528
+ const d = geo.domino;
529
+ d.whole.textContent = int(st.first + st.second);
530
+ d.halves.forEach((hf, k) => hf.g.setAttribute('aria-label', T('add-domino-cover', int(k ? st.second : st.first))));
531
+ d.card.setAttribute('display', s.cover ? 'inline' : 'none');
532
+ d.card.setAttribute('aria-label', T('add-domino-lift'));
533
+ if (s.cover) { const hf = d.halves[s.cover === 'first' ? 0 : 1]; d.card.setAttribute('transform', `translate(${hf.x} ${hf.y})`); }
534
+ }
535
+
325
536
  // light marks the counters of each link the reader points at, and the numbers of the written lines.
326
537
  const GROUPS = {
327
- first: () => s.red, second: () => spec.take ? [...s.taken] : s.joined, sum: () => [...s.red, ...s.joined], waiting: () => s.tray,
328
- left: () => shelf() ? s.jars.filter(id => !s.red.includes(id) && !s.joined.includes(id)) : spec.take ? s.red.filter(id => !s.taken.has(id)) : [],
538
+ first: () => s.red, second: () => spec.take ? [...s.taken] : s.joined, sum: () => [...s.red, ...s.joined], whole: () => [...s.red, ...s.joined], waiting: () => s.tray,
539
+ left: () => shelf() ? s.jars.filter(id => !s.red.includes(id) && !s.joined.includes(id)) : spec.take ? s.red.filter(id => !s.taken.has(id)) : spec.undo && s.done ? [...s.red, ...s.joined] : [],
540
+ missing: () => s.joined, back: () => spec.undo && s.done ? s.tray : [],
329
541
  // The parts of making ten: on an addition the second group's counters in the first frame and in the second, and
330
542
  // on a take away the rings of the second frame and of the first. They light while the steps show.
331
543
  'to-ten': () => !stepsOf(spec, now()) ? [] : spec.take ? s.red.slice(10).filter(id => s.taken.has(id)) : [...s.red, ...s.joined].slice(s.red.length, 10),
332
544
  rest: () => !stepsOf(spec, now()) ? [] : spec.take ? s.red.slice(0, 10).filter(id => s.taken.has(id)) : [...[...s.red, ...s.joined].slice(10), ...s.later],
333
545
  };
546
+ // shown gives the fact the figure shows: the fact of a link the reader points at, else the fact picked. With two
547
+ // equal parts a link to the second of two equal facts shows the first.
548
+ function shown() {
549
+ const pointed = [...lit].filter(id => FACT_IDS.includes(id)).at(-1);
550
+ return pointed ? sameFact(pointed, s.red.length, fills(spec) ? s.whole - s.red.length : s.joined.length) : picked;
551
+ }
334
552
  function light() {
335
- const on = new Set([...lit].flatMap(id => GROUPS[id]?.() ?? []));
336
- for (const [id, n] of nodes) n.g.classList.toggle('counters-lit', on.has(id));
553
+ const on = new Set([...lit].flatMap(id => GROUPS[id]?.() ?? [])), f = family() ? shown() : null, away = f && takenBy(f);
554
+ // A fact shows in the counters: an addition lights them all, and a subtraction takes its part away and lights
555
+ // the part left.
556
+ const taken = new Set(away ? GROUPS[away]() : []);
557
+ if (f) for (const id of away ? GROUPS[away === 'first' ? 'second' : 'first']() : GROUPS.whole()) on.add(id);
558
+ for (const [id, n] of nodes) {
559
+ n.g.classList.toggle('counters-lit', on.has(id));
560
+ if (family()) n.g.classList.toggle('counters-taken', taken.has(id));
561
+ }
562
+ // The places stand for the counters of the missing part not put in yet, and light with them.
563
+ const places = lit.has('missing') || lit.has('whole') || (!!f && away !== 'second');
564
+ for (const p of marks.children) p.classList.toggle('counters-hot', places);
337
565
  for (const d of dies) d.g.classList.toggle('counters-hot', lit.has(d.g.dataset.ref));
338
566
  for (const part of back.querySelectorAll('[data-ref]')) part.classList.toggle('counters-hot', lit.has(part.dataset.ref));
339
567
  }
@@ -353,7 +581,7 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
353
581
  // vars gives a challenge's words: its numbers, the reader's numbers and step, and the lines of its done line.
354
582
  function vars() {
355
583
  const g = goalOf(), a = s.answer, steps = g.steps?.(target);
356
- return { ...g.words(base.challenge, target, { int }), n: int(a.sum), calc: words(g.line(target, false), int), line: words(g.line(target, true), int),
584
+ return { ...g.words(base.challenge, target, { int }), n: int(a.sum), put: int(s.joined.length), made: int(s.red.length + s.joined.length), calc: words(g.line(target, false), int), line: words(g.line(target, true), int),
357
585
  step: target.sign ? words({ a: target.a, sign: target.sign, b: a.toTen, open: true }, int) : '', x: int(a.toTen), y: int(a.rest), toTen: steps ? words(steps[0], int) : '', rest: steps ? words(steps[1], int) : '' };
358
586
  }
359
587
  function judge() {
@@ -372,6 +600,11 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
372
600
  if (s.checked) return;
373
601
  s.checked = true;
374
602
  if (!holds().done) return render('move');
603
+ if (base.challenge.goal === 'missing-part') {
604
+ render('move');
605
+ if (moving()) go.cheer([...nodes.values()].map(n => n.pop));
606
+ return;
607
+ }
375
608
  const run = play, g = base.challenge.goal, t = target;
376
609
  const first = g === 'find-sum' ? () => { s.red = ids(t.a, 'r'); } : t.sign === '+' ? () => { s.joined.push(...s.tray.splice(-toTen())); } : () => { for (const id of s.red.slice(-toTen())) s.taken.add(id); };
377
610
  const then = g === 'find-sum' ? () => { s.joined = ids(t.b, 'b'); } : t.sign === '+' ? () => { s.joined.push(...s.tray.splice(0)); } : () => { for (const id of s.red.slice(10 - (t.b - toTen()), 10).reverse()) s.taken.add(id); };
@@ -394,6 +627,8 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
394
627
  // written lines and their words take a line of the panel each, so the panel keeps its height as they change.
395
628
  function drawPanel() {
396
629
  const c = base.challenge, ctls = [];
630
+ if (spec.claimCtl) ctls.push(stepper({ id: 'claim', label: name('claim'), min: spec.claimCtl.lo, max: spec.claimCtl.hi, value: s.claim, write: int, change: v => { s.claim = v; changed(); } }));
631
+ if (spec.wholeCtl) ctls.push(stepper({ id: 'whole', label: name('whole'), min: spec.wholeCtl.lo, max: spec.wholeCtl.hi, value: s.whole, write: int, change: setWhole }));
397
632
  for (const k of ['first', 'second']) {
398
633
  const r = spec[`${k}Ctl`];
399
634
  if (r) ctls.push(stepper({ id: k, label: name(k), min: r.lo, max: r.hi, value: now()[k], write: int, change: v => setGroup(k, v) }));
@@ -421,6 +656,7 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
421
656
  if (!spec) return;
422
657
  if (on) lit.add(id); else lit.delete(id);
423
658
  for (const l of lines) l.light(id, on, l.whole);
659
+ block?.light(id, on);
424
660
  if (geo) light();
425
661
  for (const part of panel.querySelectorAll('[data-ref]')) part.classList.toggle('hot', on && part.dataset.ref === id);
426
662
  }
@@ -433,7 +669,7 @@ export default function add({ host, stage, panel, T, lang, moving, motion: fx })
433
669
  if (!geo) render('jump');
434
670
  },
435
671
  hide() {
436
- stop(); play++; spec = null; base = null; goal = null; geo = null; nodes = new Map(); lines = [];
672
+ stop(); play++; spec = null; base = null; goal = null; geo = null; nodes = new Map(); lines = []; block = null; picked = null;
437
673
  for (const g of [back, layer, front]) g.replaceChildren();
438
674
  },
439
675
  point,