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; });
32 const squareWidth = 40; //TODO: compute this
36 attrs: { "id": "choices" },
37 'class': { 'row': true },
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",
45 this.choices.map(m => { //a "choice" is a move
50 ['board'+sizeY]: true,
53 'width': (100/this.choices.length) + "%",
54 'padding-bottom': (100/this.choices.length) + "%",
59 attrs: { "src": '/images/pieces/' +
60 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
61 'class': { 'choice-piece': true },
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=[]; },
72 // Create board element (+ reserves if needed by variant or mode)
73 const lm = this.lastMove;
74 const showLight = this.hints && this.vname != "Dark";
83 [...Array(sizeX).keys()].map(i => {
84 let ci = (this.orientation=='w' ? i : sizeX-i-1);
91 style: { 'opacity': this.choices.length>0?"0.5":"1" },
93 [...Array(sizeY).keys()].map(j => {
94 let cj = (this.orientation=='w' ? j : sizeY-j-1);
96 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
97 || this.analyze || (!!this.userColor
98 && this.vr.enlightened[this.userColor][ci][cj])))
106 'ghost': !!this.selectedPiece
107 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
110 src: "/images/pieces/" +
111 V.getPpath(this.vr.board[ci][cj]) + ".svg",
117 if (this.hints && hintSquares[ci][cj])
127 src: "/images/mark.svg",
138 ['board'+sizeY]: true,
139 'light-square': (i+j)%2==0,
140 'dark-square': (i+j)%2==1,
142 'in-shadow': this.vname=="Dark" && !this.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],
149 id: getSquareId({x:ci,y:cj}),
158 const playingColor = this.userColor || "w"; //default for an observer
159 let elementArray = [choices, gameDiv];
160 if (!!this.vr.reserve)
162 const shiftIdx = (playingColor=="w" ? 0 : 1);
163 let myReservePiecesArray = [];
164 for (let i=0; i<V.RESERVE_PIECES.length; i++)
166 myReservePiecesArray.push(h('div',
168 'class': {'board':true, ['board'+sizeY]:true},
169 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
174 'class': {"piece":true, "reserve":true},
176 "src": "/images/pieces/" +
177 this.vr.getReservePpath(playingColor,i) + ".svg",
181 {"class": { "reserve-count": true } },
182 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
186 let oppReservePiecesArray = [];
187 const oppCol = V.GetOppCol(playingColor);
188 for (let i=0; i<V.RESERVE_PIECES.length; i++)
190 oppReservePiecesArray.push(h('div',
192 'class': {'board':true, ['board'+sizeY]:true},
193 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
198 'class': {"piece":true, "reserve":true},
200 "src": "/images/pieces/" +
201 this.vr.getReservePpath(oppCol,i) + ".svg",
205 {"class": { "reserve-count": true } },
206 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
210 let reserves = h('div',
222 "reserve-row-1": true,
228 { 'class': { 'row': true }},
229 oppReservePiecesArray
233 elementArray.push(reserves);
238 // NOTE: click = mousedown + mouseup
240 mousedown: this.mousedown,
241 mousemove: this.mousemove,
242 mouseup: this.mouseup,
243 touchstart: this.mousedown,
244 touchmove: this.mousemove,
245 touchend: this.mouseup,
252 mousedown: function(e) {
253 e = e || window.event;
256 while (!ingame && elem !== null)
258 if (elem.classList.contains("game"))
263 elem = elem.parentElement;
265 if (!ingame) //let default behavior (click on button...)
267 e.preventDefault(); //disable native drag & drop
268 if (!this.selectedPiece && e.target.classList.contains("piece"))
270 // Next few lines to center the piece on mouse cursor
271 let rect = e.target.parentNode.getBoundingClientRect();
273 x: rect.x + rect.width/2,
274 y: rect.y + rect.width/2,
275 id: e.target.parentNode.id
277 this.selectedPiece = e.target.cloneNode();
278 this.selectedPiece.style.position = "absolute";
279 this.selectedPiece.style.top = 0;
280 this.selectedPiece.style.display = "inline-block";
281 this.selectedPiece.style.zIndex = 3000;
282 const startSquare = getSquareFromId(e.target.parentNode.id);
283 this.possibleMoves = [];
284 const color = (this.analyze ? this.vr.turn : this.userColor);
285 if (this.vr.canIplay(color,startSquare))
286 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
287 // Next line add moving piece just after current image
288 // (required for Crazyhouse reserve)
289 e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling);
292 mousemove: function(e) {
293 if (!this.selectedPiece)
295 e = e || window.event;
296 // If there is an active element, move it around
297 if (!!this.selectedPiece)
299 const [offsetX,offsetY] = !!e.clientX
300 ? [e.clientX,e.clientY] //desktop browser
301 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
302 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
303 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
306 mouseup: function(e) {
307 if (!this.selectedPiece)
309 e = e || window.event;
310 // Read drop target (or parentElement, parentNode... if type == "img")
311 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
312 const [offsetX,offsetY] = !!e.clientX
313 ? [e.clientX,e.clientY]
314 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
315 let landing = document.elementFromPoint(offsetX, offsetY);
316 this.selectedPiece.style.zIndex = 3000;
317 // Next condition: classList.contains(piece) fails because of marks
318 while (landing.tagName == "IMG")
319 landing = landing.parentNode;
320 if (this.start.id == landing.id)
322 // A click: selectedPiece and possibleMoves are already filled
325 // OK: process move attempt
326 let endSquare = getSquareFromId(landing.id);
327 let moves = this.findMatchingMoves(endSquare);
328 this.possibleMoves = [];
329 if (moves.length > 1)
330 this.choices = moves;
331 else if (moves.length==1)
333 // Else: impossible move
334 this.selectedPiece.parentNode.removeChild(this.selectedPiece);
335 delete this.selectedPiece;
336 this.selectedPiece = null;
338 findMatchingMoves: function(endSquare) {
339 // Run through moves list and return the matching set (if promotions...)
341 this.possibleMoves.forEach(function(m) {
342 if (endSquare[0] == m.end.x && endSquare[1] == m.end.y)
347 play: function(move) {
348 this.$emit('play-move', move);
364 // NOTE: no variants with reserve of size != 8
369 display: inline-block
374 padding-bottom: 12.5%
389 @media screen and (max-width: 767px)
394 margin: 0 auto 0 auto
398 background-color: rgba(0,0,0,0)
401 background-color: #e6ee9c
403 background-color: skyblue
412 img.piece, img.mark-square
431 background-color: #00cc66 !important
434 filter: brightness(50%)
437 background-color: #cc3300 !important
439 .light-square.lichess
440 background-color: #f0d9b5;
442 background-color: #b58863;
444 .light-square.chesscom
445 background-color: #e5e5ca;
446 .dark-square.chesscom
447 background-color: #6f8f57;
449 .light-square.chesstempo
450 background-color: #fdfdfd;
451 .dark-square.chesstempo
452 background-color: #88a0a8;