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 const [sizeX,sizeY] = [V.size.x,V.size.y];
26 // Precompute hints squares to facilitate rendering
27 let hintSquares = ArrayFun.init(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 = ArrayFun.init(sizeX, sizeY, false);
31 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
33 let firstRow = document.querySelector(".game > .row");
34 const squareWidth = (!!firstRow
35 ? document.querySelector(".game > .row").offsetWidth / sizeY
36 : 40); //arbitrary value (not relevant)
40 attrs: { "id": "choices" },
41 'class': { 'row': true },
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",
49 this.choices.map(m => { //a "choice" is a move
54 ['board'+sizeY]: true,
57 'width': (100/this.choices.length) + "%",
58 'padding-bottom': (100/this.choices.length) + "%",
63 attrs: { "src": '/images/pieces/' +
64 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
65 'class': { 'choice-piece': true },
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=[]; },
76 // Create board element (+ reserves if needed by variant or mode)
77 const lm = this.lastMove;
78 const showLight = this.hints && this.vname != "Dark";
87 [...Array(sizeX).keys()].map(i => {
88 let ci = (this.orientation=='w' ? i : sizeX-i-1);
95 style: { 'opacity': this.choices.length>0?"0.5":"1" },
97 [...Array(sizeY).keys()].map(j => {
98 let cj = (this.orientation=='w' ? j : sizeY-j-1);
100 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
101 || this.analyze || (!!this.userColor
102 && this.vr.enlightened[this.userColor][ci][cj])))
110 'ghost': !!this.selectedPiece
111 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
114 src: "/images/pieces/" +
115 V.getPpath(this.vr.board[ci][cj]) + ".svg",
121 if (this.hints && hintSquares[ci][cj])
131 src: "/images/mark.svg",
142 ['board'+sizeY]: true,
143 'light-square': (i+j)%2==0,
144 'dark-square': (i+j)%2==1,
146 'in-shadow': this.vname=="Dark" && !this.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],
153 id: getSquareId({x:ci,y:cj}),
162 const playingColor = this.userColor || "w"; //default for an observer
163 let elementArray = [choices, gameDiv];
164 if (!!this.vr.reserve)
166 const shiftIdx = (playingColor=="w" ? 0 : 1);
167 let myReservePiecesArray = [];
168 for (let i=0; i<V.RESERVE_PIECES.length; i++)
170 myReservePiecesArray.push(h('div',
172 'class': {'board':true, ['board'+sizeY]:true},
173 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
178 'class': {"piece":true, "reserve":true},
180 "src": "/images/pieces/" +
181 this.vr.getReservePpath(playingColor,i) + ".svg",
185 {"class": { "reserve-count": true } },
186 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
190 let oppReservePiecesArray = [];
191 const oppCol = V.GetOppCol(playingColor);
192 for (let i=0; i<V.RESERVE_PIECES.length; i++)
194 oppReservePiecesArray.push(h('div',
196 'class': {'board':true, ['board'+sizeY]:true},
197 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
202 'class': {"piece":true, "reserve":true},
204 "src": "/images/pieces/" +
205 this.vr.getReservePpath(oppCol,i) + ".svg",
209 {"class": { "reserve-count": true } },
210 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
214 let reserves = h('div',
226 "reserve-row-1": true,
232 { 'class': { 'row': true }},
233 oppReservePiecesArray
237 elementArray.push(reserves);
242 // NOTE: click = mousedown + mouseup
244 mousedown: this.mousedown,
245 mousemove: this.mousemove,
246 mouseup: this.mouseup,
247 touchstart: this.mousedown,
248 touchmove: this.mousemove,
249 touchend: this.mouseup,
256 mousedown: function(e) {
257 e = e || window.event;
260 while (!ingame && elem !== null)
262 if (elem.classList.contains("game"))
267 elem = elem.parentElement;
269 if (!ingame) //let default behavior (click on button...)
271 e.preventDefault(); //disable native drag & drop
272 if (!this.selectedPiece && e.target.classList.contains("piece"))
274 // Next few lines to center the piece on mouse cursor
275 let rect = e.target.parentNode.getBoundingClientRect();
277 x: rect.x + rect.width/2,
278 y: rect.y + rect.width/2,
279 id: e.target.parentNode.id
281 this.selectedPiece = e.target.cloneNode();
282 this.selectedPiece.style.position = "absolute";
283 this.selectedPiece.style.top = 0;
284 this.selectedPiece.style.display = "inline-block";
285 this.selectedPiece.style.zIndex = 3000;
286 const startSquare = getSquareFromId(e.target.parentNode.id);
287 this.possibleMoves = [];
288 const color = (this.analyze ? this.vr.turn : this.userColor);
289 if (this.vr.canIplay(color,startSquare))
290 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
291 // Next line add moving piece just after current image
292 // (required for Crazyhouse reserve)
293 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
296 mousemove: function(e) {
297 if (!this.selectedPiece)
299 e = e || window.event;
300 // If there is an active element, move it around
301 if (!!this.selectedPiece)
303 const [offsetX,offsetY] = !!e.clientX
304 ? [e.clientX,e.clientY] //desktop browser
305 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
306 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
307 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
310 mouseup: function(e) {
311 if (!this.selectedPiece)
313 e = e || window.event;
314 // Read drop target (or parentElement, parentNode... if type == "img")
315 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
316 const [offsetX,offsetY] = !!e.clientX
317 ? [e.clientX,e.clientY]
318 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
319 let landing = document.elementFromPoint(offsetX, offsetY);
320 this.selectedPiece.style.zIndex = 3000;
321 // Next condition: classList.contains(piece) fails because of marks
322 while (landing.tagName == "IMG")
323 landing = landing.parentNode;
324 if (this.start.id == landing.id)
326 // A click: selectedPiece and possibleMoves are already filled
329 // OK: process move attempt
330 let endSquare = getSquareFromId(landing.id);
331 let moves = this.findMatchingMoves(endSquare);
332 this.possibleMoves = [];
333 if (moves.length > 1)
334 this.choices = moves;
335 else if (moves.length==1)
337 // Else: impossible move
338 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
339 delete this.selectedPiece;
340 this.selectedPiece = null;
342 findMatchingMoves: function(endSquare) {
343 // Run through moves list and return the matching set (if promotions...)
345 this.possibleMoves.forEach(function(m) {
346 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
351 play: function(move) {
352 this.$emit('play-move', move);
368 // NOTE: no variants with reserve of size != 8
373 display: inline-block
378 padding-bottom: 12.5%
389 width: #{'min(80vw, 500px)'}
393 @media screen and (max-width: 767px)
398 margin: 0 auto 0 auto
402 background-color: rgba(0,0,0,0)
405 background-color: #e6ee9c
407 background-color: skyblue
416 img.piece, img.mark-square
435 background-color: #00cc66 !important
438 filter: brightness(50%)
441 background-color: #cc3300 !important
443 .light-square.lichess
444 background-color: #f0d9b5;
446 background-color: #b58863;
448 .light-square.chesscom
449 background-color: #e5e5ca;
450 .dark-square.chesscom
451 background-color: #6f8f57;
453 .light-square.chesstempo
454 background-color: #fdfdfd;
455 .dark-square.chesstempo
456 background-color: #88a0a8;