X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=public%2Fjavascripts%2Fcomponents%2Fboard.js;h=a59e41f7bc1d68d048b498f08fca1676986c36e4;hb=a3ab5fdb6e9d614f55bb7ecef5887ddb7875df4b;hp=cec6049eeebce78b1fb406f8428dd58b3f67fe7a;hpb=fd373b27c15a98f891e1158639abc50e19466449;p=vchess.git diff --git a/public/javascripts/components/board.js b/public/javascripts/components/board.js index cec6049e..a59e41f7 100644 --- a/public/javascripts/components/board.js +++ b/public/javascripts/components/board.js @@ -1,7 +1,11 @@ +// This can work for squared boards (2 or 4 players), with some adaptations (TODO) +// TODO: for 3 players, write a "board3.js" Vue.component('my-board', { // Last move cannot be guessed from here, and is required to highlight squares // vr: object to check moves, print board... - props: ["vr","lastMove","mode","orientation","userColor","gameOver"], + // mode: HH, HC or analyze + // userColor: for mode HH or HC + props: ["vr","lastMove","mode","orientation","userColor"], data: function () { return { hints: (!localStorage["hints"] ? true : localStorage["hints"] === "1"), @@ -14,6 +18,8 @@ Vue.component('my-board', { }; }, render(h) { + if (!this.vr) + return; const [sizeX,sizeY] = [V.size.x,V.size.y]; // Precompute hints squares to facilitate rendering let hintSquares = doubleArray(sizeX, sizeY, false); @@ -21,6 +27,7 @@ Vue.component('my-board', { // Also precompute in-check squares let incheckSq = doubleArray(sizeX, sizeY, false); this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; }); + const squareWidth = 40; //TODO: compute this const choices = h( 'div', { @@ -85,7 +92,8 @@ Vue.component('my-board', { let cj = (this.orientation=='w' ? j : sizeY-j-1); let elems = []; if (this.vr.board[ci][cj] != V.EMPTY && (variant.name!="Dark" - || this.gameOver || this.vr.enlightened[this.userColor][ci][cj])) + || this.gameOver || this.mode == "analyze" + || this.vr.enlightened[this.userColor][ci][cj])) { elems.push( h( @@ -130,12 +138,13 @@ Vue.component('my-board', { 'dark-square': (i+j)%2==1, [this.bcolor]: true, 'in-shadow': variant.name=="Dark" && !this.gameOver + && this.mode != "analyze" && !this.vr.enlightened[this.userColor][ci][cj], 'highlight': showLight && !!lm && _.isMatch(lm.end, {x:ci,y:cj}), 'incheck': showLight && incheckSq[ci][cj], }, attrs: { - id: this.getSquareId({x:ci,y:cj}), + id: getSquareId({x:ci,y:cj}), }, }, elems @@ -154,7 +163,7 @@ Vue.component('my-board', { myReservePiecesArray.push(h('div', { 'class': {'board':true, ['board'+sizeY]:true}, - attrs: { id: this.getSquareId({x:sizeX+shiftIdx,y:i}) } + attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) } }, [ h('img', @@ -178,7 +187,7 @@ Vue.component('my-board', { oppReservePiecesArray.push(h('div', { 'class': {'board':true, ['board'+sizeY]:true}, - attrs: { id: this.getSquareId({x:sizeX+(1-shiftIdx),y:i}) } + attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) } }, [ h('img', @@ -244,16 +253,6 @@ Vue.component('my-board', { ); }, methods: { - // Get the identifier of a HTML square from its numeric coordinates o.x,o.y. - getSquareId: function(o) { - // NOTE: a separator is required to allow any size of board - return "sq-" + o.x + "-" + o.y; - }, - // Inverse function - getSquareFromId: function(id) { - let idParts = id.split('-'); - return [parseInt(idParts[1]), parseInt(idParts[2])]; - }, mousedown: function(e) { e = e || window.event; let ingame = false; @@ -284,7 +283,7 @@ Vue.component('my-board', { this.selectedPiece.style.top = 0; this.selectedPiece.style.display = "inline-block"; this.selectedPiece.style.zIndex = 3000; - const startSquare = this.getSquareFromId(e.target.parentNode.id); + const startSquare = getSquareFromId(e.target.parentNode.id); this.possibleMoves = []; const color = this.mode=="analyze" || this.gameOver ? this.vr.turn @@ -330,7 +329,7 @@ Vue.component('my-board', { return; } // OK: process move attempt - let endSquare = this.getSquareFromId(landing.id); + let endSquare = getSquareFromId(landing.id); let moves = this.findMatchingMoves(endSquare); this.possibleMoves = []; if (moves.length > 1)