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) { | |
0b7d99ec | 27 | const [sizeX,sizeY] = [V.size.x,V.size.y]; |
15c1295a | 28 | const smallScreen = (window.innerWidth <= 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 | { | |
da06a6eb | 412 | attrs: { "role": "dialog", "aria-labelledby": "eogMessage" }, |
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 | { | |
da06a6eb | 428 | attrs: { "id": "eogMessage" }, |
01a135e2 BA |
429 | "class": { "section": true }, |
430 | domProps: { innerHTML: eogMessage }, | |
431 | } | |
432 | ) | |
433 | ] | |
1d184b4c BA |
434 | ) |
435 | ] | |
436 | ) | |
437 | ]; | |
438 | elementArray = elementArray.concat(modalEog); | |
439 | } | |
12b46d8f | 440 | // NOTE: this modal could be in Pug view (no usage of Vue functions or variables) |
1d184b4c BA |
441 | const modalNewgame = [ |
442 | h('input', | |
443 | { | |
ecf44502 | 444 | attrs: { "id": "modal-newgame", type: "checkbox" }, |
1d184b4c BA |
445 | "class": { "modal": true }, |
446 | }), | |
447 | h('div', | |
448 | { | |
da06a6eb | 449 | attrs: { "role": "dialog", "aria-labelledby": "newGameTxt" }, |
1d184b4c BA |
450 | }, |
451 | [ | |
452 | h('div', | |
453 | { | |
454 | "class": { "card": true, "smallpad": true }, | |
455 | }, | |
456 | [ | |
457 | h('label', | |
458 | { | |
ecf44502 | 459 | attrs: { "id": "close-newgame", "for": "modal-newgame" }, |
1d184b4c BA |
460 | "class": { "modal-close": true }, |
461 | } | |
462 | ), | |
463 | h('h3', | |
464 | { | |
da06a6eb | 465 | attrs: { "id": "newGameTxt" }, |
1d184b4c BA |
466 | "class": { "section": true }, |
467 | domProps: { innerHTML: "New game" }, | |
468 | } | |
469 | ), | |
470 | h('p', | |
471 | { | |
472 | "class": { "section": true }, | |
473 | domProps: { innerHTML: "Waiting for opponent..." }, | |
474 | } | |
475 | ) | |
476 | ] | |
477 | ) | |
478 | ] | |
479 | ) | |
480 | ]; | |
481 | elementArray = elementArray.concat(modalNewgame); | |
2748531f BA |
482 | const modalFenEdit = [ |
483 | h('input', | |
484 | { | |
485 | attrs: { "id": "modal-fenedit", type: "checkbox" }, | |
486 | "class": { "modal": true }, | |
487 | }), | |
488 | h('div', | |
489 | { | |
da06a6eb | 490 | attrs: { "role": "dialog", "aria-labelledby": "titleFenedit" }, |
2748531f BA |
491 | }, |
492 | [ | |
493 | h('div', | |
494 | { | |
495 | "class": { "card": true, "smallpad": true }, | |
496 | }, | |
497 | [ | |
498 | h('label', | |
499 | { | |
500 | attrs: { "id": "close-fenedit", "for": "modal-fenedit" }, | |
501 | "class": { "modal-close": true }, | |
502 | } | |
503 | ), | |
504 | h('h3', | |
505 | { | |
da06a6eb | 506 | attrs: { "id": "titleFenedit" }, |
2748531f BA |
507 | "class": { "section": true }, |
508 | domProps: { innerHTML: "Position + flags (FEN):" }, | |
509 | } | |
510 | ), | |
511 | h('input', | |
512 | { | |
513 | attrs: { | |
514 | "id": "input-fen", | |
515 | type: "text", | |
516 | value: VariantRules.GenRandInitFen(), | |
517 | }, | |
518 | } | |
519 | ), | |
520 | h('button', | |
521 | { | |
522 | on: { click: | |
523 | () => { | |
524 | const fen = document.getElementById("input-fen").value; | |
525 | document.getElementById("modal-fenedit").checked = false; | |
526 | this.newGame("friend", fen); | |
527 | } | |
528 | }, | |
529 | domProps: { innerHTML: "Ok" }, | |
530 | } | |
531 | ), | |
532 | h('button', | |
533 | { | |
534 | on: { click: | |
535 | () => { | |
536 | document.getElementById("input-fen").value = | |
537 | VariantRules.GenRandInitFen(); | |
538 | } | |
539 | }, | |
540 | domProps: { innerHTML: "Random" }, | |
541 | } | |
542 | ), | |
543 | ] | |
544 | ) | |
545 | ] | |
546 | ) | |
547 | ]; | |
548 | elementArray = elementArray.concat(modalFenEdit); | |
88af03d2 BA |
549 | const modalSettings = [ |
550 | h('input', | |
551 | { | |
552 | attrs: { "id": "modal-settings", type: "checkbox" }, | |
553 | "class": { "modal": true }, | |
554 | }), | |
555 | h('div', | |
556 | { | |
da06a6eb | 557 | attrs: { "role": "dialog", "aria-labelledby": "settingsTitle" }, |
88af03d2 BA |
558 | }, |
559 | [ | |
560 | h('div', | |
561 | { | |
562 | "class": { "card": true, "smallpad": true }, | |
563 | }, | |
564 | [ | |
565 | h('label', | |
566 | { | |
567 | attrs: { "id": "close-settings", "for": "modal-settings" }, | |
568 | "class": { "modal-close": true }, | |
569 | } | |
570 | ), | |
571 | h('h3', | |
572 | { | |
da06a6eb | 573 | attrs: { "id": "settingsTitle" }, |
88af03d2 | 574 | "class": { "section": true }, |
a897b421 | 575 | domProps: { innerHTML: "Preferences" }, |
88af03d2 BA |
576 | } |
577 | ), | |
12b46d8f BA |
578 | h('fieldset', |
579 | { }, | |
580 | [ | |
2812515a | 581 | //h('legend', { domProps: { innerHTML: "Legend title" } }), |
12b46d8f BA |
582 | h('label', |
583 | { | |
2812515a | 584 | attrs: { for: "setHints" }, |
12b46d8f BA |
585 | domProps: { innerHTML: "Show hints?" }, |
586 | }, | |
587 | ), | |
588 | h('input', | |
589 | { | |
590 | attrs: { | |
591 | "id": "setHints", | |
592 | type: "checkbox", | |
593 | checked: this.hints, | |
594 | }, | |
595 | on: { "change": this.toggleHints }, | |
596 | } | |
597 | ), | |
598 | ] | |
a897b421 | 599 | ), |
12b46d8f BA |
600 | h('fieldset', |
601 | { }, | |
602 | [ | |
603 | h('label', | |
604 | { | |
2812515a | 605 | attrs: { for: "selectColor" }, |
12b46d8f BA |
606 | domProps: { innerHTML: "Board colors" }, |
607 | }, | |
608 | ), | |
609 | h("select", | |
610 | { | |
611 | attrs: { "id": "selectColor" }, | |
612 | on: { "change": this.setColor }, | |
613 | }, | |
614 | [ | |
615 | h("option", | |
616 | { | |
617 | domProps: { | |
618 | "value": "lichess", | |
2812515a | 619 | innerHTML: "brown" |
12b46d8f BA |
620 | }, |
621 | attrs: { "selected": this.color=="lichess" }, | |
622 | } | |
623 | ), | |
624 | h("option", | |
625 | { | |
626 | domProps: { | |
627 | "value": "chesscom", | |
2812515a | 628 | innerHTML: "green" |
12b46d8f BA |
629 | }, |
630 | attrs: { "selected": this.color=="chesscom" }, | |
631 | } | |
632 | ), | |
633 | h("option", | |
634 | { | |
635 | domProps: { | |
636 | "value": "chesstempo", | |
2812515a | 637 | innerHTML: "blue" |
12b46d8f BA |
638 | }, |
639 | attrs: { "selected": this.color=="chesstempo" }, | |
640 | } | |
641 | ), | |
642 | ], | |
643 | ), | |
644 | ] | |
645 | ), | |
646 | h('fieldset', | |
647 | { }, | |
648 | [ | |
649 | h('label', | |
650 | { | |
2812515a | 651 | attrs: { for: "selectSound" }, |
130db3ef | 652 | domProps: { innerHTML: "Play sounds?" }, |
12b46d8f BA |
653 | }, |
654 | ), | |
655 | h("select", | |
656 | { | |
657 | attrs: { "id": "selectSound" }, | |
658 | on: { "change": this.setSound }, | |
659 | }, | |
660 | [ | |
661 | h("option", | |
662 | { | |
663 | domProps: { | |
664 | "value": "0", | |
130db3ef | 665 | innerHTML: "None" |
12b46d8f | 666 | }, |
73cbe9de | 667 | attrs: { "selected": this.sound==0 }, |
12b46d8f BA |
668 | } |
669 | ), | |
670 | h("option", | |
671 | { | |
672 | domProps: { | |
673 | "value": "1", | |
130db3ef | 674 | innerHTML: "Newgame" |
12b46d8f | 675 | }, |
73cbe9de | 676 | attrs: { "selected": this.sound==1 }, |
12b46d8f BA |
677 | } |
678 | ), | |
679 | h("option", | |
680 | { | |
681 | domProps: { | |
682 | "value": "2", | |
130db3ef | 683 | innerHTML: "All" |
12b46d8f | 684 | }, |
73cbe9de | 685 | attrs: { "selected": this.sound==2 }, |
12b46d8f BA |
686 | } |
687 | ), | |
688 | ], | |
689 | ), | |
690 | ] | |
a897b421 | 691 | ), |
88af03d2 BA |
692 | ] |
693 | ) | |
694 | ] | |
695 | ) | |
696 | ]; | |
697 | elementArray = elementArray.concat(modalSettings); | |
1d184b4c BA |
698 | const actions = h('div', |
699 | { | |
700 | attrs: { "id": "actions" }, | |
701 | 'class': { 'text-center': true }, | |
702 | }, | |
703 | actionArray | |
704 | ); | |
705 | elementArray.push(actions); | |
01a135e2 BA |
706 | if (this.score != "*") |
707 | { | |
708 | elementArray.push( | |
709 | h('div', | |
61a262b2 BA |
710 | { |
711 | attrs: { id: "pgn-div" }, | |
712 | "class": { "section-content": true }, | |
713 | }, | |
01a135e2 | 714 | [ |
01ca2adc BA |
715 | h('a', |
716 | { | |
717 | attrs: { | |
718 | id: "download", | |
719 | href: "#", | |
720 | } | |
721 | } | |
722 | ), | |
01a135e2 BA |
723 | h('p', |
724 | { | |
01ca2adc | 725 | attrs: { id: "pgn-game" }, |
85be503d BA |
726 | domProps: { innerHTML: this.pgnTxt } |
727 | } | |
61a262b2 BA |
728 | ), |
729 | h('button', | |
730 | { | |
731 | attrs: { "id": "downloadBtn" }, | |
732 | on: { click: this.download }, | |
733 | domProps: { innerHTML: "Download game" }, | |
734 | } | |
735 | ), | |
85be503d BA |
736 | ] |
737 | ) | |
738 | ); | |
739 | } | |
740 | else if (this.mode != "idle") | |
741 | { | |
2748531f | 742 | // Show current FEN |
85be503d BA |
743 | elementArray.push( |
744 | h('div', | |
61a262b2 BA |
745 | { |
746 | attrs: { id: "fen-div" }, | |
747 | "class": { "section-content": true }, | |
748 | }, | |
85be503d BA |
749 | [ |
750 | h('p', | |
751 | { | |
752 | attrs: { id: "fen-string" }, | |
2748531f | 753 | domProps: { innerHTML: this.vr.getFen() } |
01a135e2 BA |
754 | } |
755 | ) | |
756 | ] | |
757 | ) | |
758 | ); | |
759 | } | |
1d184b4c BA |
760 | return h( |
761 | 'div', | |
762 | { | |
763 | 'class': { | |
764 | "col-sm-12":true, | |
765 | "col-md-8":true, | |
766 | "col-md-offset-2":true, | |
767 | "col-lg-6":true, | |
768 | "col-lg-offset-3":true, | |
769 | }, | |
dda21a71 | 770 | // NOTE: click = mousedown + mouseup |
1d184b4c BA |
771 | on: { |
772 | mousedown: this.mousedown, | |
773 | mousemove: this.mousemove, | |
774 | mouseup: this.mouseup, | |
ffea77d9 BA |
775 | touchstart: this.mousedown, |
776 | touchmove: this.mousemove, | |
777 | touchend: this.mouseup, | |
1d184b4c BA |
778 | }, |
779 | }, | |
780 | elementArray | |
781 | ); | |
782 | }, | |
783 | created: function() { | |
784 | const url = socketUrl; | |
785 | const continuation = (localStorage.getItem("variant") === variant); | |
15c1295a | 786 | this.myid = (continuation ? localStorage.getItem("myid") : getRandString()); |
92ff5bae BA |
787 | if (!continuation) |
788 | { | |
92342261 BA |
789 | // HACK: play a small silent sound to allow "new game" sound later |
790 | // if tab not focused (TODO: does it really work ?!) | |
92ff5bae BA |
791 | new Audio("/sounds/silent.mp3").play().then(() => {}).catch(err => {}); |
792 | } | |
1d184b4c | 793 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant); |
d35f20e4 | 794 | const socketOpenListener = () => { |
1d184b4c BA |
795 | if (continuation) |
796 | { | |
dfb4afc1 BA |
797 | const fen = localStorage.getItem("fen"); |
798 | const mycolor = localStorage.getItem("mycolor"); | |
799 | const oppid = localStorage.getItem("oppid"); | |
800 | const moves = JSON.parse(localStorage.getItem("moves")); | |
801 | this.newGame("human", fen, mycolor, oppid, moves, true); | |
a29d9d6b | 802 | // Send ping to server (answer pong if opponent is connected) |
ecf44502 | 803 | this.conn.send(JSON.stringify({code:"ping",oppid:this.oppid})); |
1d184b4c | 804 | } |
30ff6e04 BA |
805 | else if (localStorage.getItem("newgame") === variant) |
806 | { | |
807 | // New game request has been cancelled on disconnect | |
001344b9 | 808 | this.newGame("human", undefined, undefined, undefined, undefined, "reconnect"); |
30ff6e04 | 809 | } |
1d184b4c | 810 | }; |
d35f20e4 | 811 | const socketMessageListener = msg => { |
1d184b4c BA |
812 | const data = JSON.parse(msg.data); |
813 | switch (data.code) | |
814 | { | |
15c1295a BA |
815 | case "duplicate": |
816 | // We opened another tab on the same game | |
817 | this.mode = "idle"; | |
818 | this.vr = null; | |
819 | alert("Already playing a game in this variant on another tab!"); | |
820 | break; | |
1d184b4c | 821 | case "newgame": //opponent found |
92342261 BA |
822 | // oppid: opponent socket ID |
823 | this.newGame("human", data.fen, data.color, data.oppid); | |
1d184b4c BA |
824 | break; |
825 | case "newmove": //..he played! | |
826 | this.play(data.move, "animate"); | |
827 | break; | |
f3802fcd | 828 | case "pong": //received if we sent a ping (game still alive on our side) |
1d184b4c | 829 | this.oppConnected = true; |
a29d9d6b | 830 | const L = this.vr.moves.length; |
f3802fcd | 831 | // Send our "last state" informations to opponent |
a29d9d6b BA |
832 | this.conn.send(JSON.stringify({ |
833 | code:"lastate", | |
f3802fcd | 834 | oppid:this.oppid, |
a29d9d6b BA |
835 | lastMove:L>0?this.vr.moves[L-1]:undefined, |
836 | movesCount:L, | |
837 | })); | |
1d184b4c | 838 | break; |
a29d9d6b BA |
839 | case "lastate": //got opponent infos about last move (we might have resigned) |
840 | if (this.mode!="human" || this.oppid!=data.oppid) | |
841 | { | |
842 | // OK, we resigned | |
843 | this.conn.send(JSON.stringify({ | |
844 | code:"lastate", | |
f3802fcd | 845 | oppid:this.oppid, |
a29d9d6b BA |
846 | lastMove:undefined, |
847 | movesCount:-1, | |
848 | })); | |
849 | } | |
850 | else if (data.movesCount < 0) | |
851 | { | |
852 | // OK, he resigned | |
853 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); | |
854 | } | |
855 | else if (data.movesCount < this.vr.moves.length) | |
856 | { | |
857 | // We must tell last move to opponent | |
858 | const L = this.vr.moves.length; | |
859 | this.conn.send(JSON.stringify({ | |
860 | code:"lastate", | |
f3802fcd | 861 | oppid:this.oppid, |
a29d9d6b BA |
862 | lastMove:this.vr.moves[L-1], |
863 | movesCount:L, | |
864 | })); | |
865 | } | |
866 | else if (data.movesCount > this.vr.moves.length) //just got last move from him | |
867 | this.play(data.lastMove, "animate"); | |
ecf44502 | 868 | break; |
1d184b4c | 869 | case "resign": //..you won! |
dfb4afc1 | 870 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); |
1d184b4c | 871 | break; |
f3802fcd | 872 | // TODO: also use (dis)connect info to count online players? |
1d184b4c BA |
873 | case "connect": |
874 | case "disconnect": | |
875 | if (this.mode == "human" && this.oppid == data.id) | |
876 | this.oppConnected = (data.code == "connect"); | |
877 | break; | |
878 | } | |
879 | }; | |
d35f20e4 | 880 | const socketCloseListener = () => { |
d35f20e4 BA |
881 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant); |
882 | this.conn.addEventListener('open', socketOpenListener); | |
883 | this.conn.addEventListener('message', socketMessageListener); | |
884 | this.conn.addEventListener('close', socketCloseListener); | |
885 | }; | |
886 | this.conn.onopen = socketOpenListener; | |
887 | this.conn.onmessage = socketMessageListener; | |
888 | this.conn.onclose = socketCloseListener; | |
e64084da BA |
889 | // Listen to keyboard left/right to navigate in game |
890 | document.onkeydown = event => { | |
2748531f | 891 | if (this.mode == "idle" && !!this.vr && this.vr.moves.length > 0 |
e64084da BA |
892 | && [37,39].includes(event.keyCode)) |
893 | { | |
894 | event.preventDefault(); | |
895 | if (event.keyCode == 37) //Back | |
896 | this.undo(); | |
897 | else //Forward (39) | |
898 | this.play(); | |
899 | } | |
900 | }; | |
1d184b4c BA |
901 | }, |
902 | methods: { | |
01ca2adc BA |
903 | download: function() { |
904 | let content = document.getElementById("pgn-game").innerHTML; | |
905 | content = content.replace(/<br>/g, "\n"); | |
906 | // Prepare and trigger download link | |
907 | let downloadAnchor = document.getElementById("download"); | |
908 | downloadAnchor.setAttribute("download", "game.pgn"); | |
92342261 BA |
909 | downloadAnchor.href = "data:text/plain;charset=utf-8," + |
910 | encodeURIComponent(content); | |
01ca2adc BA |
911 | downloadAnchor.click(); |
912 | }, | |
dfb4afc1 BA |
913 | endGame: function(score) { |
914 | this.score = score; | |
ecf44502 | 915 | let modalBox = document.getElementById("modal-eog"); |
186516b8 | 916 | modalBox.checked = true; |
204e289b | 917 | // Variants may have special PGN structure (so next function isn't defined here) |
3840e240 BA |
918 | this.pgnTxt = this.vr.getPGN(this.mycolor, this.score, this.fenStart, this.mode); |
919 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
1d184b4c BA |
920 | if (this.mode == "human") |
921 | this.clearStorage(); | |
3840e240 | 922 | this.mode = "idle"; |
e64084da | 923 | this.cursor = this.vr.moves.length; //to navigate in finished game |
1d184b4c BA |
924 | this.oppid = ""; |
925 | }, | |
f3802fcd BA |
926 | getEndgameMessage: function(score) { |
927 | let eogMessage = "Unfinished"; | |
928 | switch (this.score) | |
929 | { | |
930 | case "1-0": | |
931 | eogMessage = "White win"; | |
932 | break; | |
933 | case "0-1": | |
934 | eogMessage = "Black win"; | |
935 | break; | |
936 | case "1/2": | |
937 | eogMessage = "Draw"; | |
938 | break; | |
939 | } | |
940 | return eogMessage; | |
941 | }, | |
762b7c9c BA |
942 | setStorage: function() { |
943 | localStorage.setItem("myid", this.myid); | |
944 | localStorage.setItem("variant", variant); | |
945 | localStorage.setItem("mycolor", this.mycolor); | |
946 | localStorage.setItem("oppid", this.oppid); | |
947 | localStorage.setItem("fenStart", this.fenStart); | |
948 | localStorage.setItem("moves", JSON.stringify(this.vr.moves)); | |
1d184b4c | 949 | localStorage.setItem("fen", this.vr.getFen()); |
762b7c9c BA |
950 | }, |
951 | updateStorage: function() { | |
dfb4afc1 | 952 | localStorage.setItem("moves", JSON.stringify(this.vr.moves)); |
762b7c9c | 953 | localStorage.setItem("fen", this.vr.getFen()); |
1d184b4c BA |
954 | }, |
955 | clearStorage: function() { | |
956 | delete localStorage["variant"]; | |
957 | delete localStorage["myid"]; | |
958 | delete localStorage["mycolor"]; | |
959 | delete localStorage["oppid"]; | |
762b7c9c | 960 | delete localStorage["fenStart"]; |
1d184b4c | 961 | delete localStorage["fen"]; |
dfb4afc1 | 962 | delete localStorage["moves"]; |
1d184b4c | 963 | }, |
c148615e BA |
964 | // HACK because mini-css tooltips are persistent after click... |
965 | getRidOfTooltip: function(elt) { | |
966 | elt.style.visibility = "hidden"; | |
967 | setTimeout(() => { elt.style.visibility="visible"; }, 100); | |
968 | }, | |
88af03d2 BA |
969 | showSettings: function(e) { |
970 | this.getRidOfTooltip(e.currentTarget); | |
971 | document.getElementById("modal-settings").checked = true; | |
972 | }, | |
973 | toggleHints: function() { | |
974 | this.hints = !this.hints; | |
975 | setCookie("hints", this.hints ? "1" : "0"); | |
976 | }, | |
2812515a BA |
977 | setColor: function(e) { |
978 | this.color = e.target.options[e.target.selectedIndex].value; | |
979 | setCookie("color", this.color); | |
12b46d8f | 980 | }, |
2812515a | 981 | setSound: function(e) { |
130db3ef | 982 | this.sound = parseInt(e.target.options[e.target.selectedIndex].value); |
2812515a | 983 | setCookie("sound", this.sound); |
12b46d8f | 984 | }, |
c148615e BA |
985 | clickGameSeek: function(e) { |
986 | this.getRidOfTooltip(e.currentTarget); | |
f3802fcd BA |
987 | if (this.mode == "human") |
988 | return; //no newgame while playing | |
989 | if (this.seek) | |
01a135e2 | 990 | { |
283d06a4 | 991 | this.conn.send(JSON.stringify({code:"cancelnewgame"})); |
f3802fcd | 992 | delete localStorage["newgame"]; //cancel game seek |
01a135e2 BA |
993 | this.seek = false; |
994 | } | |
f3802fcd | 995 | else |
f3802fcd | 996 | this.newGame("human"); |
f3802fcd | 997 | }, |
c148615e BA |
998 | clickComputerGame: function(e) { |
999 | this.getRidOfTooltip(e.currentTarget); | |
f3802fcd BA |
1000 | if (this.mode == "human") |
1001 | return; //no newgame while playing | |
1002 | this.newGame("computer"); | |
1003 | }, | |
2748531f BA |
1004 | clickFriendGame: function(e) { |
1005 | this.getRidOfTooltip(e.currentTarget); | |
1006 | document.getElementById("modal-fenedit").checked = true; | |
1007 | }, | |
2748531f BA |
1008 | resign: function(e) { |
1009 | this.getRidOfTooltip(e.currentTarget); | |
c148615e BA |
1010 | if (this.mode == "human" && this.oppConnected) |
1011 | { | |
1012 | try { | |
1013 | this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid})); | |
1014 | } catch (INVALID_STATE_ERR) { | |
1015 | return; //socket is not ready (and not yet reconnected) | |
1016 | } | |
1017 | } | |
1018 | this.endGame(this.mycolor=="w"?"0-1":"1-0"); | |
1019 | }, | |
dfb4afc1 | 1020 | newGame: function(mode, fenInit, color, oppId, moves, continuation) { |
523da5d5 | 1021 | const fen = fenInit || VariantRules.GenRandInitFen(); |
1d184b4c BA |
1022 | console.log(fen); //DEBUG |
1023 | if (mode=="human" && !oppId) | |
1024 | { | |
cd3174c5 BA |
1025 | const storageVariant = localStorage.getItem("variant"); |
1026 | if (!!storageVariant && storageVariant !== variant) | |
7931e479 | 1027 | return alert("Finish your " + storageVariant + " game first!"); |
1d184b4c | 1028 | // Send game request and wait.. |
01a135e2 BA |
1029 | localStorage["newgame"] = variant; |
1030 | this.seek = true; | |
1d184b4c | 1031 | this.clearStorage(); //in case of |
d35f20e4 BA |
1032 | try { |
1033 | this.conn.send(JSON.stringify({code:"newgame", fen:fen})); | |
1034 | } catch (INVALID_STATE_ERR) { | |
1035 | return; //nothing achieved | |
1036 | } | |
f3802fcd | 1037 | if (continuation !== "reconnect") //TODO: bad HACK... |
a68d899d | 1038 | { |
ecf44502 | 1039 | let modalBox = document.getElementById("modal-newgame"); |
a68d899d BA |
1040 | modalBox.checked = true; |
1041 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
1042 | } | |
1d184b4c BA |
1043 | return; |
1044 | } | |
dfb4afc1 | 1045 | this.vr = new VariantRules(fen, moves || []); |
c148615e | 1046 | this.score = "*"; |
3840e240 | 1047 | this.pgnTxt = ""; //redundant with this.score = "*", but cleaner |
1d184b4c | 1048 | this.mode = mode; |
1fcaa356 | 1049 | this.incheck = []; //in case of |
2748531f | 1050 | this.fenStart = (continuation ? localStorage.getItem("fenStart") : fen); |
1d184b4c BA |
1051 | if (mode=="human") |
1052 | { | |
1053 | // Opponent found! | |
2812515a | 1054 | if (!continuation) //not playing sound on game continuation |
1d184b4c | 1055 | { |
2812515a BA |
1056 | if (this.sound >= 1) |
1057 | new Audio("/sounds/newgame.mp3").play().then(() => {}).catch(err => {}); | |
ecf44502 | 1058 | document.getElementById("modal-newgame").checked = false; |
1d184b4c BA |
1059 | } |
1060 | this.oppid = oppId; | |
15c1295a | 1061 | this.oppConnected = !continuation; |
1d184b4c | 1062 | this.mycolor = color; |
186516b8 | 1063 | this.seek = false; |
e64a4eff BA |
1064 | if (!!moves && moves.length > 0) //imply continuation |
1065 | { | |
e64a4eff | 1066 | const lastMove = moves[moves.length-1]; |
cd4cad04 | 1067 | this.vr.undo(lastMove); |
46302e64 | 1068 | this.incheck = this.vr.getCheckSquares(lastMove); |
e64a4eff BA |
1069 | this.vr.play(lastMove, "ingame"); |
1070 | } | |
186516b8 | 1071 | delete localStorage["newgame"]; |
762b7c9c | 1072 | this.setStorage(); //in case of interruptions |
1d184b4c | 1073 | } |
2748531f | 1074 | else if (mode == "computer") |
1d184b4c | 1075 | { |
523da5d5 | 1076 | this.mycolor = Math.random() < 0.5 ? 'w' : 'b'; |
1d184b4c BA |
1077 | if (this.mycolor == 'b') |
1078 | setTimeout(this.playComputerMove, 500); | |
1079 | } | |
2748531f | 1080 | //else: against a (IRL) friend: nothing more to do |
1d184b4c BA |
1081 | }, |
1082 | playComputerMove: function() { | |
4b353936 | 1083 | const timeStart = Date.now(); |
46302e64 | 1084 | const compMove = this.vr.getComputerMove(); |
4b353936 BA |
1085 | // (first move) HACK: avoid selecting elements before they appear on page: |
1086 | const delay = Math.max(500-(Date.now()-timeStart), 0); | |
4ecf423b BA |
1087 | setTimeout(() => { |
1088 | if (this.mode == "computer") //Warning: mode could have changed! | |
1089 | this.play(compMove, "animate") | |
1090 | }, delay); | |
1d184b4c BA |
1091 | }, |
1092 | // Get the identifier of a HTML table cell from its numeric coordinates o.x,o.y. | |
1093 | getSquareId: function(o) { | |
1094 | // NOTE: a separator is required to allow any size of board | |
1095 | return "sq-" + o.x + "-" + o.y; | |
1096 | }, | |
1097 | // Inverse function | |
1098 | getSquareFromId: function(id) { | |
1099 | let idParts = id.split('-'); | |
1100 | return [parseInt(idParts[1]), parseInt(idParts[2])]; | |
1101 | }, | |
1102 | mousedown: function(e) { | |
1103 | e = e || window.event; | |
44461547 BA |
1104 | let ingame = false; |
1105 | let elem = e.target; | |
1106 | while (!ingame && elem !== null) | |
1107 | { | |
1108 | if (elem.classList.contains("game")) | |
1109 | { | |
1110 | ingame = true; | |
1111 | break; | |
1112 | } | |
1113 | elem = elem.parentElement; | |
1114 | } | |
1115 | if (!ingame) //let default behavior (click on button...) | |
1116 | return; | |
1d184b4c BA |
1117 | e.preventDefault(); //disable native drag & drop |
1118 | if (!this.selectedPiece && e.target.classList.contains("piece")) | |
1119 | { | |
1120 | // Next few lines to center the piece on mouse cursor | |
1121 | let rect = e.target.parentNode.getBoundingClientRect(); | |
1122 | this.start = { | |
1123 | x: rect.x + rect.width/2, | |
1124 | y: rect.y + rect.width/2, | |
1125 | id: e.target.parentNode.id | |
1126 | }; | |
1127 | this.selectedPiece = e.target.cloneNode(); | |
1128 | this.selectedPiece.style.position = "absolute"; | |
1129 | this.selectedPiece.style.top = 0; | |
1130 | this.selectedPiece.style.display = "inline-block"; | |
1131 | this.selectedPiece.style.zIndex = 3000; | |
1132 | let startSquare = this.getSquareFromId(e.target.parentNode.id); | |
2748531f BA |
1133 | const iCanPlay = this.mode!="idle" |
1134 | && (this.mode=="friend" || this.vr.canIplay(this.mycolor,startSquare)); | |
1135 | this.possibleMoves = iCanPlay ? this.vr.getPossibleMovesFrom(startSquare) : []; | |
92342261 BA |
1136 | // Next line add moving piece just after current image |
1137 | // (required for Crazyhouse reserve) | |
5bd679d5 | 1138 | e.target.parentNode.insertBefore(this.selectedPiece, e.target.nextSibling); |
1d184b4c BA |
1139 | } |
1140 | }, | |
1141 | mousemove: function(e) { | |
1142 | if (!this.selectedPiece) | |
1143 | return; | |
1144 | e = e || window.event; | |
1145 | // If there is an active element, move it around | |
1146 | if (!!this.selectedPiece) | |
1147 | { | |
ffea77d9 BA |
1148 | const [offsetX,offsetY] = !!e.clientX |
1149 | ? [e.clientX,e.clientY] //desktop browser | |
1150 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone | |
1151 | this.selectedPiece.style.left = (offsetX-this.start.x) + "px"; | |
1152 | this.selectedPiece.style.top = (offsetY-this.start.y) + "px"; | |
1d184b4c BA |
1153 | } |
1154 | }, | |
1155 | mouseup: function(e) { | |
1156 | if (!this.selectedPiece) | |
1157 | return; | |
1158 | e = e || window.event; | |
1159 | // Read drop target (or parentElement, parentNode... if type == "img") | |
92342261 | 1160 | this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords |
ffea77d9 BA |
1161 | const [offsetX,offsetY] = !!e.clientX |
1162 | ? [e.clientX,e.clientY] | |
1163 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; | |
1164 | let landing = document.elementFromPoint(offsetX, offsetY); | |
1d184b4c | 1165 | this.selectedPiece.style.zIndex = 3000; |
92342261 BA |
1166 | // Next condition: classList.contains(piece) fails because of marks |
1167 | while (landing.tagName == "IMG") | |
1d184b4c | 1168 | landing = landing.parentNode; |
92342261 BA |
1169 | if (this.start.id == landing.id) |
1170 | { | |
1171 | // A click: selectedPiece and possibleMoves are already filled | |
1d184b4c | 1172 | return; |
92342261 | 1173 | } |
1d184b4c BA |
1174 | // OK: process move attempt |
1175 | let endSquare = this.getSquareFromId(landing.id); | |
1176 | let moves = this.findMatchingMoves(endSquare); | |
1177 | this.possibleMoves = []; | |
1178 | if (moves.length > 1) | |
1179 | this.choices = moves; | |
1180 | else if (moves.length==1) | |
1181 | this.play(moves[0]); | |
1182 | // Else: impossible move | |
1183 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); | |
1184 | delete this.selectedPiece; | |
1185 | this.selectedPiece = null; | |
1186 | }, | |
1187 | findMatchingMoves: function(endSquare) { | |
1188 | // Run through moves list and return the matching set (if promotions...) | |
1189 | let moves = []; | |
1190 | this.possibleMoves.forEach(function(m) { | |
1191 | if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) | |
1192 | moves.push(m); | |
1193 | }); | |
1194 | return moves; | |
1195 | }, | |
1196 | animateMove: function(move) { | |
1197 | let startSquare = document.getElementById(this.getSquareId(move.start)); | |
1198 | let endSquare = document.getElementById(this.getSquareId(move.end)); | |
1199 | let rectStart = startSquare.getBoundingClientRect(); | |
1200 | let rectEnd = endSquare.getBoundingClientRect(); | |
1201 | let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; | |
92ff5bae BA |
1202 | let movingPiece = |
1203 | document.querySelector("#" + this.getSquareId(move.start) + " > img.piece"); | |
92342261 | 1204 | // HACK for animation (with positive translate, image slides "under background") |
1d184b4c BA |
1205 | // Possible improvement: just alter squares on the piece's way... |
1206 | squares = document.getElementsByClassName("board"); | |
1207 | for (let i=0; i<squares.length; i++) | |
1208 | { | |
1209 | let square = squares.item(i); | |
1210 | if (square.id != this.getSquareId(move.start)) | |
1211 | square.style.zIndex = "-1"; | |
1212 | } | |
92342261 BA |
1213 | movingPiece.style.transform = "translate(" + translation.x + "px," + |
1214 | translation.y + "px)"; | |
1d184b4c BA |
1215 | movingPiece.style.transitionDuration = "0.2s"; |
1216 | movingPiece.style.zIndex = "3000"; | |
1217 | setTimeout( () => { | |
1218 | for (let i=0; i<squares.length; i++) | |
1219 | squares.item(i).style.zIndex = "auto"; | |
1220 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap | |
1221 | this.play(move); | |
1222 | }, 200); | |
1223 | }, | |
1224 | play: function(move, programmatic) { | |
e64084da BA |
1225 | if (!move) |
1226 | { | |
1227 | // Navigate after game is over | |
1228 | if (this.cursor >= this.vr.moves.length) | |
1229 | return; //already at the end | |
1230 | move = this.vr.moves[this.cursor++]; | |
1231 | } | |
1d184b4c BA |
1232 | if (!!programmatic) //computer or human opponent |
1233 | { | |
1234 | this.animateMove(move); | |
1235 | return; | |
1236 | } | |
1237 | // Not programmatic, or animation is over | |
1238 | if (this.mode == "human" && this.vr.turn == this.mycolor) | |
a29d9d6b | 1239 | this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid})); |
2812515a BA |
1240 | if (this.sound == 2) |
1241 | new Audio("/sounds/chessmove1.mp3").play().then(() => {}).catch(err => {}); | |
e64084da | 1242 | if (this.mode != "idle") |
3300df38 BA |
1243 | { |
1244 | this.incheck = this.vr.getCheckSquares(move); //is opponent in check? | |
e64084da | 1245 | this.vr.play(move, "ingame"); |
3300df38 | 1246 | } |
e64084da | 1247 | else |
3300df38 | 1248 | { |
e64084da | 1249 | VariantRules.PlayOnBoard(this.vr.board, move); |
3300df38 BA |
1250 | this.$forceUpdate(); //TODO: ?! |
1251 | } | |
1d184b4c BA |
1252 | if (this.mode == "human") |
1253 | this.updateStorage(); //after our moves and opponent moves | |
e64084da BA |
1254 | if (this.mode != "idle") |
1255 | { | |
1256 | const eog = this.vr.checkGameOver(); | |
1257 | if (eog != "*") | |
1258 | this.endGame(eog); | |
1259 | } | |
1260 | if (this.mode == "computer" && this.vr.turn != this.mycolor) | |
1d184b4c BA |
1261 | setTimeout(this.playComputerMove, 500); |
1262 | }, | |
e64084da BA |
1263 | undo: function() { |
1264 | // Navigate after game is over | |
1265 | if (this.cursor == 0) | |
1266 | return; //already at the beginning | |
3300df38 BA |
1267 | if (this.cursor == this.vr.moves.length) |
1268 | this.incheck = []; //in case of... | |
e64084da BA |
1269 | const move = this.vr.moves[--this.cursor]; |
1270 | VariantRules.UndoOnBoard(this.vr.board, move); | |
1271 | this.$forceUpdate(); //TODO: ?! | |
2748531f BA |
1272 | }, |
1273 | undoInGame: function() { | |
1274 | const lm = this.vr.lastMove; | |
1275 | if (!!lm) | |
1276 | this.vr.undo(lm); | |
1277 | }, | |
1d184b4c BA |
1278 | }, |
1279 | }) |