X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=base_rules.js;h=4e2eddd93423e5ab8a3fb94d48484acdf5c92e49;hb=3c61449b830db29c95d82cdacc4dae710cc705a8;hp=6303060aa497bbaff63d594301c28c88f2512a67;hpb=bc34b505e536fc0d66747197fbb62b5ea1f85721;p=xogo.git diff --git a/base_rules.js b/base_rules.js index 6303060..4e2eddd 100644 --- a/base_rules.js +++ b/base_rules.js @@ -7,6 +7,10 @@ import Move from "/utils/Move.js"; // NOTE: ChessRules is aliased as window.C, and variants as window.V export default class ChessRules { + static get Aliases() { + return {'C': ChessRules}; + } + ///////////////////////// // VARIANT SPECIFICATIONS @@ -26,11 +30,18 @@ export default class ChessRules { { label: "Asymmetric random", value: 2 } ] }], - check: [{ - label: "Capture king?", - defaut: false, - variable: "taking" - }], + check: [ + { + label: "Capture king", + defaut: false, + variable: "taking" + }, + { + label: "Falling pawn", + defaut: false, + variable: "pawnfall" + } + ], // Game modifiers (using "elementary variants"). Default: false styles: [ "atomic", @@ -373,8 +384,6 @@ export default class ChessRules { // Fen string fully describes the game state constructor(o) { - window.C = ChessRules; //easier alias - this.options = o.options; this.playerColor = o.color; this.afterPlay = o.afterPlay; @@ -478,8 +487,9 @@ export default class ChessRules { // Apply diff this.enlightened --> newEnlightened on board graphUpdateEnlightened(newEnlightened) { - let container = document.getElementById(this.containerId); - const r = container.getBoundingClientRect(); + let chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); + const r = chessboard.getBoundingClientRect(); const pieceWidth = this.getPieceWidth(r.width); for (let x=0; x this.re_drawBoardElements(); this.re_drawBoardElements(); this.initMouseEvents(); - const container = document.getElementById(this.containerId); - new ResizeObserver(this.rescale).observe(container); + const chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); + new ResizeObserver(this.rescale).observe(chessboard); } re_drawBoardElements() { const board = this.getSvgChessboard(); const oppCol = C.GetOppCol(this.playerColor); - let container = document.getElementById(this.containerId); - container.innerHTML = ""; - container.insertAdjacentHTML('beforeend', board); - let cb = container.querySelector("#" + this.containerId + "_SVG"); + let chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); + chessboard.innerHTML = ""; + chessboard.insertAdjacentHTML('beforeend', board); const aspectRatio = this.size.y / this.size.x; // Compare window ratio width / height to aspectRatio: const windowRatio = window.innerWidth / window.innerHeight; @@ -599,13 +610,13 @@ export default class ChessRules { cbWidth = cbHeight * aspectRatio; } } - container.style.width = cbWidth + "px"; - container.style.height = cbHeight + "px"; + chessboard.style.width = cbWidth + "px"; + chessboard.style.height = cbHeight + "px"; // Center chessboard: const spaceLeft = (window.innerWidth - cbWidth) / 2, spaceTop = (window.innerHeight - cbHeight) / 2; - container.style.left = spaceLeft + "px"; - container.style.top = spaceTop + "px"; + chessboard.style.left = spaceLeft + "px"; + chessboard.style.top = spaceTop + "px"; // Give sizes instead of recomputing them, // because chessboard might not be drawn yet. this.setupPieces({ @@ -624,7 +635,7 @@ export default class ChessRules { + class="chessboard_SVG"> `; for (let i=0; i < sizeX; i++) { for (let j=0; j < sizeY; j++) { @@ -665,8 +676,9 @@ export default class ChessRules { } } else this.g_pieces = ArrayFun.init(this.size.x, this.size.y, null); - let container = document.getElementById(this.containerId); - if (!r) r = container.getBoundingClientRect(); + let chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); + if (!r) r = chessboard.getBoundingClientRect(); const pieceWidth = this.getPieceWidth(r.width); for (let i=0; i < this.size.x; i++) { for (let j=0; j < this.size.y; j++) { @@ -683,7 +695,7 @@ export default class ChessRules { this.g_pieces[i][j].style.height = pieceWidth + "px"; const [ip, jp] = this.getPixelPosition(i, j, r); this.g_pieces[i][j].style.transform = `translate(${ip}px,${jp}px)`; - container.appendChild(this.g_pieces[i][j]); + chessboard.appendChild(this.g_pieces[i][j]); } } } @@ -709,10 +721,9 @@ export default class ChessRules { } } else this.r_pieces = { 'w': {}, 'b': {} }; - if (!r) { - const container = document.getElementById(this.containerId); - r = container.getBoundingClientRect(); - } + let chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); + if (!r) r = chessboard.getBoundingClientRect(); for (let c of colors) { if (!this.reserve[c]) continue; const nbR = this.getNbReservePieces(c); @@ -729,7 +740,7 @@ export default class ChessRules { // NOTE: +1 fix display bug on Firefox at least rcontainer.style.width = (nbR * sqResSize + 1) + "px"; rcontainer.style.height = sqResSize + "px"; - document.getElementById("boardContainer").appendChild(rcontainer); + chessboard.appendChild(rcontainer); for (let p of Object.keys(this.reserve[c])) { if (this.reserve[c][p] == 0) continue; let r_cell = document.createElement("div"); @@ -774,25 +785,26 @@ export default class ChessRules { // After resize event: no need to destroy/recreate pieces rescale() { - let container = document.getElementById(this.containerId); + const container = document.getElementById(this.containerId); if (!container) return; //useful at initial loading - const r = container.getBoundingClientRect(); + let chessboard = container.querySelector(".chessboard"); + const r = chessboard.getBoundingClientRect(); const newRatio = r.width / r.height; const aspectRatio = this.size.y / this.size.x; let newWidth = r.width, newHeight = r.height; if (newRatio > aspectRatio) { newWidth = r.height * aspectRatio; - container.style.width = newWidth + "px"; + chessboard.style.width = newWidth + "px"; } else if (newRatio < aspectRatio) { newHeight = r.width / aspectRatio; - container.style.height = newHeight + "px"; + chessboard.style.height = newHeight + "px"; } const newX = (window.innerWidth - newWidth) / 2; - container.style.left = newX + "px"; + chessboard.style.left = newX + "px"; const newY = (window.innerHeight - newHeight) / 2; - container.style.top = newY + "px"; + 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++) { @@ -834,7 +846,7 @@ export default class ChessRules { } } - // Return the absolute pixel coordinates given current position. + // Return the absolute pixel coordinates (on board) given current position. // Our coordinate system differs from CSS one (x <--> y). // We return here the CSS coordinates (more useful). getPixelPosition(i, j, r) { @@ -847,10 +859,13 @@ export default class ChessRules { } initMouseEvents() { - let container = document.getElementById(this.containerId); + let chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); const getOffset = e => { - if (e.clientX) return {x: e.clientX, y: e.clientY}; //Mouse + if (e.clientX) + // Mouse + return {x: e.clientX, y: e.clientY}; let touchLocation = null; if (e.targetTouches && e.targetTouches.length >= 1) // Touch screen, dragstart @@ -860,14 +875,14 @@ export default class ChessRules { touchLocation = e.changedTouches[0]; if (touchLocation) return {x: touchLocation.clientX, y: touchLocation.clientY}; - return [0, 0]; //Big trouble here =) + return [0, 0]; //shouldn't reach here =) } const centerOnCursor = (piece, e) => { const centerShift = sqSize / 2; const offset = getOffset(e); - piece.style.left = (offset.x - centerShift) + "px"; - piece.style.top = (offset.y - centerShift) + "px"; + piece.style.left = (offset.x - r.x - centerShift) + "px"; + piece.style.top = (offset.y - r.y - centerShift) + "px"; } let start = null, @@ -877,7 +892,7 @@ export default class ChessRules { const mousedown = (e) => { // Disable zoom on smartphones: if (e.touches && e.touches.length > 1) e.preventDefault(); - r = container.getBoundingClientRect(); + r = chessboard.getBoundingClientRect(); sqSize = this.getSquareWidth(r.width); const square = this.idToCoords(e.target.id); if (square) { @@ -896,9 +911,9 @@ export default class ChessRules { curPiece.style.width = sqSize + "px"; curPiece.style.height = sqSize + "px"; centerOnCursor(curPiece, e); - document.getElementById("boardContainer").appendChild(curPiece); + chessboard.appendChild(curPiece); startPiece.style.opacity = "0.4"; - container.style.cursor = "none"; + chessboard.style.cursor = "none"; } } } @@ -915,7 +930,7 @@ export default class ChessRules { }; const mouseup = (e) => { - const newR = container.getBoundingClientRect(); + const newR = chessboard.getBoundingClientRect(); if (newR.width != r.width || newR.height != r.height) { this.rescale(); return; @@ -924,7 +939,7 @@ export default class ChessRules { const [x, y] = [start.x, start.y]; start = null; e.preventDefault(); - container.style.cursor = "pointer"; + chessboard.style.cursor = "pointer"; startPiece.style.opacity = "1"; const offset = getOffset(e); const landingElt = document.elementFromPoint(offset.x, offset.y); @@ -957,22 +972,22 @@ export default class ChessRules { showChoices(moves, r) { let container = document.getElementById(this.containerId); + let chessboard = container.querySelector(".chessboard"); let choices = document.createElement("div"); choices.id = "choices"; choices.style.width = r.width + "px"; choices.style.height = r.height + "px"; choices.style.left = r.x + "px"; choices.style.top = r.y + "px"; - container.style.opacity = "0.5"; - let boardContainer = document.getElementById("boardContainer"); - boardContainer.appendChild(choices); + chessboard.style.opacity = "0.5"; + container.appendChild(choices); const squareWidth = this.getSquareWidth(r.width); const firstUpLeft = (r.width - (moves.length * squareWidth)) / 2; const firstUpTop = (r.height - squareWidth) / 2; const color = moves[0].appear[0].c; const callback = (m) => { - container.style.opacity = "1"; - boardContainer.removeChild(choices); + chessboard.style.opacity = "1"; + container.removeChild(choices); this.playPlusVisual(m, r); } for (let i=0; i < moves.length; i++) { @@ -1545,15 +1560,16 @@ export default class ChessRules { return !!enpassantMove ? [enpassantMove] : []; } - // Consider all potential promotions: + // Consider all potential promotions. + // NOTE: "promotions" arg = special override for Hiddenqueen variant addPawnMoves([x1, y1], [x2, y2], moves, promotions) { let finalPieces = ["p"]; const color = this.getColor(x1, y1); const oppCol = C.GetOppCol(color); const lastRank = (color == "w" ? 0 : this.size.x - 1); - if (x2 == lastRank && (!this.options["rifle"] || this.board[x2][y2] == "")) - { - // promotions arg: special override for Hiddenqueen variant + const promotionOk = + x2 == lastRank && (!this.options["rifle"] || this.board[x2][y2] == ""); + if (promotionOk && !this.options["pawnfall"]) { if ( this.options["cannibal"] && this.board[x2][y2] != "" && @@ -1566,8 +1582,15 @@ export default class ChessRules { finalPieces = this.pawnSpecs.promotions; } for (let piece of finalPieces) { - const tr = (piece != "p" ? { c: color, p: piece } : null); - moves.push(this.getBasicMove([x1, y1], [x2, y2], tr)); + const tr = !this.options["pawnfall"] && piece != "p" + ? { c: color, p: piece } + : null; + let newMove = this.getBasicMove([x1, y1], [x2, y2], tr); + if (promotionOk && this.options["pawnfall"]) { + newMove.appear.shift(); + newMove.pawnfall = true; //required in prePlay() + } + moves.push(newMove); } } @@ -1779,7 +1802,7 @@ export default class ChessRules { // Is (king at) given position under check by "color" ? underCheck([x, y], color) { - if (this.taking || this.options["dark"]) return false; + if (this.options["taking"] || this.options["dark"]) return false; color = color || C.GetOppCol(this.getColor(x, y)); const pieces = this.pieces(color); return Object.keys(pieces).some(p => { @@ -1849,7 +1872,7 @@ export default class ChessRules { return res; }); } - if (this.taking || this.options["dark"]) return moves; + if (this.options["taking"] || this.options["dark"]) return moves; const kingPos = this.searchKingPos(color); let filtered = {}; //avoid re-checking similar moves (promotions...) return moves.filter(m => { @@ -1955,7 +1978,7 @@ export default class ChessRules { } } const minSize = Math.min(move.appear.length, move.vanish.length); - if (this.hasReserve) { + if (this.hasReserve && !move.pawnfall) { const color = this.turn; for (let i=minSize; i { if (this.enlightened && !this.enlightened[a.x][a.y]) return; @@ -2077,7 +2101,7 @@ export default class ChessRules { this.g_pieces[a.x][a.y].style.height = pieceWidth + "px"; const [ip, jp] = this.getPixelPosition(a.x, a.y, r); this.g_pieces[a.x][a.y].style.transform = `translate(${ip}px,${jp}px)`; - container.appendChild(this.g_pieces[a.x][a.y]); + chessboard.appendChild(this.g_pieces[a.x][a.y]); }); } @@ -2097,7 +2121,7 @@ export default class ChessRules { nbR++; } const rsqSize = this.getReserveSquareSize(r.width, nbR); - return [ridx * rsqSize, rsqSize]; //slightly inaccurate... TODO? + return [-ridx * rsqSize, rsqSize]; //slightly inaccurate... TODO? } animate(move, callback) { @@ -2109,7 +2133,8 @@ export default class ChessRules { const dropMove = (typeof i1 == "string"); const startArray = (dropMove ? this.r_pieces : this.g_pieces); let startPiece = startArray[i1][j1]; - let container = document.getElementById(this.containerId); + let chessboard = + document.getElementById(this.containerId).querySelector(".chessboard"); const clonePiece = ( !dropMove && this.options["rifle"] || @@ -2125,7 +2150,7 @@ export default class ChessRules { startPiece.classList.add(pieces[this.captured.p]["class"]); // Color: OK } - container.appendChild(startPiece); + chessboard.appendChild(startPiece); } const [i2, j2] = [move.end.x, move.end.y]; let startCoords; @@ -2136,7 +2161,7 @@ export default class ChessRules { ]; } else startCoords = [i1, j1]; - const r = container.getBoundingClientRect(); + const r = chessboard.getBoundingClientRect(); const arrival = this.getPixelPosition(i2, j2, r); //TODO: arrival on drop? let rs = [0, 0]; if (dropMove) rs = this.getReserveShift(i1, j1, r); @@ -2167,8 +2192,7 @@ export default class ChessRules { playReceivedMove(moves, callback) { const launchAnimation = () => { - const r = - document.getElementById(this.containerId).getBoundingClientRect(); + const r = container.querySelector(".chessboard").getBoundingClientRect(); const animateRec = i => { this.animate(moves[i], () => { this.playVisual(moves[i], r); @@ -2181,15 +2205,15 @@ export default class ChessRules { }; // Delay if user wasn't focused: const checkDisplayThenAnimate = (delay) => { - if (boardContainer.style.display == "none") { + if (container.style.display == "none") { alert("New move! Let's go back to game..."); document.getElementById("gameInfos").style.display = "none"; - boardContainer.style.display = "block"; + container.style.display = "block"; setTimeout(launchAnimation, 700); } else setTimeout(launchAnimation, delay || 0); }; - let boardContainer = document.getElementById("boardContainer"); + let container = document.getElementById(this.containerId); if (document.hidden) { document.onvisibilitychange = () => { document.onvisibilitychange = undefined;