X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=base_rules.js;h=311750689fdb7b797a1689e897db05b6af245c0e;hb=6d8c4fc35e9072806f91b29874753e12942c2bae;hp=75545c241e3b1ff5dede763b872ebba1ff07c27b;hpb=1a7c0492c3273db398a2d740b4d3b5786e67b9c7;p=xogo.git diff --git a/base_rules.js b/base_rules.js index 75545c2..3117506 100644 --- a/base_rules.js +++ b/base_rules.js @@ -290,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; @@ -312,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]); @@ -320,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 } @@ -371,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 @@ -527,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() { @@ -582,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); @@ -592,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; } @@ -766,44 +774,51 @@ 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; + const epsilon = 1e-4; //arbitrary small value to avoid instabilities + if (newRatio - self.size.ratio > epsilon) + newWidth = newHeight * self.size.ratio; + else if (newRatio - self.size.ratio < -epsilon) + 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.hasReserve) - this.rescaleReserve(newR); + if (self.hasReserve) + self.rescaleReserve(newR); } rescaleReserve(r) {