@openbooknetwork/hesap 0.2.2 → 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/package.json +2 -2
- package/src/client.css +89 -1
- package/src/families/counters/client.css +89 -1
- package/src/families/counters/client.js +9 -12
- package/src/families/counters/count-figures.js +23 -3
- package/src/families/counters/count-model.js +5 -0
- package/src/families/counters/count.js +27 -4
- package/src/families/counters/count.mjs +5 -3
- package/src/families/counters/counter.js +11 -0
- package/src/families/counters/i18n/en.yaml +68 -0
- package/src/families/counters/i18n/tr.yaml +68 -0
- package/src/families/counters/index.mjs +9 -6
- package/src/families/counters/match-figures.js +60 -0
- package/src/families/counters/match-goals.js +63 -0
- package/src/families/counters/match-model.js +84 -0
- package/src/families/counters/match.js +379 -0
- package/src/families/counters/match.mjs +96 -0
- package/src/families/counters/order-figures.js +46 -0
- package/src/families/counters/order-model.js +69 -0
- package/src/families/counters/order.js +199 -0
- package/src/families/counters/order.mjs +62 -0
- package/src/families/counters/reading.js +66 -0
- package/src/families/counters/scenes.js +18 -0
- package/src/i18n/en.yaml +68 -0
- package/src/i18n/tr.yaml +68 -0
- package/src/kit/panel.js +7 -4
- package/src/node.mjs +10 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The Node half of the match scene: two rows of objects paired one to one. It checks a page's fields and says what
|
|
2
|
+
// each link shows when the page opens.
|
|
3
|
+
import { schema } from '@openbooknetwork/kitap';
|
|
4
|
+
import { MATCH_LOOKS, ROWS, ROW_MAX, SPREAD_MAX, PLAYERS, GOALS_MAX, roomOf, opening, counts, wordsOf, tagKey, measuresOf, wordsLinked, readingOf, verdictOf } from './match-model.js';
|
|
5
|
+
import { SIGN_FIELD } from './reading.js';
|
|
6
|
+
import { MATCH_GOALS, matchStart } from './match-goals.js';
|
|
7
|
+
import { numbers } from '../../kit/format.js';
|
|
8
|
+
import { list, value, string, challenge, drawn } from '../../node.mjs';
|
|
9
|
+
|
|
10
|
+
const { z, check } = schema;
|
|
11
|
+
const Int = z.number().int();
|
|
12
|
+
const Range = z.tuple([Int, Int]);
|
|
13
|
+
const Stepper = z.object({ stepper: Range, start: Int.optional() }).strict();
|
|
14
|
+
const Row = z.union([Int, Stepper]);
|
|
15
|
+
const Fields = z.object({
|
|
16
|
+
look: z.enum(MATCH_LOOKS).default('rows'), // Two rows of counters, cups above saucers, players above chairs, or two scores.
|
|
17
|
+
top: Row.optional(), // The objects of the top row as the page opens, as a number or on a stepper.
|
|
18
|
+
bottom: Row.optional(), // The objects of the bottom row.
|
|
19
|
+
pair: z.enum(['drag', 'auto']).optional(), // The reader draws each pair, or the scene pairs the rows in order.
|
|
20
|
+
spread: z.enum(ROWS).optional(), // A Spacing slider sets the gap between the counters of this row.
|
|
21
|
+
// same shows the words "The same" between rows whose every counter has a partner. true gives each row a tag at
|
|
22
|
+
// its end that reads more, fewer or the same.
|
|
23
|
+
word: z.union([z.literal('same'), z.literal(true)]).optional(),
|
|
24
|
+
tap: z.literal('tag').optional(), // A tap on a counter left over gives it the next number.
|
|
25
|
+
trace: z.boolean().default(false), // A counter added since the page opened carries a plus, and one taken leaves a place.
|
|
26
|
+
trail: z.boolean().default(false), // The bottom row holds the top row as it was one step ago.
|
|
27
|
+
// A line of the two numbers with a sign between them: a tile that opens on this sign, or a sign that follows.
|
|
28
|
+
sign: z.enum(SIGN_FIELD).optional(),
|
|
29
|
+
// A challenge names a goal of match-goals.js and the ranges it draws its rows from.
|
|
30
|
+
challenge: z.object({ goal: z.string(), top: Range.optional(), bottom: Range.optional(), rows: Range.optional(), apart: Range.optional() }).strict().optional(),
|
|
31
|
+
readout: z.array(z.string()).default([]),
|
|
32
|
+
}).strict();
|
|
33
|
+
|
|
34
|
+
function read(fields, { at }, ctx) {
|
|
35
|
+
const pg = check(Fields, fields, at), fail = words => ctx.fail(at, words);
|
|
36
|
+
const top = value(pg.top), bottom = value(pg.bottom);
|
|
37
|
+
const spec = { ...pg, scene: 'match', pair: pg.pair ?? 'drag', top: top.n, topCtl: top.ctl, bottom: bottom.n, bottomCtl: bottom.ctl };
|
|
38
|
+
if (pg.challenge) { readChallenge(pg, spec, at, ctx); return { data: spec, has: () => false, offers: 'nothing, a challenge page draws its numbers as it opens' }; }
|
|
39
|
+
for (const [v, name] of [[top, 'top'], [bottom, 'bottom']]) {
|
|
40
|
+
if (v.n == null) { fail(`${name} is missing, write the objects of the ${name} row, such as 5, or { stepper: [1, 10], start: 5 }`); continue; }
|
|
41
|
+
const lo = v.ctl?.lo ?? v.n, hi = v.ctl?.hi ?? v.n;
|
|
42
|
+
if (lo > hi || v.n < lo || v.n > hi) fail(`${name} has the range [${lo}, ${hi}] and starts at ${v.n}, write [low, high] and a start between them`);
|
|
43
|
+
else if (lo < 0 || hi > ROW_MAX) fail(`${name} reaches ${v.ctl ? `[${lo}, ${hi}]` : v.n}, a row holds from 0 to ${ROW_MAX} objects`);
|
|
44
|
+
}
|
|
45
|
+
if (pg.sign && !['rows', 'scoreboard'].includes(pg.look)) fail('sign stands between the numbers of two rows or two scores, write look: rows or look: scoreboard');
|
|
46
|
+
if (pg.look === 'scoreboard') {
|
|
47
|
+
for (const f of ['pair', 'word', 'tap', 'trace', 'trail', 'spread']) if (pg[f]) fail(`${f} belongs to look: rows, the buttons add the goals of the scoreboard`);
|
|
48
|
+
if (top.ctl || bottom.ctl || [top.n, bottom.n].some(n => n > GOALS_MAX)) fail(`top and bottom are the scores of the home team and the away team, write each as a number from 0 to ${GOALS_MAX}`);
|
|
49
|
+
if (pg.sign !== 'follow') fail('the scoreboard keeps the sign between its scores, write sign: follow');
|
|
50
|
+
}
|
|
51
|
+
if (pg.look === 'cups' && pg.pair) fail('pair belongs to look: rows, a drag seats a cup on a saucer in the cups look');
|
|
52
|
+
if (pg.look === 'chairs') {
|
|
53
|
+
for (const f of ['pair', 'word', 'tap', 'trace', 'trail', 'spread']) if (pg[f]) fail(`${f} belongs to look: rows, the buttons run the rounds of the chairs`);
|
|
54
|
+
if (top.ctl || bottom.ctl || top.n < PLAYERS[0] || top.n > PLAYERS[1] || bottom.n !== top.n - 1) fail(`top and bottom are the players and the chairs, write top as a number from ${PLAYERS[0]} to ${PLAYERS[1]} and bottom as one less`);
|
|
55
|
+
}
|
|
56
|
+
if (pg.spread) {
|
|
57
|
+
if (pg.look !== 'rows' || spec.pair !== 'auto') fail('spread moves the counters of a row under lines the scene draws, write look: rows and pair: auto');
|
|
58
|
+
if (roomOf(spec, pg.spread) > SPREAD_MAX) fail(`spread doubles the length of the ${pg.spread} row, which reaches ${roomOf(spec, pg.spread)} counters, give it ${SPREAD_MAX} at most`);
|
|
59
|
+
}
|
|
60
|
+
if (!['chairs', 'scoreboard'].includes(pg.look)) {
|
|
61
|
+
if (pg.word === true && (pg.look !== 'rows' || spec.pair !== 'auto')) fail('word: true names the row with counters left over by lines the scene draws, write look: rows and pair: auto');
|
|
62
|
+
if (pg.trail) {
|
|
63
|
+
if (pg.look !== 'rows' || spec.pair !== 'auto' || !top.ctl || bottom.ctl || Math.abs(top.n - bottom.n) !== 1) fail('trail keeps the top row of one step ago in the bottom row, write look: rows, pair: auto, top on a stepper and bottom as a number one less or one more than its start');
|
|
64
|
+
for (const f of ['trace', 'spread']) if (pg[f]) fail(`${f} and trail both follow the steps of a row, keep one`);
|
|
65
|
+
}
|
|
66
|
+
for (const f of ['tap', 'trace']) if (pg[f] && (pg.look !== 'rows' || spec.pair !== 'auto')) fail(`${f} works on counters left over by lines the scene draws, write look: rows and pair: auto`);
|
|
67
|
+
}
|
|
68
|
+
const ids = measuresOf(spec), linked = [...ids, ...wordsLinked(spec)];
|
|
69
|
+
for (const id of pg.readout) if (!ids.includes(id)) fail(`readout names "${id}", this page reads out ${list(ids)}`);
|
|
70
|
+
return { data: spec, has: id => linked.includes(id), offers: `one of ${list(linked)}` };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A challenge page names its goal and the ranges the goal draws from, and leaves every other field out. The goal
|
|
74
|
+
// draws the rows, and gives the page its stepper or its sign tile.
|
|
75
|
+
function readChallenge(pg, spec, at, ctx) {
|
|
76
|
+
const c = pg.challenge, goal = challenge(c, MATCH_GOALS, { scene: 'match', at, ctx });
|
|
77
|
+
if (!goal) return;
|
|
78
|
+
for (const [f, v] of Object.entries(pg)) if (!['challenge', 'look'].includes(f) && v !== undefined && v !== false && !(f === 'readout' && !v.length)) ctx.fail(at, `${f} is set by challenge.goal ${c.goal}, leave it out`);
|
|
79
|
+
if (pg.look !== 'rows') ctx.fail(at, `look is set by challenge.goal ${c.goal}, which draws two rows of counters, leave it out`);
|
|
80
|
+
const problem = goal.problem(c);
|
|
81
|
+
if (problem) return ctx.fail(at, `challenge.${problem}`);
|
|
82
|
+
drawn(goal, c, spec, { at, ctx, starts: matchStart });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// A link shows a count or the word of a row's tag as the page opens.
|
|
86
|
+
function start(spec, lang, id) {
|
|
87
|
+
const s = opening(spec);
|
|
88
|
+
if (wordsLinked(spec).includes(id)) { const w = wordsOf(spec, s)[id.split('-')[0]]; return string(lang, `counters_${tagKey(w)}`, { n: numbers(lang).int(w.n) }); }
|
|
89
|
+
if (!measuresOf(spec).includes(id)) return null;
|
|
90
|
+
const int = numbers(lang).int;
|
|
91
|
+
if (id === 'reading') return string(lang, `counters_${readingOf(spec, s)}`, { a: int(s.top), b: int(s.bottom) });
|
|
92
|
+
if (id === 'verdict') return string(lang, `counters_${verdictOf(spec, s)}`);
|
|
93
|
+
return int(counts(spec, s)[id]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default { read, start };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The parts of an order figure: a counter drawn small, a block of rows of ten with its number, and a number card.
|
|
2
|
+
import { svg as el } from '../../kit/dom.js';
|
|
3
|
+
import { txt } from './figures.js';
|
|
4
|
+
import { tenPlace } from './count-figures.js';
|
|
5
|
+
|
|
6
|
+
// dot is a counter of radius r that takes no touch, in the family's colour a or b.
|
|
7
|
+
export const dot = (colour, r, p) => el('g', { class: `counters-counter counters-${colour}`, transform: `translate(${p.x} ${p.y})` },
|
|
8
|
+
[el('circle', { class: 'counters-body', r }), el('circle', { class: 'counters-ring', r: r * 0.6 })]);
|
|
9
|
+
|
|
10
|
+
const GAP = 22, FIVE = 4, DOT = 9, PAD = 8; // A block's counters, the gap after the fifth and the edge round them.
|
|
11
|
+
const NUM = 46; // The room above a block for its number.
|
|
12
|
+
export const BLOCK = { w: 10 * GAP + FIVE + 2 * PAD, h: NUM + 10 * GAP + 2 * PAD, num: NUM / 2 - 2 };
|
|
13
|
+
|
|
14
|
+
// block draws a number's room for 100 counters in rows of ten at x, 0, with the number above it. set lays n
|
|
15
|
+
// counters out, with the full rows solid and the last row lighter. frame surrounds the full rows, the last row or
|
|
16
|
+
// the whole block, cover hides the rows.
|
|
17
|
+
export function block(row, colour, x) {
|
|
18
|
+
const num = txt(`counters-block-num counters-${colour}`, x + BLOCK.w / 2, BLOCK.num), top = NUM + PAD;
|
|
19
|
+
num.setAttribute('data-ref', row);
|
|
20
|
+
const dots = Array.from({ length: 100 }, (_, i) => { const p = tenPlace(i, GAP, FIVE); return dot(colour, DOT, { x: x + PAD + p.x, y: top + p.y }); });
|
|
21
|
+
const held = el('g', {}, dots), edge = el('rect', { class: 'counters-decide', rx: 8, display: 'none' });
|
|
22
|
+
const lid = el('g', { class: 'counters-cover', display: 'none' }, [el('rect', { x, y: NUM, width: BLOCK.w, height: BLOCK.h - NUM, rx: 10 }), txt('counters-cover-mark', x + BLOCK.w / 2, NUM + (BLOCK.h - NUM) / 2, '?')]);
|
|
23
|
+
return {
|
|
24
|
+
nodes: [el('rect', { class: 'counters-tray', x, y: NUM, width: BLOCK.w, height: BLOCK.h - NUM, rx: 10 }), held, edge, lid, num],
|
|
25
|
+
set(n, words) {
|
|
26
|
+
num.textContent = words;
|
|
27
|
+
dots.forEach((d, i) => { d.setAttribute('display', i < n ? 'inline' : 'none'); d.classList.toggle('counters-light', i >= Math.floor(n / 10) * 10); });
|
|
28
|
+
},
|
|
29
|
+
// kind is rows, last or all, or null for no frame. An empty run of rows keeps a thin frame at its place.
|
|
30
|
+
frame(kind, n) {
|
|
31
|
+
edge.setAttribute('display', kind ? 'inline' : 'none');
|
|
32
|
+
if (!kind) return;
|
|
33
|
+
const full = Math.floor(n / 10), from = kind === 'last' ? full : 0, rows = kind === 'last' ? 1 : kind === 'rows' ? full : 10, m = kind === 'all' ? PAD : 3;
|
|
34
|
+
for (const [k, v] of Object.entries({ x: x + PAD - m, y: top + from * GAP - m, width: BLOCK.w - 2 * PAD + 2 * m, height: rows * GAP + 2 * m })) edge.setAttribute(k, v);
|
|
35
|
+
},
|
|
36
|
+
cover(on) { lid.setAttribute('display', on ? 'inline' : 'none'); held.setAttribute('display', on ? 'none' : 'inline'); },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const CARD = { w: 84, h: 108 };
|
|
41
|
+
// card draws a number card round 0, 0: the number, and under it the number in small rows of ten.
|
|
42
|
+
export function card(n, words) {
|
|
43
|
+
const g = 5.6, left = -5 * g - 0.8, top = -CARD.h / 2 + 40, full = Math.floor(n / 10) * 10;
|
|
44
|
+
const minis = Array.from({ length: n }, (_, i) => { const p = tenPlace(i, g, 1.6); return el('circle', { class: `counters-mini${i >= full ? ' counters-light' : ''}`, cx: left + p.x, cy: top + p.y, r: 2.2 }); });
|
|
45
|
+
return [el('rect', { class: 'counters-card-box', x: -CARD.w / 2, y: -CARD.h / 2, width: CARD.w, height: CARD.h, rx: 9 }), txt('counters-card-num', 0, -CARD.h / 2 + 21, words), ...minis];
|
|
46
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// What an order page holds and what it reads out: two numbers on one count or as two blocks of rows of ten, or three
|
|
2
|
+
// number cards. The build and the browser both use this file, so a page's text and its live values agree.
|
|
3
|
+
import { relation, isTile, signShown, readingKey, verdictKey } from './reading.js';
|
|
4
|
+
|
|
5
|
+
// A page draws its two numbers as markers on one count of 100, or as two blocks of rows of ten.
|
|
6
|
+
export const SHOWS = ['count', 'blocks'];
|
|
7
|
+
export const ROWS = ['top', 'bottom'];
|
|
8
|
+
export const TOP = 100; // The greatest number a page shows.
|
|
9
|
+
export const CARDS = 3; // The cards a page puts in order.
|
|
10
|
+
|
|
11
|
+
// A page puts cards in order when it names their numbers or asks for a draw.
|
|
12
|
+
export const isCards = spec => !!(spec.cards || spec.draw);
|
|
13
|
+
|
|
14
|
+
// opening gives the numbers as the page opens. cards holds the numbers of the cards on show, which New numbers
|
|
15
|
+
// draws again. A page that asks for a draw and names no cards opens without them, and the browser draws them. sign is the sign on the tile of a page that has one. open says whether the rows of the blocks are in sight, and place
|
|
16
|
+
// holds the place of each card, counted from 0, or null for a card in the tray.
|
|
17
|
+
export const opening = spec => ({ top: spec.top, bottom: spec.bottom, open: !spec.cover, sign: isTile(spec) ? spec.sign : null,
|
|
18
|
+
cards: spec.cards ?? [], place: isCards(spec) ? Array(CARDS).fill(null) : [] });
|
|
19
|
+
|
|
20
|
+
// A number in rows of ten has full rows and a last row: 3 and 4 for 34, 4 and 0 for 40.
|
|
21
|
+
export const tens = n => ({ rows: Math.floor(n / 10), last: n % 10 });
|
|
22
|
+
|
|
23
|
+
// counts gives every number a page can show.
|
|
24
|
+
export function counts(spec, s) {
|
|
25
|
+
const t = tens(s.top), b = tens(s.bottom);
|
|
26
|
+
return { top: s.top, bottom: s.bottom, 'top-rows': t.rows, 'top-last': t.last, 'bottom-rows': b.rows, 'bottom-last': b.last };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// decides names the rows that decide which number is greater: the full rows when their numbers differ, the last
|
|
30
|
+
// rows when the full rows are the same, and all of both blocks when the numbers are the same.
|
|
31
|
+
export const decides = s => s.top === s.bottom ? 'all' : tens(s.top).rows !== tens(s.bottom).rows ? 'rows' : 'last';
|
|
32
|
+
|
|
33
|
+
// between says how the cards of two neighbouring places stand, or null while one of the places is empty.
|
|
34
|
+
export function between(spec, s, k) {
|
|
35
|
+
const [a, b] = [k, k + 1].map(p => s.cards[s.place.indexOf(p)]);
|
|
36
|
+
return a === undefined || b === undefined ? null : relation(a, b);
|
|
37
|
+
}
|
|
38
|
+
// The cards stand in order when each is less than the one after it.
|
|
39
|
+
export const ordered = (spec, s) => Array.from({ length: CARDS - 1 }, (_, k) => between(spec, s, k)).every(r => r === 'less');
|
|
40
|
+
|
|
41
|
+
// The measures a page can read out and link in its text. A covered page keeps what gives the answer away to itself.
|
|
42
|
+
export function measuresOf(spec) {
|
|
43
|
+
if (isCards(spec)) return [];
|
|
44
|
+
if (spec.cover) return ['top', 'bottom'];
|
|
45
|
+
return ['top', 'bottom', ...(spec.reading || spec.sign ? ['reading'] : []), ...(isTile(spec) ? ['verdict'] : []), ...(spec.show === 'blocks' ? ['top-rows', 'top-last', 'bottom-rows', 'bottom-last'] : [])];
|
|
46
|
+
}
|
|
47
|
+
// The strings of the line of words and of the mark, as the page stands.
|
|
48
|
+
export const readingOf = (spec, s) => readingKey(s.top, s.bottom, signShown(spec, s));
|
|
49
|
+
export const verdictOf = (spec, s) => verdictKey(s.top, s.bottom, signShown(spec, s));
|
|
50
|
+
|
|
51
|
+
// On one count the first number's pin stands above its counter and the second's below. With the first number one
|
|
52
|
+
// row under the second, both pins meet between the two counters, so pinShift moves the first left and the second
|
|
53
|
+
// right there. It gives each pin's shift as -1, 0 or 1.
|
|
54
|
+
export const pinShift = s => s.top - s.bottom === 10 ? { top: -1, bottom: 1 } : { top: 0, bottom: 0 };
|
|
55
|
+
|
|
56
|
+
// drawCards draws three numbers from 10 to 99 for the cards. Two have the same full rows and different last rows,
|
|
57
|
+
// so the last row decides between them, and the third has other full rows. In one draw of three the third is one
|
|
58
|
+
// of the two with its digits swapped, as 63 beside 36, where that gives such a number. The cards come in an order
|
|
59
|
+
// that is not yet the right one, and differ from the cards in last.
|
|
60
|
+
export function drawCards(dice, last = []) {
|
|
61
|
+
const digits = Array.from({ length: 10 }, (_, i) => i), swap = n => (n % 10) * 10 + Math.floor(n / 10);
|
|
62
|
+
for (;;) {
|
|
63
|
+
const rows = dice.int(1, 9), pair = dice.some(digits, 2).map(d => rows * 10 + d);
|
|
64
|
+
const turned = dice.shuffle(pair).map(swap).filter(n => n >= 10 && tens(n).rows !== rows);
|
|
65
|
+
const third = dice.int(1, 3) === 1 && turned.length ? turned[0] : dice.pick(digits.slice(1).filter(r => r !== rows)) * 10 + dice.pick(digits);
|
|
66
|
+
const cards = dice.shuffle([...pair, third]), sorted = a => [...a].sort((x, y) => x - y).join();
|
|
67
|
+
if (cards.join() !== sorted(cards) && sorted(cards) !== sorted(last)) return cards;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// The order scene: two numbers compared and three numbers put in order. The reader moves two markers along one
|
|
2
|
+
// count of 100, lays two numbers out as blocks of rows of ten, or drags three number cards into places.
|
|
3
|
+
import { ROWS, CARDS, opening, isCards, counts, decides, between, ordered, measuresOf, readingOf, verdictOf, pinShift, drawCards } from './order-model.js';
|
|
4
|
+
import { readingLine, signTile, verdictMark, isTile, nextSign, signShown, signOf } from './reading.js';
|
|
5
|
+
import { show, put, R } from './counter.js';
|
|
6
|
+
import { rows } from './count-figures.js';
|
|
7
|
+
import { dot, block, card, BLOCK, CARD } from './order-figures.js';
|
|
8
|
+
import { txt } from './figures.js';
|
|
9
|
+
import { carry, press } from './move.js';
|
|
10
|
+
import { driver } from './driver.js';
|
|
11
|
+
import { svg as el, svgPoint } from '../../kit/dom.js';
|
|
12
|
+
import { slider, stepper, chip, button, row as line } from '../../kit/panel.js';
|
|
13
|
+
import { live } from '../../kit/text.js';
|
|
14
|
+
import { numbers } from '../../kit/format.js';
|
|
15
|
+
import { dice, fresh } from '../../kit/random.js';
|
|
16
|
+
|
|
17
|
+
const PAD = 14; // The figure keeps this far from the stage's edges, in pixels.
|
|
18
|
+
// The top number is the first number, in red, and the bottom number the second, in blue. The colour is a second
|
|
19
|
+
// cue: the first number stands left or above, and the second right or below.
|
|
20
|
+
const COLOUR = { top: 'a', bottom: 'b' };
|
|
21
|
+
const LINE = 44; // The room under a figure for the line of words.
|
|
22
|
+
const far = (p, q) => Math.hypot(p.x - q.x, p.y - q.y);
|
|
23
|
+
|
|
24
|
+
export default function order({ host, stage, panel, T, lang, moving, motion: fx }) {
|
|
25
|
+
const go = driver(fx);
|
|
26
|
+
const svg = el('svg', { class: 'counters-svg' });
|
|
27
|
+
stage.appendChild(svg);
|
|
28
|
+
// spec is the page and s its numbers, or the places of its cards. fig is the figure as drawn: its size, the
|
|
29
|
+
// greatest scale it takes and a paint that follows s. reveal is the button of a covered page.
|
|
30
|
+
let spec = null, s = null, fig = null, area = { w: 0, h: 0 }, chips = [], reveal = null, cards = [];
|
|
31
|
+
|
|
32
|
+
const int = n => numbers(lang()).int(n);
|
|
33
|
+
const words = () => T(readingOf(spec, s), { a: int(s.top), b: int(s.bottom) });
|
|
34
|
+
new ResizeObserver(() => { const r = svg.getBoundingClientRect(); if (spec && (Math.round(r.width) !== area.w || Math.round(r.height) !== area.h)) build(); }).observe(svg);
|
|
35
|
+
|
|
36
|
+
// build draws the page's figure to fit the stage.
|
|
37
|
+
function build() {
|
|
38
|
+
const r = svg.getBoundingClientRect(); area = { w: Math.round(r.width), h: Math.round(r.height) };
|
|
39
|
+
for (const c of cards) c.tween?.pause();
|
|
40
|
+
cards = [];
|
|
41
|
+
if (!area.w || !area.h) { fig = null; return; } // A phone is showing the text, so the figure is drawn when the stage shows.
|
|
42
|
+
fig = isCards(spec) ? cardsFig() : spec.show === 'count' ? countFig() : blocksFig();
|
|
43
|
+
const scale = Math.min(fig.max, (area.w - 2 * PAD) / fig.w, (area.h - 2 * PAD) / fig.h);
|
|
44
|
+
svg.setAttribute('viewBox', `${fig.w / 2 - area.w / 2 / scale} ${fig.h / 2 - area.h / 2 / scale} ${area.w / scale} ${area.h / scale}`);
|
|
45
|
+
svg.replaceChildren(...fig.nodes);
|
|
46
|
+
fig.paint(false); refresh();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// One count of 100 in rows of ten with a marker on each number. The counters up to the less number are filled
|
|
50
|
+
// grey. The counters after it up to the greater number are hollow, with a heavy outline in that number's colour.
|
|
51
|
+
function countFig() {
|
|
52
|
+
const top = 18, tray = rows(100)[0], d = tray.draw(0, top), at = i => d.places.slots.pos(i);
|
|
53
|
+
for (const l of d.refs.labels) l.t.textContent = int(l.n);
|
|
54
|
+
const dots = Array.from({ length: 100 }, (_, i) => dot('b', R, at(i)));
|
|
55
|
+
// A marker is a frame round its counter and a pin that holds the number. The first number has a ring and a round
|
|
56
|
+
// pin above its counter, the second a square frame and a square pin below, so the two differ without their colours.
|
|
57
|
+
const pins = ROWS.map((r, k) => {
|
|
58
|
+
const half = R + 5.5, ring = k ? el('rect', { class: `counters-pin-ring counters-${COLOUR[r]}`, width: 2 * half, height: 2 * half, rx: 4 }) : el('circle', { class: `counters-pin-ring counters-${COLOUR[r]}`, r: R + 2.5 });
|
|
59
|
+
const box = el('rect', { class: 'counters-pin-box', y: -10, height: 20, rx: k ? 3 : 10 }), num = el('text', { class: 'counters-pin-num' });
|
|
60
|
+
return { r, k, ring, box, num, g: el('g', { class: `counters-pin counters-${COLOUR[r]}`, 'data-ref': r }, [box, num]) };
|
|
61
|
+
});
|
|
62
|
+
const said = readingLine(tray.tray / 2, top + tray.h + LINE / 2 + 4);
|
|
63
|
+
said.show(spec.reading);
|
|
64
|
+
return {
|
|
65
|
+
w: tray.w, h: top + tray.h + (spec.reading ? LINE : 0), max: 1.7,
|
|
66
|
+
nodes: [...d.nodes, ...dots, ...pins.map(p => p.ring), ...pins.map(p => p.g), said.el],
|
|
67
|
+
paint() {
|
|
68
|
+
const lo = Math.min(s.top, s.bottom), hi = Math.max(s.top, s.bottom), big = COLOUR[s.top > s.bottom ? 'top' : 'bottom'];
|
|
69
|
+
dots.forEach((g, i) => { const cls = `counters-counter ${i < lo ? 'counters-both' : i < hi ? `counters-${big} counters-hollow` : 'counters-pale'}`; if (g.getAttribute('class') !== cls) g.setAttribute('class', cls); });
|
|
70
|
+
const shift = pinShift(s);
|
|
71
|
+
for (const p of pins) {
|
|
72
|
+
const c = at(s[p.r] - 1), text = int(s[p.r]), w = 14 + 8 * text.length;
|
|
73
|
+
if (p.k) { p.ring.setAttribute('x', c.x - R - 5.5); p.ring.setAttribute('y', c.y - R - 5.5); } else { p.ring.setAttribute('cx', c.x); p.ring.setAttribute('cy', c.y); }
|
|
74
|
+
p.num.textContent = text; p.box.setAttribute('x', -w / 2); p.box.setAttribute('width', w);
|
|
75
|
+
p.g.setAttribute('transform', `translate(${c.x + shift[p.r] * (w / 2 + 1)} ${c.y + (p.k ? 1 : -1) * R * 1.3})`);
|
|
76
|
+
}
|
|
77
|
+
said.say(words());
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Two blocks side by side, each a number in rows of ten. A frame surrounds the rows that decide, and a covered
|
|
83
|
+
// page hides the rows, the frame and the line until the reader asks for them.
|
|
84
|
+
function blocksFig() {
|
|
85
|
+
const tiled = isTile(spec), between = tiled ? 62 : 28, w = 2 * BLOCK.w + between, blocks = ROWS.map((r, k) => ({ r, ...block(r, COLOUR[r], k * (BLOCK.w + between)) }));
|
|
86
|
+
const said = readingLine(w / 2, BLOCK.h + LINE / 2 + 4), low = BLOCK.h + (spec.reading ? LINE : 0);
|
|
87
|
+
// The sign tile stands between the two numbers, and the mark that says true or false under the blocks.
|
|
88
|
+
const tile = signTile(74), mark = verdictMark(), extra = [];
|
|
89
|
+
if (tiled) {
|
|
90
|
+
extra.push(el('g', { transform: `translate(${w / 2} ${BLOCK.num})` }, [tile.el]), el('g', { transform: `translate(${w / 2 - 44} ${low + 24})` }, [mark.el]));
|
|
91
|
+
tile.open(true, T('sign'));
|
|
92
|
+
press(tile.el, () => { s.sign = nextSign(s.sign); fig.paint(true); refresh(); });
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
w, h: low + (tiled ? 44 : 0), max: 2.4,
|
|
96
|
+
nodes: [...blocks.flatMap(b => b.nodes), said.el, ...extra],
|
|
97
|
+
paint(anim) {
|
|
98
|
+
const kind = spec.decide && s.open ? decides(s) : null;
|
|
99
|
+
for (const b of blocks) { b.set(s[b.r], int(s[b.r])); b.frame(kind, s[b.r]); b.cover(!s.open); }
|
|
100
|
+
said.show(spec.reading && s.open); said.say(words());
|
|
101
|
+
if (reveal) reveal.disabled = s.open;
|
|
102
|
+
if (tiled) { const turned = tile.set(signShown(spec, s)), told = mark.set(signOf(s.top, s.bottom) === s.sign, T(verdictOf(spec, s))); if (anim && moving()) { if (turned) go.flash(tile.pop); if (told) go.flash(mark.el); } }
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Three number cards in a tray above three places. A card let go on a place sits there and trades with a card
|
|
108
|
+
// that holds the place, and anywhere else it goes back to the tray. A tap sends a card to the first empty place,
|
|
109
|
+
// or back to the tray.
|
|
110
|
+
function cardsFig() {
|
|
111
|
+
const gap = 22, edge = 12, w = CARDS * CARD.w + (CARDS - 1) * gap + 2 * edge, trayH = CARD.h + 2 * edge, py = trayH + 50 + CARD.h / 2;
|
|
112
|
+
const x = k => edge + k * (CARD.w + gap) + CARD.w / 2, home = i => ({ x: x(i), y: trayH / 2 }), spot = k => ({ x: x(k), y: py });
|
|
113
|
+
const posOf = i => s.place[i] == null ? home(i) : spot(s.place[i]);
|
|
114
|
+
const labels = [txt('counters-place-word', x(0), trayH + 32, T('least')), txt('counters-place-word', x(CARDS - 1), trayH + 32, T('greatest'))];
|
|
115
|
+
const places = Array.from({ length: CARDS }, (_, k) => el('rect', { class: 'counters-place', x: x(k) - CARD.w / 2, y: py - CARD.h / 2, width: CARD.w, height: CARD.h, rx: 9 }));
|
|
116
|
+
const says = Array.from({ length: CARDS - 1 }, (_, k) => txt('counters-between', (x(k) + x(k + 1)) / 2, py + CARD.h / 2 + 22));
|
|
117
|
+
const frame = el('rect', { class: 'counters-decide', x: edge - 6, y: py - CARD.h / 2 - 6, width: w - 2 * edge + 12, height: CARD.h + 12, rx: 12, display: 'none' });
|
|
118
|
+
const tick = el('path', { class: 'counters-tick a', d: 'M -9 0 l 6 7 l 12 -14', display: 'none' }), tickAt = el('g', { transform: `translate(${x(1)} ${trayH + 30})` }, [tick]);
|
|
119
|
+
const layer = el('g', { class: 'counters-layer' });
|
|
120
|
+
let hold = null, done = false;
|
|
121
|
+
const sit = (i, k) => { const j = s.place.indexOf(k); if (j >= 0 && j !== i) s.place[j] = s.place[i]; s.place[i] = k; };
|
|
122
|
+
cards = s.cards.map((n, i) => {
|
|
123
|
+
const pop = el('g', { class: 'counters-pop a' }, card(n, int(n))), g = el('g', { class: 'counters-card counters-moves', tabindex: 0, role: 'button', 'aria-label': int(n) }, [pop]);
|
|
124
|
+
const c = { g, pop, at: { x: 0, y: 0 } };
|
|
125
|
+
let dx = 0, dy = 0;
|
|
126
|
+
carry(g, {
|
|
127
|
+
point: e => svgPoint(svg, e), raise: () => layer.append(g),
|
|
128
|
+
start(q) { c.tween?.pause(); hold = c; dx = c.at.x - q.x; dy = c.at.y - q.y; },
|
|
129
|
+
move(q) { put(c, { x: q.x + dx, y: q.y + dy }); },
|
|
130
|
+
drop(q) {
|
|
131
|
+
hold = null;
|
|
132
|
+
if (q) { const k = Array.from({ length: CARDS }, (_, k) => k).sort((a, b) => far(spot(a), c.at) - far(spot(b), c.at))[0]; if (far(spot(k), c.at) < CARD.h * 0.6) sit(i, k); else s.place[i] = null; }
|
|
133
|
+
fig.paint(true);
|
|
134
|
+
},
|
|
135
|
+
tap() { hold = null; const free = Array.from({ length: CARDS }, (_, k) => k).find(k => !s.place.includes(k)); s.place[i] = s.place[i] == null && free !== undefined ? free : null; fig.paint(true); },
|
|
136
|
+
});
|
|
137
|
+
put(c, posOf(i)); layer.append(g);
|
|
138
|
+
return c;
|
|
139
|
+
});
|
|
140
|
+
return {
|
|
141
|
+
w, h: py + CARD.h / 2 + 38, max: 1.7,
|
|
142
|
+
nodes: [el('rect', { class: 'counters-tray', x: 0, y: 0, width: w, height: trayH, rx: 12 }), ...labels, ...places, frame, tickAt, ...says, layer],
|
|
143
|
+
paint(anim) {
|
|
144
|
+
const glide = anim && moving();
|
|
145
|
+
cards.forEach((c, i) => {
|
|
146
|
+
const p = posOf(i);
|
|
147
|
+
if (c === hold || (c.at.x === p.x && c.at.y === p.y)) return;
|
|
148
|
+
c.tween?.pause();
|
|
149
|
+
if (glide) c.tween = go.tween(c.at, p, () => show(c)); else put(c, p);
|
|
150
|
+
});
|
|
151
|
+
says.forEach((t, k) => { const rel = between(spec, s, k), text = rel ? T(`is-${rel}`) : ''; if (t.textContent !== text) { t.textContent = text; if (text && glide) go.flash(t); } });
|
|
152
|
+
const now = ordered(spec, s);
|
|
153
|
+
for (const part of [frame, tick]) part.setAttribute('display', now ? 'inline' : 'none');
|
|
154
|
+
if (now && !done && glide) { go.pop(tick); go.cheer(cards.map(c => c.pop)); }
|
|
155
|
+
done = now;
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// refresh writes every value that follows the numbers, in the readouts and the text.
|
|
161
|
+
function refresh() {
|
|
162
|
+
const st = counts(spec, s), ids = measuresOf(spec), value = id => id === 'reading' ? words() : id === 'verdict' ? T(verdictOf(spec, s)) : int(st[id]);
|
|
163
|
+
for (const k of chips) if (k.set(value(k.id)) && moving()) go.flash(k.el);
|
|
164
|
+
live(host, { value: id => ids.includes(id) ? value(id) : null });
|
|
165
|
+
}
|
|
166
|
+
// A change of a number closes the covers of a covered page.
|
|
167
|
+
function set(r, v) { s[r] = v; if (spec.cover) s.open = false; fig?.paint(true); refresh(); }
|
|
168
|
+
|
|
169
|
+
// The panel holds a slider or a stepper for each number that has one, the button of a covered page and the readouts.
|
|
170
|
+
function drawPanel() {
|
|
171
|
+
const ctls = isCards(spec) ? [] : ROWS.filter(r => spec[`${r}Ctl`]).map(r => {
|
|
172
|
+
const c = spec[`${r}Ctl`], opts = { id: r, label: T(`order-${r}`), min: c.lo, max: c.hi, value: s[r], write: int };
|
|
173
|
+
if (c.kind === 'stepper') return stepper({ ...opts, change: v => set(r, v) });
|
|
174
|
+
const sl = slider({ ...opts, input: v => set(r, v) }); sl.width(3);
|
|
175
|
+
return sl;
|
|
176
|
+
});
|
|
177
|
+
// New numbers puts three other cards in the tray and empties the places.
|
|
178
|
+
if (spec.draw) ctls.push(button({ label: T('new-numbers'), act: 'new-numbers', press: () => { s = { ...opening(spec), cards: drawCards(dice(fresh()), s.cards) }; build(); } }));
|
|
179
|
+
reveal = spec.cover ? button({ label: T('show-rows'), act: 'reveal', primary: true, press: () => { s.open = true; fig?.paint(true); refresh(); } }) : null;
|
|
180
|
+
// The line of words takes a line of the panel to itself, so the panel keeps its height as the words change.
|
|
181
|
+
chips = spec.readout.map(id => chip({ id, label: T(`order-${id}`) ?? T(`match-${id}`), cls: id === 'reading' ? 'counters-chip-wide' : '' }));
|
|
182
|
+
for (const k of chips) if (k.id !== 'reading') k.width(k.id === 'verdict' ? 5 : 3);
|
|
183
|
+
panel.replaceChildren(...[ctls.length ? line('ctls', ctls) : null, reveal ? line('acts', [reveal]) : null, chips.length ? line('out', chips) : null].filter(Boolean));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function point(id, on) {
|
|
187
|
+
if (!spec) return;
|
|
188
|
+
for (const part of svg.querySelectorAll(`[data-ref="${id}"]`)) part.classList.toggle('counters-hot', on);
|
|
189
|
+
for (const part of panel.querySelectorAll('[data-ref]')) part.classList.toggle('hot', on && part.dataset.ref === id);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return {
|
|
193
|
+
// A page that asks for a draw and names no cards opens on a draw, a new one each time.
|
|
194
|
+
show(page) { spec = page; s = opening(spec); if (spec.draw && !spec.cards) s.cards = drawCards(dice(fresh())); drawPanel(); build(); if (!fig) refresh(); },
|
|
195
|
+
hide() { for (const c of cards) c.tween?.pause(); cards = []; spec = null; fig = null; svg.replaceChildren(); },
|
|
196
|
+
point,
|
|
197
|
+
lang() { if (!spec) return; drawPanel(); build(); if (!fig) refresh(); },
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The Node half of the order scene: two numbers compared on one count or as two blocks, and three cards put in
|
|
2
|
+
// order. It checks a page's fields and says what each link shows when the page opens.
|
|
3
|
+
import { schema } from '@openbooknetwork/kitap';
|
|
4
|
+
import { SHOWS, TOP, CARDS, opening, counts, measuresOf, readingOf, verdictOf } from './order-model.js';
|
|
5
|
+
import { SIGNS } from './reading.js';
|
|
6
|
+
import { numbers } from '../../kit/format.js';
|
|
7
|
+
import { list, value, string } from '../../node.mjs';
|
|
8
|
+
|
|
9
|
+
const { z, check } = schema;
|
|
10
|
+
const Int = z.number().int();
|
|
11
|
+
const Range = z.tuple([Int, Int]);
|
|
12
|
+
const Num = z.union([Int, z.object({ slider: Range, start: Int.optional() }).strict(), z.object({ stepper: Range, start: Int.optional() }).strict()]);
|
|
13
|
+
const Fields = z.object({
|
|
14
|
+
show: z.enum(SHOWS).optional(), // Two markers on one count of 100, or two blocks of rows of ten.
|
|
15
|
+
top: Num.optional(), // The red number, as a number or on a slider or a stepper.
|
|
16
|
+
bottom: Num.optional(), // The blue number.
|
|
17
|
+
reading: z.boolean().default(false), // A line of words states the comparison.
|
|
18
|
+
decide: z.boolean().default(false), // A frame surrounds the rows of the blocks that decide.
|
|
19
|
+
cover: z.boolean().default(false), // Covers hide the blocks until the reader asks for the rows.
|
|
20
|
+
sign: z.enum(SIGNS).optional(), // A sign tile between the two numbers of the blocks, opening on this sign.
|
|
21
|
+
cards: z.array(Int).optional(), // Three numbers on cards for the reader to put in order.
|
|
22
|
+
// A New numbers button draws three other cards. A page with draw and no cards opens on a draw too.
|
|
23
|
+
draw: z.boolean().default(false),
|
|
24
|
+
readout: z.array(z.string()).default([]),
|
|
25
|
+
}).strict();
|
|
26
|
+
|
|
27
|
+
function read(fields, { at }, ctx) {
|
|
28
|
+
const pg = check(Fields, fields, at), fail = words => ctx.fail(at, words);
|
|
29
|
+
const top = value(pg.top), bottom = value(pg.bottom);
|
|
30
|
+
const spec = { ...pg, scene: 'order', top: top.n, topCtl: top.ctl, bottom: bottom.n, bottomCtl: bottom.ctl };
|
|
31
|
+
if (pg.cards || pg.draw) {
|
|
32
|
+
if (pg.cards && (pg.cards.length !== CARDS || new Set(pg.cards).size !== CARDS || pg.cards.some(n => n < 0 || n > TOP))) fail(`cards is ${JSON.stringify(pg.cards)}, write ${CARDS} different numbers from 0 to ${TOP}, such as [63, 36, 39]`);
|
|
33
|
+
for (const f of ['top', 'bottom', 'show']) if (pg[f] !== undefined) fail(`${f} belongs to pages with two numbers, leave it out of a page with cards`);
|
|
34
|
+
for (const f of ['reading', 'decide', 'cover', 'sign']) if (pg[f]) fail(`${f} belongs to pages with two numbers, leave it out of a page with cards`);
|
|
35
|
+
} else {
|
|
36
|
+
if (!pg.show) fail(`show is missing, write how the two numbers are drawn: ${list(SHOWS)}, or give the page cards or draw: true`);
|
|
37
|
+
const low = pg.show === 'count' ? 1 : 0;
|
|
38
|
+
for (const [v, name] of [[top, 'top'], [bottom, 'bottom']]) {
|
|
39
|
+
if (v.n == null) { fail(`${name} is missing, write the ${name} number, such as 18, or { slider: [${low}, ${TOP}], start: 18 }`); continue; }
|
|
40
|
+
const lo = v.ctl?.lo ?? v.n, hi = v.ctl?.hi ?? v.n;
|
|
41
|
+
if (lo > hi || v.n < lo || v.n > hi) fail(`${name} has the range [${lo}, ${hi}] and starts at ${v.n}, write [low, high] and a start between them`);
|
|
42
|
+
else if (lo < low || hi > TOP) fail(`${name} reaches ${v.ctl ? `[${lo}, ${hi}]` : v.n}, ${pg.show === 'count' ? 'a marker stands on a counter' : 'a block holds a number'} from ${low} to ${TOP}`);
|
|
43
|
+
}
|
|
44
|
+
for (const f of ['decide', 'cover']) if (pg[f] && pg.show !== 'blocks') fail(`${f} belongs to the rows of two blocks, write show: blocks`);
|
|
45
|
+
if (pg.sign && pg.show !== 'blocks') fail('sign stands between the numbers of two blocks, write show: blocks');
|
|
46
|
+
if (pg.sign && pg.cover) fail('sign says whether the line is true, which gives away what cover hides, keep one');
|
|
47
|
+
}
|
|
48
|
+
const ids = measuresOf(spec);
|
|
49
|
+
for (const id of pg.readout) if (!ids.includes(id)) fail(`readout names "${id}", this page reads out ${list(ids) || 'nothing'}`);
|
|
50
|
+
return { data: spec, has: id => ids.includes(id), offers: ids.length ? `one of ${list(ids)}` : 'nothing, the cards carry their numbers' };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// A link shows a number or the line of words as the page opens.
|
|
54
|
+
function start(spec, lang, id) {
|
|
55
|
+
if (!measuresOf(spec).includes(id)) return null;
|
|
56
|
+
const s = opening(spec), int = numbers(lang).int;
|
|
57
|
+
if (id === 'reading') return string(lang, `counters_${readingOf(spec, s)}`, { a: int(s.top), b: int(s.bottom) });
|
|
58
|
+
if (id === 'verdict') return string(lang, `counters_${verdictOf(spec, s)}`);
|
|
59
|
+
return int(counts(spec, s)[id]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default { read, start };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// How two numbers compare, in a sign and in words: the three signs, the line of words such as "18 is less than 25",
|
|
2
|
+
// and whether a line with a sign is true. match-model.js and order-model.js take the rules from here, so the build
|
|
3
|
+
// and the browser agree, and match.js and order.js draw the line, the sign tile and the mark with the parts below.
|
|
4
|
+
import { svg as el } from '../../kit/dom.js';
|
|
5
|
+
|
|
6
|
+
// relation says how a stands to b: less, same or greater.
|
|
7
|
+
export const relation = (a, b) => a < b ? 'less' : a > b ? 'greater' : 'same';
|
|
8
|
+
// The signs in the order a tap on the tile goes through them, and the sign that makes a true line of a and b.
|
|
9
|
+
export const SIGNS = ['<', '=', '>'];
|
|
10
|
+
export const signOf = (a, b) => a < b ? '<' : a > b ? '>' : '=';
|
|
11
|
+
export const nextSign = sign => SIGNS[(SIGNS.indexOf(sign) + 1) % SIGNS.length];
|
|
12
|
+
// A page's sign field is one of the signs for a tile the reader taps, or follow for a sign the scene keeps true.
|
|
13
|
+
export const SIGN_FIELD = [...SIGNS, 'follow'];
|
|
14
|
+
export const isTile = spec => SIGNS.includes(spec.sign);
|
|
15
|
+
// signShown gives the sign a line shows: the tile's, or the true one on a follow page. A page without a sign has none.
|
|
16
|
+
export const signShown = (spec, s) => isTile(spec) ? s.sign : spec.sign === 'follow' ? signOf(s.top, s.bottom) : null;
|
|
17
|
+
|
|
18
|
+
const WORDS = { '<': 'less', '=': 'equal', '>': 'greater' };
|
|
19
|
+
// The string of the line of words, which takes the two numbers as {a} and {b}. A line with a sign reads the sign,
|
|
20
|
+
// so equal numbers read "is equal to" and a false line reads as it is written. Without a sign the line states how
|
|
21
|
+
// the numbers stand, and equal numbers read "is the same as".
|
|
22
|
+
export const readingKey = (a, b, sign) => `reading-${sign ? WORDS[sign] : relation(a, b)}`;
|
|
23
|
+
// The string of the mark beside a line with a sign tile: true or false.
|
|
24
|
+
export const verdictKey = (a, b, sign) => `verdict-${signOf(a, b) === sign}`;
|
|
25
|
+
|
|
26
|
+
// readingLine makes the line of words as a part of a figure, with its middle at x, y. say writes its words, and
|
|
27
|
+
// show puts it on or off the figure.
|
|
28
|
+
export function readingLine(x, y) {
|
|
29
|
+
const text = el('text', { class: 'counters-reading', 'data-ref': 'reading', x, y });
|
|
30
|
+
return { el: text, say(words) { if (text.textContent !== words) text.textContent = words; }, show(on) { text.setAttribute('display', on ? 'inline' : 'none'); } };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// signTile makes the sign between two numbers, with its middle at 0, 0 of its own group. set writes the sign and
|
|
34
|
+
// says whether it changed. open makes it a tile the reader taps, named by label. reach is the width of the square
|
|
35
|
+
// that takes a touch, which a figure drawn small makes wider.
|
|
36
|
+
export function signTile(reach = 64) {
|
|
37
|
+
const text = el('text', { class: 'counters-sign-tile-text' }), pop = el('g', { class: 'a' }, [el('rect', { class: 'counters-sign-tile-box', x: -26, y: -26, width: 52, height: 52, rx: 9 }), text]);
|
|
38
|
+
// The tile takes a touch a little past its box, so it is about 44px across on a phone.
|
|
39
|
+
const hit = el('rect', { class: 'counters-hit', x: -reach / 2, y: -reach / 2, width: reach, height: reach });
|
|
40
|
+
const g = el('g', { class: 'counters-sign-tile' }, [hit, pop]);
|
|
41
|
+
return {
|
|
42
|
+
el: g, pop,
|
|
43
|
+
set(sign) { if (text.textContent === sign) return false; text.textContent = sign; return true; },
|
|
44
|
+
open(on, label) {
|
|
45
|
+
g.classList.toggle('counters-sign-tile-open', on);
|
|
46
|
+
for (const [name, v] of [['tabindex', 0], ['role', 'button'], ['aria-label', label]]) if (on) g.setAttribute(name, v); else g.removeAttribute(name);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// verdictMark makes the mark beside a line: a tick or a cross with the word true or false. It starts at 0, 0 of
|
|
52
|
+
// its own group and runs to the right.
|
|
53
|
+
export function verdictMark() {
|
|
54
|
+
const sign = el('path', { class: 'counters-mark-sign' }), text = el('text', { class: 'counters-mark-word', x: 26 });
|
|
55
|
+
const g = el('g', { class: 'counters-mark', 'data-ref': 'verdict' }, [sign, text]);
|
|
56
|
+
return {
|
|
57
|
+
el: g,
|
|
58
|
+
set(ok, words) {
|
|
59
|
+
g.classList.toggle('counters-mark-true', ok); g.classList.toggle('counters-mark-false', !ok);
|
|
60
|
+
sign.setAttribute('d', ok ? 'M 1 0 l 6 7 l 12 -14' : 'M 2 -8 l 16 16 M 18 -8 l -16 16');
|
|
61
|
+
if (text.textContent === words) return false;
|
|
62
|
+
text.textContent = words; return true;
|
|
63
|
+
},
|
|
64
|
+
show(on) { g.setAttribute('display', on ? 'inline' : 'none'); },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// The scenes of a family share one stage, and one of them shows at a time. A scene adds its svg to the stage as it
|
|
2
|
+
// is made. A scene off show takes its svg off the stage, so the scene on show gets every touch.
|
|
3
|
+
export function scenes(makers, stage) {
|
|
4
|
+
// made holds each scene once it has been made, with its svg, and now is the scene on show.
|
|
5
|
+
const made = {};
|
|
6
|
+
let now = null;
|
|
7
|
+
const make = name => { const scene = makers[name](); return { scene, svg: stage.lastElementChild }; };
|
|
8
|
+
const off = () => { if (!now) return; now.scene.hide(); now.svg.setAttribute('display', 'none'); now = null; };
|
|
9
|
+
return {
|
|
10
|
+
show(name, ...page) {
|
|
11
|
+
const next = made[name] ??= make(name);
|
|
12
|
+
if (now !== next) off();
|
|
13
|
+
now = next; now.svg.removeAttribute('display'); now.scene.show(...page);
|
|
14
|
+
},
|
|
15
|
+
hide: off,
|
|
16
|
+
now: () => now?.scene ?? null,
|
|
17
|
+
};
|
|
18
|
+
}
|