| 1 | <script> |
| 2 | // This can work for squared boards (2 or 4 players), with some adaptations (TODO) |
| 3 | // TODO: for 3 players, write a "board3.js" |
| 4 | export default { |
| 5 | name: 'my-board', |
| 6 | // Last move cannot be guessed from here, and is required to highlight squares |
| 7 | // vr: object to check moves, print board... |
| 8 | // mode: HH, HC or analyze |
| 9 | // userColor: for mode HH or HC |
| 10 | props: ["vr","lastMove","mode","orientation","userColor"], |
| 11 | data: function () { |
| 12 | return { |
| 13 | hints: (!localStorage["hints"] ? true : localStorage["hints"] === "1"), |
| 14 | bcolor: localStorage["bcolor"] || "lichess", //lichess, chesscom or chesstempo |
| 15 | possibleMoves: [], //filled after each valid click/dragstart |
| 16 | choices: [], //promotion pieces, or checkered captures... (as moves) |
| 17 | selectedPiece: null, //moving piece (or clicked piece) |
| 18 | incheck: [], |
| 19 | start: {}, //pixels coordinates + id of starting square (click or drag) |
| 20 | }; |
| 21 | }, |
| 22 | render(h) { |
| 23 | if (!this.vr) |
| 24 | return; |
| 25 | const [sizeX,sizeY] = [V.size.x,V.size.y]; |
| 26 | // Precompute hints squares to facilitate rendering |
| 27 | let hintSquares = doubleArray(sizeX, sizeY, false); |
| 28 | this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; }); |
| 29 | // Also precompute in-check squares |
| 30 | let incheckSq = doubleArray(sizeX, sizeY, false); |
| 31 | this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; }); |
| 32 | const squareWidth = 40; //TODO: compute this |
| 33 | const choices = h( |
| 34 | 'div', |
| 35 | { |
| 36 | attrs: { "id": "choices" }, |
| 37 | 'class': { 'row': true }, |
| 38 | style: { |
| 39 | "display": this.choices.length>0?"block":"none", |
| 40 | "top": "-" + ((sizeY/2)*squareWidth+squareWidth/2) + "px", |
| 41 | "width": (this.choices.length * squareWidth) + "px", |
| 42 | "height": squareWidth + "px", |
| 43 | }, |
| 44 | }, |
| 45 | this.choices.map(m => { //a "choice" is a move |
| 46 | return h('div', |
| 47 | { |
| 48 | 'class': { |
| 49 | 'board': true, |
| 50 | ['board'+sizeY]: true, |
| 51 | }, |
| 52 | style: { |
| 53 | 'width': (100/this.choices.length) + "%", |
| 54 | 'padding-bottom': (100/this.choices.length) + "%", |
| 55 | }, |
| 56 | }, |
| 57 | [h('img', |
| 58 | { |
| 59 | attrs: { "src": '/images/pieces/' + |
| 60 | V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' }, |
| 61 | 'class': { 'choice-piece': true }, |
| 62 | on: { |
| 63 | "click": e => { this.play(m); this.choices=[]; }, |
| 64 | // NOTE: add 'touchstart' event to fix a problem on smartphones |
| 65 | "touchstart": e => { this.play(m); this.choices=[]; }, |
| 66 | }, |
| 67 | }) |
| 68 | ] |
| 69 | ); |
| 70 | }) |
| 71 | ); |
| 72 | // Create board element (+ reserves if needed by variant or mode) |
| 73 | const lm = this.lastMove; |
| 74 | const showLight = this.hints && variant.name != "Dark"; |
| 75 | const gameDiv = h( |
| 76 | 'div', |
| 77 | { |
| 78 | 'class': { |
| 79 | 'game': true, |
| 80 | 'clearer': true, |
| 81 | }, |
| 82 | }, |
| 83 | [...Array(sizeX).keys()].map(i => { |
| 84 | let ci = (this.orientation=='w' ? i : sizeX-i-1); |
| 85 | return h( |
| 86 | 'div', |
| 87 | { |
| 88 | 'class': { |
| 89 | 'row': true, |
| 90 | }, |
| 91 | style: { 'opacity': this.choices.length>0?"0.5":"1" }, |
| 92 | }, |
| 93 | [...Array(sizeY).keys()].map(j => { |
| 94 | let cj = (this.orientation=='w' ? j : sizeY-j-1); |
| 95 | let elems = []; |
| 96 | if (this.vr.board[ci][cj] != V.EMPTY && (variant.name!="Dark" |
| 97 | || this.gameOver || this.mode == "analyze" |
| 98 | || this.vr.enlightened[this.userColor][ci][cj])) |
| 99 | { |
| 100 | elems.push( |
| 101 | h( |
| 102 | 'img', |
| 103 | { |
| 104 | 'class': { |
| 105 | 'piece': true, |
| 106 | 'ghost': !!this.selectedPiece |
| 107 | && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj, |
| 108 | }, |
| 109 | attrs: { |
| 110 | src: "/images/pieces/" + |
| 111 | V.getPpath(this.vr.board[ci][cj]) + ".svg", |
| 112 | }, |
| 113 | } |
| 114 | ) |
| 115 | ); |
| 116 | } |
| 117 | if (this.hints && hintSquares[ci][cj]) |
| 118 | { |
| 119 | elems.push( |
| 120 | h( |
| 121 | 'img', |
| 122 | { |
| 123 | 'class': { |
| 124 | 'mark-square': true, |
| 125 | }, |
| 126 | attrs: { |
| 127 | src: "/images/mark.svg", |
| 128 | }, |
| 129 | } |
| 130 | ) |
| 131 | ); |
| 132 | } |
| 133 | return h( |
| 134 | 'div', |
| 135 | { |
| 136 | 'class': { |
| 137 | 'board': true, |
| 138 | ['board'+sizeY]: true, |
| 139 | 'light-square': (i+j)%2==0, |
| 140 | 'dark-square': (i+j)%2==1, |
| 141 | [this.bcolor]: true, |
| 142 | 'in-shadow': variant.name=="Dark" && !this.gameOver |
| 143 | && this.mode != "analyze" |
| 144 | && !this.vr.enlightened[this.userColor][ci][cj], |
| 145 | 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj, |
| 146 | 'incheck': showLight && incheckSq[ci][cj], |
| 147 | }, |
| 148 | attrs: { |
| 149 | id: getSquareId({x:ci,y:cj}), |
| 150 | }, |
| 151 | }, |
| 152 | elems |
| 153 | ); |
| 154 | }) |
| 155 | ); |
| 156 | }) |
| 157 | ); |
| 158 | let elementArray = [choices, gameDiv]; |
| 159 | if (!!this.vr.reserve) |
| 160 | { |
| 161 | const shiftIdx = (this.userColor=="w" ? 0 : 1); |
| 162 | let myReservePiecesArray = []; |
| 163 | for (let i=0; i<V.RESERVE_PIECES.length; i++) |
| 164 | { |
| 165 | myReservePiecesArray.push(h('div', |
| 166 | { |
| 167 | 'class': {'board':true, ['board'+sizeY]:true}, |
| 168 | attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) } |
| 169 | }, |
| 170 | [ |
| 171 | h('img', |
| 172 | { |
| 173 | 'class': {"piece":true, "reserve":true}, |
| 174 | attrs: { |
| 175 | "src": "/images/pieces/" + |
| 176 | this.vr.getReservePpath(this.userColor,i) + ".svg", |
| 177 | } |
| 178 | }), |
| 179 | h('sup', |
| 180 | {"class": { "reserve-count": true } }, |
| 181 | [ this.vr.reserve[this.userColor][V.RESERVE_PIECES[i]] ] |
| 182 | ) |
| 183 | ])); |
| 184 | } |
| 185 | let oppReservePiecesArray = []; |
| 186 | const oppCol = V.GetOppCol(this.userColor); |
| 187 | for (let i=0; i<V.RESERVE_PIECES.length; i++) |
| 188 | { |
| 189 | oppReservePiecesArray.push(h('div', |
| 190 | { |
| 191 | 'class': {'board':true, ['board'+sizeY]:true}, |
| 192 | attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) } |
| 193 | }, |
| 194 | [ |
| 195 | h('img', |
| 196 | { |
| 197 | 'class': {"piece":true, "reserve":true}, |
| 198 | attrs: { |
| 199 | "src": "/images/pieces/" + |
| 200 | this.vr.getReservePpath(oppCol,i) + ".svg", |
| 201 | } |
| 202 | }), |
| 203 | h('sup', |
| 204 | {"class": { "reserve-count": true } }, |
| 205 | [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ] |
| 206 | ) |
| 207 | ])); |
| 208 | } |
| 209 | let reserves = h('div', |
| 210 | { |
| 211 | 'class':{ |
| 212 | 'game': true, |
| 213 | "reserve-div": true, |
| 214 | }, |
| 215 | }, |
| 216 | [ |
| 217 | h('div', |
| 218 | { |
| 219 | 'class': { |
| 220 | 'row': true, |
| 221 | "reserve-row-1": true, |
| 222 | }, |
| 223 | }, |
| 224 | myReservePiecesArray |
| 225 | ), |
| 226 | h('div', |
| 227 | { 'class': { 'row': true }}, |
| 228 | oppReservePiecesArray |
| 229 | ) |
| 230 | ] |
| 231 | ); |
| 232 | elementArray.push(reserves); |
| 233 | } |
| 234 | return h( |
| 235 | 'div', |
| 236 | { |
| 237 | 'class': { |
| 238 | "col-sm-12":true, |
| 239 | "col-md-10":true, |
| 240 | "col-md-offset-1":true, |
| 241 | "col-lg-8":true, |
| 242 | "col-lg-offset-2":true, |
| 243 | }, |
| 244 | // NOTE: click = mousedown + mouseup |
| 245 | on: { |
| 246 | mousedown: this.mousedown, |
| 247 | mousemove: this.mousemove, |
| 248 | mouseup: this.mouseup, |
| 249 | touchstart: this.mousedown, |
| 250 | touchmove: this.mousemove, |
| 251 | touchend: this.mouseup, |
| 252 | }, |
| 253 | }, |
| 254 | elementArray |
| 255 | ); |
| 256 | }, |
| 257 | methods: { |
| 258 | mousedown: function(e) { |
| 259 | e = e || window.event; |
| 260 | let ingame = false; |
| 261 | let elem = e.target; |
| 262 | while (!ingame && elem !== null) |
| 263 | { |
| 264 | if (elem.classList.contains("game")) |
| 265 | { |
| 266 | ingame = true; |
| 267 | break; |
| 268 | } |
| 269 | elem = elem.parentElement; |
| 270 | } |
| 271 | if (!ingame) //let default behavior (click on button...) |
| 272 | return; |
| 273 | e.preventDefault(); //disable native drag & drop |
| 274 | if (!this.selectedPiece && e.target.classList.contains("piece")) |
| 275 | { |
| 276 | // Next few lines to center the piece on mouse cursor |
| 277 | let rect = e.target.parentNode.getBoundingClientRect(); |
| 278 | this.start = { |
| 279 | x: rect.x + rect.width/2, |
| 280 | y: rect.y + rect.width/2, |
| 281 | id: e.target.parentNode.id |
| 282 | }; |
| 283 | this.selectedPiece = e.target.cloneNode(); |
| 284 | this.selectedPiece.style.position = "absolute"; |
| 285 | this.selectedPiece.style.top = 0; |
| 286 | this.selectedPiece.style.display = "inline-block"; |
| 287 | this.selectedPiece.style.zIndex = 3000; |
| 288 | const startSquare = getSquareFromId(e.target.parentNode.id); |
| 289 | this.possibleMoves = []; |
| 290 | const color = this.mode=="analyze" || this.gameOver |
| 291 | ? this.vr.turn |
| 292 | : this.userColor; |
| 293 | if (this.vr.canIplay(color,startSquare)) |
| 294 | this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare); |
| 295 | // Next line add moving piece just after current image |
| 296 | // (required for Crazyhouse reserve) |
| 297 | e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling); |
| 298 | } |
| 299 | }, |
| 300 | mousemove: function(e) { |
| 301 | if (!this.selectedPiece) |
| 302 | return; |
| 303 | e = e || window.event; |
| 304 | // If there is an active element, move it around |
| 305 | if (!!this.selectedPiece) |
| 306 | { |
| 307 | const [offsetX,offsetY] = !!e.clientX |
| 308 | ? [e.clientX,e.clientY] //desktop browser |
| 309 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone |
| 310 | this.selectedPiece.style.left = (offsetX-this.start.x) + "px"; |
| 311 | this.selectedPiece.style.top = (offsetY-this.start.y) + "px"; |
| 312 | } |
| 313 | }, |
| 314 | mouseup: function(e) { |
| 315 | if (!this.selectedPiece) |
| 316 | return; |
| 317 | e = e || window.event; |
| 318 | // Read drop target (or parentElement, parentNode... if type == "img") |
| 319 | this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords |
| 320 | const [offsetX,offsetY] = !!e.clientX |
| 321 | ? [e.clientX,e.clientY] |
| 322 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; |
| 323 | let landing = document.elementFromPoint(offsetX, offsetY); |
| 324 | this.selectedPiece.style.zIndex = 3000; |
| 325 | // Next condition: classList.contains(piece) fails because of marks |
| 326 | while (landing.tagName == "IMG") |
| 327 | landing = landing.parentNode; |
| 328 | if (this.start.id == landing.id) |
| 329 | { |
| 330 | // A click: selectedPiece and possibleMoves are already filled |
| 331 | return; |
| 332 | } |
| 333 | // OK: process move attempt |
| 334 | let endSquare = getSquareFromId(landing.id); |
| 335 | let moves = this.findMatchingMoves(endSquare); |
| 336 | this.possibleMoves = []; |
| 337 | if (moves.length > 1) |
| 338 | this.choices = moves; |
| 339 | else if (moves.length==1) |
| 340 | this.play(moves[0]); |
| 341 | // Else: impossible move |
| 342 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); |
| 343 | delete this.selectedPiece; |
| 344 | this.selectedPiece = null; |
| 345 | }, |
| 346 | findMatchingMoves: function(endSquare) { |
| 347 | // Run through moves list and return the matching set (if promotions...) |
| 348 | let moves = []; |
| 349 | this.possibleMoves.forEach(function(m) { |
| 350 | if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) |
| 351 | moves.push(m); |
| 352 | }); |
| 353 | return moves; |
| 354 | }, |
| 355 | play: function(move) { |
| 356 | this.$emit('play-move', move); |
| 357 | }, |
| 358 | }, |
| 359 | }; |
| 360 | </script> |