@altnovatrail/picture-kit 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 +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { textWidth } from "../pen.js";
|
|
3
|
+
import { int, isObj, list, only, text } from "../read.js";
|
|
4
|
+
import { colorNames, colors, ink, isColor, outline, textOn } from "../style.js";
|
|
5
|
+
import { el, n, poly } from "../svg.js";
|
|
6
|
+
import { drawThing } from "../things.js";
|
|
7
|
+
const ringColors = ["yellow", "red", "blue", "white", "green", "purple"];
|
|
8
|
+
const zoneColors = [
|
|
9
|
+
"red",
|
|
10
|
+
"blue",
|
|
11
|
+
"green",
|
|
12
|
+
"orange",
|
|
13
|
+
"purple",
|
|
14
|
+
"pink",
|
|
15
|
+
"white",
|
|
16
|
+
"gray",
|
|
17
|
+
];
|
|
18
|
+
const mostTargets = 4;
|
|
19
|
+
const mostHits = 12;
|
|
20
|
+
const mostHoops = 4;
|
|
21
|
+
const leastBand = 21;
|
|
22
|
+
const labelMost = 8;
|
|
23
|
+
const scoreMost = 999;
|
|
24
|
+
/** The way every dart leans, from its tip to its flights. */
|
|
25
|
+
const lean = { x: Math.sin((40 * Math.PI) / 180), y: -Math.cos((40 * Math.PI) / 180) };
|
|
26
|
+
/**
|
|
27
|
+
* radiusOf is the radius of each of t round boards: they stand in a row, four
|
|
28
|
+
* in two rows of two, and a dart in the outer ring may reach about 20 pixels
|
|
29
|
+
* past the rim, so that a row keeps within the widest picture.
|
|
30
|
+
*/
|
|
31
|
+
function radiusOf(t) {
|
|
32
|
+
return t === 1 ? 150 : t === 2 ? 136 : t === 3 ? 84 : 120;
|
|
33
|
+
}
|
|
34
|
+
function scoreOf(s) {
|
|
35
|
+
if (typeof s === "number")
|
|
36
|
+
return { score: s, ask: false };
|
|
37
|
+
return {
|
|
38
|
+
...(s.score !== undefined ? { score: s.score } : {}),
|
|
39
|
+
ask: s.ask === true,
|
|
40
|
+
...(s.color ? { color: s.color } : {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function polar(r, degrees) {
|
|
44
|
+
const a = (degrees * Math.PI) / 180;
|
|
45
|
+
return { x: r * Math.cos(a), y: r * Math.sin(a) };
|
|
46
|
+
}
|
|
47
|
+
/** along is a point of a dart: t along it from its tip, side across it, at its scale k. */
|
|
48
|
+
function along(tip, t, side = 0, k = 1) {
|
|
49
|
+
return {
|
|
50
|
+
x: tip.x + k * (lean.x * t - lean.y * side),
|
|
51
|
+
y: tip.y + k * (lean.y * t + lean.x * side),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** dartScale is how large a board's darts are drawn: larger on a larger board. */
|
|
55
|
+
function dartScale(R) {
|
|
56
|
+
return Math.max(0.9, Math.min(1.25, R / 115));
|
|
57
|
+
}
|
|
58
|
+
function pointSegment(p, a, b) {
|
|
59
|
+
const dx = b.x - a.x;
|
|
60
|
+
const dy = b.y - a.y;
|
|
61
|
+
const t = Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / (dx * dx + dy * dy)));
|
|
62
|
+
return Math.hypot(p.x - a.x - t * dx, p.y - a.y - t * dy);
|
|
63
|
+
}
|
|
64
|
+
function crosses(a, b, c, d) {
|
|
65
|
+
const side = (p, q, r) => (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
|
|
66
|
+
return side(a, b, c) * side(a, b, d) < 0 && side(c, d, a) * side(c, d, b) < 0;
|
|
67
|
+
}
|
|
68
|
+
function segments(a, b, c, d) {
|
|
69
|
+
if (crosses(a, b, c, d))
|
|
70
|
+
return 0;
|
|
71
|
+
return Math.min(pointSegment(a, c, d), pointSegment(b, c, d), pointSegment(c, a, b), pointSegment(d, a, b));
|
|
72
|
+
}
|
|
73
|
+
function segmentRect(a, b, r) {
|
|
74
|
+
const inside = (p) => p.x >= r.x0 && p.x <= r.x1 && p.y >= r.y0 && p.y <= r.y1;
|
|
75
|
+
if (inside(a) || inside(b))
|
|
76
|
+
return 0;
|
|
77
|
+
const c = [
|
|
78
|
+
{ x: r.x0, y: r.y0 },
|
|
79
|
+
{ x: r.x1, y: r.y0 },
|
|
80
|
+
{ x: r.x1, y: r.y1 },
|
|
81
|
+
{ x: r.x0, y: r.y1 },
|
|
82
|
+
];
|
|
83
|
+
return Math.min(...c.map((p, i) => segments(a, b, p, c[(i + 1) % 4])));
|
|
84
|
+
}
|
|
85
|
+
/** A dart is two capsules: its needle and barrel, then its flights, wider. */
|
|
86
|
+
function capsules(tip, k) {
|
|
87
|
+
return [
|
|
88
|
+
{ a: tip, b: along(tip, 20, 0, k), r: 4 * k },
|
|
89
|
+
{ a: along(tip, 20, 0, k), b: along(tip, 33, 0, k), r: 8 * k },
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
function dartBox(tip, k) {
|
|
93
|
+
const pts = [-8, 8].flatMap((side) => [along(tip, 33, side, k), along(tip, 27, side, k)]);
|
|
94
|
+
pts.push(tip);
|
|
95
|
+
return {
|
|
96
|
+
x0: Math.min(...pts.map((p) => p.x)) - 2,
|
|
97
|
+
y0: Math.min(...pts.map((p) => p.y)) - 2,
|
|
98
|
+
x1: Math.max(...pts.map((p) => p.x)) + 2,
|
|
99
|
+
y1: Math.max(...pts.map((p) => p.y)) + 2,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function clearOf(tip, darts, labels, k) {
|
|
103
|
+
const mine = capsules(tip, k);
|
|
104
|
+
for (const other of darts)
|
|
105
|
+
for (const c of capsules(other, k))
|
|
106
|
+
for (const m of mine)
|
|
107
|
+
if (segments(m.a, m.b, c.a, c.b) < m.r + c.r + 1)
|
|
108
|
+
return false;
|
|
109
|
+
for (const r of labels)
|
|
110
|
+
for (const m of mine)
|
|
111
|
+
if (segmentRect(m.a, m.b, r) < m.r + 2)
|
|
112
|
+
return false;
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
function labelOf(s, at, size) {
|
|
116
|
+
const { score, ask } = scoreOf(s);
|
|
117
|
+
const w = ask ? size + 8 : textWidth(String(score), size) + 6;
|
|
118
|
+
const h = ask ? size + 8 : size + 2;
|
|
119
|
+
return {
|
|
120
|
+
at,
|
|
121
|
+
size,
|
|
122
|
+
box: { x0: at.x - w / 2, y0: at.y - h / 2, x1: at.x + w / 2, y1: at.y + h / 2 },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** inRing tells whether a tip lies well inside ring j, counted from 1 at the centre. */
|
|
126
|
+
function inRing(p, j, band) {
|
|
127
|
+
const r = Math.hypot(p.x, p.y);
|
|
128
|
+
return r >= (j - 1) * band + (j === 1 ? 0 : 5) && r <= j * band - 5;
|
|
129
|
+
}
|
|
130
|
+
/** inZone tells whether a tip lies well inside part i of k, or in the bull when i is "bull". */
|
|
131
|
+
function inZone(p, i, k, bullR, R) {
|
|
132
|
+
const r = Math.hypot(p.x, p.y);
|
|
133
|
+
if (i === "bull")
|
|
134
|
+
return r <= bullR - 5;
|
|
135
|
+
if (r < bullR + 5 || r > R - 5)
|
|
136
|
+
return false;
|
|
137
|
+
const span = 360 / k;
|
|
138
|
+
for (const edge of [-90 + (i - 1) * span, -90 + i * span]) {
|
|
139
|
+
const e = polar(1, edge);
|
|
140
|
+
if (Math.abs(p.x * e.y - p.y * e.x) < 5 && p.x * e.x + p.y * e.y > 0)
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
let a = (Math.atan2(p.y, p.x) * 180) / Math.PI + 90;
|
|
144
|
+
a = ((a % 360) + 360) % 360;
|
|
145
|
+
return a > (i - 1) * span && a < i * span;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* roundOf lays out a round board at its radius: the labels of its scores,
|
|
149
|
+
* then each dart, in the order of its hits, at the first of its candidate
|
|
150
|
+
* places whose tip is well inside its ring or part and whose dart covers no
|
|
151
|
+
* label and no other dart. Darts lean up and to the right, so the scores of
|
|
152
|
+
* the rings stand below the centre, where no dart reaches over them; a hit
|
|
153
|
+
* centre or bull's-eye moves its score down to leave its upper half to the
|
|
154
|
+
* darts. The candidates of a ring are spread round it, each ring turned from
|
|
155
|
+
* the last, so that darts stand apart.
|
|
156
|
+
*/
|
|
157
|
+
function roundOf(b, R) {
|
|
158
|
+
const hits = b.hits ?? [];
|
|
159
|
+
const labels = [];
|
|
160
|
+
const tips = [];
|
|
161
|
+
const k_ = dartScale(R);
|
|
162
|
+
if (b.rings) {
|
|
163
|
+
const k = b.rings.length;
|
|
164
|
+
const band = R / k;
|
|
165
|
+
const size = Math.min(18, Math.floor(band * 0.75));
|
|
166
|
+
const hitCentre = hits.includes(1);
|
|
167
|
+
b.rings.forEach((s, j) => {
|
|
168
|
+
const at = j === 0 ? { x: 0, y: hitCentre ? band * 0.52 : 0 } : { x: 0, y: (j + 0.5) * band };
|
|
169
|
+
labels.push(labelOf(s, at, size));
|
|
170
|
+
});
|
|
171
|
+
const boxes = labels.map((l) => l.box);
|
|
172
|
+
for (const ring of hits) {
|
|
173
|
+
const j = ring;
|
|
174
|
+
const many = hits.filter((h) => h === j).length;
|
|
175
|
+
const base = 200 + 47 * j;
|
|
176
|
+
const radii = j === 1 ? [0.45 * band, 0.25 * band, 0.65 * band] : [(j - 0.5) * band];
|
|
177
|
+
const angles = [
|
|
178
|
+
...Array.from({ length: many }, (_, i) => base + (i * 360) / many),
|
|
179
|
+
...Array.from({ length: 36 }, (_, i) => base + 5 + i * 10),
|
|
180
|
+
];
|
|
181
|
+
const spot = radii
|
|
182
|
+
.flatMap((r) => angles.map((a) => polar(r, a)))
|
|
183
|
+
.find((p) => inRing(p, j, band) && clearOf(p, tips, boxes, k_));
|
|
184
|
+
if (!spot)
|
|
185
|
+
return { R, k: k_, band, bullR: 0, labels, tips, short: j };
|
|
186
|
+
tips.push(spot);
|
|
187
|
+
}
|
|
188
|
+
return { R, k: k_, band, bullR: 0, labels, tips };
|
|
189
|
+
}
|
|
190
|
+
const zones = b.zones;
|
|
191
|
+
const k = zones.length;
|
|
192
|
+
const bullR = b.bull !== undefined ? R * 0.28 : 0;
|
|
193
|
+
const span = 360 / k;
|
|
194
|
+
const size = Math.min(18, Math.floor((R - bullR) * 0.3));
|
|
195
|
+
const at = bullR > 0 ? (bullR + R) / 2 : R * 0.6;
|
|
196
|
+
zones.forEach((s, i) => {
|
|
197
|
+
labels.push(labelOf(s, polar(at, -90 + (i + 0.5) * span), size));
|
|
198
|
+
});
|
|
199
|
+
const hitBull = hits.includes("bull");
|
|
200
|
+
if (b.bull !== undefined)
|
|
201
|
+
labels.push(labelOf(b.bull, { x: 0, y: hitBull ? bullR * 0.52 : 0 }, Math.min(18, Math.floor(bullR * 0.6))));
|
|
202
|
+
const boxes = labels.map((l) => l.box);
|
|
203
|
+
for (const zone of hits) {
|
|
204
|
+
const candidates = [];
|
|
205
|
+
if (zone === "bull") {
|
|
206
|
+
for (const r of [0.5, 0.3, 0.7])
|
|
207
|
+
for (let a = 0; a < 360; a += 20)
|
|
208
|
+
candidates.push(polar(bullR * r, 200 + a));
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const from = -90 + (zone - 1) * span;
|
|
212
|
+
for (const r of [0.78, 0.5, 0.64, 0.9, 0.36])
|
|
213
|
+
for (const f of [0.3, 0.7, 0.5, 0.15, 0.85, 0.4, 0.6]) {
|
|
214
|
+
const rr = bullR + (R - bullR) * r;
|
|
215
|
+
if (rr > bullR + 6)
|
|
216
|
+
candidates.push(polar(rr, from + f * span));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const spot = candidates.find((p) => inZone(p, zone, k, bullR, R) && clearOf(p, tips, boxes, k_));
|
|
220
|
+
if (!spot)
|
|
221
|
+
return { R, k: k_, band: 0, bullR, labels, tips, short: zone };
|
|
222
|
+
tips.push(spot);
|
|
223
|
+
}
|
|
224
|
+
return { R, k: k_, band: 0, bullR, labels, tips };
|
|
225
|
+
}
|
|
226
|
+
/** perRowOf is how many of t boards stand in a row: four stand in two rows of two. */
|
|
227
|
+
function perRowOf(t) {
|
|
228
|
+
return t === 4 ? 2 : t;
|
|
229
|
+
}
|
|
230
|
+
/** roomOf is how wide the pegs of a ring toss board may spread when t boards stand together. */
|
|
231
|
+
function roomOf(t) {
|
|
232
|
+
const perRow = perRowOf(t);
|
|
233
|
+
return Math.floor((640 - 2 * margin - (perRow - 1) * 24) / perRow) - 24;
|
|
234
|
+
}
|
|
235
|
+
function tossOf(b, room) {
|
|
236
|
+
const k = b.pegs.length;
|
|
237
|
+
const pitch = Math.min(84, Math.floor(room / k));
|
|
238
|
+
return { pitch, width: k * pitch + 24 };
|
|
239
|
+
}
|
|
240
|
+
// --- drawing ------------------------------------------------------------------
|
|
241
|
+
function drawDart(pen, tip, color, k) {
|
|
242
|
+
const c = colors[color];
|
|
243
|
+
const at = (t, side = 0) => along(tip, t, side, k);
|
|
244
|
+
const parts = el("path", {
|
|
245
|
+
d: poly([tip, at(9)], false),
|
|
246
|
+
stroke: ink,
|
|
247
|
+
"stroke-width": 2.2,
|
|
248
|
+
"stroke-linecap": "round",
|
|
249
|
+
}) +
|
|
250
|
+
el("path", {
|
|
251
|
+
d: poly([at(8, -2.8), at(20, -3.4), at(20, 3.4), at(8, 2.8)]),
|
|
252
|
+
fill: colors.gray.fill,
|
|
253
|
+
stroke: ink,
|
|
254
|
+
"stroke-width": 1.5,
|
|
255
|
+
"stroke-linejoin": "round",
|
|
256
|
+
}) +
|
|
257
|
+
el("path", { d: poly([at(20), at(27)], false), stroke: ink, "stroke-width": 2 }) +
|
|
258
|
+
el("path", {
|
|
259
|
+
d: poly([at(22), at(28, -7.5), at(33, -6.5), at(31)]),
|
|
260
|
+
fill: c.fill,
|
|
261
|
+
stroke: ink,
|
|
262
|
+
"stroke-width": 1.5,
|
|
263
|
+
"stroke-linejoin": "round",
|
|
264
|
+
}) +
|
|
265
|
+
el("path", {
|
|
266
|
+
d: poly([at(22), at(28, 7.5), at(33, 6.5), at(31)]),
|
|
267
|
+
fill: c.fill,
|
|
268
|
+
stroke: ink,
|
|
269
|
+
"stroke-width": 1.5,
|
|
270
|
+
"stroke-linejoin": "round",
|
|
271
|
+
}) +
|
|
272
|
+
el("circle", {
|
|
273
|
+
cx: tip.x,
|
|
274
|
+
cy: tip.y,
|
|
275
|
+
r: 2.8 * k,
|
|
276
|
+
fill: ink,
|
|
277
|
+
stroke: "#ffffff",
|
|
278
|
+
"stroke-width": 1,
|
|
279
|
+
});
|
|
280
|
+
pen.add(`<g class="dart">${parts}</g>`);
|
|
281
|
+
}
|
|
282
|
+
function drawLabel(pen, s, l, fill) {
|
|
283
|
+
const { score, ask } = scoreOf(s);
|
|
284
|
+
if (ask)
|
|
285
|
+
drawThing(pen, { ask: true }, l.at.x, l.at.y, l.size + 6);
|
|
286
|
+
else
|
|
287
|
+
pen.text(l.at.x, l.at.y, String(score), { size: l.size, fill: textOn(fill) });
|
|
288
|
+
}
|
|
289
|
+
function sectorPath(r0, r1, a0, a1) {
|
|
290
|
+
const p0 = polar(r1, a0);
|
|
291
|
+
const p1 = polar(r1, a1);
|
|
292
|
+
const large = a1 - a0 > 180 ? 1 : 0;
|
|
293
|
+
if (r0 <= 0)
|
|
294
|
+
return `M0 0L${n(p0.x)} ${n(p0.y)}A${n(r1)} ${n(r1)} 0 ${large} 1 ${n(p1.x)} ${n(p1.y)}Z`;
|
|
295
|
+
const q0 = polar(r0, a0);
|
|
296
|
+
const q1 = polar(r0, a1);
|
|
297
|
+
return `M${n(q0.x)} ${n(q0.y)}L${n(p0.x)} ${n(p0.y)}A${n(r1)} ${n(r1)} 0 ${large} 1 ${n(p1.x)} ${n(p1.y)}L${n(q1.x)} ${n(q1.y)}A${n(r0)} ${n(r0)} 0 ${large} 0 ${n(q0.x)} ${n(q0.y)}Z`;
|
|
298
|
+
}
|
|
299
|
+
/** drawRound draws a round board centred at 0, 0 and gives the box it takes. */
|
|
300
|
+
function drawRound(pen, b, R) {
|
|
301
|
+
const round = roundOf(b, R);
|
|
302
|
+
if (b.rings) {
|
|
303
|
+
const k = b.rings.length;
|
|
304
|
+
for (let j = k; j >= 1; j--) {
|
|
305
|
+
const s = scoreOf(b.rings[j - 1]);
|
|
306
|
+
pen.el("circle", {
|
|
307
|
+
cx: 0,
|
|
308
|
+
cy: 0,
|
|
309
|
+
r: j * round.band,
|
|
310
|
+
fill: colors[s.color ?? ringColors[j - 1]].fill,
|
|
311
|
+
stroke: ink,
|
|
312
|
+
"stroke-width": j === k ? outline : 2,
|
|
313
|
+
class: "ring",
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
b.rings.forEach((s, j) => {
|
|
317
|
+
drawLabel(pen, s, round.labels[j], scoreOf(s).color ?? ringColors[j]);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
const zones = b.zones;
|
|
322
|
+
const span = 360 / zones.length;
|
|
323
|
+
zones.forEach((s, i) => {
|
|
324
|
+
pen.el("path", {
|
|
325
|
+
d: sectorPath(round.bullR, R, -90 + i * span, -90 + (i + 1) * span),
|
|
326
|
+
fill: colors[scoreOf(s).color ?? zoneColors[i % zoneColors.length]].fill,
|
|
327
|
+
stroke: ink,
|
|
328
|
+
"stroke-width": 2,
|
|
329
|
+
"stroke-linejoin": "round",
|
|
330
|
+
class: "zone",
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
pen.el("circle", { cx: 0, cy: 0, r: R, fill: "none", stroke: ink, "stroke-width": outline });
|
|
334
|
+
if (b.bull !== undefined)
|
|
335
|
+
pen.el("circle", {
|
|
336
|
+
cx: 0,
|
|
337
|
+
cy: 0,
|
|
338
|
+
r: round.bullR,
|
|
339
|
+
fill: colors[scoreOf(b.bull).color ?? "yellow"].fill,
|
|
340
|
+
stroke: ink,
|
|
341
|
+
"stroke-width": 2,
|
|
342
|
+
class: "bull",
|
|
343
|
+
});
|
|
344
|
+
zones.forEach((s, i) => {
|
|
345
|
+
drawLabel(pen, s, round.labels[i], scoreOf(s).color ?? zoneColors[i % zoneColors.length]);
|
|
346
|
+
});
|
|
347
|
+
if (b.bull !== undefined)
|
|
348
|
+
drawLabel(pen, b.bull, round.labels[zones.length], scoreOf(b.bull).color ?? "yellow");
|
|
349
|
+
}
|
|
350
|
+
for (const tip of round.tips)
|
|
351
|
+
drawDart(pen, tip, b.color ?? "green", round.k);
|
|
352
|
+
const boxes = round.tips.map((tip) => dartBox(tip, round.k));
|
|
353
|
+
return {
|
|
354
|
+
x0: Math.min(-R - 2, ...boxes.map((x) => x.x0)),
|
|
355
|
+
y0: Math.min(-R - 2, ...boxes.map((x) => x.y0)),
|
|
356
|
+
x1: Math.max(R + 2, ...boxes.map((x) => x.x1)),
|
|
357
|
+
y1: Math.max(R + 2, ...boxes.map((x) => x.y1)),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
/** drawToss draws a ring toss board, its left end at 0 and its floor at 0, and gives its box. */
|
|
361
|
+
function drawToss(pen, b, toss) {
|
|
362
|
+
const pegs = b.pegs;
|
|
363
|
+
const hoop = colors[b.color ?? "red"];
|
|
364
|
+
const pegH = 104;
|
|
365
|
+
const board = { y0: -18, y1: 14 };
|
|
366
|
+
pen.el("rect", {
|
|
367
|
+
x: 0,
|
|
368
|
+
y: board.y0,
|
|
369
|
+
width: toss.width,
|
|
370
|
+
height: board.y1 - board.y0,
|
|
371
|
+
rx: 8,
|
|
372
|
+
fill: colors.brown.fill,
|
|
373
|
+
stroke: ink,
|
|
374
|
+
"stroke-width": outline,
|
|
375
|
+
});
|
|
376
|
+
pegs.forEach((peg, i) => {
|
|
377
|
+
const cx = 12 + (i + 0.5) * toss.pitch;
|
|
378
|
+
const foot = board.y0 + 6;
|
|
379
|
+
const hoops = peg.hoops ?? 0;
|
|
380
|
+
const ring = (h, half) => {
|
|
381
|
+
const y = foot - 7 - h * 12;
|
|
382
|
+
const rx = Math.min(26, toss.pitch / 2 - 6);
|
|
383
|
+
const d = half === "back"
|
|
384
|
+
? `M${n(cx - rx)} ${n(y)}A${n(rx)} 7 0 0 1 ${n(cx + rx)} ${n(y)}`
|
|
385
|
+
: `M${n(cx - rx)} ${n(y)}A${n(rx)} 7 0 0 0 ${n(cx + rx)} ${n(y)}`;
|
|
386
|
+
pen.el("path", {
|
|
387
|
+
d,
|
|
388
|
+
fill: "none",
|
|
389
|
+
stroke: ink,
|
|
390
|
+
"stroke-width": 8.5,
|
|
391
|
+
"stroke-linecap": "round",
|
|
392
|
+
});
|
|
393
|
+
pen.el("path", {
|
|
394
|
+
d,
|
|
395
|
+
fill: "none",
|
|
396
|
+
stroke: hoop.fill,
|
|
397
|
+
"stroke-width": 4.5,
|
|
398
|
+
"stroke-linecap": "round",
|
|
399
|
+
...(half === "front" ? { class: "hoop" } : {}),
|
|
400
|
+
});
|
|
401
|
+
};
|
|
402
|
+
for (let h = 0; h < hoops; h++)
|
|
403
|
+
ring(h, "back");
|
|
404
|
+
pen.el("path", {
|
|
405
|
+
d: `M${n(cx - 6)} ${n(foot)}V${n(foot - pegH + 6)}A6 6 0 0 1 ${n(cx + 6)} ${n(foot - pegH + 6)}V${n(foot)}Z`,
|
|
406
|
+
fill: colors.yellow.fill,
|
|
407
|
+
stroke: ink,
|
|
408
|
+
"stroke-width": 2,
|
|
409
|
+
class: "peg",
|
|
410
|
+
});
|
|
411
|
+
for (let h = 0; h < hoops; h++)
|
|
412
|
+
ring(h, "front");
|
|
413
|
+
const tag = { y: (board.y0 + board.y1) / 2 + 22 };
|
|
414
|
+
if (peg.ask)
|
|
415
|
+
drawThing(pen, { ask: true }, cx, tag.y, 30);
|
|
416
|
+
else {
|
|
417
|
+
const w = Math.max(28, textWidth(String(peg.score), 17) + 12);
|
|
418
|
+
pen.el("rect", {
|
|
419
|
+
x: cx - w / 2,
|
|
420
|
+
y: tag.y - 13,
|
|
421
|
+
width: w,
|
|
422
|
+
height: 26,
|
|
423
|
+
rx: 7,
|
|
424
|
+
fill: "#ffffff",
|
|
425
|
+
stroke: ink,
|
|
426
|
+
"stroke-width": 2,
|
|
427
|
+
});
|
|
428
|
+
pen.text(cx, tag.y, String(peg.score), { size: 17 });
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
return { x0: 0, y0: board.y0 - pegH, x1: toss.width, y1: board.y1 + 28 };
|
|
432
|
+
}
|
|
433
|
+
/** drawBelow writes a board's name and its score under it, and gives the height they take. */
|
|
434
|
+
function drawBelow(pen, b, cx, y) {
|
|
435
|
+
let h = 0;
|
|
436
|
+
if (b.label) {
|
|
437
|
+
pen.text(cx, y + 14, b.label, { size: 18 });
|
|
438
|
+
h += 28;
|
|
439
|
+
}
|
|
440
|
+
if (b.total !== undefined) {
|
|
441
|
+
const top = y + h + 4;
|
|
442
|
+
if (b.total === "ask")
|
|
443
|
+
drawThing(pen, { ask: true }, cx, top + 17, 36);
|
|
444
|
+
else {
|
|
445
|
+
const w = Math.max(40, textWidth(String(b.total), 20) + 18);
|
|
446
|
+
pen.el("rect", {
|
|
447
|
+
x: cx - w / 2,
|
|
448
|
+
y: top,
|
|
449
|
+
width: w,
|
|
450
|
+
height: 34,
|
|
451
|
+
rx: 9,
|
|
452
|
+
fill: "#ffffff",
|
|
453
|
+
stroke: ink,
|
|
454
|
+
"stroke-width": 2,
|
|
455
|
+
class: "total",
|
|
456
|
+
});
|
|
457
|
+
pen.text(cx, top + 17, String(b.total), { size: 20 });
|
|
458
|
+
}
|
|
459
|
+
h += 40;
|
|
460
|
+
}
|
|
461
|
+
return h;
|
|
462
|
+
}
|
|
463
|
+
// --- checking -------------------------------------------------------------------
|
|
464
|
+
function checkScore(s, path, problems) {
|
|
465
|
+
if (typeof s === "number") {
|
|
466
|
+
int(s, path, problems, 0, scoreMost);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
if (!isObj(s)) {
|
|
470
|
+
problems.push(`${path}: must be a score, a whole number, or {"score", "color"}, or {"ask": true}`);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
only(s, ["score", "ask", "color"], path, problems);
|
|
474
|
+
if (s.ask !== undefined && s.ask !== true)
|
|
475
|
+
problems.push(`${path}.ask: must be true, or left out`);
|
|
476
|
+
if (s.ask === true) {
|
|
477
|
+
if (s.score !== undefined)
|
|
478
|
+
problems.push(`${path}.score: an asked score shows none; leave it out`);
|
|
479
|
+
}
|
|
480
|
+
else
|
|
481
|
+
int(s.score, `${path}.score`, problems, 0, scoreMost);
|
|
482
|
+
if (s.color !== undefined && !isColor(s.color))
|
|
483
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
484
|
+
}
|
|
485
|
+
function checkBoard(b, path, t, problems) {
|
|
486
|
+
if (!isObj(b)) {
|
|
487
|
+
problems.push(`${path}: must be a board: rings, zones or pegs`);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
only(b, ["rings", "zones", "bull", "pegs", "hits", "color", "label", "total"], path, problems);
|
|
491
|
+
const shapes = ["rings", "zones", "pegs"].filter((m) => b[m] !== undefined);
|
|
492
|
+
if (shapes.length !== 1) {
|
|
493
|
+
problems.push(`${path}: has exactly one of rings, zones and pegs${shapes.length > 1 ? `, not ${shapes.join(" and ")}` : ""}`);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const before = problems.length;
|
|
497
|
+
const R = radiusOf(t);
|
|
498
|
+
if (b.rings !== undefined) {
|
|
499
|
+
const most = Math.min(6, Math.floor(R / leastBand));
|
|
500
|
+
const rings = list(b.rings, `${path}.rings`, problems, 2, 6);
|
|
501
|
+
if (rings.length > most && rings.length <= 6)
|
|
502
|
+
problems.push(`${path}.rings: ${rings.length} rings are too thin when ${t} boards stand together; at most ${most}`);
|
|
503
|
+
rings.forEach((s, j) => {
|
|
504
|
+
checkScore(s, `${path}.rings[${j}]`, problems);
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
if (b.zones !== undefined)
|
|
508
|
+
list(b.zones, `${path}.zones`, problems, 2, 8).forEach((s, j) => {
|
|
509
|
+
checkScore(s, `${path}.zones[${j}]`, problems);
|
|
510
|
+
});
|
|
511
|
+
if (b.bull !== undefined) {
|
|
512
|
+
if (b.zones === undefined)
|
|
513
|
+
problems.push(`${path}.bull: only a board of zones has a bull's-eye`);
|
|
514
|
+
else
|
|
515
|
+
checkScore(b.bull, `${path}.bull`, problems);
|
|
516
|
+
}
|
|
517
|
+
if (b.pegs !== undefined) {
|
|
518
|
+
const most = Math.min(6, Math.floor(roomOf(t) / 44));
|
|
519
|
+
const pegs = list(b.pegs, `${path}.pegs`, problems, 2, 6);
|
|
520
|
+
if (pegs.length > most && pegs.length <= 6)
|
|
521
|
+
problems.push(`${path}.pegs: ${pegs.length} pegs do not fit when ${t} boards stand together; at most ${most}`);
|
|
522
|
+
pegs.forEach((p, j) => {
|
|
523
|
+
const at = `${path}.pegs[${j}]`;
|
|
524
|
+
if (!isObj(p)) {
|
|
525
|
+
problems.push(`${at}: must be a peg: a score, and the hoops on it`);
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
only(p, ["score", "ask", "hoops"], at, problems);
|
|
529
|
+
if (p.ask !== undefined && p.ask !== true)
|
|
530
|
+
problems.push(`${at}.ask: must be true, or left out`);
|
|
531
|
+
if (p.ask === true) {
|
|
532
|
+
if (p.score !== undefined)
|
|
533
|
+
problems.push(`${at}.score: an asked score shows none; leave it out`);
|
|
534
|
+
}
|
|
535
|
+
else
|
|
536
|
+
int(p.score, `${at}.score`, problems, 0, scoreMost);
|
|
537
|
+
if (p.hoops !== undefined)
|
|
538
|
+
int(p.hoops, `${at}.hoops`, problems, 0, mostHoops);
|
|
539
|
+
});
|
|
540
|
+
if (b.hits !== undefined)
|
|
541
|
+
problems.push(`${path}.hits: a ring toss board counts hoops on its pegs, not hits`);
|
|
542
|
+
}
|
|
543
|
+
if (b.hits !== undefined && b.pegs === undefined) {
|
|
544
|
+
const k = Array.isArray(b.rings) ? b.rings.length : Array.isArray(b.zones) ? b.zones.length : 0;
|
|
545
|
+
list(b.hits, `${path}.hits`, problems, 1, mostHits).forEach((h, j) => {
|
|
546
|
+
const at = `${path}.hits[${j}]`;
|
|
547
|
+
if (h === "bull") {
|
|
548
|
+
if (b.bull === undefined)
|
|
549
|
+
problems.push(`${at}: the board has no bull's-eye`);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
int(h, at, problems, 1, k);
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
if (b.color !== undefined && !isColor(b.color))
|
|
556
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
557
|
+
if (b.label !== undefined)
|
|
558
|
+
text(b.label, `${path}.label`, problems, labelMost);
|
|
559
|
+
if (b.total !== undefined && b.total !== "ask")
|
|
560
|
+
int(b.total, `${path}.total`, problems, 0, 9999);
|
|
561
|
+
if (problems.length > before)
|
|
562
|
+
return;
|
|
563
|
+
const board = b;
|
|
564
|
+
if (board.pegs === undefined && (board.hits?.length ?? 0) > 0) {
|
|
565
|
+
const region = roundOf(board, R).short;
|
|
566
|
+
if (region !== undefined) {
|
|
567
|
+
const where = region === "bull" ? "the bull's-eye" : board.rings ? `ring ${region}` : `zone ${region}`;
|
|
568
|
+
const asked = (board.hits ?? []).filter((h) => h === region).length;
|
|
569
|
+
const most = capacityOf(board, R, region, asked);
|
|
570
|
+
const fit = most === 0 ? "none fits" : most === 1 ? "only 1 fits" : `at most ${most} fit`;
|
|
571
|
+
problems.push(`${path}.hits: ${asked} ${asked === 1 ? "dart does" : "darts do"} not fit in ${where}; ${fit} there at this size`);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (typeof board.total === "number") {
|
|
575
|
+
const sum = sumOf(board);
|
|
576
|
+
if (sum !== undefined && sum !== board.total)
|
|
577
|
+
problems.push(`${path}.total: the ${board.pegs ? "hoops" : "darts"} score ${sum}, not ${board.total}`);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* capacityOf is how many of its darts a region takes at most, the board's
|
|
582
|
+
* other hits as they are: the most that a refusal may promise will pass.
|
|
583
|
+
*/
|
|
584
|
+
function capacityOf(b, R, region, asked) {
|
|
585
|
+
for (let m = asked - 1; m > 0; m--) {
|
|
586
|
+
let drop = asked - m;
|
|
587
|
+
const hits = [...(b.hits ?? [])]
|
|
588
|
+
.reverse()
|
|
589
|
+
.filter((h) => !(h === region && drop-- > 0))
|
|
590
|
+
.reverse();
|
|
591
|
+
if (roundOf({ ...b, hits }, R).short === undefined)
|
|
592
|
+
return m;
|
|
593
|
+
}
|
|
594
|
+
return 0;
|
|
595
|
+
}
|
|
596
|
+
/** sumOf is what a board's darts or hoops score, or undefined when a score they need is asked. */
|
|
597
|
+
export function sumOf(b) {
|
|
598
|
+
if (b.pegs) {
|
|
599
|
+
let sum = 0;
|
|
600
|
+
for (const p of b.pegs) {
|
|
601
|
+
if ((p.hoops ?? 0) === 0)
|
|
602
|
+
continue;
|
|
603
|
+
if (p.ask)
|
|
604
|
+
return undefined;
|
|
605
|
+
sum += (p.score ?? 0) * (p.hoops ?? 0);
|
|
606
|
+
}
|
|
607
|
+
return sum;
|
|
608
|
+
}
|
|
609
|
+
let sum = 0;
|
|
610
|
+
for (const h of b.hits ?? []) {
|
|
611
|
+
const s = scoreOf(h === "bull" ? b.bull : (b.rings ?? b.zones)[h - 1]);
|
|
612
|
+
if (s.ask)
|
|
613
|
+
return undefined;
|
|
614
|
+
sum += s.score ?? 0;
|
|
615
|
+
}
|
|
616
|
+
return sum;
|
|
617
|
+
}
|
|
618
|
+
// --- words ----------------------------------------------------------------------------
|
|
619
|
+
function an(words) {
|
|
620
|
+
return `${/^[aeiou]/.test(words) ? "an" : "a"} ${words}`;
|
|
621
|
+
}
|
|
622
|
+
function joinAnd(words) {
|
|
623
|
+
if (words.length <= 1)
|
|
624
|
+
return words.join("");
|
|
625
|
+
return `${words.slice(0, -1).join(", ")} and ${words[words.length - 1]}`;
|
|
626
|
+
}
|
|
627
|
+
function worth(s) {
|
|
628
|
+
const { score, ask } = scoreOf(s);
|
|
629
|
+
return ask ? 'marked "?"' : `worth ${score}`;
|
|
630
|
+
}
|
|
631
|
+
function ordinal(i) {
|
|
632
|
+
const end = i % 10 === 1 && i !== 11
|
|
633
|
+
? "st"
|
|
634
|
+
: i % 10 === 2 && i !== 12
|
|
635
|
+
? "nd"
|
|
636
|
+
: i % 10 === 3 && i !== 13
|
|
637
|
+
? "rd"
|
|
638
|
+
: "th";
|
|
639
|
+
return `${i}${end}`;
|
|
640
|
+
}
|
|
641
|
+
function sayBoard(b) {
|
|
642
|
+
const out = [];
|
|
643
|
+
const dart = colors[b.color ?? (b.pegs ? "red" : "green")].word;
|
|
644
|
+
if (b.rings) {
|
|
645
|
+
const k = b.rings.length;
|
|
646
|
+
const parts = b.rings.map((s, j) => {
|
|
647
|
+
const c = colors[scoreOf(s).color ?? ringColors[j]].word;
|
|
648
|
+
return j === 0 ? `${an(c)} centre ${worth(s)}` : `${an(c)} ring ${worth(s)}`;
|
|
649
|
+
});
|
|
650
|
+
out.push(`a target of ${k} rings; from the centre out: ${joinAnd(parts)}.`);
|
|
651
|
+
const hits = b.hits ?? [];
|
|
652
|
+
if (hits.length > 0) {
|
|
653
|
+
const counts = b.rings
|
|
654
|
+
.map((s, j) => ({ j: j + 1, s, k: hits.filter((h) => h === j + 1).length }))
|
|
655
|
+
.filter((x) => x.k > 0)
|
|
656
|
+
.map(({ j, s, k: m }) => {
|
|
657
|
+
const c = colors[scoreOf(s).color ?? ringColors[j - 1]].word;
|
|
658
|
+
return `${m} in the ${c} ${j === 1 ? "centre" : "ring"} (${scoreOf(s).ask ? "?" : scoreOf(s).score})`;
|
|
659
|
+
});
|
|
660
|
+
out.push(`${hits.length} ${dart} dart${hits.length === 1 ? " has" : "s have"} hit it: ${joinAnd(counts)}.`);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
else if (b.zones) {
|
|
664
|
+
const k = b.zones.length;
|
|
665
|
+
const parts = b.zones.map((s, i) => `${an(colors[scoreOf(s).color ?? zoneColors[i % zoneColors.length]].word)} part ${worth(s)}`);
|
|
666
|
+
out.push(`a round target cut into ${k} equal parts; clockwise from the top: ${joinAnd(parts)}.`);
|
|
667
|
+
if (b.bull !== undefined)
|
|
668
|
+
out.push(`In the middle, ${an(colors[scoreOf(b.bull).color ?? "yellow"].word)} circle ${worth(b.bull)}.`);
|
|
669
|
+
const hits = b.hits ?? [];
|
|
670
|
+
if (hits.length > 0) {
|
|
671
|
+
const places = [];
|
|
672
|
+
if (hits.includes("bull")) {
|
|
673
|
+
const s = scoreOf(b.bull);
|
|
674
|
+
places.push(`${hits.filter((h) => h === "bull").length} in the middle circle (${s.ask ? "?" : s.score})`);
|
|
675
|
+
}
|
|
676
|
+
b.zones.forEach((s, i) => {
|
|
677
|
+
const m = hits.filter((h) => h === i + 1).length;
|
|
678
|
+
if (m === 0)
|
|
679
|
+
return;
|
|
680
|
+
const c = colors[scoreOf(s).color ?? zoneColors[i % zoneColors.length]].word;
|
|
681
|
+
places.push(`${m} in the ${c} part (${scoreOf(s).ask ? "?" : scoreOf(s).score})`);
|
|
682
|
+
});
|
|
683
|
+
out.push(`${hits.length} ${dart} dart${hits.length === 1 ? " has" : "s have"} hit it: ${joinAnd(places)}.`);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
const pegs = b.pegs;
|
|
688
|
+
out.push(`a ring toss board with ${pegs.length} pegs in a row, worth from the left ${joinAnd(pegs.map((p) => (p.ask ? "?" : String(p.score))))}.`);
|
|
689
|
+
const on = pegs
|
|
690
|
+
.map((p, i) => ({ i, k: p.hoops ?? 0, p }))
|
|
691
|
+
.filter((x) => x.k > 0)
|
|
692
|
+
.map(({ i, k, p }) => `${k} on the ${ordinal(i + 1)} peg (${p.ask ? "?" : p.score})`);
|
|
693
|
+
const total = pegs.reduce((s, p) => s + (p.hoops ?? 0), 0);
|
|
694
|
+
out.push(total === 0
|
|
695
|
+
? "No hoop is on a peg."
|
|
696
|
+
: `${total} ${dart} hoop${total === 1 ? " is" : "s are"} on the pegs: ${joinAnd(on)}${on.length < pegs.length ? "; the other pegs have none" : ""}.`);
|
|
697
|
+
}
|
|
698
|
+
if (b.total !== undefined)
|
|
699
|
+
out.push(b.total === "ask"
|
|
700
|
+
? "Under it, a question mark for its score."
|
|
701
|
+
: `Under it, the score ${b.total}.`);
|
|
702
|
+
return out.join(" ");
|
|
703
|
+
}
|
|
704
|
+
export const target = {
|
|
705
|
+
kind: "target",
|
|
706
|
+
members: ["kind", "targets"],
|
|
707
|
+
check(scene, problems) {
|
|
708
|
+
only(scene, this.members, "scene", problems);
|
|
709
|
+
const boards = list(scene.targets, "targets", problems, 1, mostTargets);
|
|
710
|
+
boards.forEach((b, i) => {
|
|
711
|
+
checkBoard(b, `targets[${i}]`, boards.length, problems);
|
|
712
|
+
});
|
|
713
|
+
},
|
|
714
|
+
draw(scene, pen) {
|
|
715
|
+
const boards = scene.targets;
|
|
716
|
+
const t = boards.length;
|
|
717
|
+
const R = radiusOf(t);
|
|
718
|
+
const perRow = perRowOf(t);
|
|
719
|
+
const room = roomOf(t);
|
|
720
|
+
const drawn = boards.map((b) => {
|
|
721
|
+
const sub = pen.sub();
|
|
722
|
+
const toss = b.pegs ? tossOf(b, room) : undefined;
|
|
723
|
+
const box = toss ? drawToss(sub, b, toss) : drawRound(sub, b, R);
|
|
724
|
+
const below = pen.sub();
|
|
725
|
+
const h = drawBelow(below, b, 0, 0);
|
|
726
|
+
// The middle of the board itself, which its name and score stand under.
|
|
727
|
+
return { sub, box, below, h, middle: toss ? toss.width / 2 : 0 };
|
|
728
|
+
});
|
|
729
|
+
// Boards of a row share the line of their centres, or of their floors, and the line of their names.
|
|
730
|
+
let y = margin;
|
|
731
|
+
let width = 0;
|
|
732
|
+
for (let row = 0; row * perRow < t; row++) {
|
|
733
|
+
const items = drawn.slice(row * perRow, row * perRow + perRow);
|
|
734
|
+
const up = Math.max(...items.map((d) => -d.box.y0));
|
|
735
|
+
const down = Math.max(...items.map((d) => d.box.y1));
|
|
736
|
+
const belowH = Math.max(...items.map((d) => d.h));
|
|
737
|
+
let x = margin;
|
|
738
|
+
for (const d of items) {
|
|
739
|
+
const ox = x - d.box.x0;
|
|
740
|
+
pen.place(d.sub, ox, y + up);
|
|
741
|
+
pen.place(d.below, ox + d.middle, y + up + down + 8);
|
|
742
|
+
x += d.box.x1 - d.box.x0 + 24;
|
|
743
|
+
}
|
|
744
|
+
width = Math.max(width, x - 24 + margin);
|
|
745
|
+
y += up + down + (belowH > 0 ? belowH + 8 : 0) + 16;
|
|
746
|
+
}
|
|
747
|
+
return { width, height: y - 16 + margin };
|
|
748
|
+
},
|
|
749
|
+
describe(scene) {
|
|
750
|
+
const boards = scene.targets;
|
|
751
|
+
if (boards.length === 1) {
|
|
752
|
+
const b = boards[0];
|
|
753
|
+
const s = sayBoard(b);
|
|
754
|
+
return `${b.label ? `"${b.label}": ` : ""}${s[0].toUpperCase()}${s.slice(1)}`;
|
|
755
|
+
}
|
|
756
|
+
const rows = boards.length === 4 ? "4 boards in two rows of two" : `${boards.length} boards side by side`;
|
|
757
|
+
return `${rows}. ${boards
|
|
758
|
+
.map((b, i) => `Board ${i + 1}${b.label ? `, "${b.label}"` : ""}: ${sayBoard(b)}`)
|
|
759
|
+
.join(" ")}`;
|
|
760
|
+
},
|
|
761
|
+
};
|