Commit | Line | Data |
---|---|---|
92342261 | 1 | // Game logic on a variant page |
1d184b4c BA |
2 | Vue.component('my-game', { |
3 | data: function() { | |
4 | return { | |
5 | vr: null, //object to check moves, store them, FEN.. | |
6 | mycolor: "w", | |
7 | possibleMoves: [], //filled after each valid click/dragstart | |
92342261 | 8 | choices: [], //promotion pieces, or checkered captures... (as moves) |
1d184b4c BA |
9 | start: {}, //pixels coordinates + id of starting square (click or drag) |
10 | selectedPiece: null, //moving piece (or clicked piece) | |
92342261 | 11 | conn: null, //socket connection |
dfb4afc1 | 12 | score: "*", //'*' means 'unfinished' |
2748531f | 13 | mode: "idle", //human, friend, computer or idle (when not playing) |
1d184b4c BA |
14 | oppid: "", //opponent ID in case of HH game |
15 | oppConnected: false, | |
186516b8 | 16 | seek: false, |
762b7c9c | 17 | fenStart: "", |
4b5fe306 | 18 | incheck: [], |
3840e240 | 19 | pgnTxt: "", |
88af03d2 | 20 | hints: (getCookie("hints") === "1" ? true : false), |
a897b421 BA |
21 | color: getCookie("color", "lichess"), //lichess, chesscom or chesstempo |
22 | // sound level: 0 = no sound, 1 = sound only on newgame, 2 = always | |
8b7aead3 | 23 | sound: parseInt(getCookie("sound", "2")), |
1d184b4c BA |
24 | }; |
25 | }, | |
26 | render(h) { | |
c94bc812 | 27 | const [sizeX,sizeY] = VariantRules.size; |
0bf75837 | 28 | const smallScreen = (screen.width <= 420); |
1d184b4c BA |
29 | // Precompute hints squares to facilitate rendering |
30 | let hintSquares = doubleArray(sizeX, sizeY, false); | |
31 | this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; }); | |
4b5fe306 BA |
32 | // Also precompute in-check squares |
33 | let incheckSq = doubleArray(sizeX, sizeY, false); | |
34 | this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; }); | |
1d184b4c | 35 | let elementArray = []; |
2748531f | 36 | let actionArray = []; |
1dcf83e8 BA |
37 | actionArray.push( |
38 | h('button', | |
39 | { | |
40 | on: { click: this.clickGameSeek }, | |
41 | attrs: { "aria-label": 'New online game' }, | |
42 | 'class': { | |
43 | "tooltip": true, | |
44 | "bottom": true, //display below | |
45 | "seek": this.seek, | |
46 | "playing": this.mode == "human", | |
b8a0ec4a | 47 | "small": smallScreen, |
1d184b4c | 48 | }, |
1dcf83e8 BA |
49 | }, |
50 | [h('i', { 'class': { "material-icons": true } }, "accessibility")]) | |
51 | ); | |
2748531f BA |
52 | if (["idle","computer"].includes(this.mode)) |
53 | { | |
54 | actionArray.push( | |
55 | h('button', | |
1d184b4c | 56 | { |
f3802fcd | 57 | on: { click: this.clickComputerGame }, |
1d184b4c | 58 | attrs: { "aria-label": 'New game VS computer' }, |
186516b8 BA |
59 | 'class': { |
60 | "tooltip":true, | |
0706ea91 | 61 | "bottom": true, |
2748531f | 62 | "playing": this.mode == "computer", |
b8a0ec4a | 63 | "small": smallScreen, |
186516b8 | 64 | }, |
1d184b4c BA |
65 | }, |
66 | [h('i', { 'class': { "material-icons": true } }, "computer")]) | |
2748531f BA |
67 | ); |
68 | } | |
69 | if (["idle","friend"].includes(this.mode)) | |
70 | { | |
71 | actionArray.push( | |
72 | h('button', | |
73 | { | |
74 | on: { click: this.clickFriendGame }, | |
75 | attrs: { "aria-label": 'New IRL game' }, | |
76 | 'class': { | |
77 | "tooltip":true, | |
78 | "bottom": true, | |
79 | "playing": this.mode == "friend", | |
b8a0ec4a | 80 | "small": smallScreen, |
2748531f BA |
81 | }, |
82 | }, | |
83 | [h('i', { 'class': { "material-icons": true } }, "people")]) | |
84 | ); | |
85 | } | |
1d184b4c BA |
86 | if (!!this.vr) |
87 | { | |
bdb1f12d BA |
88 | const square00 = document.getElementById("sq-0-0"); |
89 | const squareWidth = !!square00 | |
90 | ? parseFloat(window.getComputedStyle(square00).width.slice(0,-2)) | |
91 | : 0; | |
88af03d2 BA |
92 | const settingsBtnElt = document.getElementById("settingsBtn"); |
93 | const indicWidth = !!settingsBtnElt //-2 for border: | |
94 | ? parseFloat(window.getComputedStyle(settingsBtnElt).height.slice(0,-2)) - 2 | |
05290bf9 | 95 | : (smallScreen ? 31 : 37); |
1d184b4c BA |
96 | if (this.mode == "human") |
97 | { | |
98 | let connectedIndic = h( | |
99 | 'div', | |
100 | { | |
101 | "class": { | |
bdb1f12d BA |
102 | "topindicator": true, |
103 | "indic-left": true, | |
1d184b4c BA |
104 | "connected": this.oppConnected, |
105 | "disconnected": !this.oppConnected, | |
106 | }, | |
bdb1f12d BA |
107 | style: { |
108 | "width": indicWidth + "px", | |
109 | "height": indicWidth + "px", | |
110 | }, | |
1d184b4c BA |
111 | } |
112 | ); | |
113 | elementArray.push(connectedIndic); | |
114 | } | |
bdb1f12d BA |
115 | let turnIndic = h( |
116 | 'div', | |
117 | { | |
118 | "class": { | |
119 | "topindicator": true, | |
120 | "indic-right": true, | |
121 | "white-turn": this.vr.turn=="w", | |
122 | "black-turn": this.vr.turn=="b", | |
123 | }, | |
124 | style: { | |
125 | "width": indicWidth + "px", | |
126 | "height": indicWidth + "px", | |
127 | }, | |
128 | } | |
129 | ); | |
130 | elementArray.push(turnIndic); | |
88af03d2 | 131 | let settingsBtn = h( |
3ed62725 BA |
132 | 'button', |
133 | { | |
88af03d2 BA |
134 | on: { click: this.showSettings }, |
135 | attrs: { | |
136 | "aria-label": 'Settings', | |
137 | "id": "settingsBtn", | |
138 | }, | |
3ed62725 | 139 | 'class': { |
88af03d2 | 140 | "tooltip": true, |
3ed62725 BA |
141 | "topindicator": true, |
142 | "indic-right": true, | |
05290bf9 BA |
143 | "settings-btn": !smallScreen, |
144 | "settings-btn-small": smallScreen, | |
3ed62725 BA |
145 | }, |
146 | }, | |
88af03d2 | 147 | [h('i', { 'class': { "material-icons": true } }, "settings")] |
3ed62725 | 148 | ); |
88af03d2 | 149 | elementArray.push(settingsBtn); |
1d184b4c BA |
150 | let choices = h('div', |
151 | { | |
152 | attrs: { "id": "choices" }, | |
153 | 'class': { 'row': true }, | |
154 | style: { | |
1d184b4c BA |
155 | "display": this.choices.length>0?"block":"none", |
156 | "top": "-" + ((sizeY/2)*squareWidth+squareWidth/2) + "px", | |
157 | "width": (this.choices.length * squareWidth) + "px", | |
158 | "height": squareWidth + "px", | |
159 | }, | |
160 | }, | |
161 | this.choices.map( m => { //a "choice" is a move | |
162 | return h('div', | |
163 | { | |
c94bc812 BA |
164 | 'class': { |
165 | 'board': true, | |
8a196305 | 166 | ['board'+sizeY]: true, |
c94bc812 | 167 | }, |
1d184b4c BA |
168 | style: { |
169 | 'width': (100/this.choices.length) + "%", | |
170 | 'padding-bottom': (100/this.choices.length) + "%", | |
171 | }, | |
172 | }, | |
173 | [h('img', | |
174 | { | |
4b353936 BA |
175 | attrs: { "src": '/images/pieces/' + |
176 | VariantRules.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' }, | |
c94bc812 | 177 | 'class': { 'choice-piece': true }, |
1d184b4c BA |
178 | on: { "click": e => { this.play(m); this.choices=[]; } }, |
179 | }) | |
180 | ] | |
181 | ); | |
182 | }) | |
183 | ); | |
184 | // Create board element (+ reserves if needed by variant or mode) | |
130db3ef BA |
185 | const lm = this.vr.lastMove; |
186 | const showLight = this.hints && | |
187 | (this.mode!="idle" || this.cursor==this.vr.moves.length); | |
1d184b4c BA |
188 | let gameDiv = h('div', |
189 | { | |
190 | 'class': { 'game': true }, | |
191 | }, | |
192 | [_.range(sizeX).map(i => { | |
193 | let ci = this.mycolor=='w' ? i : sizeX-i-1; | |
194 | return h( | |
195 | 'div', | |
196 | { | |
197 | 'class': { | |
198 | 'row': true, | |
199 | }, | |
200 | style: { 'opacity': this.choices.length>0?"0.5":"1" }, | |
201 | }, | |
202 | _.range(sizeY).map(j => { | |
203 | let cj = this.mycolor=='w' ? j : sizeY-j-1; | |
204 | let elems = []; | |
205 | if (this.vr.board[ci][cj] != VariantRules.EMPTY) | |
206 | { | |
207 | elems.push( | |
208 | h( | |
209 | 'img', | |
210 | { | |
211 | 'class': { | |
212 | 'piece': true, | |
4b353936 BA |
213 | 'ghost': !!this.selectedPiece |
214 | && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj, | |
1d184b4c BA |
215 | }, |
216 | attrs: { | |
4b353936 BA |
217 | src: "/images/pieces/" + |
218 | VariantRules.getPpath(this.vr.board[ci][cj]) + ".svg", | |
1d184b4c BA |
219 | }, |
220 | } | |
221 | ) | |
222 | ); | |
223 | } | |
88af03d2 | 224 | if (this.hints && hintSquares[ci][cj]) |
1d184b4c BA |
225 | { |
226 | elems.push( | |
227 | h( | |
228 | 'img', | |
229 | { | |
230 | 'class': { | |
231 | 'mark-square': true, | |
232 | }, | |
233 | attrs: { | |
234 | src: "/images/mark.svg", | |
235 | }, | |
236 | } | |
237 | ) | |
238 | ); | |
239 | } | |
1d184b4c BA |
240 | return h( |
241 | 'div', | |
242 | { | |
243 | 'class': { | |
244 | 'board': true, | |
8a196305 | 245 | ['board'+sizeY]: true, |
3300df38 BA |
246 | 'light-square': (i+j)%2==0, |
247 | 'dark-square': (i+j)%2==1, | |
a897b421 | 248 | [this.color]: true, |
3300df38 BA |
249 | 'highlight': showLight && !!lm && _.isMatch(lm.end, {x:ci,y:cj}), |
250 | 'incheck': showLight && incheckSq[ci][cj], | |
1d184b4c BA |
251 | }, |
252 | attrs: { | |
253 | id: this.getSquareId({x:ci,y:cj}), | |
254 | }, | |
255 | }, | |
256 | elems | |
257 | ); | |
258 | }) | |
259 | ); | |
260 | }), choices] | |
261 | ); | |
204e289b BA |
262 | if (this.mode != "idle") |
263 | { | |
264 | actionArray.push( | |
265 | h('button', | |
266 | { | |
267 | on: { click: this.resign }, | |
268 | attrs: { "aria-label": 'Resign' }, | |
269 | 'class': { | |
270 | "tooltip":true, | |
271 | "bottom": true, | |
b8a0ec4a | 272 | "small": smallScreen, |
204e289b | 273 | }, |
0706ea91 | 274 | }, |
204e289b BA |
275 | [h('i', { 'class': { "material-icons": true } }, "flag")]) |
276 | ); | |
277 | } | |
e64084da BA |
278 | else if (this.vr.moves.length > 0) |
279 | { | |
280 | // A game finished, and another is not started yet: allow navigation | |
281 | actionArray = actionArray.concat([ | |
282 | h('button', | |
283 | { | |
e64084da BA |
284 | on: { click: e => this.undo() }, |
285 | attrs: { "aria-label": 'Undo' }, | |
92342261 BA |
286 | "class": { |
287 | "small": smallScreen, | |
288 | "marginleft": true, | |
289 | }, | |
e64084da BA |
290 | }, |
291 | [h('i', { 'class': { "material-icons": true } }, "fast_rewind")]), | |
292 | h('button', | |
293 | { | |
294 | on: { click: e => this.play() }, | |
295 | attrs: { "aria-label": 'Play' }, | |
b8a0ec4a | 296 | "class": { "small": smallScreen }, |
e64084da BA |
297 | }, |
298 | [h('i', { 'class': { "material-icons": true } }, "fast_forward")]), | |
299 | ] | |
300 | ); | |
301 | } | |
2748531f BA |
302 | if (this.mode == "friend") |
303 | { | |
304 | actionArray = actionArray.concat( | |
305 | [ | |
306 | h('button', | |
307 | { | |
2748531f BA |
308 | on: { click: this.undoInGame }, |
309 | attrs: { "aria-label": 'Undo' }, | |
92342261 BA |
310 | "class": { |
311 | "small": smallScreen, | |
312 | "marginleft": true, | |
313 | }, | |
2748531f BA |
314 | }, |
315 | [h('i', { 'class': { "material-icons": true } }, "undo")] | |
316 | ), | |
317 | h('button', | |
318 | { | |
319 | on: { click: () => { this.mycolor = this.vr.getOppCol(this.mycolor) } }, | |
320 | attrs: { "aria-label": 'Flip' }, | |
b8a0ec4a | 321 | "class": { "small": smallScreen }, |
2748531f BA |
322 | }, |
323 | [h('i', { 'class': { "material-icons": true } }, "cached")] | |
324 | ), | |
325 | ]); | |
326 | } | |
1d184b4c | 327 | elementArray.push(gameDiv); |
5c42c64e | 328 | if (!!this.vr.reserve) |
1221ac47 | 329 | { |
6752407b | 330 | const shiftIdx = (this.mycolor=="w" ? 0 : 1); |
5c42c64e | 331 | let myReservePiecesArray = []; |
1221ac47 BA |
332 | for (let i=0; i<VariantRules.RESERVE_PIECES.length; i++) |
333 | { | |
5c42c64e BA |
334 | myReservePiecesArray.push(h('div', |
335 | { | |
336 | 'class': {'board':true, ['board'+sizeY]:true}, | |
6752407b | 337 | attrs: { id: this.getSquareId({x:sizeX+shiftIdx,y:i}) } |
5c42c64e BA |
338 | }, |
339 | [ | |
340 | h('img', | |
1221ac47 BA |
341 | { |
342 | 'class': {"piece":true}, | |
343 | attrs: { | |
344 | "src": "/images/pieces/" + | |
345 | this.vr.getReservePpath(this.mycolor,i) + ".svg", | |
1221ac47 | 346 | } |
5c42c64e BA |
347 | }), |
348 | h('sup', | |
92342261 | 349 | {"class": { "reserve-count": true } }, |
5c42c64e BA |
350 | [ this.vr.reserve[this.mycolor][VariantRules.RESERVE_PIECES[i]] ] |
351 | ) | |
352 | ])); | |
353 | } | |
354 | let oppReservePiecesArray = []; | |
355 | const oppCol = this.vr.getOppCol(this.mycolor); | |
356 | for (let i=0; i<VariantRules.RESERVE_PIECES.length; i++) | |
357 | { | |
358 | oppReservePiecesArray.push(h('div', | |
359 | { | |
360 | 'class': {'board':true, ['board'+sizeY]:true}, | |
6752407b | 361 | attrs: { id: this.getSquareId({x:sizeX+(1-shiftIdx),y:i}) } |
5c42c64e BA |
362 | }, |
363 | [ | |
364 | h('img', | |
365 | { | |
366 | 'class': {"piece":true}, | |
367 | attrs: { | |
368 | "src": "/images/pieces/" + | |
369 | this.vr.getReservePpath(oppCol,i) + ".svg", | |
370 | } | |
371 | }), | |
372 | h('sup', | |
92342261 | 373 | {"class": { "reserve-count": true } }, |
5c42c64e BA |
374 | [ this.vr.reserve[oppCol][VariantRules.RESERVE_PIECES[i]] ] |
375 | ) | |
376 | ])); | |
1221ac47 | 377 | } |
5c42c64e BA |
378 | let reserves = h('div', |
379 | { | |
92342261 BA |
380 | 'class':{ |
381 | 'game': true, | |
382 | "reserve-div": true, | |
383 | }, | |
5c42c64e BA |
384 | }, |
385 | [ | |
386 | h('div', | |
387 | { | |
92342261 BA |
388 | 'class': { |
389 | 'row': true, | |
390 | "reserve-row-1": true, | |
391 | }, | |
5c42c64e BA |
392 | }, |
393 | myReservePiecesArray | |
394 | ), | |
1221ac47 BA |
395 | h('div', |
396 | { 'class': { 'row': true }}, | |
5c42c64e | 397 | oppReservePiecesArray |
1221ac47 | 398 | ) |
5c42c64e | 399 | ] |
1221ac47 | 400 | ); |
5c42c64e | 401 | elementArray.push(reserves); |
1221ac47 | 402 | } |
f3802fcd | 403 | const eogMessage = this.getEndgameMessage(this.score); |
1d184b4c BA |
404 | const modalEog = [ |
405 | h('input', | |
406 | { | |
ecf44502 | 407 | attrs: { "id": "modal-eog", type: "checkbox" }, |
1d184b4c BA |
408 | "class": { "modal": true }, |
409 | }), | |
410 | h('div', | |
411 | { | |
c148615e | 412 | attrs: { "role": "dialog", "aria-labelledby": "modal-eog" }, |
1d184b4c BA |
413 | }, |
414 | [ | |
415 | h('div', | |
416 | { | |
417 | "class": { "card": true, "smallpad": true }, | |
418 | }, | |
01a135e2 BA |
419 | [ |
420 | h('label', | |
421 | { | |
422 | attrs: { "for": "modal-eog" }, | |
423 | "class": { "modal-close": true }, | |
424 | } | |
425 | ), | |
426 | h('h3', | |
427 | { | |
428 | "class": { "section": true }, | |
429 | domProps: { innerHTML: eogMessage }, | |
430 | } | |
431 | ) | |
432 | ] | |
1d184b4c BA |
433 | ) |
434 | ] | |
435 | ) | |
436 | ]; | |
437 | elementArray = elementArray.concat(modalEog); | |
438 | } | |
12b46d8f | 439 | // NOTE: this modal could be in Pug view (no usage of Vue functions or variables) |
1d184b4c BA |
440 | const modalNewgame = [ |
441 | h('input', | |
442 | { | |
ecf44502 | 443 | attrs: { "id": "modal-newgame", type: "checkbox" }, |
1d184b4c BA |
444 | "class": { "modal": true }, |
445 | }), | |
446 | h('div', | |
447 | { | |
c148615e | 448 | attrs: { "role": "dialog", "aria-labelledby": "modal-newgame" }, |
1d184b4c BA |
449 | }, |
450 | [ | |
451 | h('div', | |
452 | { | |
453 | "class": { "card": true, "smallpad": true }, | |
454 | }, | |
455 | [ | |
456 | h('label', | |
457 | { | |
ecf44502 | 458 | attrs: { "id": "close-newgame", "for": "modal-newgame" }, |
1d184b4c BA |
459 | "class": { "modal-close": true }, |
460 | } | |
461 | ), | |
462 | h('h3', | |
463 | { | |
464 | "class": { "section": true }, | |
465 | domProps: { innerHTML: "New game" }, | |
466 | } | |
467 | ), | |
468 | h('p', | |
469 | { | |
470 | "class": { "section": true }, | |
471 | domProps: { innerHTML: "Waiting for opponent..." }, | |
472 | } | |
473 | ) | |
474 | ] | |
475 | ) | |
476 | ] | |
477 | ) | |
478 | ]; | |
479 | elementArray = elementArray.concat(modalNewgame); | |
2748531f BA |
480 | const modalFenEdit = [ |
481 | h('input', | |
482 | { | |
483 | attrs: { "id": "modal-fenedit", type: "checkbox" }, | |
484 | "class": { "modal": true }, | |
485 | }), | |
486 | h('div', | |
487 | { | |
488 | attrs: { "role": "dialog", "aria-labelledby": "modal-fenedit" }, | |
489 | }, | |
490 | [ | |
491 | h('div', | |
492 | { | |
493 | "class": { "card": true, "smallpad": true }, | |
494 | }, | |
495 | [ | |
496 | h('label', | |
497 | { | |
498 | attrs: { "id": "close-fenedit", "for": "modal-fenedit" }, | |
499 | "class": { "modal-close": true }, | |
500 | } | |
501 | ), | |
502 | h('h3', | |
503 | { | |
504 | "class": { "section": true }, | |
505 | domProps: { innerHTML: "Position + flags (FEN):" }, | |
506 | } | |
507 | ), | |
508 | h('input', | |
509 | { | |
510 | attrs: { | |
511 | "id": "input-fen", | |
512 | type: "text", | |
513 | value: VariantRules.GenRandInitFen(), | |
514 | }, | |
515 | } | |
516 | ), | |
517 | h('button', | |
518 | { | |
519 | on: { click: | |
520 | () => { | |
521 | const fen = document.getElementById("input-fen").value; | |
522 | document.getElementById("modal-fenedit").checked = false; | |
523 | this.newGame("friend", fen); | |
524 | } | |
525 | }, | |
526 | domProps: { innerHTML: "Ok" }, | |
527 | } | |
528 | ), | |
529 | h('button', | |
530 | { | |
531 | on: { click: | |
532 | () => { | |
533 | document.getElementById("input-fen").value = | |
534 | VariantRules.GenRandInitFen(); | |
535 | } | |
536 | }, | |
537 | domProps: { innerHTML: "Random" }, | |
538 | } | |
539 | ), | |
540 | ] | |
541 | ) | |
542 | ] | |
543 | ) | |
544 | ]; | |
545 | elementArray = elementArray.concat(modalFenEdit); | |
88af03d2 BA |
546 | const modalSettings = [ |
547 | h('input', | |
548 | { | |
549 | attrs: { "id": "modal-settings", type: "checkbox" }, | |
550 | "class": { "modal": true }, | |
551 | }), | |
552 | h('div', | |
553 | { | |
554 | attrs: { "role": "dialog", "aria-labelledby": "modal-settings" }, | |
555 | }, | |
556 | [ | |
557 | h('div', | |
558 | { | |
559 | "class": { "card": true, "smallpad": true }, | |
560 | }, | |
561 | [ | |
562 | h('label', | |
563 | { | |
564 | attrs: { "id": "close-settings", "for": "modal-settings" }, | |
565 | "class": { "modal-close": true }, | |
566 | } | |
567 | ), | |
568 | h('h3', | |
569 | { | |
570 | "class": { "section": true }, | |
a897b421 | 571 | domProps: { innerHTML: "Preferences" }, |
88af03d2 BA |
572 | } |
573 | ), | |
12b46d8f BA |
574 | h('fieldset', |
575 | { }, | |
576 | [ | |
2812515a | 577 | //h('legend', { domProps: { innerHTML: "Legend title" } }), |
12b46d8f BA |
578 | h('label', |
579 | { | |
2812515a | 580 | attrs: { for: "setHints" }, |
12b46d8f BA |
581 | domProps: { innerHTML: "Show hints?" }, |
582 | }, | |
583 | ), | |
584 | h('input', | |
585 | { | |
586 | attrs: { | |
587 | "id": "setHints", | |
588 | type: "checkbox", | |
589 | checked: this.hints, | |
590 | }, | |
591 | on: { "change": this.toggleHints }, | |
592 | } | |
593 | ), | |
594 | ] | |
a897b421 | 595 | ), |
12b46d8f BA |
596 | h('fieldset', |
597 | { }, | |
598 | [ | |
599 | h('label', | |
600 | { | |
2812515a | 601 | attrs: { for: "selectColor" }, |
12b46d8f BA |
602 | domProps: { innerHTML: "Board colors" }, |
603 | }, | |
604 | ), | |
605 | h("select", | |
606 | { | |
607 | attrs: { "id": "selectColor" }, | |
608 | on: { "change": this.setColor }, | |
609 | }, | |
610 | [ | |
611 | h("option", | |
612 | { | |
613 | domProps: { | |
614 | "value": "lichess", | |
2812515a | 615 | innerHTML: "brown" |
12b46d8f BA |
616 | }, |
617 | attrs: { "selected": this.color=="lichess" }, | |
618 | } | |
619 | ), | |
620 | h("option", | |
621 | { | |
622 | domProps: { | |
623 | "value": "chesscom", | |
2812515a | 624 | innerHTML: "green" |
12b46d8f BA |
625 | }, |
626 | attrs: { "selected": this.color=="chesscom" }, | |
627 | } | |
628 | ), | |
629 | h("option", | |
630 | { | |
631 | domProps: { | |
632 | "value": "chesstempo", | |
2812515a | 633 | innerHTML: "blue" |
12b46d8f BA |
634 | }, |
635 | attrs: { "selected": this.color=="chesstempo" }, | |
636 | } | |
637 | ), | |
638 | ], | |
639 | ), | |
640 | ] | |
641 | ), | |
642 | h('fieldset', | |
643 | { }, | |
644 | [ | |
645 | h('label', | |
646 | { | |
2812515a | 647 | attrs: { for: "selectSound" }, |
130db3ef | 648 | domProps: { innerHTML: "Play sounds?" }, |
12b46d8f BA |
649 | }, |
650 | ), | |
651 | h("select", | |
652 | { | |
653 | attrs: { "id": "selectSound" }, | |
654 | on: { "change": this.setSound }, | |
655 | }, | |
656 | [ | |
657 | h("option", | |
658 | { | |
659 | domProps: { | |
660 | "value": "0", | |
130db3ef | 661 | innerHTML: "None" |
12b46d8f BA |
662 | }, |
663 | } | |
664 | ), | |
665 | h("option", | |
666 | { | |
667 | domProps: { | |
668 | "value": "1", | |
130db3ef | 669 | innerHTML: "Newgame" |
12b46d8f BA |
670 | }, |
671 | } | |
672 | ), | |
673 | h("option", | |
674 | { | |
675 | domProps: { | |
676 | "value": "2", | |
130db3ef | 677 | innerHTML: "All" |
12b46d8f BA |
678 | }, |
679 | } | |
680 | ), | |
681 | ], | |
682 | ), | |
683 | ] | |
a897b421 | 684 | ), |
88af03d2 BA |
685 | ] |
686 | ) | |
687 | ] | |
688 | ) | |
689 | ]; | |
690 | elementArray = elementArray.concat(modalSettings); | |
1d184b4c BA |
691 | const actions = h('div', |
692 | { | |
693 | attrs: { "id": "actions" }, | |
694 | 'class': { 'text-center': true }, | |
695 | }, | |
696 | actionArray | |
697 | ); | |
698 | elementArray.push(actions); | |
01a135e2 BA |
699 | if (this.score != "*") |
700 | { | |
701 | elementArray.push( | |
702 | h('div', | |
61a262b2 BA |
703 | { |
704 | attrs: { id: "pgn-div" }, | |
705 | "class": { "section-content": true }, | |
706 | }, | |
01a135e2 | 707 | [ |
01ca2adc BA |
708 | h('a', |
709 | { | |
710 | attrs: { | |
711 | id: "download", | |
712 | href: "#", | |
713 | } | |
714 | } | |
715 | ), | |
01a135e2 BA |
716 | h('p', |
717 | { | |
01ca2adc | 718 | attrs: { id: "pgn-game" }, |
85be503d BA |
719 | domProps: { innerHTML: this.pgnTxt } |
720 | } | |
61a262b2 BA |
721 | ), |
722 | h('button', | |
723 | { | |
724 | attrs: { "id": "downloadBtn" }, | |
725 | on: { click: this.download }, | |
726 | domProps: { innerHTML: "Download game" }, | |
727 | } | |
728 | ), | |
85be503d BA |
729 | ] |
730 | ) | |
731 | ); | |
732 | } | |
733 | else if (this.mode != "idle") | |
734 | { | |
2748531f | 735 | // Show current FEN |
85be503d BA |
736 | elementArray.push( |
737 | h('div', | |
61a262b2 BA |
738 | { |
739 | attrs: { id: "fen-div" }, | |
740 | "class": { "section-content": true }, | |
741 | }, | |
85be503d BA |
742 | [ |
743 | h('p', | |
744 | { | |
745 | attrs: { id: "fen-string" }, | |
2748531f | 746 | domProps: { innerHTML: this.vr.getFen() } |
01a135e2 BA |
747 | } |
748 | ) | |
749 | ] | |
750 | ) | |
751 | ); | |
752 | } | |
1d184b4c BA |
753 | return h( |
754 | 'div', | |
755 | { | |
756 | 'class': { | |
757 | "col-sm-12":true, | |
758 | "col-md-8":true, | |
759 | "col-md-offset-2":true, | |
760 | "col-lg-6":true, | |
761 | "col-lg-offset-3":true, | |
762 | }, | |
dda21a71 | 763 | // NOTE: click = mousedown + mouseup |
1d184b4c BA |
764 | on: { |
765 | mousedown: this.mousedown, | |
766 | mousemove: this.mousemove, | |
767 | mouseup: this.mouseup, | |
ffea77d9 BA |
768 | touchstart: this.mousedown, |
769 | touchmove: this.mousemove, | |
770 | touchend: this.mouseup, | |
1d184b4c BA |
771 | }, |
772 | }, | |
773 | elementArray | |
774 | ); | |
775 | }, | |
776 | created: function() { | |
777 | const url = socketUrl; | |
778 | const continuation = (localStorage.getItem("variant") === variant); | |
b019d603 | 779 | this.myid = continuation ? localStorage.getItem("myid") : getRandString(); |
92ff5bae BA |
780 | if (!continuation) |
781 | { | |
92342261 BA |
782 | // HACK: play a small silent sound to allow "new game" sound later |
783 | // if tab not focused (TODO: does it really work ?!) | |
92ff5bae BA |
784 | new Audio("/sounds/silent.mp3").play().then(() => {}).catch(err => {}); |
785 | } | |
1d184b4c | 786 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant); |
d35f20e4 | 787 | const socketOpenListener = () => { |
1d184b4c BA |
788 | if (continuation) |
789 | { | |
dfb4afc1 BA |
790 | const fen = localStorage.getItem("fen"); |
791 | const mycolor = localStorage.getItem("mycolor"); | |
792 | const oppid = localStorage.getItem("oppid"); | |
793 | const moves = JSON.parse(localStorage.getItem("moves")); | |
794 | this.newGame("human", fen, mycolor, oppid, moves, true); | |
a29d9d6b | 795 | // Send ping to server (answer pong if opponent is connected) |
ecf44502 | 796 | this.conn.send(JSON.stringify({code:"ping",oppid:this.oppid})); |
1d184b4c | 797 | } |
30ff6e04 BA |
798 | else if (localStorage.getItem("newgame") === variant) |
799 | { | |
800 | // New game request has been cancelled on disconnect | |
001344b9 | 801 | this.newGame("human", undefined, undefined, undefined, undefined, "reconnect"); |
30ff6e04 | 802 | } |
1d184b4c | 803 | }; |
d35f20e4 | 804 | const socketMessageListener = msg => { |
1d184b4c BA |
805 | const data = JSON.parse(msg.data); |
806 | switch (data.code) | |
807 | { | |
808 | case "newgame": //opponent found | |
92342261 BA |
809 | // oppid: opponent socket ID |
810 | this.newGame("human", data.fen, data.color, data.oppid); | |
1d184b4c BA |
811 | break; |
812 | case "newmove": //..he played! | |
813 | this.play(data.move, "animate"); | |
814 | break; | |
f3802fcd | 815 | case "pong": //received if we sent a ping (game still alive on our side) |
1d184b4c | 816 | this.oppConnected = true; |
a29d9d6b | 817 | const L = this.vr.moves.length; |
f3802fcd | 818 | // Send our "last state" informations to opponent |
a29d9d6b BA |
819 | this.conn.send(JSON.stringify({ |
820 | code:"lastate", | |
f3802fcd | 821 | oppid:this.oppid, |
a29d9d6b BA |
822 | lastMove:L>0?this.vr.moves[L-1]:undefined, |
823 | movesCount:L, | |
824 | })); | |
1d184b4c | 825 | break; |
a29d9d6b BA |
826 | case "lastate": //got opponent infos about last move (we might have resigned) |
827 | if (this.mode!="human" || this.oppid!=data.oppid) | |
828 | { | |
829 | // OK, we resigned | |
830 | this.conn.send(JSON.stringify({ | |
831 | code:"lastate", | |
f3802fcd | 832 | oppid:this.oppid, |
a29d9d6b BA |
833 | lastMove:undefined, |
834 | movesCount:-1, | |
835 | })); | |
836 | } | |
837 | else if (data.movesCount < 0) | |
838 | { | |
839 | // OK, he resigned | |
840 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); | |
841 | } | |
842 | else if (data.movesCount < this.vr.moves.length) | |
843 | { | |
844 | // We must tell last move to opponent | |
845 | const L = this.vr.moves.length; | |
846 | this.conn.send(JSON.stringify({ | |
847 | code:"lastate", | |
f3802fcd | 848 | oppid:this.oppid, |
a29d9d6b BA |
849 | lastMove:this.vr.moves[L-1], |
850 | movesCount:L, | |
851 | })); | |
852 | } | |
853 | else if (data.movesCount > this.vr.moves.length) //just got last move from him | |
854 | this.play(data.lastMove, "animate"); | |
ecf44502 | 855 | break; |
1d184b4c | 856 | case "resign": //..you won! |
dfb4afc1 | 857 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); |
1d184b4c | 858 | break; |
f3802fcd | 859 | // TODO: also use (dis)connect info to count online players? |
1d184b4c BA |
860 | case "connect": |
861 | case "disconnect": | |
862 | if (this.mode == "human" && this.oppid == data.id) | |
863 | this.oppConnected = (data.code == "connect"); | |
864 | break; | |
865 | } | |
866 | }; | |
d35f20e4 | 867 | const socketCloseListener = () => { |
d35f20e4 BA |
868 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant); |
869 | this.conn.addEventListener('open', socketOpenListener); | |
870 | this.conn.addEventListener('message', socketMessageListener); | |
871 | this.conn.addEventListener('close', socketCloseListener); | |
872 | }; | |
873 | this.conn.onopen = socketOpenListener; | |
874 | this.conn.onmessage = socketMessageListener; | |
875 | this.conn.onclose = socketCloseListener; | |
e64084da BA |
876 | // Listen to keyboard left/right to navigate in game |
877 | document.onkeydown = event => { | |
2748531f | 878 | if (this.mode == "idle" && !!this.vr && this.vr.moves.length > 0 |
e64084da BA |
879 | && [37,39].includes(event.keyCode)) |
880 | { | |
881 | event.preventDefault(); | |
882 | if (event.keyCode == 37) //Back | |
883 | this.undo(); | |
884 | else //Forward (39) | |
885 | this.play(); | |
886 | } | |
887 | }; | |
1d184b4c BA |
888 | }, |
889 | methods: { | |
01ca2adc BA |
890 | download: function() { |
891 | let content = document.getElementById("pgn-game").innerHTML; | |
892 | content = content.replace(/<br>/g, "\n"); | |
893 | // Prepare and trigger download link | |
894 | let downloadAnchor = document.getElementById("download"); | |
895 | downloadAnchor.setAttribute("download", "game.pgn"); | |
92342261 BA |
896 | downloadAnchor.href = "data:text/plain;charset=utf-8," + |
897 | encodeURIComponent(content); | |
01ca2adc BA |
898 | downloadAnchor.click(); |
899 | }, | |
dfb4afc1 BA |
900 | endGame: function(score) { |
901 | this.score = score; | |
ecf44502 | 902 | let modalBox = document.getElementById("modal-eog"); |
186516b8 | 903 | modalBox.checked = true; |
204e289b | 904 | // Variants may have special PGN structure (so next function isn't defined here) |
3840e240 BA |
905 | this.pgnTxt = this.vr.getPGN(this.mycolor, this.score, this.fenStart, this.mode); |
906 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
1d184b4c BA |
907 | if (this.mode == "human") |
908 | this.clearStorage(); | |
3840e240 | 909 | this.mode = "idle"; |
e64084da | 910 | this.cursor = this.vr.moves.length; //to navigate in finished game |
1d184b4c BA |
911 | this.oppid = ""; |
912 | }, | |
f3802fcd BA |
913 | getEndgameMessage: function(score) { |
914 | let eogMessage = "Unfinished"; | |
915 | switch (this.score) | |
916 | { | |
917 | case "1-0": | |
918 | eogMessage = "White win"; | |
919 | break; | |
920 | case "0-1": | |
921 | eogMessage = "Black win"; | |
922 | break; | |
923 | case "1/2": | |
924 | eogMessage = "Draw"; | |
925 | break; | |
926 | } | |
927 | return eogMessage; | |
928 | }, | |
762b7c9c BA |
929 | setStorage: function() { |
930 | localStorage.setItem("myid", this.myid); | |
931 | localStorage.setItem("variant", variant); | |
932 | localStorage.setItem("mycolor", this.mycolor); | |
933 | localStorage.setItem("oppid", this.oppid); | |
934 | localStorage.setItem("fenStart", this.fenStart); | |
935 | localStorage.setItem("moves", JSON.stringify(this.vr.moves)); | |
1d184b4c | 936 | localStorage.setItem("fen", this.vr.getFen()); |
762b7c9c BA |
937 | }, |
938 | updateStorage: function() { | |
dfb4afc1 | 939 | localStorage.setItem("moves", JSON.stringify(this.vr.moves)); |
762b7c9c | 940 | localStorage.setItem("fen", this.vr.getFen()); |
1d184b4c BA |
941 | }, |
942 | clearStorage: function() { | |
943 | delete localStorage["variant"]; | |
944 | delete localStorage["myid"]; | |
945 | delete localStorage["mycolor"]; | |
946 | delete localStorage["oppid"]; | |
762b7c9c | 947 | delete localStorage["fenStart"]; |
1d184b4c | 948 | delete localStorage["fen"]; |
dfb4afc1 | 949 | delete localStorage["moves"]; |
1d184b4c | 950 | }, |
c148615e BA |
951 | // HACK because mini-css tooltips are persistent after click... |
952 | getRidOfTooltip: function(elt) { | |
953 | elt.style.visibility = "hidden"; | |
954 | setTimeout(() => { elt.style.visibility="visible"; }, 100); | |
955 | }, | |
88af03d2 BA |
956 | showSettings: function(e) { |
957 | this.getRidOfTooltip(e.currentTarget); | |
958 | document.getElementById("modal-settings").checked = true; | |
959 | }, | |
960 | toggleHints: function() { | |
961 | this.hints = !this.hints; | |
962 | setCookie("hints", this.hints ? "1" : "0"); | |
963 | }, | |
2812515a BA |
964 | setColor: function(e) { |
965 | this.color = e.target.options[e.target.selectedIndex].value; | |
966 | setCookie("color", this.color); | |
12b46d8f | 967 | }, |
2812515a | 968 | setSound: function(e) { |
130db3ef | 969 | this.sound = parseInt(e.target.options[e.target.selectedIndex].value); |
2812515a | 970 | setCookie("sound", this.sound); |
12b46d8f | 971 | }, |
c148615e BA |
972 | clickGameSeek: function(e) { |
973 | this.getRidOfTooltip(e.currentTarget); | |
f3802fcd BA |
974 | if (this.mode == "human") |
975 | return; //no newgame while playing | |
976 | if (this.seek) | |
01a135e2 | 977 | { |
283d06a4 | 978 | this.conn.send(JSON.stringify({code:"cancelnewgame"})); |
f3802fcd | 979 | delete localStorage["newgame"]; //cancel game seek |
01a135e2 BA |
980 | this.seek = false; |
981 | } | |
f3802fcd | 982 | else |
f3802fcd | 983 | this.newGame("human"); |
f3802fcd | 984 | }, |
c148615e BA |
985 | clickComputerGame: function(e) { |
986 | this.getRidOfTooltip(e.currentTarget); | |
f3802fcd BA |
987 | if (this.mode == "human") |
988 | return; //no newgame while playing | |
989 | this.newGame("computer"); | |
990 | }, | |
2748531f BA |
991 | clickFriendGame: function(e) { |
992 | this.getRidOfTooltip(e.currentTarget); | |
993 | document.getElementById("modal-fenedit").checked = true; | |
994 | }, | |
2748531f BA |
995 | resign: function(e) { |
996 | this.getRidOfTooltip(e.currentTarget); | |
c148615e BA |
997 | if (this.mode == "human" && this.oppConnected) |
998 | { | |
999 | try { | |
1000 | this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid})); | |
1001 | } catch (INVALID_STATE_ERR) { | |
1002 | return; //socket is not ready (and not yet reconnected) | |
1003 | } | |
1004 | } | |
1005 | this.endGame(this.mycolor=="w"?"0-1":"1-0"); | |
1006 | }, | |
dfb4afc1 | 1007 | newGame: function(mode, fenInit, color, oppId, moves, continuation) { |
55eb331d | 1008 | const fen = fenInit || VariantRules.GenRandInitFen(); |
1d184b4c BA |
1009 | console.log(fen); //DEBUG |
1010 | if (mode=="human" && !oppId) | |
1011 | { | |
cd3174c5 BA |
1012 | const storageVariant = localStorage.getItem("variant"); |
1013 | if (!!storageVariant && storageVariant !== variant) | |
1014 | { | |
cd3174c5 BA |
1015 | alert("Finish your " + storageVariant + " game first!"); |
1016 | return; | |
1017 | } | |
1d184b4c | 1018 | // Send game request and wait.. |
01a135e2 BA |
1019 | localStorage["newgame"] = variant; |
1020 | this.seek = true; | |
1d184b4c | 1021 | this.clearStorage(); //in case of |
d35f20e4 BA |
1022 | try { |
1023 | this.conn.send(JSON.stringify({code:"newgame", fen:fen})); | |
1024 | } catch (INVALID_STATE_ERR) { | |
1025 | return; //nothing achieved | |
1026 | } | |
f3802fcd | 1027 | if (continuation !== "reconnect") //TODO: bad HACK... |
a68d899d | 1028 | { |
ecf44502 | 1029 | let modalBox = document.getElementById("modal-newgame"); |
a68d899d BA |
1030 | modalBox.checked = true; |
1031 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
1032 | } | |
1d184b4c BA |
1033 | return; |
1034 | } | |
dfb4afc1 | 1035 | this.vr = new VariantRules(fen, moves || []); |
c148615e | 1036 | this.score = "*"; |
3840e240 | 1037 | this.pgnTxt = ""; //redundant with this.score = "*", but cleaner |
1d184b4c | 1038 | this.mode = mode; |
1fcaa356 | 1039 | this.incheck = []; //in case of |
2748531f | 1040 | this.fenStart = (continuation ? localStorage.getItem("fenStart") : fen); |
1d184b4c BA |
1041 | if (mode=="human") |
1042 | { | |
1043 | // Opponent found! | |
2812515a | 1044 | if (!continuation) //not playing sound on game continuation |
1d184b4c | 1045 | { |
2812515a BA |
1046 | if (this.sound >= 1) |
1047 | new Audio("/sounds/newgame.mp3").play().then(() => {}).catch(err => {}); | |
ecf44502 | 1048 | document.getElementById("modal-newgame").checked = false; |
1d184b4c BA |
1049 | } |
1050 | this.oppid = oppId; | |
1051 | this.oppConnected = true; | |
1052 | this.mycolor = color; | |
186516b8 | 1053 | this.seek = false; |
e64a4eff BA |
1054 | if (!!moves && moves.length > 0) //imply continuation |
1055 | { | |
e64a4eff | 1056 | const lastMove = moves[moves.length-1]; |
cd4cad04 | 1057 | this.vr.undo(lastMove); |
46302e64 | 1058 | this.incheck = this.vr.getCheckSquares(lastMove); |
e64a4eff BA |
1059 | this.vr.play(lastMove, "ingame"); |
1060 | } | |
186516b8 | 1061 | delete localStorage["newgame"]; |
762b7c9c | 1062 | this.setStorage(); //in case of interruptions |
1d184b4c | 1063 | } |
2748531f | 1064 | else if (mode == "computer") |
1d184b4c | 1065 | { |
f3c10e18 | 1066 | this.mycolor = Math.random() < 0.5 ? 'w' : 'b'; |
1d184b4c BA |
1067 | if (this.mycolor == 'b') |
1068 | setTimeout(this.playComputerMove, 500); | |
1069 | } | |
2748531f | 1070 | //else: against a (IRL) friend: nothing more to do |
1d184b4c BA |
1071 | }, |
1072 | playComputerMove: function() { | |
4b353936 | 1073 | const timeStart = Date.now(); |
46302e64 | 1074 | const compMove = this.vr.getComputerMove(); |
4b353936 BA |
1075 | // (first move) HACK: avoid selecting elements before they appear on page: |
1076 | const delay = Math.max(500-(Date.now()-timeStart), 0); | |
1077 | setTimeout(() => this.play(compMove, "animate"), delay); | |
1d184b4c BA |
1078 | }, |
1079 | // Get the identifier of a HTML table cell from its numeric coordinates o.x,o.y. | |
1080 | getSquareId: function(o) { | |
1081 | // NOTE: a separator is required to allow any size of board | |
1082 | return "sq-" + o.x + "-" + o.y; | |
1083 | }, | |
1084 | // Inverse function | |
1085 | getSquareFromId: function(id) { | |
1086 | let idParts = id.split('-'); | |
1087 | return [parseInt(idParts[1]), parseInt(idParts[2])]; | |
1088 | }, | |
1089 | mousedown: function(e) { | |
1090 | e = e || window.event; | |
44461547 BA |
1091 | let ingame = false; |
1092 | let elem = e.target; | |
1093 | while (!ingame && elem !== null) | |
1094 | { | |
1095 | if (elem.classList.contains("game")) | |
1096 | { | |
1097 | ingame = true; | |
1098 | break; | |
1099 | } | |
1100 | elem = elem.parentElement; | |
1101 | } | |
1102 | if (!ingame) //let default behavior (click on button...) | |
1103 | return; | |
1d184b4c BA |
1104 | e.preventDefault(); //disable native drag & drop |
1105 | if (!this.selectedPiece && e.target.classList.contains("piece")) | |
1106 | { | |
1107 | // Next few lines to center the piece on mouse cursor | |
1108 | let rect = e.target.parentNode.getBoundingClientRect(); | |
1109 | this.start = { | |
1110 | x: rect.x + rect.width/2, | |
1111 | y: rect.y + rect.width/2, | |
1112 | id: e.target.parentNode.id | |
1113 | }; | |
1114 | this.selectedPiece = e.target.cloneNode(); | |
1115 | this.selectedPiece.style.position = "absolute"; | |
1116 | this.selectedPiece.style.top = 0; | |
1117 | this.selectedPiece.style.display = "inline-block"; | |
1118 | this.selectedPiece.style.zIndex = 3000; | |
1119 | let startSquare = this.getSquareFromId(e.target.parentNode.id); | |
2748531f BA |
1120 | const iCanPlay = this.mode!="idle" |
1121 | && (this.mode=="friend" || this.vr.canIplay(this.mycolor,startSquare)); | |
1122 | this.possibleMoves = iCanPlay ? this.vr.getPossibleMovesFrom(startSquare) : []; | |
92342261 BA |
1123 | // Next line add moving piece just after current image |
1124 | // (required for Crazyhouse reserve) | |
5bd679d5 | 1125 | e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling); |
1d184b4c BA |
1126 | } |
1127 | }, | |
1128 | mousemove: function(e) { | |
1129 | if (!this.selectedPiece) | |
1130 | return; | |
1131 | e = e || window.event; | |
1132 | // If there is an active element, move it around | |
1133 | if (!!this.selectedPiece) | |
1134 | { | |
ffea77d9 BA |
1135 | const [offsetX,offsetY] = !!e.clientX |
1136 | ? [e.clientX,e.clientY] //desktop browser | |
1137 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone | |
1138 | this.selectedPiece.style.left = (offsetX-this.start.x) + "px"; | |
1139 | this.selectedPiece.style.top = (offsetY-this.start.y) + "px"; | |
1d184b4c BA |
1140 | } |
1141 | }, | |
1142 | mouseup: function(e) { | |
1143 | if (!this.selectedPiece) | |
1144 | return; | |
1145 | e = e || window.event; | |
1146 | // Read drop target (or parentElement, parentNode... if type == "img") | |
92342261 | 1147 | this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords |
ffea77d9 BA |
1148 | const [offsetX,offsetY] = !!e.clientX |
1149 | ? [e.clientX,e.clientY] | |
1150 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; | |
1151 | let landing = document.elementFromPoint(offsetX, offsetY); | |
1d184b4c | 1152 | this.selectedPiece.style.zIndex = 3000; |
92342261 BA |
1153 | // Next condition: classList.contains(piece) fails because of marks |
1154 | while (landing.tagName == "IMG") | |
1d184b4c | 1155 | landing = landing.parentNode; |
92342261 BA |
1156 | if (this.start.id == landing.id) |
1157 | { | |
1158 | // A click: selectedPiece and possibleMoves are already filled | |
1d184b4c | 1159 | return; |
92342261 | 1160 | } |
1d184b4c BA |
1161 | // OK: process move attempt |
1162 | let endSquare = this.getSquareFromId(landing.id); | |
1163 | let moves = this.findMatchingMoves(endSquare); | |
1164 | this.possibleMoves = []; | |
1165 | if (moves.length > 1) | |
1166 | this.choices = moves; | |
1167 | else if (moves.length==1) | |
1168 | this.play(moves[0]); | |
1169 | // Else: impossible move | |
1170 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); | |
1171 | delete this.selectedPiece; | |
1172 | this.selectedPiece = null; | |
1173 | }, | |
1174 | findMatchingMoves: function(endSquare) { | |
1175 | // Run through moves list and return the matching set (if promotions...) | |
1176 | let moves = []; | |
1177 | this.possibleMoves.forEach(function(m) { | |
1178 | if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) | |
1179 | moves.push(m); | |
1180 | }); | |
1181 | return moves; | |
1182 | }, | |
1183 | animateMove: function(move) { | |
1184 | let startSquare = document.getElementById(this.getSquareId(move.start)); | |
1185 | let endSquare = document.getElementById(this.getSquareId(move.end)); | |
1186 | let rectStart = startSquare.getBoundingClientRect(); | |
1187 | let rectEnd = endSquare.getBoundingClientRect(); | |
1188 | let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; | |
92ff5bae BA |
1189 | let movingPiece = |
1190 | document.querySelector("#" + this.getSquareId(move.start) + " > img.piece"); | |
92342261 | 1191 | // HACK for animation (with positive translate, image slides "under background") |
1d184b4c BA |
1192 | // Possible improvement: just alter squares on the piece's way... |
1193 | squares = document.getElementsByClassName("board"); | |
1194 | for (let i=0; i<squares.length; i++) | |
1195 | { | |
1196 | let square = squares.item(i); | |
1197 | if (square.id != this.getSquareId(move.start)) | |
1198 | square.style.zIndex = "-1"; | |
1199 | } | |
92342261 BA |
1200 | movingPiece.style.transform = "translate(" + translation.x + "px," + |
1201 | translation.y + "px)"; | |
1d184b4c BA |
1202 | movingPiece.style.transitionDuration = "0.2s"; |
1203 | movingPiece.style.zIndex = "3000"; | |
1204 | setTimeout( () => { | |
1205 | for (let i=0; i<squares.length; i++) | |
1206 | squares.item(i).style.zIndex = "auto"; | |
1207 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap | |
1208 | this.play(move); | |
1209 | }, 200); | |
1210 | }, | |
1211 | play: function(move, programmatic) { | |
e64084da BA |
1212 | if (!move) |
1213 | { | |
1214 | // Navigate after game is over | |
1215 | if (this.cursor >= this.vr.moves.length) | |
1216 | return; //already at the end | |
1217 | move = this.vr.moves[this.cursor++]; | |
1218 | } | |
1d184b4c BA |
1219 | if (!!programmatic) //computer or human opponent |
1220 | { | |
1221 | this.animateMove(move); | |
1222 | return; | |
1223 | } | |
1224 | // Not programmatic, or animation is over | |
1225 | if (this.mode == "human" && this.vr.turn == this.mycolor) | |
a29d9d6b | 1226 | this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid})); |
2812515a BA |
1227 | if (this.sound == 2) |
1228 | new Audio("/sounds/chessmove1.mp3").play().then(() => {}).catch(err => {}); | |
e64084da | 1229 | if (this.mode != "idle") |
3300df38 BA |
1230 | { |
1231 | this.incheck = this.vr.getCheckSquares(move); //is opponent in check? | |
e64084da | 1232 | this.vr.play(move, "ingame"); |
3300df38 | 1233 | } |
e64084da | 1234 | else |
3300df38 | 1235 | { |
e64084da | 1236 | VariantRules.PlayOnBoard(this.vr.board, move); |
3300df38 BA |
1237 | this.$forceUpdate(); //TODO: ?! |
1238 | } | |
1d184b4c BA |
1239 | if (this.mode == "human") |
1240 | this.updateStorage(); //after our moves and opponent moves | |
e64084da BA |
1241 | if (this.mode != "idle") |
1242 | { | |
1243 | const eog = this.vr.checkGameOver(); | |
1244 | if (eog != "*") | |
1245 | this.endGame(eog); | |
1246 | } | |
1247 | if (this.mode == "computer" && this.vr.turn != this.mycolor) | |
1d184b4c BA |
1248 | setTimeout(this.playComputerMove, 500); |
1249 | }, | |
e64084da BA |
1250 | undo: function() { |
1251 | // Navigate after game is over | |
1252 | if (this.cursor == 0) | |
1253 | return; //already at the beginning | |
3300df38 BA |
1254 | if (this.cursor == this.vr.moves.length) |
1255 | this.incheck = []; //in case of... | |
e64084da BA |
1256 | const move = this.vr.moves[--this.cursor]; |
1257 | VariantRules.UndoOnBoard(this.vr.board, move); | |
1258 | this.$forceUpdate(); //TODO: ?! | |
2748531f BA |
1259 | }, |
1260 | undoInGame: function() { | |
1261 | const lm = this.vr.lastMove; | |
1262 | if (!!lm) | |
1263 | this.vr.undo(lm); | |
1264 | }, | |
1d184b4c BA |
1265 | }, |
1266 | }) |