@johnmorrisdotca/sugoroku 1.1.0 → 1.1.1
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/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/board.js +1 -1
- package/dist/colours.js +6 -1
- package/dist/computer.d.ts +1 -1
- package/dist/computer.js +9 -4
- package/dist/draw-entry.d.ts +3 -3
- package/dist/draw-entry.js +2 -2
- package/dist/draw.js +30 -11
- package/dist/element.d.ts +3 -122
- package/dist/element.js +327 -405
- package/dist/elementFrame.d.ts +21 -0
- package/dist/elementFrame.js +110 -0
- package/dist/evaluate.js +5 -2
- package/dist/game.js +16 -2
- package/dist/geometry.js +44 -27
- package/dist/index.d.ts +11 -11
- package/dist/index.js +9 -9
- package/dist/match.js +14 -2
- package/dist/mount.d.ts +2 -2
- package/dist/mount.js +80 -354
- package/dist/mountView.d.ts +58 -0
- package/dist/mountView.js +459 -0
- package/dist/notation.js +6 -2
- package/dist/play-entry.d.ts +4 -4
- package/dist/play-entry.js +1 -1
- package/dist/positionElement.d.ts +107 -0
- package/dist/positionElement.js +303 -0
- package/dist/positionId.js +4 -1
- package/dist/record.d.ts +1 -1
- package/dist/record.js +27 -31
- package/dist/rules.js +34 -5
- package/dist/strings.js +3 -1
- package/dist/variants.js +19 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +11 -11
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { BAR, boardPoint } from "./board.js";
|
|
2
|
+
import { drawSugoroku } from "./draw.js";
|
|
3
|
+
import { canBeaver, canDouble, legalMoves, turnIsPlayed } from "./game.js";
|
|
4
|
+
import { boardSize, CHECKER } from "./geometry.js";
|
|
5
|
+
import { diceToPlay } from "./plays.js";
|
|
6
|
+
import { otherSide, sideIndex } from "./side.js";
|
|
7
|
+
/** Draw the board, keep the words and buttons up to date, and play by pointer. */
|
|
8
|
+
export function attachSugorokuView(input) {
|
|
9
|
+
const host = input.host;
|
|
10
|
+
const document = input.doc;
|
|
11
|
+
const view = input.page;
|
|
12
|
+
const boardBox = input.boardBox;
|
|
13
|
+
const score = input.score;
|
|
14
|
+
const status = input.status;
|
|
15
|
+
const slotA = input.slotA;
|
|
16
|
+
const slotB = input.slotB;
|
|
17
|
+
const slotC = input.slotC;
|
|
18
|
+
const dragAllowed = input.dragAllowed;
|
|
19
|
+
const say = input.say;
|
|
20
|
+
const nameOf = input.nameOf;
|
|
21
|
+
const human = input.human;
|
|
22
|
+
const isComputer = input.isComputer;
|
|
23
|
+
const spec = input.spec;
|
|
24
|
+
const makeMove = input.makeMove;
|
|
25
|
+
let selected = null;
|
|
26
|
+
let message = null;
|
|
27
|
+
let portrait = false;
|
|
28
|
+
function create(owner, tag, className, text) {
|
|
29
|
+
const element = owner.createElement(tag);
|
|
30
|
+
element.className = className;
|
|
31
|
+
if (text !== undefined)
|
|
32
|
+
element.textContent = text;
|
|
33
|
+
return element;
|
|
34
|
+
}
|
|
35
|
+
const hasCube = () => {
|
|
36
|
+
const settings = input.settings();
|
|
37
|
+
return settings.rules.cube && settings.rules.points !== 1;
|
|
38
|
+
};
|
|
39
|
+
/** Whose home board is at the bottom. */
|
|
40
|
+
const viewSide = () => {
|
|
41
|
+
const look = input.look();
|
|
42
|
+
if (look.view !== undefined && look.view !== "auto")
|
|
43
|
+
return look.view;
|
|
44
|
+
if (isComputer("white") && human("black"))
|
|
45
|
+
return "black";
|
|
46
|
+
return "white";
|
|
47
|
+
};
|
|
48
|
+
// Drawing.
|
|
49
|
+
const diceShown = () => {
|
|
50
|
+
const game = input.game();
|
|
51
|
+
const lastOpening = input.lastOpening();
|
|
52
|
+
if (game.phase === "opening") {
|
|
53
|
+
return lastOpening === null
|
|
54
|
+
? null
|
|
55
|
+
: { side: "white", values: [lastOpening[0], lastOpening[1]], spent: [false, false] };
|
|
56
|
+
}
|
|
57
|
+
if (game.dice === null || game.turn === null)
|
|
58
|
+
return null;
|
|
59
|
+
const values = diceToPlay(spec(), game.dice);
|
|
60
|
+
const unused = [...game.need];
|
|
61
|
+
const planLeft = [...game.plan];
|
|
62
|
+
const spent = values.map((value) => {
|
|
63
|
+
const inPlan = planLeft.indexOf(value);
|
|
64
|
+
if (inPlan === -1)
|
|
65
|
+
return true;
|
|
66
|
+
planLeft.splice(inPlan, 1);
|
|
67
|
+
const open = unused.indexOf(value);
|
|
68
|
+
if (open === -1)
|
|
69
|
+
return true;
|
|
70
|
+
unused.splice(open, 1);
|
|
71
|
+
return false;
|
|
72
|
+
});
|
|
73
|
+
return { side: game.turn, values, spent };
|
|
74
|
+
};
|
|
75
|
+
const legalNow = () => {
|
|
76
|
+
const game = input.game();
|
|
77
|
+
return game.phase === "playing" && game.turn !== null && human(game.turn) ? legalMoves(game) : [];
|
|
78
|
+
};
|
|
79
|
+
const draw = () => {
|
|
80
|
+
const game = input.game();
|
|
81
|
+
const look = input.look();
|
|
82
|
+
const moves = legalNow();
|
|
83
|
+
const mover = game.turn;
|
|
84
|
+
const sources = [...new Set(moves.map((move) => move.from))];
|
|
85
|
+
const targets = selected === null ? [] : [...new Set(moves.filter((move) => move.from === selected).map((move) => move.to))];
|
|
86
|
+
return drawSugoroku(game.position, {
|
|
87
|
+
variant: spec(),
|
|
88
|
+
view: viewSide(),
|
|
89
|
+
home: look.home,
|
|
90
|
+
orientation: portrait ? "portrait" : "landscape",
|
|
91
|
+
numbers: look.numbers,
|
|
92
|
+
board: look.board,
|
|
93
|
+
checkers: look.checkers,
|
|
94
|
+
colours: look.colours,
|
|
95
|
+
theme: look.theme,
|
|
96
|
+
dice: diceShown(),
|
|
97
|
+
cube: hasCube() ? { value: game.cube.value, owner: game.cube.owner } : null,
|
|
98
|
+
highlight: mover === null ? null : { side: mover, from: selected, targets, movable: sources },
|
|
99
|
+
label: say("board", { variant: say(`variant_${spec().key}`) }),
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
// The words.
|
|
103
|
+
const pointsText = (count) => say(count === 1 ? "point" : "points", { n: count });
|
|
104
|
+
const diceText = (dice) => dice === null ? "" : diceToPlay(spec(), dice).join(" ");
|
|
105
|
+
const statusText = () => {
|
|
106
|
+
const game = input.game();
|
|
107
|
+
const match = input.match();
|
|
108
|
+
const lastOpening = input.lastOpening();
|
|
109
|
+
const waitingForDice = input.waitingForDice();
|
|
110
|
+
if (message !== null)
|
|
111
|
+
return message;
|
|
112
|
+
const side = game.turn;
|
|
113
|
+
if (game.phase === "opening")
|
|
114
|
+
return waitingForDice
|
|
115
|
+
? say("waitingDice")
|
|
116
|
+
: lastOpening !== null && lastOpening[0] === lastOpening[1]
|
|
117
|
+
? say("openingTie", { die: lastOpening[0] })
|
|
118
|
+
: say("opening");
|
|
119
|
+
if (game.phase === "over") {
|
|
120
|
+
const result = game.result;
|
|
121
|
+
let line;
|
|
122
|
+
if (result.winner === null)
|
|
123
|
+
line = say("drawn");
|
|
124
|
+
else if (result.how === "drop")
|
|
125
|
+
line = say("wonDrop", { side: nameOf(result.winner), points: pointsText(result.points) });
|
|
126
|
+
else if (result.how === "concede")
|
|
127
|
+
line = say("wonConcede", { side: nameOf(result.winner), points: pointsText(result.points) });
|
|
128
|
+
else
|
|
129
|
+
line = say("wonBy", {
|
|
130
|
+
side: nameOf(result.winner),
|
|
131
|
+
kind: say(result.kind === "single" ? "singleGame" : result.kind),
|
|
132
|
+
points: pointsText(result.points),
|
|
133
|
+
});
|
|
134
|
+
if (match.over)
|
|
135
|
+
line += ` ${match.winner === null ? say("matchDrawn") : say("matchWon", { side: nameOf(match.winner), a: match.score[sideIndex(match.winner)], b: match.score[sideIndex(otherSide(match.winner))] })}`;
|
|
136
|
+
return line;
|
|
137
|
+
}
|
|
138
|
+
if (side === null)
|
|
139
|
+
return "";
|
|
140
|
+
if (game.phase === "double-offered" || game.phase === "beaver-offered") {
|
|
141
|
+
const offerer = game.offeredBy;
|
|
142
|
+
const value = game.phase === "double-offered" ? game.cube.value * 2 : game.cube.value * 4;
|
|
143
|
+
const lose = pointsText(game.phase === "double-offered" ? game.cube.value : game.cube.value * 2);
|
|
144
|
+
if (human(otherSide(offerer)))
|
|
145
|
+
return say(game.phase === "double-offered" ? "answerDouble" : "answerBeaver", {
|
|
146
|
+
side: nameOf(offerer),
|
|
147
|
+
value,
|
|
148
|
+
points: lose,
|
|
149
|
+
});
|
|
150
|
+
return say("thinking", { side: nameOf(otherSide(offerer)) });
|
|
151
|
+
}
|
|
152
|
+
if (game.phase === "before-roll") {
|
|
153
|
+
if (isComputer(side))
|
|
154
|
+
return say("thinking", { side: nameOf(side) });
|
|
155
|
+
return isComputer(otherSide(side)) ? say("toRollYou") : say("toRoll", { side: nameOf(side) });
|
|
156
|
+
}
|
|
157
|
+
if (isComputer(side) && game.moves.length === 0 && game.need.length > 0)
|
|
158
|
+
return say("thinking", { side: nameOf(side) });
|
|
159
|
+
if (game.plan.length === 0)
|
|
160
|
+
return say(human(side) && isComputer(otherSide(side)) ? "noMoveYou" : "noMove", { side: nameOf(side) });
|
|
161
|
+
if (turnIsPlayed(game))
|
|
162
|
+
return human(side) ? say("played") : say("toPlay", { side: nameOf(side), dice: diceText(game.dice) });
|
|
163
|
+
return human(side) && isComputer(otherSide(side))
|
|
164
|
+
? say("toPlayYou", { dice: diceText(game.dice) })
|
|
165
|
+
: say("toPlay", { side: nameOf(side), dice: diceText(game.dice) });
|
|
166
|
+
};
|
|
167
|
+
const scoreText = () => {
|
|
168
|
+
const settings = input.settings();
|
|
169
|
+
const match = input.match();
|
|
170
|
+
const game = input.game();
|
|
171
|
+
const { rules } = settings;
|
|
172
|
+
const parts = [
|
|
173
|
+
say("score", { white: nameOf("white"), a: match.score[0], black: nameOf("black"), b: match.score[1] }),
|
|
174
|
+
rules.points === 0 ? say("money") : say("matchTo", { points: rules.points }),
|
|
175
|
+
];
|
|
176
|
+
if (game.crawford && game.phase !== "over")
|
|
177
|
+
parts.push(say("crawford"));
|
|
178
|
+
else if (hasCube())
|
|
179
|
+
parts.push(game.cube.owner === null
|
|
180
|
+
? say("cubeMiddle")
|
|
181
|
+
: say("cubeHeld", { value: game.cube.value, side: nameOf(game.cube.owner) }));
|
|
182
|
+
return parts.join(" · ");
|
|
183
|
+
};
|
|
184
|
+
const slots = () => {
|
|
185
|
+
const game = input.game();
|
|
186
|
+
const match = input.match();
|
|
187
|
+
const waitingForDice = input.waitingForDice();
|
|
188
|
+
const off = { text: "", enabled: false, run: () => undefined };
|
|
189
|
+
const side = game.turn;
|
|
190
|
+
const turnOfHuman = side !== null && human(side);
|
|
191
|
+
if (game.phase === "over") {
|
|
192
|
+
return [
|
|
193
|
+
match.over
|
|
194
|
+
? { text: say("newMatch"), enabled: true, run: () => input.actions().newMatch(), primary: true }
|
|
195
|
+
: { text: say("nextGame"), enabled: true, run: () => input.actions().nextGame(), primary: true },
|
|
196
|
+
match.over ? off : { text: say("newMatch"), enabled: true, run: () => input.actions().newMatch() },
|
|
197
|
+
off,
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
if (game.phase === "opening") {
|
|
201
|
+
const nobody = isComputer("white") && isComputer("black");
|
|
202
|
+
return [
|
|
203
|
+
{
|
|
204
|
+
text: say("rollOpening"),
|
|
205
|
+
enabled: !nobody && !waitingForDice,
|
|
206
|
+
run: () => input.actions().roll(),
|
|
207
|
+
primary: true,
|
|
208
|
+
attention: !nobody,
|
|
209
|
+
},
|
|
210
|
+
off,
|
|
211
|
+
off,
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
if (game.phase === "double-offered" || game.phase === "beaver-offered") {
|
|
215
|
+
const answerer = otherSide(game.offeredBy);
|
|
216
|
+
const mine = human(answerer);
|
|
217
|
+
return [
|
|
218
|
+
{ text: say("take"), enabled: mine, run: () => input.actions().take(), primary: true, attention: mine },
|
|
219
|
+
{ text: say("drop"), enabled: mine, run: () => input.actions().drop() },
|
|
220
|
+
{ text: say("beaver"), enabled: mine && canBeaver(game), run: () => input.actions().beaver() },
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
if (game.phase === "before-roll") {
|
|
224
|
+
return [
|
|
225
|
+
{
|
|
226
|
+
text: say("roll"),
|
|
227
|
+
enabled: turnOfHuman && !waitingForDice,
|
|
228
|
+
run: () => input.actions().roll(),
|
|
229
|
+
primary: true,
|
|
230
|
+
attention: turnOfHuman,
|
|
231
|
+
},
|
|
232
|
+
{ text: say("double"), enabled: turnOfHuman && canDouble(game), run: () => input.actions().double() },
|
|
233
|
+
{ text: say("undo"), enabled: false, run: () => undefined },
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
const played = turnIsPlayed(game);
|
|
237
|
+
return [
|
|
238
|
+
{
|
|
239
|
+
text: say("done"),
|
|
240
|
+
enabled: turnOfHuman && played,
|
|
241
|
+
run: () => input.actions().done(),
|
|
242
|
+
primary: true,
|
|
243
|
+
attention: turnOfHuman && played,
|
|
244
|
+
},
|
|
245
|
+
{ text: say("double"), enabled: false, run: () => undefined },
|
|
246
|
+
{ text: say("undo"), enabled: turnOfHuman && game.moves.length > 0, run: () => input.actions().undo() },
|
|
247
|
+
];
|
|
248
|
+
};
|
|
249
|
+
let handlers = [() => undefined, () => undefined, () => undefined];
|
|
250
|
+
const render = () => {
|
|
251
|
+
const game = input.game();
|
|
252
|
+
const look = input.look();
|
|
253
|
+
if (input.stopped())
|
|
254
|
+
return;
|
|
255
|
+
const width = host.clientWidth || 600;
|
|
256
|
+
portrait = look.orientation === "portrait" || (look.orientation !== "landscape" && width < 560);
|
|
257
|
+
const size = boardSize({ cube: hasCube(), orientation: portrait ? "portrait" : "landscape" });
|
|
258
|
+
boardBox.style.aspectRatio = `${size.width} / ${size.height}`;
|
|
259
|
+
boardBox.innerHTML = draw();
|
|
260
|
+
host.dataset.phase = game.phase;
|
|
261
|
+
host.dataset.turn = game.turn ?? "";
|
|
262
|
+
host.dataset.over = String(game.phase === "over");
|
|
263
|
+
host.dataset.orientation = portrait ? "portrait" : "landscape";
|
|
264
|
+
host.dataset.selected = selected === null ? "" : String(selected);
|
|
265
|
+
host.dataset.cube = String(game.cube.value);
|
|
266
|
+
host.dataset.need = game.need.join("");
|
|
267
|
+
host.dataset.ready = "true";
|
|
268
|
+
score.textContent = scoreText();
|
|
269
|
+
status.textContent = statusText();
|
|
270
|
+
const [a, b, c] = slots();
|
|
271
|
+
const buttons = [slotA, slotB, slotC];
|
|
272
|
+
[a, b, c].forEach((slot, index) => {
|
|
273
|
+
const button = buttons[index];
|
|
274
|
+
button.textContent = slot.text;
|
|
275
|
+
button.disabled = !slot.enabled;
|
|
276
|
+
button.dataset.primary = String(slot.primary === true);
|
|
277
|
+
button.dataset.attention = String(slot.attention === true);
|
|
278
|
+
button.style.visibility = slot.text === "" ? "hidden" : "visible";
|
|
279
|
+
});
|
|
280
|
+
handlers = [a.run, b.run, c.run];
|
|
281
|
+
};
|
|
282
|
+
slotA.addEventListener("click", () => handlers[0]());
|
|
283
|
+
slotB.addEventListener("click", () => handlers[1]());
|
|
284
|
+
slotC.addEventListener("click", () => handlers[2]());
|
|
285
|
+
// Touch and mouse.
|
|
286
|
+
const whereIs = (target) => {
|
|
287
|
+
const game = input.game();
|
|
288
|
+
const mover = game.turn;
|
|
289
|
+
if (mover === null || target === null)
|
|
290
|
+
return null;
|
|
291
|
+
const bar = target.closest("[data-bar]");
|
|
292
|
+
if (bar !== null)
|
|
293
|
+
return bar.dataset.bar === mover ? BAR : null;
|
|
294
|
+
const tray = target.closest("[data-tray]");
|
|
295
|
+
if (tray !== null)
|
|
296
|
+
return tray.dataset.tray === mover ? 0 : null;
|
|
297
|
+
const point = target.closest("[data-board]");
|
|
298
|
+
if (point === null || point.dataset.board === undefined)
|
|
299
|
+
return null;
|
|
300
|
+
return boardPoint(spec(), mover, Number(point.dataset.board));
|
|
301
|
+
};
|
|
302
|
+
const press = (own) => {
|
|
303
|
+
const moves = legalNow();
|
|
304
|
+
if (own === null) {
|
|
305
|
+
selected = null;
|
|
306
|
+
}
|
|
307
|
+
else if (selected !== null && moves.some((move) => move.from === selected && move.to === own)) {
|
|
308
|
+
makeMove({ from: selected, to: own });
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
else if (moves.some((move) => move.from === own)) {
|
|
312
|
+
selected = selected === own ? null : own;
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
selected = null;
|
|
316
|
+
}
|
|
317
|
+
render();
|
|
318
|
+
};
|
|
319
|
+
let drag = null;
|
|
320
|
+
const underPointer = (x, y) => {
|
|
321
|
+
const root = host.getRootNode();
|
|
322
|
+
return root.elementFromPoint?.(x, y) ?? null;
|
|
323
|
+
};
|
|
324
|
+
const sampleColour = (side) => {
|
|
325
|
+
const svg = boardBox.querySelector("svg");
|
|
326
|
+
const face = svg?.querySelector(`.sg-checker[data-side="${side}"] .sg-face`);
|
|
327
|
+
const style = face === null || face === undefined ? null : view.getComputedStyle(face);
|
|
328
|
+
return { fill: style?.fill ?? "#fff", edge: style?.stroke ?? "#000" };
|
|
329
|
+
};
|
|
330
|
+
const onDown = (event) => {
|
|
331
|
+
const game = input.game();
|
|
332
|
+
if (event.button !== 0 && event.pointerType === "mouse")
|
|
333
|
+
return;
|
|
334
|
+
if (game.phase !== "playing" || game.turn === null || !human(game.turn))
|
|
335
|
+
return;
|
|
336
|
+
const own = whereIs(event.target);
|
|
337
|
+
if (own === null)
|
|
338
|
+
return;
|
|
339
|
+
const moves = legalNow();
|
|
340
|
+
if (dragAllowed && moves.some((move) => move.from === own)) {
|
|
341
|
+
drag = { pointer: event.pointerId, from: own, x: event.clientX, y: event.clientY, active: false, ghost: null };
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const onMove = (event) => {
|
|
345
|
+
const game = input.game();
|
|
346
|
+
if (drag === null || event.pointerId !== drag.pointer)
|
|
347
|
+
return;
|
|
348
|
+
if (!drag.active) {
|
|
349
|
+
if (Math.hypot(event.clientX - drag.x, event.clientY - drag.y) < 8)
|
|
350
|
+
return;
|
|
351
|
+
drag.active = true;
|
|
352
|
+
selected = drag.from;
|
|
353
|
+
render();
|
|
354
|
+
const svg = boardBox.querySelector("svg");
|
|
355
|
+
const rect = svg?.getBoundingClientRect();
|
|
356
|
+
const viewWidth = svg?.viewBox.baseVal.width ?? 1;
|
|
357
|
+
const scale = rect === undefined ? 1 : rect.width / viewWidth;
|
|
358
|
+
const side = game.turn;
|
|
359
|
+
const colours = sampleColour(side);
|
|
360
|
+
const ghost = create(document, "div", "sgp-ghost");
|
|
361
|
+
const diameter = CHECKER * scale;
|
|
362
|
+
ghost.style.width = `${diameter}px`;
|
|
363
|
+
ghost.style.height = `${diameter}px`;
|
|
364
|
+
ghost.style.background = colours.fill;
|
|
365
|
+
ghost.style.borderColor = colours.edge;
|
|
366
|
+
host.append(ghost);
|
|
367
|
+
drag.ghost = ghost;
|
|
368
|
+
try {
|
|
369
|
+
boardBox.setPointerCapture(event.pointerId);
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
/* Capture is a nicety. */
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (drag.ghost !== null) {
|
|
376
|
+
const w = drag.ghost.offsetWidth;
|
|
377
|
+
drag.ghost.style.transform = `translate(${event.clientX - w / 2}px, ${event.clientY - w / 2}px)`;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
const onUp = (event) => {
|
|
381
|
+
if (drag === null || event.pointerId !== drag.pointer) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const finished = drag;
|
|
385
|
+
drag = null;
|
|
386
|
+
finished.ghost?.remove();
|
|
387
|
+
try {
|
|
388
|
+
boardBox.releasePointerCapture(event.pointerId);
|
|
389
|
+
}
|
|
390
|
+
catch {
|
|
391
|
+
/* Already released. */
|
|
392
|
+
}
|
|
393
|
+
if (!finished.active)
|
|
394
|
+
return;
|
|
395
|
+
const to = whereIs(underPointer(event.clientX, event.clientY));
|
|
396
|
+
if (to !== null && makeMove({ from: finished.from, to }))
|
|
397
|
+
return;
|
|
398
|
+
selected = null;
|
|
399
|
+
render();
|
|
400
|
+
};
|
|
401
|
+
const onCancel = (event) => {
|
|
402
|
+
if (drag === null || event.pointerId !== drag.pointer)
|
|
403
|
+
return;
|
|
404
|
+
drag.ghost?.remove();
|
|
405
|
+
const wasActive = drag.active;
|
|
406
|
+
drag = null;
|
|
407
|
+
if (wasActive) {
|
|
408
|
+
selected = null;
|
|
409
|
+
render();
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
/** A tap or a click: the drag handlers deal with a drag, and a click that follows one is ignored. */
|
|
413
|
+
let lastDragEnd = 0;
|
|
414
|
+
const onClick = (event) => {
|
|
415
|
+
const game = input.game();
|
|
416
|
+
if (Date.now() - lastDragEnd < 60)
|
|
417
|
+
return;
|
|
418
|
+
if (game.phase !== "playing" || game.turn === null || !human(game.turn))
|
|
419
|
+
return;
|
|
420
|
+
press(whereIs(event.target));
|
|
421
|
+
};
|
|
422
|
+
boardBox.addEventListener("pointerdown", onDown);
|
|
423
|
+
boardBox.addEventListener("pointermove", onMove);
|
|
424
|
+
boardBox.addEventListener("pointerup", (event) => {
|
|
425
|
+
const wasDrag = drag?.active === true;
|
|
426
|
+
onUp(event);
|
|
427
|
+
if (wasDrag)
|
|
428
|
+
lastDragEnd = Date.now();
|
|
429
|
+
});
|
|
430
|
+
boardBox.addEventListener("pointercancel", onCancel);
|
|
431
|
+
boardBox.addEventListener("click", onClick);
|
|
432
|
+
// Following the page: its width, and its language.
|
|
433
|
+
const resize = typeof view.ResizeObserver === "undefined"
|
|
434
|
+
? null
|
|
435
|
+
: new view.ResizeObserver(() => {
|
|
436
|
+
const look = input.look();
|
|
437
|
+
const wide = look.orientation === "portrait" || (look.orientation !== "landscape" && (host.clientWidth || 600) < 560);
|
|
438
|
+
if (wide !== portrait)
|
|
439
|
+
render();
|
|
440
|
+
});
|
|
441
|
+
resize?.observe(host);
|
|
442
|
+
const watch = typeof view.MutationObserver === "undefined"
|
|
443
|
+
? null
|
|
444
|
+
: new view.MutationObserver(() => {
|
|
445
|
+
if (input.followsPageLanguage())
|
|
446
|
+
render();
|
|
447
|
+
});
|
|
448
|
+
watch?.observe(document.documentElement, { attributes: true, attributeFilter: ["lang"] });
|
|
449
|
+
const resetPointer = () => {
|
|
450
|
+
selected = null;
|
|
451
|
+
message = null;
|
|
452
|
+
};
|
|
453
|
+
const disconnect = () => {
|
|
454
|
+
resize?.disconnect();
|
|
455
|
+
watch?.disconnect();
|
|
456
|
+
drag?.ghost?.remove();
|
|
457
|
+
};
|
|
458
|
+
return { render, resetPointer, disconnect };
|
|
459
|
+
}
|
package/dist/notation.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BAR, POINTS } from "./board.js";
|
|
2
|
-
import { diceMustPlay, diceToPlay, nextMoves } from "./plays.js";
|
|
3
2
|
import { applyMove } from "./moves.js";
|
|
3
|
+
import { diceMustPlay, diceToPlay, nextMoves } from "./plays.js";
|
|
4
4
|
import { sideIndex } from "./side.js";
|
|
5
5
|
/**
|
|
6
6
|
* MOVES AS TEXT, the way backgammon writes them: each checker as the point it
|
|
@@ -57,7 +57,11 @@ export function formatPlay(moves) {
|
|
|
57
57
|
const texts = paths.map((path) => {
|
|
58
58
|
const first = path[0];
|
|
59
59
|
const steps = path.map((move) => `${point(move.to)}${move.hit ? "*" : ""}`);
|
|
60
|
-
return {
|
|
60
|
+
return {
|
|
61
|
+
start: first.from,
|
|
62
|
+
end: path[path.length - 1].to,
|
|
63
|
+
text: `${point(first.from)}/${steps.join("/")}`,
|
|
64
|
+
};
|
|
61
65
|
});
|
|
62
66
|
texts.sort((a, b) => b.start - a.start || a.end - b.end || (a.text < b.text ? -1 : a.text > b.text ? 1 : 0));
|
|
63
67
|
const counted = [];
|
package/dist/play-entry.d.ts
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* either side or both, optional sounds, and the words in English and Japanese. A
|
|
5
5
|
* separate entry (`@johnmorrisdotca/sugoroku/play`), so a server never loads any of it.
|
|
6
6
|
*/
|
|
7
|
-
export { ensureSugorokuPlayStyle, mountSugoroku } from "./mount.ts";
|
|
8
7
|
export type { SugorokuEventDetail, SugorokuLook, SugorokuMount, SugorokuMountOptions } from "./mount.ts";
|
|
8
|
+
export { ensureSugorokuPlayStyle, mountSugoroku } from "./mount.ts";
|
|
9
9
|
export { SUGOROKU_PLAY_STYLE } from "./playStyle.ts";
|
|
10
|
-
export { SUGOROKU_STRINGS, sugorokuLanguageOf, sugorokuSay } from "./strings.ts";
|
|
11
|
-
export type { SugorokuLanguage } from "./strings.ts";
|
|
12
|
-
export { createSounds, packagedSounds } from "./sound.ts";
|
|
13
10
|
export type { SoundName, SoundPlayer, SoundUrls } from "./sound.ts";
|
|
11
|
+
export { createSounds, packagedSounds } from "./sound.ts";
|
|
12
|
+
export type { SugorokuLanguage } from "./strings.ts";
|
|
13
|
+
export { SUGOROKU_STRINGS, sugorokuLanguageOf, sugorokuSay } from "./strings.ts";
|
package/dist/play-entry.js
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export { ensureSugorokuPlayStyle, mountSugoroku } from "./mount.js";
|
|
8
8
|
export { SUGOROKU_PLAY_STYLE } from "./playStyle.js";
|
|
9
|
-
export { SUGOROKU_STRINGS, sugorokuLanguageOf, sugorokuSay } from "./strings.js";
|
|
10
9
|
export { createSounds, packagedSounds } from "./sound.js";
|
|
10
|
+
export { SUGOROKU_STRINGS, sugorokuLanguageOf, sugorokuSay } from "./strings.js";
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { type Position } from "./board.ts";
|
|
2
|
+
import { type SugorokuDrawOptions } from "./draw.ts";
|
|
3
|
+
import { SugorokuFrame } from "./elementFrame.ts";
|
|
4
|
+
import type { Side } from "./side.ts";
|
|
5
|
+
import { type VariantKey } from "./variants.ts";
|
|
6
|
+
/**
|
|
7
|
+
* Reads a GNU position ID or bounded JSON position; null means invalid counts or input.
|
|
8
|
+
* @param text - Position ID, JSON position, or null for the variant's start.
|
|
9
|
+
* @param variant - The native variant used for checker counts and layout.
|
|
10
|
+
* @param turn - The on-roll side encoded by a GNU ID.
|
|
11
|
+
* @returns A complete position, or null for malformed or inconsistent input.
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { readEmbedPosition } from "@johnmorrisdotca/sugoroku/element";
|
|
15
|
+
* console.log(readEmbedPosition("4HPwATDgc/ABMA")?.points[0].length);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function readEmbedPosition(text: string | null, variant?: VariantKey, turn?: Side): Position | null;
|
|
19
|
+
/** Host data and all native SVG drawing choices for a position.
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import type { SugorokuPositionOptions } from "@johnmorrisdotca/sugoroku/element";
|
|
23
|
+
* const options: SugorokuPositionOptions = {};
|
|
24
|
+
* console.log(Object.keys(options).length); // 0
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export type SugorokuPositionOptions = SugorokuDrawOptions & {
|
|
28
|
+
position?: Position;
|
|
29
|
+
onChange?: (position: Position) => void;
|
|
30
|
+
};
|
|
31
|
+
/** Native draw keys, checked by the shared builder.
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { SUGOROKU_DRAW_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
|
|
35
|
+
* console.log(typeof SUGOROKU_DRAW_OPTION_KEYS); // object
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare const SUGOROKU_DRAW_OPTION_KEYS: string[];
|
|
39
|
+
/** All host position options, checked by the builder.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { SUGOROKU_POSITION_OPTION_KEYS } from "@johnmorrisdotca/sugoroku/element";
|
|
43
|
+
* console.log(typeof SUGOROKU_POSITION_OPTION_KEYS); // object
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare const SUGOROKU_POSITION_OPTION_KEYS: string[];
|
|
47
|
+
/** A read-only backgammon position, drawn by the native SVG writer.
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { SugorokuPosition } from "@johnmorrisdotca/sugoroku/element";
|
|
51
|
+
* console.log(SugorokuPosition.observedAttributes.includes("lang")); // true
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare class SugorokuPosition extends SugorokuFrame {
|
|
55
|
+
#private;
|
|
56
|
+
static observedAttributes: string[];
|
|
57
|
+
/** Structured current position, or null after an input error. */
|
|
58
|
+
get value(): Position | null;
|
|
59
|
+
/** Native drawing options and trusted host position data. */
|
|
60
|
+
get options(): SugorokuPositionOptions;
|
|
61
|
+
set options(value: SugorokuPositionOptions);
|
|
62
|
+
connectedCallback(): void;
|
|
63
|
+
disconnectedCallback(): void;
|
|
64
|
+
attributeChangedCallback(): void;
|
|
65
|
+
/** Position ID or JSON, reflected. Assign an object to serialize it. */
|
|
66
|
+
get position(): string | null;
|
|
67
|
+
set position(value: string | Position | null);
|
|
68
|
+
/** The `variant` attribute, reflected. */
|
|
69
|
+
get variant(): string | null;
|
|
70
|
+
set variant(value: string | number | boolean | null | undefined);
|
|
71
|
+
/** The `turn` attribute, reflected. */
|
|
72
|
+
get turn(): string | null;
|
|
73
|
+
set turn(value: string | number | boolean | null | undefined);
|
|
74
|
+
/** The `board` attribute, reflected. */
|
|
75
|
+
get board(): string | null;
|
|
76
|
+
set board(value: string | number | boolean | null | undefined);
|
|
77
|
+
/** The `checkers` attribute, reflected. */
|
|
78
|
+
get checkers(): string | null;
|
|
79
|
+
set checkers(value: string | number | boolean | null | undefined);
|
|
80
|
+
/** The `theme` attribute, reflected. */
|
|
81
|
+
get theme(): string | null;
|
|
82
|
+
set theme(value: string | number | boolean | null | undefined);
|
|
83
|
+
/** The `home` attribute, reflected. */
|
|
84
|
+
get home(): string | null;
|
|
85
|
+
set home(value: string | number | boolean | null | undefined);
|
|
86
|
+
/** The `view` attribute, reflected. */
|
|
87
|
+
get view(): string | null;
|
|
88
|
+
set view(value: string | number | boolean | null | undefined);
|
|
89
|
+
/** The `numbers` attribute, reflected. */
|
|
90
|
+
get numbers(): string | null;
|
|
91
|
+
set numbers(value: string | number | boolean | null | undefined);
|
|
92
|
+
/** The `orientation` attribute, reflected. */
|
|
93
|
+
get orientation(): string | null;
|
|
94
|
+
set orientation(value: string | number | boolean | null | undefined);
|
|
95
|
+
}
|
|
96
|
+
/** Defines a static position, or a subclass under a host's tag. Safe on a server.
|
|
97
|
+
* @param tagName - The custom tag name; omitted to use the package default.
|
|
98
|
+
* @param registry - The custom-element registry; omitted outside a browser.
|
|
99
|
+
* @returns Nothing; existing definitions and server imports remain safe.
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* import { definePosition } from "@johnmorrisdotca/sugoroku/element";
|
|
103
|
+
* definePosition();
|
|
104
|
+
* console.log(typeof definePosition); // function
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function definePosition(tagName?: string, registry?: CustomElementRegistry | undefined): void;
|