X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=base_rules.js;h=33411f0ef502638bd526bddf16ae669392844885;hb=535c464b0543306a0ea36c66f07dcfad7a951efc;hp=7191c563b1a416903d4ccc1acefd61f06bd698ec;hpb=adf7c6595b5e41e7ecbb3d40cb33b21996e6b16c;p=xogo.git diff --git a/base_rules.js b/base_rules.js index 7191c56..33411f0 100644 --- a/base_rules.js +++ b/base_rules.js @@ -98,7 +98,7 @@ export default class ChessRules { this.options["teleport"] && this.subTurnTeleport == 2 && this.board[coords.x][coords.y] == "" ) { - return new Move({ + let res = new Move({ start: {x: this.captured.x, y: this.captured.y}, appear: [ new PiPo({ @@ -108,9 +108,10 @@ export default class ChessRules { p: this.captured.p }) ], - vanish: [], - drag: {c: this.captured.c, p: this.captured.p} + vanish: [] }); + res.drag = {c: this.captured.c, p: this.captured.p}; + return res; } return null; } @@ -289,19 +290,20 @@ export default class ChessRules { return fen; } + static FenEmptySquares(count) { + // if more than 9 consecutive free spaces, break the integer, + // otherwise FEN parsing will fail. + if (count <= 9) + return count; + // Most boards of size < 18: + if (count <= 18) + return "9" + (count - 9); + // Except Gomoku: + return "99" + (count - 18); + } + // Position part of the FEN string getPosition() { - const format = (count) => { - // if more than 9 consecutive free spaces, break the integer, - // otherwise FEN parsing will fail. - if (count <= 9) - return count; - // Most boards of size < 18: - if (count <= 18) - return "9" + (count - 9); - // Except Gomoku: - return "99" + (count - 18); - }; let position = ""; for (let i = 0; i < this.size.y; i++) { let emptyCount = 0; @@ -311,7 +313,7 @@ export default class ChessRules { else { if (emptyCount > 0) { // Add empty squares in-between - position += format(emptyCount); + position += C.FenEmptySquares(emptyCount); emptyCount = 0; } position += this.board2fen(this.board[i][j]); @@ -319,7 +321,7 @@ export default class ChessRules { } if (emptyCount > 0) // "Flush remainder" - position += format(emptyCount); + position += C.FenEmptySquares(emptyCount); if (i < this.size.y - 1) position += "/"; //separate rows } @@ -370,6 +372,11 @@ export default class ChessRules { constructor(o) { this.options = o.options; + // Fill missing options (always the case if random challenge) + (V.Options.select || []).concat(V.Options.input || []).forEach(opt => { + if (this.options[opt.variable] === undefined) + this.options[opt.variable] = opt.defaut; + }); this.playerColor = o.color; this.afterPlay = o.afterPlay; //trigger some actions after playing a move @@ -526,7 +533,8 @@ export default class ChessRules { this.initMouseEvents(); const chessboard = document.getElementById(this.containerId).querySelector(".chessboard"); - new ResizeObserver(this.rescale).observe(chessboard); + // TODO: calling with "this" seems required by Hex. Understand why... + new ResizeObserver(() => this.rescale(this)).observe(chessboard); } re_drawBoardElements() { @@ -549,7 +557,7 @@ export default class ChessRules { cbHeight = Math.min(window.innerHeight, 767); cbWidth = cbHeight * this.size.ratio; } - if (this.reserve) { + if (this.hasReserve) { const sqSize = cbWidth / this.size.y; // NOTE: allocate space for reserves (up/down) even if they are empty // Cannot use getReserveSquareSize() here, but sqSize is an upper bound. @@ -581,8 +589,7 @@ export default class ChessRules { let board = ` - `; + class="chessboard_SVG">`; for (let i=0; i < this.size.x; i++) { for (let j=0; j < this.size.y; j++) { const ii = (flipped ? this.size.x - 1 - i : i); @@ -591,16 +598,18 @@ export default class ChessRules { if (this.enlightened && !this.enlightened[ii][jj]) classes += " in-shadow"; // NOTE: x / y reversed because coordinates system is reversed. - board += ``; + board += ` + `; } } - board += ""; + board += ""; return board; } @@ -648,7 +657,7 @@ export default class ChessRules { } } } - if (this.reserve) + if (this.hasReserve) this.re_drawReserve(['w', 'b'], r); } @@ -765,44 +774,50 @@ export default class ChessRules { } // After resize event: no need to destroy/recreate pieces - rescale() { - const container = document.getElementById(this.containerId); + rescale(self) { + const container = document.getElementById(self.containerId); if (!container) return; //useful at initial loading let chessboard = container.querySelector(".chessboard"); - const r = chessboard.getBoundingClientRect(); - const newRatio = r.width / r.height; - let newWidth = r.width, - newHeight = r.height; - if (newRatio > this.size.ratio) { - newWidth = r.height * this.size.ratio; - chessboard.style.width = newWidth + "px"; - } - else if (newRatio < this.size.ratio) { - newHeight = r.width / this.size.ratio; - chessboard.style.height = newHeight + "px"; - } + let r = chessboard.getBoundingClientRect(); + let [newWidth, newHeight] = [r.width, r.height]; + // Stay in window: + if (newWidth > window.innerWidth) + newWidth = window.innerWidth; + if (newHeight > window.innerHeight) + newHeight = window.innerHeight; + const newRatio = newWidth / newHeight; + if (newRatio > self.size.ratio) + newWidth = newHeight * self.size.ratio; + else if (newRatio < self.size.ratio) + newHeight = newWidth / self.size.ratio; + chessboard.style.width = newWidth + "px"; + chessboard.style.height = newHeight + "px"; const newX = (window.innerWidth - newWidth) / 2; chessboard.style.left = newX + "px"; const newY = (window.innerHeight - newHeight) / 2; chessboard.style.top = newY + "px"; const newR = {x: newX, y: newY, width: newWidth, height: newHeight}; - const pieceWidth = this.getPieceWidth(newWidth); - for (let i=0; i < this.size.x; i++) { - for (let j=0; j < this.size.y; j++) { - if (this.g_pieces[i][j]) { - // NOTE: could also use CSS transform "scale" - this.g_pieces[i][j].style.width = pieceWidth + "px"; - this.g_pieces[i][j].style.height = pieceWidth + "px"; - const [ip, jp] = this.getPixelPosition(i, j, newR); - // Translate coordinates to use chessboard as reference: - this.g_pieces[i][j].style.transform = - `translate(${ip - newX}px,${jp - newY}px)`; + const pieceWidth = self.getPieceWidth(newWidth); + // NOTE: next "if" for variants which use squares filling + // instead of "physical", moving pieces + if (this.g_pieces) { + for (let i=0; i < self.size.x; i++) { + for (let j=0; j < self.size.y; j++) { + if (self.g_pieces[i][j]) { + // NOTE: could also use CSS transform "scale" + self.g_pieces[i][j].style.width = pieceWidth + "px"; + self.g_pieces[i][j].style.height = pieceWidth + "px"; + const [ip, jp] = self.getPixelPosition(i, j, newR); + // Translate coordinates to use chessboard as reference: + self.g_pieces[i][j].style.transform = + `translate(${ip - newX}px,${jp - newY}px)`; + } } } } - if (this.reserve) - this.rescaleReserve(newR); + if (self.hasReserve) + self.rescaleReserve(newR); } rescaleReserve(r) { @@ -1207,7 +1222,7 @@ export default class ChessRules { getDropMovesFrom([c, p]) { // NOTE: by design, this.reserve[c][p] >= 1 on user click - // (but not necessarily otherwise) + // (but not necessarily otherwise: atLeastOneMove() etc) if (this.reserve[c][p] == 0) return []; let moves = [];