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