@johnmorrisdotca/sugoroku 1.1.0 → 1.2.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/CHANGELOG.md +20 -0
- package/README.md +6 -4
- 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-fr.data.d.ts +2 -0
- package/dist/strings-fr.data.js +60 -0
- package/dist/strings.d.ts +4 -4
- package/dist/strings.js +10 -5
- package/dist/variants.js +19 -2
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +11 -11
package/dist/mount.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import { BAR, boardPoint } from "./board.js";
|
|
2
1
|
import { computerAction, sideToAct } from "./computer.js";
|
|
3
2
|
import { openingFrom, randomDice, rollFrom, seededDice } from "./dice.js";
|
|
4
|
-
import { drawSugoroku } from "./draw.js";
|
|
5
|
-
import { boardSize, CHECKER } from "./geometry.js";
|
|
6
3
|
import { beaverDouble, canBeaver, canDouble, concede, dropDouble, endTurn, legalMoves, offerDouble, playMove, rollDice, rollOpening, takeDouble, turnIsPlayed, undoMove, } from "./game.js";
|
|
7
4
|
import { finishGame, newMatch, startGame } from "./match.js";
|
|
5
|
+
import { attachSugorokuView } from "./mountView.js";
|
|
8
6
|
import { formatMove, formatPlay } from "./notation.js";
|
|
9
|
-
import { diceToPlay } from "./plays.js";
|
|
10
7
|
import { SUGOROKU_PLAY_STYLE } from "./playStyle.js";
|
|
11
8
|
import { GameRecorder } from "./record.js";
|
|
12
9
|
import { presetByKey, settingsFor } from "./rules.js";
|
|
13
|
-
import { otherSide
|
|
10
|
+
import { otherSide } from "./side.js";
|
|
14
11
|
import { createSounds, packagedSounds } from "./sound.js";
|
|
15
12
|
import { sugorokuLanguageOf, sugorokuSay } from "./strings.js";
|
|
16
13
|
/** Put the style in the page once: in the document's head, or in the shadow root the host is in.
|
|
@@ -73,7 +70,16 @@ export function mountSugoroku(host, options = {}) {
|
|
|
73
70
|
let recorder = new GameRecorder(settings, seed);
|
|
74
71
|
recorder.startGame();
|
|
75
72
|
// What may change while it is mounted.
|
|
76
|
-
let look = {
|
|
73
|
+
let look = {
|
|
74
|
+
board: options.board,
|
|
75
|
+
checkers: options.checkers,
|
|
76
|
+
colours: options.colours,
|
|
77
|
+
theme: options.theme,
|
|
78
|
+
home: options.home,
|
|
79
|
+
view: options.view,
|
|
80
|
+
numbers: options.numbers,
|
|
81
|
+
orientation: options.orientation,
|
|
82
|
+
};
|
|
77
83
|
let computer = { white: options.computer?.white ?? null, black: options.computer?.black ?? null };
|
|
78
84
|
let language = options.language;
|
|
79
85
|
let names = { ...options.names };
|
|
@@ -81,7 +87,7 @@ export function mountSugoroku(host, options = {}) {
|
|
|
81
87
|
const hostDice = options.dice === "host";
|
|
82
88
|
const reducedMotion = view.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
|
|
83
89
|
const delay = options.computerDelay ?? (reducedMotion ? 0 : 450);
|
|
84
|
-
const soundsFor = (setting) =>
|
|
90
|
+
const soundsFor = (setting) => setting === undefined || setting === false ? null : createSounds(setting === true ? packagedSounds() : setting);
|
|
85
91
|
let sounds = soundsFor(options.sound);
|
|
86
92
|
// The page's own parts.
|
|
87
93
|
host.classList.add("sugoroku-play");
|
|
@@ -102,14 +108,11 @@ export function mountSugoroku(host, options = {}) {
|
|
|
102
108
|
controls.append(slotA, slotB, slotC);
|
|
103
109
|
controls.hidden = options.controls === false;
|
|
104
110
|
host.replaceChildren(score, boardBox, status, controls);
|
|
105
|
-
let selected = null;
|
|
106
111
|
let timer = null;
|
|
107
112
|
let queue = [];
|
|
108
113
|
let gone = false;
|
|
109
|
-
let portrait = false;
|
|
110
114
|
let waitingForDice = false;
|
|
111
115
|
let lastOpening = null;
|
|
112
|
-
let message = null;
|
|
113
116
|
const lang = () => language ?? sugorokuLanguageOf(host);
|
|
114
117
|
const say = (key, values = {}) => sugorokuSay(lang(), key, values);
|
|
115
118
|
const isComputer = (side) => computer[side] !== null;
|
|
@@ -124,190 +127,20 @@ export function mountSugoroku(host, options = {}) {
|
|
|
124
127
|
return say("you");
|
|
125
128
|
return say(side);
|
|
126
129
|
};
|
|
127
|
-
const eventDetail = (side, extra = {}) => ({
|
|
130
|
+
const eventDetail = (side, extra = {}) => ({
|
|
131
|
+
side,
|
|
132
|
+
game,
|
|
133
|
+
match,
|
|
134
|
+
...extra,
|
|
135
|
+
});
|
|
128
136
|
const emit = (name, detail, callback) => {
|
|
129
137
|
host.dispatchEvent(new view.CustomEvent(name, { detail: JSON.parse(JSON.stringify(detail)), bubbles: true, composed: true }));
|
|
130
138
|
callback?.(detail);
|
|
131
139
|
};
|
|
132
140
|
const spec = () => game.settings.variant;
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (look.view !== undefined && look.view !== "auto")
|
|
137
|
-
return look.view;
|
|
138
|
-
if (isComputer("white") && human("black"))
|
|
139
|
-
return "black";
|
|
140
|
-
return "white";
|
|
141
|
-
};
|
|
142
|
-
// Drawing.
|
|
143
|
-
const diceShown = () => {
|
|
144
|
-
if (game.phase === "opening") {
|
|
145
|
-
return lastOpening === null ? null : { side: "white", values: [lastOpening[0], lastOpening[1]], spent: [false, false] };
|
|
146
|
-
}
|
|
147
|
-
if (game.dice === null || game.turn === null)
|
|
148
|
-
return null;
|
|
149
|
-
const values = diceToPlay(spec(), game.dice);
|
|
150
|
-
const unused = [...game.need];
|
|
151
|
-
const planLeft = [...game.plan];
|
|
152
|
-
const spent = values.map((value) => {
|
|
153
|
-
const inPlan = planLeft.indexOf(value);
|
|
154
|
-
if (inPlan === -1)
|
|
155
|
-
return true;
|
|
156
|
-
planLeft.splice(inPlan, 1);
|
|
157
|
-
const open = unused.indexOf(value);
|
|
158
|
-
if (open === -1)
|
|
159
|
-
return true;
|
|
160
|
-
unused.splice(open, 1);
|
|
161
|
-
return false;
|
|
162
|
-
});
|
|
163
|
-
return { side: game.turn, values, spent };
|
|
164
|
-
};
|
|
165
|
-
const legalNow = () => (game.phase === "playing" && game.turn !== null && human(game.turn) ? legalMoves(game) : []);
|
|
166
|
-
const draw = () => {
|
|
167
|
-
const moves = legalNow();
|
|
168
|
-
const mover = game.turn;
|
|
169
|
-
const sources = [...new Set(moves.map((move) => move.from))];
|
|
170
|
-
const targets = selected === null ? [] : [...new Set(moves.filter((move) => move.from === selected).map((move) => move.to))];
|
|
171
|
-
return drawSugoroku(game.position, {
|
|
172
|
-
variant: spec(),
|
|
173
|
-
view: viewSide(),
|
|
174
|
-
home: look.home,
|
|
175
|
-
orientation: portrait ? "portrait" : "landscape",
|
|
176
|
-
numbers: look.numbers,
|
|
177
|
-
board: look.board,
|
|
178
|
-
checkers: look.checkers,
|
|
179
|
-
colours: look.colours,
|
|
180
|
-
theme: look.theme,
|
|
181
|
-
dice: diceShown(),
|
|
182
|
-
cube: hasCube() ? { value: game.cube.value, owner: game.cube.owner } : null,
|
|
183
|
-
highlight: mover === null ? null : { side: mover, from: selected, targets, movable: sources },
|
|
184
|
-
label: say("board", { variant: say(`variant_${spec().key}`) }),
|
|
185
|
-
});
|
|
186
|
-
};
|
|
187
|
-
// The words.
|
|
188
|
-
const pointsText = (count) => say(count === 1 ? "point" : "points", { n: count });
|
|
189
|
-
const diceText = (dice) => (dice === null ? "" : diceToPlay(spec(), dice).join(" "));
|
|
190
|
-
const statusText = () => {
|
|
191
|
-
if (message !== null)
|
|
192
|
-
return message;
|
|
193
|
-
const side = game.turn;
|
|
194
|
-
if (game.phase === "opening")
|
|
195
|
-
return waitingForDice ? say("waitingDice") : lastOpening !== null && lastOpening[0] === lastOpening[1] ? say("openingTie", { die: lastOpening[0] }) : say("opening");
|
|
196
|
-
if (game.phase === "over") {
|
|
197
|
-
const result = game.result;
|
|
198
|
-
let line;
|
|
199
|
-
if (result.winner === null)
|
|
200
|
-
line = say("drawn");
|
|
201
|
-
else if (result.how === "drop")
|
|
202
|
-
line = say("wonDrop", { side: nameOf(result.winner), points: pointsText(result.points) });
|
|
203
|
-
else if (result.how === "concede")
|
|
204
|
-
line = say("wonConcede", { side: nameOf(result.winner), points: pointsText(result.points) });
|
|
205
|
-
else
|
|
206
|
-
line = say("wonBy", { side: nameOf(result.winner), kind: say(result.kind === "single" ? "singleGame" : result.kind), points: pointsText(result.points) });
|
|
207
|
-
if (match.over)
|
|
208
|
-
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))] })}`;
|
|
209
|
-
return line;
|
|
210
|
-
}
|
|
211
|
-
if (side === null)
|
|
212
|
-
return "";
|
|
213
|
-
if (game.phase === "double-offered" || game.phase === "beaver-offered") {
|
|
214
|
-
const offerer = game.offeredBy;
|
|
215
|
-
const value = game.phase === "double-offered" ? game.cube.value * 2 : game.cube.value * 4;
|
|
216
|
-
const lose = pointsText(game.phase === "double-offered" ? game.cube.value : game.cube.value * 2);
|
|
217
|
-
if (human(otherSide(offerer)))
|
|
218
|
-
return say(game.phase === "double-offered" ? "answerDouble" : "answerBeaver", { side: nameOf(offerer), value, points: lose });
|
|
219
|
-
return say("thinking", { side: nameOf(otherSide(offerer)) });
|
|
220
|
-
}
|
|
221
|
-
if (game.phase === "before-roll") {
|
|
222
|
-
if (isComputer(side))
|
|
223
|
-
return say("thinking", { side: nameOf(side) });
|
|
224
|
-
return isComputer(otherSide(side)) ? say("toRollYou") : say("toRoll", { side: nameOf(side) });
|
|
225
|
-
}
|
|
226
|
-
if (isComputer(side) && game.moves.length === 0 && game.need.length > 0)
|
|
227
|
-
return say("thinking", { side: nameOf(side) });
|
|
228
|
-
if (game.plan.length === 0)
|
|
229
|
-
return say(human(side) && isComputer(otherSide(side)) ? "noMoveYou" : "noMove", { side: nameOf(side) });
|
|
230
|
-
if (turnIsPlayed(game))
|
|
231
|
-
return human(side) ? say("played") : say("toPlay", { side: nameOf(side), dice: diceText(game.dice) });
|
|
232
|
-
return human(side) && isComputer(otherSide(side)) ? say("toPlayYou", { dice: diceText(game.dice) }) : say("toPlay", { side: nameOf(side), dice: diceText(game.dice) });
|
|
233
|
-
};
|
|
234
|
-
const scoreText = () => {
|
|
235
|
-
const { rules } = settings;
|
|
236
|
-
const parts = [say("score", { white: nameOf("white"), a: match.score[0], black: nameOf("black"), b: match.score[1] }), rules.points === 0 ? say("money") : say("matchTo", { points: rules.points })];
|
|
237
|
-
if (game.crawford && game.phase !== "over")
|
|
238
|
-
parts.push(say("crawford"));
|
|
239
|
-
else if (hasCube())
|
|
240
|
-
parts.push(game.cube.owner === null ? say("cubeMiddle") : say("cubeHeld", { value: game.cube.value, side: nameOf(game.cube.owner) }));
|
|
241
|
-
return parts.join(" · ");
|
|
242
|
-
};
|
|
243
|
-
const slots = () => {
|
|
244
|
-
const off = { text: "", enabled: false, run: () => undefined };
|
|
245
|
-
const side = game.turn;
|
|
246
|
-
const turnOfHuman = side !== null && human(side);
|
|
247
|
-
if (game.phase === "over") {
|
|
248
|
-
return [match.over ? { text: say("newMatch"), enabled: true, run: () => api.newMatch(), primary: true } : { text: say("nextGame"), enabled: true, run: () => api.nextGame(), primary: true }, match.over ? off : { text: say("newMatch"), enabled: true, run: () => api.newMatch() }, off];
|
|
249
|
-
}
|
|
250
|
-
if (game.phase === "opening") {
|
|
251
|
-
const nobody = isComputer("white") && isComputer("black");
|
|
252
|
-
return [{ text: say("rollOpening"), enabled: !nobody && !waitingForDice, run: () => api.roll(), primary: true, attention: !nobody }, off, off];
|
|
253
|
-
}
|
|
254
|
-
if (game.phase === "double-offered" || game.phase === "beaver-offered") {
|
|
255
|
-
const answerer = otherSide(game.offeredBy);
|
|
256
|
-
const mine = human(answerer);
|
|
257
|
-
return [
|
|
258
|
-
{ text: say("take"), enabled: mine, run: () => api.take(), primary: true, attention: mine },
|
|
259
|
-
{ text: say("drop"), enabled: mine, run: () => api.drop() },
|
|
260
|
-
{ text: say("beaver"), enabled: mine && canBeaver(game), run: () => api.beaver() },
|
|
261
|
-
];
|
|
262
|
-
}
|
|
263
|
-
if (game.phase === "before-roll") {
|
|
264
|
-
return [
|
|
265
|
-
{ text: say("roll"), enabled: turnOfHuman && !waitingForDice, run: () => api.roll(), primary: true, attention: turnOfHuman },
|
|
266
|
-
{ text: say("double"), enabled: turnOfHuman && canDouble(game), run: () => api.double() },
|
|
267
|
-
{ text: say("undo"), enabled: false, run: () => undefined },
|
|
268
|
-
];
|
|
269
|
-
}
|
|
270
|
-
const played = turnIsPlayed(game);
|
|
271
|
-
return [
|
|
272
|
-
{ text: say("done"), enabled: turnOfHuman && played, run: () => api.done(), primary: true, attention: turnOfHuman && played },
|
|
273
|
-
{ text: say("double"), enabled: false, run: () => undefined },
|
|
274
|
-
{ text: say("undo"), enabled: turnOfHuman && game.moves.length > 0, run: () => api.undo() },
|
|
275
|
-
];
|
|
276
|
-
};
|
|
277
|
-
let handlers = [() => undefined, () => undefined, () => undefined];
|
|
278
|
-
const render = () => {
|
|
279
|
-
if (gone)
|
|
280
|
-
return;
|
|
281
|
-
const width = host.clientWidth || 600;
|
|
282
|
-
portrait = look.orientation === "portrait" || (look.orientation !== "landscape" && width < 560);
|
|
283
|
-
const size = boardSize({ cube: hasCube(), orientation: portrait ? "portrait" : "landscape" });
|
|
284
|
-
boardBox.style.aspectRatio = `${size.width} / ${size.height}`;
|
|
285
|
-
boardBox.innerHTML = draw();
|
|
286
|
-
host.dataset.phase = game.phase;
|
|
287
|
-
host.dataset.turn = game.turn ?? "";
|
|
288
|
-
host.dataset.over = String(game.phase === "over");
|
|
289
|
-
host.dataset.orientation = portrait ? "portrait" : "landscape";
|
|
290
|
-
host.dataset.selected = selected === null ? "" : String(selected);
|
|
291
|
-
host.dataset.cube = String(game.cube.value);
|
|
292
|
-
host.dataset.need = game.need.join("");
|
|
293
|
-
host.dataset.ready = "true";
|
|
294
|
-
score.textContent = scoreText();
|
|
295
|
-
status.textContent = statusText();
|
|
296
|
-
const [a, b, c] = slots();
|
|
297
|
-
const buttons = [slotA, slotB, slotC];
|
|
298
|
-
[a, b, c].forEach((slot, index) => {
|
|
299
|
-
const button = buttons[index];
|
|
300
|
-
button.textContent = slot.text;
|
|
301
|
-
button.disabled = !slot.enabled;
|
|
302
|
-
button.dataset.primary = String(slot.primary === true);
|
|
303
|
-
button.dataset.attention = String(slot.attention === true);
|
|
304
|
-
button.style.visibility = slot.text === "" ? "hidden" : "visible";
|
|
305
|
-
});
|
|
306
|
-
handlers = [a.run, b.run, c.run];
|
|
307
|
-
};
|
|
308
|
-
slotA.addEventListener("click", () => handlers[0]());
|
|
309
|
-
slotB.addEventListener("click", () => handlers[1]());
|
|
310
|
-
slotC.addEventListener("click", () => handlers[2]());
|
|
141
|
+
let render = () => undefined;
|
|
142
|
+
let resetPointer = () => undefined;
|
|
143
|
+
let disconnectView = () => undefined;
|
|
311
144
|
const play = (name) => sounds?.play(name);
|
|
312
145
|
// The game moving on.
|
|
313
146
|
const startComputer = () => {
|
|
@@ -336,11 +169,14 @@ export function mountSugoroku(host, options = {}) {
|
|
|
336
169
|
};
|
|
337
170
|
const setGame = (next) => {
|
|
338
171
|
game = next;
|
|
339
|
-
|
|
340
|
-
message = null;
|
|
172
|
+
resetPointer();
|
|
341
173
|
finishIfOver();
|
|
342
174
|
render();
|
|
343
|
-
if (options.autoEnd === true &&
|
|
175
|
+
if (options.autoEnd === true &&
|
|
176
|
+
game.phase === "playing" &&
|
|
177
|
+
turnIsPlayed(game) &&
|
|
178
|
+
game.turn !== null &&
|
|
179
|
+
human(game.turn)) {
|
|
344
180
|
timer = setTimeout(endNow, delay === 0 ? 0 : 250);
|
|
345
181
|
return;
|
|
346
182
|
}
|
|
@@ -396,7 +232,9 @@ export function mountSugoroku(host, options = {}) {
|
|
|
396
232
|
if (game.phase !== "playing" || game.turn === null)
|
|
397
233
|
return false;
|
|
398
234
|
const side = game.turn;
|
|
399
|
-
const move = legalMoves(game)
|
|
235
|
+
const move = legalMoves(game)
|
|
236
|
+
.filter((one) => one.from === step.from && one.to === step.to)
|
|
237
|
+
.sort((a, b) => a.die - b.die)[0];
|
|
400
238
|
if (move === undefined)
|
|
401
239
|
return false;
|
|
402
240
|
const next = playMove(game, move);
|
|
@@ -418,8 +256,20 @@ export function mountSugoroku(host, options = {}) {
|
|
|
418
256
|
setGame(next);
|
|
419
257
|
};
|
|
420
258
|
const cubeAction = (action, side) => {
|
|
421
|
-
const next = action === "double"
|
|
422
|
-
|
|
259
|
+
const next = action === "double"
|
|
260
|
+
? offerDouble(game)
|
|
261
|
+
: action === "take"
|
|
262
|
+
? takeDouble(game)
|
|
263
|
+
: action === "drop"
|
|
264
|
+
? dropDouble(game)
|
|
265
|
+
: beaverDouble(game);
|
|
266
|
+
(action === "double"
|
|
267
|
+
? recorder.double
|
|
268
|
+
: action === "take"
|
|
269
|
+
? recorder.take
|
|
270
|
+
: action === "drop"
|
|
271
|
+
? recorder.drop
|
|
272
|
+
: recorder.beaver).call(recorder, side);
|
|
423
273
|
play("cube");
|
|
424
274
|
emit("sugoroku-cube", eventDetail(side, { action, game: next }), options.onCube);
|
|
425
275
|
setGame(next);
|
|
@@ -468,161 +318,6 @@ export function mountSugoroku(host, options = {}) {
|
|
|
468
318
|
break;
|
|
469
319
|
}
|
|
470
320
|
};
|
|
471
|
-
// Touch and mouse.
|
|
472
|
-
const whereIs = (target) => {
|
|
473
|
-
const mover = game.turn;
|
|
474
|
-
if (mover === null || target === null)
|
|
475
|
-
return null;
|
|
476
|
-
const bar = target.closest("[data-bar]");
|
|
477
|
-
if (bar !== null)
|
|
478
|
-
return bar.dataset.bar === mover ? BAR : null;
|
|
479
|
-
const tray = target.closest("[data-tray]");
|
|
480
|
-
if (tray !== null)
|
|
481
|
-
return tray.dataset.tray === mover ? 0 : null;
|
|
482
|
-
const point = target.closest("[data-board]");
|
|
483
|
-
if (point === null || point.dataset.board === undefined)
|
|
484
|
-
return null;
|
|
485
|
-
return boardPoint(spec(), mover, Number(point.dataset.board));
|
|
486
|
-
};
|
|
487
|
-
const press = (own) => {
|
|
488
|
-
const moves = legalNow();
|
|
489
|
-
if (own === null) {
|
|
490
|
-
selected = null;
|
|
491
|
-
}
|
|
492
|
-
else if (selected !== null && moves.some((move) => move.from === selected && move.to === own)) {
|
|
493
|
-
makeMove({ from: selected, to: own });
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
else if (moves.some((move) => move.from === own)) {
|
|
497
|
-
selected = selected === own ? null : own;
|
|
498
|
-
}
|
|
499
|
-
else {
|
|
500
|
-
selected = null;
|
|
501
|
-
}
|
|
502
|
-
render();
|
|
503
|
-
};
|
|
504
|
-
let drag = null;
|
|
505
|
-
const underPointer = (x, y) => {
|
|
506
|
-
const root = host.getRootNode();
|
|
507
|
-
return root.elementFromPoint?.(x, y) ?? null;
|
|
508
|
-
};
|
|
509
|
-
const sampleColour = (side) => {
|
|
510
|
-
const svg = boardBox.querySelector("svg");
|
|
511
|
-
const face = svg?.querySelector(`.sg-checker[data-side="${side}"] .sg-face`);
|
|
512
|
-
const style = face === null || face === undefined ? null : view.getComputedStyle(face);
|
|
513
|
-
return { fill: style?.fill ?? "#fff", edge: style?.stroke ?? "#000" };
|
|
514
|
-
};
|
|
515
|
-
const onDown = (event) => {
|
|
516
|
-
if (event.button !== 0 && event.pointerType === "mouse")
|
|
517
|
-
return;
|
|
518
|
-
if (game.phase !== "playing" || game.turn === null || !human(game.turn))
|
|
519
|
-
return;
|
|
520
|
-
const own = whereIs(event.target);
|
|
521
|
-
if (own === null)
|
|
522
|
-
return;
|
|
523
|
-
const moves = legalNow();
|
|
524
|
-
if (dragAllowed && moves.some((move) => move.from === own)) {
|
|
525
|
-
drag = { pointer: event.pointerId, from: own, x: event.clientX, y: event.clientY, active: false, ghost: null };
|
|
526
|
-
}
|
|
527
|
-
};
|
|
528
|
-
const onMove = (event) => {
|
|
529
|
-
if (drag === null || event.pointerId !== drag.pointer)
|
|
530
|
-
return;
|
|
531
|
-
if (!drag.active) {
|
|
532
|
-
if (Math.hypot(event.clientX - drag.x, event.clientY - drag.y) < 8)
|
|
533
|
-
return;
|
|
534
|
-
drag.active = true;
|
|
535
|
-
selected = drag.from;
|
|
536
|
-
render();
|
|
537
|
-
const svg = boardBox.querySelector("svg");
|
|
538
|
-
const rect = svg?.getBoundingClientRect();
|
|
539
|
-
const viewWidth = svg?.viewBox.baseVal.width ?? 1;
|
|
540
|
-
const scale = rect === undefined ? 1 : rect.width / viewWidth;
|
|
541
|
-
const side = game.turn;
|
|
542
|
-
const colours = sampleColour(side);
|
|
543
|
-
const ghost = create(document, "div", "sgp-ghost");
|
|
544
|
-
const diameter = CHECKER * scale;
|
|
545
|
-
ghost.style.width = `${diameter}px`;
|
|
546
|
-
ghost.style.height = `${diameter}px`;
|
|
547
|
-
ghost.style.background = colours.fill;
|
|
548
|
-
ghost.style.borderColor = colours.edge;
|
|
549
|
-
host.append(ghost);
|
|
550
|
-
drag.ghost = ghost;
|
|
551
|
-
try {
|
|
552
|
-
boardBox.setPointerCapture(event.pointerId);
|
|
553
|
-
}
|
|
554
|
-
catch {
|
|
555
|
-
/* Capture is a nicety. */
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
if (drag.ghost !== null) {
|
|
559
|
-
const w = drag.ghost.offsetWidth;
|
|
560
|
-
drag.ghost.style.transform = `translate(${event.clientX - w / 2}px, ${event.clientY - w / 2}px)`;
|
|
561
|
-
}
|
|
562
|
-
};
|
|
563
|
-
const onUp = (event) => {
|
|
564
|
-
if (drag === null || event.pointerId !== drag.pointer) {
|
|
565
|
-
return;
|
|
566
|
-
}
|
|
567
|
-
const finished = drag;
|
|
568
|
-
drag = null;
|
|
569
|
-
finished.ghost?.remove();
|
|
570
|
-
try {
|
|
571
|
-
boardBox.releasePointerCapture(event.pointerId);
|
|
572
|
-
}
|
|
573
|
-
catch {
|
|
574
|
-
/* Already released. */
|
|
575
|
-
}
|
|
576
|
-
if (!finished.active)
|
|
577
|
-
return;
|
|
578
|
-
const to = whereIs(underPointer(event.clientX, event.clientY));
|
|
579
|
-
if (to !== null && makeMove({ from: finished.from, to }))
|
|
580
|
-
return;
|
|
581
|
-
selected = null;
|
|
582
|
-
render();
|
|
583
|
-
};
|
|
584
|
-
const onCancel = (event) => {
|
|
585
|
-
if (drag === null || event.pointerId !== drag.pointer)
|
|
586
|
-
return;
|
|
587
|
-
drag.ghost?.remove();
|
|
588
|
-
const wasActive = drag.active;
|
|
589
|
-
drag = null;
|
|
590
|
-
if (wasActive) {
|
|
591
|
-
selected = null;
|
|
592
|
-
render();
|
|
593
|
-
}
|
|
594
|
-
};
|
|
595
|
-
/** A tap or a click: the drag handlers deal with a drag, and a click that follows one is ignored. */
|
|
596
|
-
let lastDragEnd = 0;
|
|
597
|
-
const onClick = (event) => {
|
|
598
|
-
if (Date.now() - lastDragEnd < 60)
|
|
599
|
-
return;
|
|
600
|
-
if (game.phase !== "playing" || game.turn === null || !human(game.turn))
|
|
601
|
-
return;
|
|
602
|
-
press(whereIs(event.target));
|
|
603
|
-
};
|
|
604
|
-
boardBox.addEventListener("pointerdown", onDown);
|
|
605
|
-
boardBox.addEventListener("pointermove", onMove);
|
|
606
|
-
boardBox.addEventListener("pointerup", (event) => {
|
|
607
|
-
const wasDrag = drag?.active === true;
|
|
608
|
-
onUp(event);
|
|
609
|
-
if (wasDrag)
|
|
610
|
-
lastDragEnd = Date.now();
|
|
611
|
-
});
|
|
612
|
-
boardBox.addEventListener("pointercancel", onCancel);
|
|
613
|
-
boardBox.addEventListener("click", onClick);
|
|
614
|
-
// Following the page: its width, and its language.
|
|
615
|
-
const resize = typeof view.ResizeObserver === "undefined" ? null : new view.ResizeObserver(() => {
|
|
616
|
-
const wide = look.orientation === "portrait" || (look.orientation !== "landscape" && (host.clientWidth || 600) < 560);
|
|
617
|
-
if (wide !== portrait)
|
|
618
|
-
render();
|
|
619
|
-
});
|
|
620
|
-
resize?.observe(host);
|
|
621
|
-
const watch = typeof view.MutationObserver === "undefined" ? null : new view.MutationObserver(() => {
|
|
622
|
-
if (language === undefined)
|
|
623
|
-
render();
|
|
624
|
-
});
|
|
625
|
-
watch?.observe(document.documentElement, { attributes: true, attributeFilter: ["lang"] });
|
|
626
321
|
// The handle.
|
|
627
322
|
const api = {
|
|
628
323
|
host,
|
|
@@ -678,7 +373,10 @@ export function mountSugoroku(host, options = {}) {
|
|
|
678
373
|
if (change.preset !== undefined)
|
|
679
374
|
settings = settingsOf(undefined, undefined, change.preset);
|
|
680
375
|
else if (change.variant !== undefined || change.rules !== undefined)
|
|
681
|
-
settings = settingsOf(change.variant ?? settings.variant.key, {
|
|
376
|
+
settings = settingsOf(change.variant ?? settings.variant.key, {
|
|
377
|
+
...(change.variant === undefined ? settings.rules : {}),
|
|
378
|
+
...change.rules,
|
|
379
|
+
});
|
|
682
380
|
match = newMatch(settings);
|
|
683
381
|
recorder = new GameRecorder(settings, seed);
|
|
684
382
|
recorder.startGame();
|
|
@@ -707,16 +405,44 @@ export function mountSugoroku(host, options = {}) {
|
|
|
707
405
|
gone = true;
|
|
708
406
|
if (timer !== null)
|
|
709
407
|
clearTimeout(timer);
|
|
710
|
-
|
|
711
|
-
watch?.disconnect();
|
|
408
|
+
disconnectView();
|
|
712
409
|
sounds?.destroy();
|
|
713
|
-
drag?.ghost?.remove();
|
|
714
410
|
host.replaceChildren();
|
|
715
411
|
host.classList.remove("sugoroku-play");
|
|
716
412
|
for (const key of ["drag", "phase", "turn", "over", "orientation", "selected", "cube", "need", "ready"])
|
|
717
413
|
delete host.dataset[key];
|
|
718
414
|
},
|
|
719
415
|
};
|
|
416
|
+
const viewHandle = attachSugorokuView({
|
|
417
|
+
host,
|
|
418
|
+
doc: document,
|
|
419
|
+
page: view,
|
|
420
|
+
boardBox,
|
|
421
|
+
score,
|
|
422
|
+
status,
|
|
423
|
+
slotA,
|
|
424
|
+
slotB,
|
|
425
|
+
slotC,
|
|
426
|
+
dragAllowed,
|
|
427
|
+
say,
|
|
428
|
+
nameOf,
|
|
429
|
+
human,
|
|
430
|
+
isComputer,
|
|
431
|
+
spec,
|
|
432
|
+
makeMove,
|
|
433
|
+
game: () => game,
|
|
434
|
+
match: () => match,
|
|
435
|
+
settings: () => settings,
|
|
436
|
+
look: () => look,
|
|
437
|
+
waitingForDice: () => waitingForDice,
|
|
438
|
+
lastOpening: () => lastOpening,
|
|
439
|
+
followsPageLanguage: () => language === undefined,
|
|
440
|
+
stopped: () => gone,
|
|
441
|
+
actions: () => api,
|
|
442
|
+
});
|
|
443
|
+
render = viewHandle.render;
|
|
444
|
+
resetPointer = viewHandle.resetPointer;
|
|
445
|
+
disconnectView = viewHandle.disconnect;
|
|
720
446
|
render();
|
|
721
447
|
startComputer();
|
|
722
448
|
return api;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type GameState } from "./game.ts";
|
|
2
|
+
import type { Match } from "./match.ts";
|
|
3
|
+
import type { SugorokuLook } from "./mount.ts";
|
|
4
|
+
import type { Settings } from "./rules.ts";
|
|
5
|
+
import { type Side } from "./side.ts";
|
|
6
|
+
/** What the three buttons can ask the mounted game to do. */
|
|
7
|
+
export type SugorokuPlayActions = {
|
|
8
|
+
newMatch: () => void;
|
|
9
|
+
nextGame: () => void;
|
|
10
|
+
roll: () => void;
|
|
11
|
+
take: () => void;
|
|
12
|
+
drop: () => void;
|
|
13
|
+
beaver: () => void;
|
|
14
|
+
double: () => void;
|
|
15
|
+
done: () => void;
|
|
16
|
+
undo: () => void;
|
|
17
|
+
};
|
|
18
|
+
/** The drawing, the words, the buttons and the pointer. The game itself stays in `mountSugoroku`. */
|
|
19
|
+
export type SugorokuViewInput = {
|
|
20
|
+
host: HTMLElement;
|
|
21
|
+
doc: Document;
|
|
22
|
+
page: Window & typeof globalThis;
|
|
23
|
+
boardBox: HTMLElement;
|
|
24
|
+
score: HTMLElement;
|
|
25
|
+
status: HTMLElement;
|
|
26
|
+
slotA: HTMLButtonElement;
|
|
27
|
+
slotB: HTMLButtonElement;
|
|
28
|
+
slotC: HTMLButtonElement;
|
|
29
|
+
dragAllowed: boolean;
|
|
30
|
+
say: (key: string, values?: Record<string, string | number>) => string;
|
|
31
|
+
nameOf: (side: Side) => string;
|
|
32
|
+
human: (side: Side) => boolean;
|
|
33
|
+
isComputer: (side: Side) => boolean;
|
|
34
|
+
spec: () => GameState["settings"]["variant"];
|
|
35
|
+
makeMove: (step: {
|
|
36
|
+
from: number;
|
|
37
|
+
to: number;
|
|
38
|
+
}) => boolean;
|
|
39
|
+
game: () => GameState;
|
|
40
|
+
match: () => Match;
|
|
41
|
+
settings: () => Settings;
|
|
42
|
+
look: () => SugorokuLook;
|
|
43
|
+
waitingForDice: () => boolean;
|
|
44
|
+
lastOpening: () => readonly [number, number] | null;
|
|
45
|
+
followsPageLanguage: () => boolean;
|
|
46
|
+
/** The board has been taken down. */
|
|
47
|
+
stopped: () => boolean;
|
|
48
|
+
actions: () => SugorokuPlayActions;
|
|
49
|
+
};
|
|
50
|
+
export type SugorokuView = {
|
|
51
|
+
render: () => void;
|
|
52
|
+
/** Drop the selected point and any message, as a new position of the game does. */
|
|
53
|
+
resetPointer: () => void;
|
|
54
|
+
/** Listeners, the resize watch and a checker still being dragged. */
|
|
55
|
+
disconnect: () => void;
|
|
56
|
+
};
|
|
57
|
+
/** Draw the board, keep the words and buttons up to date, and play by pointer. */
|
|
58
|
+
export declare function attachSugorokuView(input: SugorokuViewInput): SugorokuView;
|