2 import { getSquareId, getSquareFromId } from "@/utils/squareId";
3 import { ArrayFun } from "@/utils/array";
4 import { store } from "@/store";
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","incheck","orientation","userColor","vname"],
13 possibleMoves: [], //filled after each valid click/dragstart
14 choices: [], //promotion pieces, or checkered captures... (as moves)
15 selectedPiece: null, //moving piece (or clicked piece)
16 start: {}, //pixels coordinates + id of starting square (click or drag)
17 settings: store.state.settings,
23 // Return empty div of class 'game' to avoid error when setting size
31 const [sizeX,sizeY] = [V.size.x,V.size.y];
32 // Precompute hints squares to facilitate rendering
33 let hintSquares = ArrayFun.init(sizeX, sizeY, false);
34 this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; });
35 // Also precompute in-check squares
36 let incheckSq = ArrayFun.init(sizeX, sizeY, false);
37 this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; });
39 // Create board element (+ reserves if needed by variant or mode)
40 const lm = this.lastMove;
41 const showLight = this.settings.highlight && this.vname != "Dark";
50 [...Array(sizeX).keys()].map(i => {
51 let ci = (this.orientation=='w' ? i : sizeX-i-1);
58 style: { 'opacity': this.choices.length>0?"0.5":"1" },
60 [...Array(sizeY).keys()].map(j => {
61 let cj = (this.orientation=='w' ? j : sizeY-j-1);
63 if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark"
64 || this.analyze || (!!this.userColor
65 && this.vr.enlightened[this.userColor][ci][cj])))
73 'ghost': !!this.selectedPiece
74 && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj,
77 src: "/images/pieces/" +
78 V.getPpath(this.vr.board[ci][cj]) + ".svg",
84 if (this.settings.hints && hintSquares[ci][cj])
94 src: "/images/mark.svg",
105 ['board'+sizeY]: true,
106 'light-square': (i+j)%2==0,
107 'dark-square': (i+j)%2==1,
108 [this.settings.bcolor]: true,
109 'in-shadow': this.vname=="Dark" && !this.analyze
111 || !this.vr.enlightened[this.userColor][ci][cj]),
112 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj,
113 'incheck': showLight && incheckSq[ci][cj],
116 id: getSquareId({x:ci,y:cj}),
125 let elementArray = [gameDiv];
126 const playingColor = this.userColor || "w"; //default for an observer
127 if (!!this.vr.reserve)
129 const shiftIdx = (playingColor=="w" ? 0 : 1);
130 let myReservePiecesArray = [];
131 for (let i=0; i<V.RESERVE_PIECES.length; i++)
133 myReservePiecesArray.push(h('div',
135 'class': {'board':true, ['board'+sizeY]:true},
136 attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) }
141 'class': {"piece":true, "reserve":true},
143 "src": "/images/pieces/" +
144 this.vr.getReservePpath(playingColor,i) + ".svg",
148 {"class": { "reserve-count": true } },
149 [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ]
153 let oppReservePiecesArray = [];
154 const oppCol = V.GetOppCol(playingColor);
155 for (let i=0; i<V.RESERVE_PIECES.length; i++)
157 oppReservePiecesArray.push(h('div',
159 'class': {'board':true, ['board'+sizeY]:true},
160 attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) }
165 'class': {"piece":true, "reserve":true},
167 "src": "/images/pieces/" +
168 this.vr.getReservePpath(oppCol,i) + ".svg",
172 {"class": { "reserve-count": true } },
173 [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ]
177 let reserves = h('div',
189 "reserve-row-1": true,
195 { 'class': { 'row': true }},
196 oppReservePiecesArray
200 elementArray.push(reserves);
202 const boardElt = document.querySelector(".game");
203 if (this.choices.length > 0 && !!boardElt) //no choices to show at first drawing
205 const squareWidth = boardElt.offsetWidth / sizeY;
206 const offset = [boardElt.offsetTop, boardElt.offsetLeft];
210 attrs: { "id": "choices" },
211 'class': { 'row': true },
213 "top": (offset[0] + (sizeY/2)*squareWidth-squareWidth/2) + "px",
214 "left": (offset[1] + squareWidth*(sizeY - this.choices.length)/2) + "px",
215 "width": (this.choices.length * squareWidth) + "px",
216 "height": squareWidth + "px",
219 this.choices.map(m => { //a "choice" is a move
224 ['board'+sizeY]: true,
227 'width': (100/this.choices.length) + "%",
228 'padding-bottom': (100/this.choices.length) + "%",
233 attrs: { "src": '/images/pieces/' +
234 V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' },
235 'class': { 'choice-piece': true },
237 "click": e => { this.play(m); this.choices=[]; },
244 elementArray.unshift(choices);
247 // NOTE: click = mousedown + mouseup
248 if ('ontouchstart' in window)
252 touchstart: this.mousedown,
253 touchmove: this.mousemove,
254 touchend: this.mouseup,
262 mousedown: this.mousedown,
263 mousemove: this.mousemove,
264 mouseup: this.mouseup,
275 mousedown: function(e) {
276 // Abort if a piece is already being processed, or target is not a piece.
277 // NOTE: just looking at classList[0] because piece is the first assigned class
278 if (!!this.selectedPiece || e.target.classList[0] != "piece")
280 e.preventDefault(); //disable native drag & drop
281 let parent = e.target.parentNode; //the surrounding square
282 // Next few lines to center the piece on mouse cursor
283 let rect = parent.getBoundingClientRect();
285 x: rect.x + rect.width/2,
286 y: rect.y + rect.width/2,
289 this.selectedPiece = e.target.cloneNode();
290 let spStyle = this.selectedPiece.style
291 spStyle.position = "absolute";
293 spStyle.display = "inline-block";
294 spStyle.zIndex = 3000;
295 const startSquare = getSquareFromId(parent.id);
296 this.possibleMoves = [];
297 const color = (this.analyze ? this.vr.turn : this.userColor);
298 if (this.vr.canIplay(color,startSquare))
299 this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare);
300 // Next line add moving piece just after current image
301 // (required for Crazyhouse reserve)
302 parent.insertBefore(this.selectedPiece, e.target.nextSibling);
304 mousemove: function(e) {
305 if (!this.selectedPiece)
307 // There is an active element: move it around
308 const [offsetX,offsetY] = !!e.clientX
309 ? [e.clientX,e.clientY] //desktop browser
310 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone
311 this.selectedPiece.style.left = (offsetX-this.start.x) + "px";
312 this.selectedPiece.style.top = (offsetY-this.start.y) + "px";
314 mouseup: function(e) {
315 if (!this.selectedPiece)
317 // There is an active element: obtain the move from start and end squares
318 this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords
319 const [offsetX,offsetY] = !!e.clientX
320 ? [e.clientX,e.clientY]
321 : [e.changedTouches[0].pageX, e.changedTouches[0].pageY];
322 let landing = document.elementFromPoint(offsetX, offsetY);
323 this.selectedPiece.style.zIndex = 3000;
324 // Next condition: classList.contains(piece) fails because of marks
325 while (landing.tagName == "IMG")
326 landing = landing.parentNode;
327 if (this.start.id == landing.id) //one or multi clicks on same piece
329 // OK: process move attempt, landing is a square node
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);
358 <style lang="sass" scoped>
368 // NOTE: no variants with reserve of size != 8
381 background-color: rgba(0,0,0,0)
384 background-color: #e6ee9c
386 background-color: skyblue
398 background-color: #00cc66 !important
401 background-color: #cc3300 !important
403 .light-square.lichess
404 background-color: #f0d9b5;
406 background-color: #b58863;
408 .light-square.chesscom
409 background-color: #e5e5ca;
410 .dark-square.chesscom
411 background-color: #6f8f57;
413 .light-square.chesstempo
414 background-color: #fdfdfd;
415 .dark-square.chesstempo
416 background-color: #88a0a8;