4cadb0bd9ba75d68e8d2950aa302c3763564e301
1 hints: (!localStorage
["hints"] ? true : localStorage
["hints"] === "1"),
2 bcolor: localStorage
["bcolor"] || "lichess", //lichess, chesscom or chesstempo
3 possibleMoves: [], //filled after each valid click/dragstart
4 choices: [], //promotion pieces, or checkered captures... (as moves)
5 selectedPiece: null, //moving piece (or clicked piece)
7 start: {}, //pixels coordinates + id of starting square (click or drag)
8 vr: null, //object to check moves, store them, FEN..
9 orientation: "w", //useful if click on "flip board"
13 const [sizeX
,sizeY
] = [V
.size
.x
,V
.size
.y
];
14 // Precompute hints squares to facilitate rendering
15 let hintSquares
= doubleArray(sizeX
, sizeY
, false);
16 this.possibleMoves
.forEach(m
=> { hintSquares
[m
.end
.x
][m
.end
.y
] = true; });
17 // Also precompute in-check squares
18 let incheckSq
= doubleArray(sizeX
, sizeY
, false);
19 this.incheck
.forEach(sq
=> { incheckSq
[sq
[0]][sq
[1]] = true; });
20 const choices
= h('div',
22 attrs: { "id": "choices" },
23 'class': { 'row': true },
25 "display": this.choices
.length
>0?"block":"none",
26 "top": "-" + ((sizeY
/2)*squareWidth+squareWidth/2) + "px",
27 "width": (this.choices
.length
* squareWidth
) + "px",
28 "height": squareWidth
+ "px",
31 this.choices
.map( m
=> { //a "choice" is a move
36 ['board'+sizeY
]: true,
39 'width': (100/this.choices
.length
) + "%",
40 'padding-bottom': (100/this.choices
.length
) + "%",
45 attrs: { "src": '/images/pieces/' +
46 VariantRules
.getPpath(m
.appear
[0].c
+m
.appear
[0].p
) + '.svg' },
47 'class': { 'choice-piece': true },
49 "click": e
=> { this.play(m
); this.choices
=[]; },
50 // NOTE: add 'touchstart' event to fix a problem on smartphones
51 "touchstart": e
=> { this.play(m
); this.choices
=[]; },
58 // Create board element (+ reserves if needed by variant or mode)
59 const lm
= this.vr
.lastMove
;
60 const showLight
= this.hints
&& variant
!="Dark" &&
61 (this.mode
!= "idle" ||
62 (this.vr
.moves
.length
> 0 && this.cursor
==this.vr
.moves
.length
));
63 const gameDiv
= h('div',
70 [_
.range(sizeX
).map(i
=> {
71 let ci
= (this.mycolor
=='w' ? i : sizeX
-i
-1);
78 style: { 'opacity': this.choices
.length
>0?"0.5":"1" },
80 _
.range(sizeY
).map(j
=> {
81 let cj
= (this.mycolor
=='w' ? j : sizeY
-j
-1);
83 if (this.vr
.board
[ci
][cj
] != VariantRules
.EMPTY
&& (variant
!="Dark"
84 || this.score
!="*" || this.vr
.enlightened
[this.mycolor
][ci
][cj
]))
92 'ghost': !!this.selectedPiece
93 && this.selectedPiece
.parentNode
.id
== "sq-"+ci
+"-"+cj
,
96 src: "/images/pieces/" +
97 VariantRules
.getPpath(this.vr
.board
[ci
][cj
]) + ".svg",
103 if (this.hints
&& hintSquares
[ci
][cj
])
113 src: "/images/mark.svg",
124 ['board'+sizeY
]: true,
125 'light-square': (i
+j
)%2==0,
126 'dark-square': (i
+j
)%2==1,
128 'in-shadow': variant
=="Dark" && this.score
=="*"
129 && !this.vr
.enlightened
[this.mycolor
][ci
][cj
],
130 'highlight': showLight
&& !!lm
&& _
.isMatch(lm
.end
, {x:ci
,y:cj
}),
131 'incheck': showLight
&& incheckSq
[ci
][cj
],
134 id: this.getSquareId({x:ci
,y:cj
}),
143 if (!!this.vr
.reserve
)
145 const shiftIdx
= (this.mycolor
=="w" ? 0 : 1);
146 let myReservePiecesArray
= [];
147 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
149 myReservePiecesArray
.push(h('div',
151 'class': {'board':true, ['board'+sizeY
]:true},
152 attrs: { id: this.getSquareId({x:sizeX
+shiftIdx
,y:i
}) }
157 'class': {"piece":true, "reserve":true},
159 "src": "/images/pieces/" +
160 this.vr
.getReservePpath(this.mycolor
,i
) + ".svg",
164 {"class": { "reserve-count": true } },
165 [ this.vr
.reserve
[this.mycolor
][VariantRules
.RESERVE_PIECES
[i
]] ]
169 let oppReservePiecesArray
= [];
170 const oppCol
= this.vr
.getOppCol(this.mycolor
);
171 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
173 oppReservePiecesArray
.push(h('div',
175 'class': {'board':true, ['board'+sizeY
]:true},
176 attrs: { id: this.getSquareId({x:sizeX
+(1-shiftIdx
),y:i
}) }
181 'class': {"piece":true, "reserve":true},
183 "src": "/images/pieces/" +
184 this.vr
.getReservePpath(oppCol
,i
) + ".svg",
188 {"class": { "reserve-count": true } },
189 [ this.vr
.reserve
[oppCol
][VariantRules
.RESERVE_PIECES
[i
]] ]
193 let reserves
= h('div',
205 "reserve-row-1": true,
211 { 'class': { 'row': true }},
212 oppReservePiecesArray
216 elementArray
.push(reserves
);
218 // Show current FEN (just below board, lower right corner)
219 // (if mode != Dark ...)
223 attrs: { id: "fen-div" },
224 "class": { "section-content": true },
229 attrs: { id: "fen-string" },
230 domProps: { innerHTML: this.vr
.getBaseFen() },
231 "class": { "text-center": true },
238 mousedown: this.mousedown
,
239 mousemove: this.mousemove
,
240 mouseup: this.mouseup
,
241 touchstart: this.mousedown
,
242 touchmove: this.mousemove
,
243 touchend: this.mouseup
,
247 // TODO: "chessground-like" component
248 // Get the identifier of a HTML table cell from its numeric coordinates o.x,o.y.
249 getSquareId: function(o
) {
250 // NOTE: a separator is required to allow any size of board
251 return "sq-" + o
.x
+ "-" + o
.y
;
254 getSquareFromId: function(id
) {
255 let idParts
= id
.split('-');
256 return [parseInt(idParts
[1]), parseInt(idParts
[2])];
258 mousedown: function(e
) {
259 e
= e
|| window
.event
;
262 while (!ingame
&& elem
!== null)
264 if (elem
.classList
.contains("game"))
269 elem
= elem
.parentElement
;
271 if (!ingame
) //let default behavior (click on button...)
273 e
.preventDefault(); //disable native drag & drop
274 if (!this.selectedPiece
&& e
.target
.classList
.contains("piece"))
276 // Next few lines to center the piece on mouse cursor
277 let rect
= e
.target
.parentNode
.getBoundingClientRect();
279 x: rect
.x
+ rect
.width
/2,
280 y: rect
.y
+ rect
.width
/2,
281 id: e
.target
.parentNode
.id
283 this.selectedPiece
= e
.target
.cloneNode();
284 this.selectedPiece
.style
.position
= "absolute";
285 this.selectedPiece
.style
.top
= 0;
286 this.selectedPiece
.style
.display
= "inline-block";
287 this.selectedPiece
.style
.zIndex
= 3000;
288 const startSquare
= this.getSquareFromId(e
.target
.parentNode
.id
);
289 this.possibleMoves
= [];
290 if (this.score
== "*")
292 const color
= ["friend","problem"].includes(this.mode
)
295 if (this.vr
.canIplay(color
,startSquare
))
296 this.possibleMoves
= this.vr
.getPossibleMovesFrom(startSquare
);
298 // Next line add moving piece just after current image
299 // (required for Crazyhouse reserve)
300 e
.target
.parentNode
.insertBefore(this.selectedPiece
, e
.target
.nextSibling
);
303 mousemove: function(e
) {
304 if (!this.selectedPiece
)
306 e
= e
|| window
.event
;
307 // If there is an active element, move it around
308 if (!!this.selectedPiece
)
310 const [offsetX
,offsetY
] = !!e
.clientX
311 ? [e
.clientX
,e
.clientY
] //desktop browser
312 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
]; //smartphone
313 this.selectedPiece
.style
.left
= (offsetX
-this.start
.x
) + "px";
314 this.selectedPiece
.style
.top
= (offsetY
-this.start
.y
) + "px";
317 mouseup: function(e
) {
318 if (!this.selectedPiece
)
320 e
= e
|| window
.event
;
321 // Read drop target (or parentElement, parentNode... if type == "img")
322 this.selectedPiece
.style
.zIndex
= -3000; //HACK to find square from final coords
323 const [offsetX
,offsetY
] = !!e
.clientX
324 ? [e
.clientX
,e
.clientY
]
325 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
];
326 let landing
= document
.elementFromPoint(offsetX
, offsetY
);
327 this.selectedPiece
.style
.zIndex
= 3000;
328 // Next condition: classList.contains(piece) fails because of marks
329 while (landing
.tagName
== "IMG")
330 landing
= landing
.parentNode
;
331 if (this.start
.id
== landing
.id
)
333 // A click: selectedPiece and possibleMoves are already filled
336 // OK: process move attempt
337 let endSquare
= this.getSquareFromId(landing
.id
);
338 let moves
= this.findMatchingMoves(endSquare
);
339 this.possibleMoves
= [];
340 if (moves
.length
> 1)
341 this.choices
= moves
;
342 else if (moves
.length
==1)
344 // Else: impossible move
345 this.selectedPiece
.parentNode
.removeChild(this.selectedPiece
);
346 delete this.selectedPiece
;
347 this.selectedPiece
= null;
349 findMatchingMoves: function(endSquare
) {
350 // Run through moves list and return the matching set (if promotions...)
352 this.possibleMoves
.forEach(function(m
) {
353 if (endSquare
[0] == m
.end
.x
&& endSquare
[1] == m
.end
.y
)
358 animateMove: function(move) {
359 let startSquare
= document
.getElementById(this.getSquareId(move.start
));
360 let endSquare
= document
.getElementById(this.getSquareId(move.end
));
361 let rectStart
= startSquare
.getBoundingClientRect();
362 let rectEnd
= endSquare
.getBoundingClientRect();
363 let translation
= {x:rectEnd
.x
-rectStart
.x
, y:rectEnd
.y
-rectStart
.y
};
365 document
.querySelector("#" + this.getSquareId(move.start
) + " > img.piece");
366 // HACK for animation (with positive translate, image slides "under background")
367 // Possible improvement: just alter squares on the piece's way...
368 squares
= document
.getElementsByClassName("board");
369 for (let i
=0; i
<squares
.length
; i
++)
371 let square
= squares
.item(i
);
372 if (square
.id
!= this.getSquareId(move.start
))
373 square
.style
.zIndex
= "-1";
375 movingPiece
.style
.transform
= "translate(" + translation
.x
+ "px," +
376 translation
.y
+ "px)";
377 movingPiece
.style
.transitionDuration
= "0.2s";
378 movingPiece
.style
.zIndex
= "3000";
380 for (let i
=0; i
<squares
.length
; i
++)
381 squares
.item(i
).style
.zIndex
= "auto";
382 movingPiece
.style
= {}; //required e.g. for 0-0 with KR swap