2 import { getSquareId, getSquareFromId } from "@/utils/squareId";
3 import { ArrayFun } from "@/utils/array";
7 // Last move cannot be guessed from here, and is required to highlight squares
8 // vr: object to check moves, print board...
9 // userColor is left undefined for an external observer
10 props: ["vr","lastMove","analyze","orientation","userColor","vname"],
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)
19 start: {}, //pixels coordinates + id of starting square (click or drag)
25 // Return empty div of class 'game' to avoid error when setting size
33 const [sizeX,sizeY] = [V.size.x,V.size.y];
34 // Precompute hints squares to facilitate rendering
35 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
36 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
37 // Also precompute in-check squares
38 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
39 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
41 let boardElt = document.querySelector(".game");
42 const squareWidth = (!!boardElt
43 ? boardElt.offsetWidth / sizeY
44 : 40); //arbitrary value (not relevant)
48 attrs: { "id": "choices" },
49 'class': { 'row': true },
51 "display": (this.choices.length > 0 ? "block" : "none"),
52 "top": ((sizeY/2)*squareWidth+squareWidth/2) + "px",
53 "width": (this.choices.length * squareWidth) + "px",
54 "height": squareWidth + "px",
57 this.choices.map(m => { //a "choice" is a move
62 ['board'+sizeY]: true,
65 'width': (100/this.choices.length) + "%",
66 'padding-bottom': (100/this.choices.length) + "%",
71 attrs: { "src": '/images/pieces/' +
72 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
73 'class': { 'choice-piece': true },
75 "click": e => { this.play(m); this.choices=[]; },
76 // NOTE: add 'touchstart' event to fix a problem on smartphones
77 "touchstart": e => { this.play(m); this.choices=[]; },
84 // Create board element (+ reserves if needed by variant or mode)
85 const lm = this.lastMove;
86 const showLight = this.hints && this.vname != "Dark";
95 [...Array(sizeX).keys()].map(i => {
96 let ci = (this.orientation=='w' ? i : sizeX-i-1);
103 style: { 'opacity': this.choices.length>0?"0.5":"1" },
105 [...Array(sizeY).keys()].map(j => {
106 let cj = (this.orientation=='w' ? j : sizeY-j-1);
108 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
109 || this.analyze || (!!this.userColor
110 && this.vr.enlightened[this.userColor][ci][cj])))
118 'ghost': !!this.selectedPiece
119 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
122 src: "/images/pieces/" +
123 V.getPpath(this.vr.board[ci][cj]) + ".svg",
129 if (this.hints && hintSquares[ci][cj])
139 src: "/images/mark.svg",
150 ['board'+sizeY]: true,
151 'light-square': (i+j)%2==0,
152 'dark-square': (i+j)%2==1,
154 'in-shadow': this.vname=="Dark" && !this.analyze
156 || !this.vr.enlightened[this.userColor][ci][cj]),
157 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
158 'incheck': showLight && incheckSq[ci][cj],
161 id: getSquareId({x:ci,y:cj}),
170 const playingColor = this.userColor || "w"; //default for an observer
171 let elementArray = [choices, gameDiv];
172 if (!!this.vr.reserve)
174 const shiftIdx = (playingColor=="w" ? 0 : 1);
175 let myReservePiecesArray = [];
176 for (let i=0; i<V.RESERVE_PIECES.length; i++)
178 myReservePiecesArray.push(h('div',
180 'class': {'board':true, ['board'+sizeY]:true},
181 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
186 'class': {"piece":true, "reserve":true},
188 "src": "/images/pieces/" +
189 this.vr.getReservePpath(playingColor,i) + ".svg",
193 {"class": { "reserve-count": true } },
194 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
198 let oppReservePiecesArray = [];
199 const oppCol = V.GetOppCol(playingColor);
200 for (let i=0; i<V.RESERVE_PIECES.length; i++)
202 oppReservePiecesArray.push(h('div',
204 'class': {'board':true, ['board'+sizeY]:true},
205 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
210 'class': {"piece":true, "reserve":true},
212 "src": "/images/pieces/" +
213 this.vr.getReservePpath(oppCol,i) + ".svg",
217 {"class": { "reserve-count": true } },
218 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
222 let reserves = h('div',
234 "reserve-row-1": true,
240 { 'class': { 'row': true }},
241 oppReservePiecesArray
245 elementArray.push(reserves);
250 // NOTE: click = mousedown + mouseup
252 mousedown: this.mousedown,
253 mousemove: this.mousemove,
254 mouseup: this.mouseup,
255 touchstart: this.mousedown,
256 touchmove: this.mousemove,
257 touchend: this.mouseup,
263 mounted: function() {
264 // NOTE (TODO?): could also be dependent on page type (game, analyze, variant)
265 const boardSize = localStorage.getItem("boardSize");
267 document.querySelector(".game").style.width = boardSize + "px";
270 mousedown: function(e) {
271 e = e || window.event;
274 while (!ingame && elem !== null)
276 if (elem.classList.contains("game"))
281 elem = elem.parentElement;
283 if (!ingame) //let default behavior (click on button...)
285 e.preventDefault(); //disable native drag & drop
286 if (!this.selectedPiece && e.target.classList.contains("piece"))
288 // Next few lines to center the piece on mouse cursor
289 let rect = e.target.parentNode.getBoundingClientRect();
291 x: rect.x + rect.width/2,
292 y: rect.y + rect.width/2,
293 id: e.target.parentNode.id
295 this.selectedPiece = e.target.cloneNode();
296 this.selectedPiece.style.position = "absolute";
297 this.selectedPiece.style.top = 0;
298 this.selectedPiece.style.display = "inline-block";
299 this.selectedPiece.style.zIndex = 3000;
300 const startSquare = getSquareFromId(e.target.parentNode.id);
301 this.possibleMoves = [];
302 const color = (this.analyze ? this.vr.turn : this.userColor);
303 if (this.vr.canIplay(color,startSquare))
304 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
305 // Next line add moving piece just after current image
306 // (required for Crazyhouse reserve)
307 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
310 mousemove: function(e) {
311 if (!this.selectedPiece)
313 e = e || window.event;
314 // If there is an active element, move it around
315 if (!!this.selectedPiece)
317 const [offsetX,offsetY] = !!e.clientX
318 ? [e.clientX,e.clientY] //desktop browser
319 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
320 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
321 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
324 mouseup: function(e) {
325 if (!this.selectedPiece)
327 e = e || window.event;
328 // Read drop target (or parentElement, parentNode... if type == "img")
329 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
330 const [offsetX,offsetY] = !!e.clientX
331 ? [e.clientX,e.clientY]
332 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
333 let landing = document.elementFromPoint(offsetX, offsetY);
334 this.selectedPiece.style.zIndex = 3000;
335 // Next condition: classList.contains(piece) fails because of marks
336 while (landing.tagName == "IMG")
337 landing = landing.parentNode;
338 if (this.start.id == landing.id)
340 // A click: selectedPiece and possibleMoves are already filled
343 // OK: process move attempt
344 let endSquare = getSquareFromId(landing.id);
345 let moves = this.findMatchingMoves(endSquare);
346 this.possibleMoves = [];
347 if (moves.length > 1)
348 this.choices = moves;
349 else if (moves.length==1)
351 // Else: impossible move
352 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
353 delete this.selectedPiece;
354 this.selectedPiece = null;
356 findMatchingMoves: function(endSquare) {
357 // Run through moves list and return the matching set (if promotions...)
359 this.possibleMoves.forEach(function(m) {
360 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
365 play: function(move) {
366 this.$emit('play-move', move);
382 // NOTE: no variants with reserve of size != 8
387 display: inline-block
392 padding-bottom: 12.5%
403 //width: #{'min(80vw, 500px)'}
407 @media screen and (max-width: 767px)
412 margin: 0 auto 0 auto
416 background-color: rgba(0,0,0,0)
419 background-color: #e6ee9c
421 background-color: skyblue
430 img.piece, img.mark-square
449 background-color: #00cc66 !important
452 filter: brightness(50%)
455 background-color: #cc3300 !important
457 .light-square.lichess
458 background-color: #f0d9b5;
460 background-color: #b58863;
462 .light-square.chesscom
463 background-color: #e5e5ca;
464 .dark-square.chesscom
465 background-color: #6f8f57;
467 .light-square.chesstempo
468 background-color: #fdfdfd;
469 .dark-square.chesstempo
470 background-color: #88a0a8;