1 Vue
.component('my-game', {
4 vr: null, //object to check moves, store them, FEN..
6 possibleMoves: [], //filled after each valid click/dragstart
7 choices: [], //promotion pieces, or checkered captures... (contain possible pieces)
8 start: {}, //pixels coordinates + id of starting square (click or drag)
9 selectedPiece: null, //moving piece (or clicked piece)
10 conn: null, //socket messages
11 score: "*", //'*' means 'unfinished'
12 mode: "idle", //human, computer or idle (when not playing)
13 oppid: "", //opponent ID in case of HH game
19 expert: document
.cookie
.length
>0 ? document
.cookie
.substr(-1)=="1" : false,
20 gameId: "", //used to limit computer moves' time
24 const [sizeX
,sizeY
] = VariantRules
.size
;
25 // Precompute hints squares to facilitate rendering
26 let hintSquares
= doubleArray(sizeX
, sizeY
, false);
27 this.possibleMoves
.forEach(m
=> { hintSquares
[m
.end
.x
][m
.end
.y
] = true; });
28 // Also precompute in-check squares
29 let incheckSq
= doubleArray(sizeX
, sizeY
, false);
30 this.incheck
.forEach(sq
=> { incheckSq
[sq
[0]][sq
[1]] = true; });
31 let elementArray
= [];
32 const playingHuman
= (this.mode
== "human");
33 const playingComp
= (this.mode
== "computer");
37 on: { click: this.clickGameSeek
},
38 attrs: { "aria-label": 'New game VS human' },
41 "bottom": true, //display below
43 "playing": playingHuman
,
46 [h('i', { 'class': { "material-icons": true } }, "accessibility")]),
49 on: { click: this.clickComputerGame
},
50 attrs: { "aria-label": 'New game VS computer' },
54 "playing": playingComp
,
57 [h('i', { 'class': { "material-icons": true } }, "computer")])
61 const square00
= document
.getElementById("sq-0-0");
62 const squareWidth
= !!square00
63 ? parseFloat(window
.getComputedStyle(square00
).width
.slice(0,-2))
65 const indicWidth
= (squareWidth
>0 ? squareWidth
/2 : 20);
66 if (this.mode
== "human")
68 let connectedIndic
= h(
74 "connected": this.oppConnected
,
75 "disconnected": !this.oppConnected
,
78 "width": indicWidth
+ "px",
79 "height": indicWidth
+ "px",
83 elementArray
.push(connectedIndic
);
91 "white-turn": this.vr
.turn
=="w",
92 "black-turn": this.vr
.turn
=="b",
95 "width": indicWidth
+ "px",
96 "height": indicWidth
+ "px",
100 elementArray
.push(turnIndic
);
101 let expertSwitch
= h(
104 on: { click: this.toggleExpertMode
},
105 attrs: { "aria-label": 'Toggle expert mode' },
108 "topindicator": true,
110 "expert-switch": true,
111 "expert-mode": this.expert
,
114 [h('i', { 'class': { "material-icons": true } }, "remove_red_eye")]
116 elementArray
.push(expertSwitch
);
117 let choices
= h('div',
119 attrs: { "id": "choices" },
120 'class': { 'row': true },
122 "display": this.choices
.length
>0?"block":"none",
123 "top": "-" + ((sizeY
/2)*squareWidth+squareWidth/2) + "px",
124 "width": (this.choices
.length
* squareWidth
) + "px",
125 "height": squareWidth
+ "px",
128 this.choices
.map( m
=> { //a "choice" is a move
133 ['board'+sizeY
]: true,
136 'width': (100/this.choices
.length
) + "%",
137 'padding-bottom': (100/this.choices
.length
) + "%",
142 attrs: { "src": '/images/pieces/' +
143 VariantRules
.getPpath(m
.appear
[0].c
+m
.appear
[0].p
) + '.svg' },
144 'class': { 'choice-piece': true },
145 on: { "click": e
=> { this.play(m
); this.choices
=[]; } },
151 // Create board element (+ reserves if needed by variant or mode)
152 let gameDiv
= h('div',
154 'class': { 'game': true },
156 [_
.range(sizeX
).map(i
=> {
157 let ci
= this.mycolor
=='w' ? i : sizeX
-i
-1;
164 style: { 'opacity': this.choices
.length
>0?"0.5":"1" },
166 _
.range(sizeY
).map(j
=> {
167 let cj
= this.mycolor
=='w' ? j : sizeY
-j
-1;
169 if (this.vr
.board
[ci
][cj
] != VariantRules
.EMPTY
)
177 'ghost': !!this.selectedPiece
178 && this.selectedPiece
.parentNode
.id
== "sq-"+ci
+"-"+cj
,
181 src: "/images/pieces/" +
182 VariantRules
.getPpath(this.vr
.board
[ci
][cj
]) + ".svg",
188 if (!this.expert
&& hintSquares
[ci
][cj
])
198 src: "/images/mark.svg",
204 const lm
= this.vr
.lastMove
;
205 const highlight
= !!lm
&& _
.isMatch(lm
.end
, {x:ci
,y:cj
});
211 ['board'+sizeY
]: true,
212 'light-square': (i
+j
)%2==0 && (this.expert
|| !highlight
),
213 'dark-square': (i
+j
)%2==1 && (this.expert
|| !highlight
),
214 'highlight': !this.expert
&& highlight
,
215 'incheck': !this.expert
&& incheckSq
[ci
][cj
],
218 id: this.getSquareId({x:ci
,y:cj
}),
227 if (this.mode
!= "idle")
232 on: { click: this.resign
},
233 attrs: { "aria-label": 'Resign' },
239 [h('i', { 'class': { "material-icons": true } }, "flag")])
242 else if (this.vr
.moves
.length
> 0)
244 // A game finished, and another is not started yet: allow navigation
245 actionArray
= actionArray
.concat([
248 style: { "margin-left": "30px" },
249 on: { click: e
=> this.undo() },
250 attrs: { "aria-label": 'Undo' },
256 [h('i', { 'class': { "material-icons": true } }, "fast_rewind")]),
259 on: { click: e
=> this.play() },
260 attrs: { "aria-label": 'Play' },
266 [h('i', { 'class': { "material-icons": true } }, "fast_forward")]),
270 elementArray
.push(gameDiv
);
271 if (!!this.vr
.reserve
)
273 const shiftIdx
= (this.mycolor
=="w" ? 0 : 1);
274 let myReservePiecesArray
= [];
275 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
277 myReservePiecesArray
.push(h('div',
279 'class': {'board':true, ['board'+sizeY
]:true},
280 attrs: { id: this.getSquareId({x:sizeX
+shiftIdx
,y:i
}) }
285 'class': {"piece":true},
287 "src": "/images/pieces/" +
288 this.vr
.getReservePpath(this.mycolor
,i
) + ".svg",
292 {style: { "padding-left":"40%"} },
293 [ this.vr
.reserve
[this.mycolor
][VariantRules
.RESERVE_PIECES
[i
]] ]
297 let oppReservePiecesArray
= [];
298 const oppCol
= this.vr
.getOppCol(this.mycolor
);
299 for (let i
=0; i
<VariantRules
.RESERVE_PIECES
.length
; i
++)
301 oppReservePiecesArray
.push(h('div',
303 'class': {'board':true, ['board'+sizeY
]:true},
304 attrs: { id: this.getSquareId({x:sizeX
+(1-shiftIdx
),y:i
}) }
309 'class': {"piece":true},
311 "src": "/images/pieces/" +
312 this.vr
.getReservePpath(oppCol
,i
) + ".svg",
316 {style: { "padding-left":"40%"} },
317 [ this.vr
.reserve
[oppCol
][VariantRules
.RESERVE_PIECES
[i
]] ]
321 let reserves
= h('div',
323 'class':{'game':true},
324 style: {"margin-bottom": "20px"},
329 'class': { 'row': true },
330 style: {"margin-bottom": "15px"},
335 { 'class': { 'row': true }},
336 oppReservePiecesArray
340 elementArray
.push(reserves
);
342 const eogMessage
= this.getEndgameMessage(this.score
);
346 attrs: { "id": "modal-eog", type: "checkbox" },
347 "class": { "modal": true },
351 attrs: { "role": "dialog", "aria-labelledby": "dialog-title" },
356 "class": { "card": true, "smallpad": true },
361 attrs: { "for": "modal-eog" },
362 "class": { "modal-close": true },
367 "class": { "section": true },
368 domProps: { innerHTML: eogMessage
},
376 elementArray
= elementArray
.concat(modalEog
);
378 const modalNewgame
= [
381 attrs: { "id": "modal-newgame", type: "checkbox" },
382 "class": { "modal": true },
386 attrs: { "role": "dialog", "aria-labelledby": "dialog-title" },
391 "class": { "card": true, "smallpad": true },
396 attrs: { "id": "close-newgame", "for": "modal-newgame" },
397 "class": { "modal-close": true },
402 "class": { "section": true },
403 domProps: { innerHTML: "New game" },
408 "class": { "section": true },
409 domProps: { innerHTML: "Waiting for opponent..." },
417 elementArray
= elementArray
.concat(modalNewgame
);
418 const actions
= h('div',
420 attrs: { "id": "actions" },
421 'class': { 'text-center': true },
425 elementArray
.push(actions
);
426 if (this.score
!= "*")
430 { attrs: { id: "pgn-div" } },
442 attrs: { id: "pgn-game" },
443 on: { click: this.download
},
444 domProps: { innerHTML: this.pgnTxt
}
451 else if (this.mode
!= "idle")
453 // Show current FEN (at least for debug)
456 { attrs: { id: "fen-div" } },
460 attrs: { id: "fen-string" },
461 domProps: { innerHTML: this.vr
.getBaseFen() }
474 "col-md-offset-2":true,
476 "col-lg-offset-3":true,
478 // NOTE: click = mousedown + mouseup
480 mousedown: this.mousedown
,
481 mousemove: this.mousemove
,
482 mouseup: this.mouseup
,
483 touchstart: this.mousedown
,
484 touchmove: this.mousemove
,
485 touchend: this.mouseup
,
491 created: function() {
492 const url
= socketUrl
;
493 const continuation
= (localStorage
.getItem("variant") === variant
);
494 this.myid
= continuation
495 ? localStorage
.getItem("myid")
496 // random enough (TODO: function)
497 : (Date
.now().toString(36) + Math
.random().toString(36).substr(2, 7)).toUpperCase();
500 // HACK: play a small silent sound to allow "new game" sound later if tab not focused
501 new Audio("/sounds/silent.mp3").play().then(() => {}).catch(err
=> {});
503 this.conn
= new WebSocket(url
+ "/?sid=" + this.myid
+ "&page=" + variant
);
504 const socketOpenListener
= () => {
507 const fen
= localStorage
.getItem("fen");
508 const mycolor
= localStorage
.getItem("mycolor");
509 const oppid
= localStorage
.getItem("oppid");
510 const moves
= JSON
.parse(localStorage
.getItem("moves"));
511 this.newGame("human", fen
, mycolor
, oppid
, moves
, true);
512 // Send ping to server (answer pong if opponent is connected)
513 this.conn
.send(JSON
.stringify({code:"ping",oppid:this.oppid
}));
515 else if (localStorage
.getItem("newgame") === variant
)
517 // New game request has been cancelled on disconnect
519 this.newGame("human", undefined, undefined, undefined, undefined, "reconnect");
522 const socketMessageListener
= msg
=> {
523 const data
= JSON
.parse(msg
.data
);
526 case "newgame": //opponent found
527 this.newGame("human", data
.fen
, data
.color
, data
.oppid
); //oppid: opponent socket ID
529 case "newmove": //..he played!
530 this.play(data
.move, "animate");
532 case "pong": //received if we sent a ping (game still alive on our side)
533 this.oppConnected
= true;
534 const L
= this.vr
.moves
.length
;
535 // Send our "last state" informations to opponent
536 this.conn
.send(JSON
.stringify({
539 lastMove:L
>0?this.vr
.moves
[L
-1]:undefined,
543 case "lastate": //got opponent infos about last move (we might have resigned)
544 if (this.mode
!="human" || this.oppid
!=data
.oppid
)
547 this.conn
.send(JSON
.stringify({
554 else if (data
.movesCount
< 0)
557 this.endGame(this.mycolor
=="w"?"1-0":"0-1");
559 else if (data
.movesCount
< this.vr
.moves
.length
)
561 // We must tell last move to opponent
562 const L
= this.vr
.moves
.length
;
563 this.conn
.send(JSON
.stringify({
566 lastMove:this.vr
.moves
[L
-1],
570 else if (data
.movesCount
> this.vr
.moves
.length
) //just got last move from him
571 this.play(data
.lastMove
, "animate");
573 case "resign": //..you won!
574 this.endGame(this.mycolor
=="w"?"1-0":"0-1");
576 // TODO: also use (dis)connect info to count online players?
579 if (this.mode
== "human" && this.oppid
== data
.id
)
580 this.oppConnected
= (data
.code
== "connect");
584 const socketCloseListener
= () => {
585 this.conn
= new WebSocket(url
+ "/?sid=" + this.myid
+ "&page=" + variant
);
586 this.conn
.addEventListener('open', socketOpenListener
);
587 this.conn
.addEventListener('message', socketMessageListener
);
588 this.conn
.addEventListener('close', socketCloseListener
);
590 this.conn
.onopen
= socketOpenListener
;
591 this.conn
.onmessage
= socketMessageListener
;
592 this.conn
.onclose
= socketCloseListener
;
593 // Listen to keyboard left/right to navigate in game
594 document
.onkeydown
= event
=> {
595 if (this.mode
== "idle" && this.vr
.moves
.length
> 0
596 && [37,39].includes(event
.keyCode
))
598 event
.preventDefault();
599 if (event
.keyCode
== 37) //Back
607 download: function() {
608 let content
= document
.getElementById("pgn-game").innerHTML
;
609 content
= content
.replace(/<br>/g, "\n");
610 // Prepare and trigger download link
611 let downloadAnchor
= document
.getElementById("download");
612 downloadAnchor
.setAttribute("download", "game.pgn");
613 downloadAnchor
.href
= "data:text/plain;charset=utf-8," + encodeURIComponent(content
);
614 downloadAnchor
.click();
616 endGame: function(score
) {
618 let modalBox
= document
.getElementById("modal-eog");
619 modalBox
.checked
= true;
620 // Variants may have special PGN structure (so next function isn't defined here)
621 this.pgnTxt
= this.vr
.getPGN(this.mycolor
, this.score
, this.fenStart
, this.mode
);
622 setTimeout(() => { modalBox
.checked
= false; }, 2000);
623 if (this.mode
== "human")
626 this.cursor
= this.vr
.moves
.length
; //to navigate in finished game
629 getEndgameMessage: function(score
) {
630 let eogMessage
= "Unfinished";
634 eogMessage
= "White win";
637 eogMessage
= "Black win";
645 toggleExpertMode: function() {
646 this.expert
= !this.expert
;
647 document
.cookie
= "expert=" + (this.expert
? "1" : "0");
650 if (this.mode
== "human" && this.oppConnected
)
653 this.conn
.send(JSON
.stringify({code: "resign", oppid: this.oppid
}));
654 } catch (INVALID_STATE_ERR
) {
655 return; //socket is not ready (and not yet reconnected)
658 this.endGame(this.mycolor
=="w"?"0-1":"1-0");
660 setStorage: function() {
661 localStorage
.setItem("myid", this.myid
);
662 localStorage
.setItem("variant", variant
);
663 localStorage
.setItem("mycolor", this.mycolor
);
664 localStorage
.setItem("oppid", this.oppid
);
665 localStorage
.setItem("fenStart", this.fenStart
);
666 localStorage
.setItem("moves", JSON
.stringify(this.vr
.moves
));
667 localStorage
.setItem("fen", this.vr
.getFen());
669 updateStorage: function() {
670 localStorage
.setItem("moves", JSON
.stringify(this.vr
.moves
));
671 localStorage
.setItem("fen", this.vr
.getFen());
673 clearStorage: function() {
674 delete localStorage
["variant"];
675 delete localStorage
["myid"];
676 delete localStorage
["mycolor"];
677 delete localStorage
["oppid"];
678 delete localStorage
["fenStart"];
679 delete localStorage
["fen"];
680 delete localStorage
["moves"];
682 clickGameSeek: function() {
683 if (this.mode
== "human")
684 return; //no newgame while playing
687 delete localStorage
["newgame"]; //cancel game seek
691 this.newGame("human");
693 clickComputerGame: function() {
694 if (this.mode
== "human")
695 return; //no newgame while playing
696 this.newGame("computer");
698 newGame: function(mode
, fenInit
, color
, oppId
, moves
, continuation
) {
699 const fen
= fenInit
|| VariantRules
.GenRandInitFen();
700 console
.log(fen
); //DEBUG
702 if (mode
=="human" && !oppId
)
704 const storageVariant
= localStorage
.getItem("variant");
705 if (!!storageVariant
&& storageVariant
!== variant
)
707 alert("Finish your " + storageVariant
+ " game first!");
710 // Send game request and wait..
711 localStorage
["newgame"] = variant
;
713 this.clearStorage(); //in case of
715 this.conn
.send(JSON
.stringify({code:"newgame", fen:fen
}));
716 } catch (INVALID_STATE_ERR
) {
717 return; //nothing achieved
719 if (continuation
!== "reconnect") //TODO: bad HACK...
721 let modalBox
= document
.getElementById("modal-newgame");
722 modalBox
.checked
= true;
723 setTimeout(() => { modalBox
.checked
= false; }, 2000);
727 // random enough (TODO: function)
728 this.gameId
= (Date
.now().toString(36) + Math
.random().toString(36).substr(2, 7)).toUpperCase();
729 this.vr
= new VariantRules(fen
, moves
|| []);
730 this.pgnTxt
= ""; //redundant with this.score = "*", but cleaner
732 this.incheck
= []; //in case of
733 this.fenStart
= continuation
734 ? localStorage
.getItem("fenStart")
735 : fen
.split(" ")[0]; //Only the position matters
741 // Not playing sound on game continuation:
742 new Audio("/sounds/newgame.mp3").play().then(() => {}).catch(err
=> {});
743 document
.getElementById("modal-newgame").checked
= false;
746 this.oppConnected
= true;
747 this.mycolor
= color
;
749 if (!!moves
&& moves
.length
> 0) //imply continuation
751 const lastMove
= moves
[moves
.length
-1];
752 this.vr
.undo(lastMove
);
753 this.incheck
= this.vr
.getCheckSquares(lastMove
);
754 this.vr
.play(lastMove
, "ingame");
756 delete localStorage
["newgame"];
757 this.setStorage(); //in case of interruptions
759 else //against computer
761 this.mycolor
= Math
.random() < 0.5 ? 'w' : 'b';
762 if (this.mycolor
== 'b')
763 setTimeout(this.playComputerMove
, 500);
766 playComputerMove: function() {
767 const timeStart
= Date
.now();
768 const nbMoves
= this.vr
.moves
.length
; //using played moves to know if search finished
769 const gameId
= this.gameId
; //to know if game was reset before timer end
772 if (gameId
!= this.gameId
)
773 return; //game stopped
774 const L
= this.vr
.moves
.length
;
775 if (nbMoves
== L
|| !this.vr
.moves
[L
-1].notation
) //move search didn't finish
776 this.vr
.shouldReturn
= true;
778 const compMove
= this.vr
.getComputerMove();
779 // (first move) HACK: avoid selecting elements before they appear on page:
780 const delay
= Math
.max(500-(Date
.now()-timeStart
), 0);
781 setTimeout(() => this.play(compMove
, "animate"), delay
);
783 // Get the identifier of a HTML table cell from its numeric coordinates o.x,o.y.
784 getSquareId: function(o
) {
785 // NOTE: a separator is required to allow any size of board
786 return "sq-" + o
.x
+ "-" + o
.y
;
789 getSquareFromId: function(id
) {
790 let idParts
= id
.split('-');
791 return [parseInt(idParts
[1]), parseInt(idParts
[2])];
793 mousedown: function(e
) {
794 e
= e
|| window
.event
;
797 while (!ingame
&& elem
!== null)
799 if (elem
.classList
.contains("game"))
804 elem
= elem
.parentElement
;
806 if (!ingame
) //let default behavior (click on button...)
808 e
.preventDefault(); //disable native drag & drop
809 if (!this.selectedPiece
&& e
.target
.classList
.contains("piece"))
811 // Next few lines to center the piece on mouse cursor
812 let rect
= e
.target
.parentNode
.getBoundingClientRect();
814 x: rect
.x
+ rect
.width
/2,
815 y: rect
.y
+ rect
.width
/2,
816 id: e
.target
.parentNode
.id
818 this.selectedPiece
= e
.target
.cloneNode();
819 this.selectedPiece
.style
.position
= "absolute";
820 this.selectedPiece
.style
.top
= 0;
821 this.selectedPiece
.style
.display
= "inline-block";
822 this.selectedPiece
.style
.zIndex
= 3000;
823 let startSquare
= this.getSquareFromId(e
.target
.parentNode
.id
);
824 this.possibleMoves
= this.mode
!="idle" && this.vr
.canIplay(this.mycolor
,startSquare
)
825 ? this.vr
.getPossibleMovesFrom(startSquare
)
827 // Next line add moving piece just after current image (required for Crazyhouse reserve)
828 e
.target
.parentNode
.insertBefore(this.selectedPiece
, e
.target
.nextSibling
);
831 mousemove: function(e
) {
832 if (!this.selectedPiece
)
834 e
= e
|| window
.event
;
835 // If there is an active element, move it around
836 if (!!this.selectedPiece
)
838 const [offsetX
,offsetY
] = !!e
.clientX
839 ? [e
.clientX
,e
.clientY
] //desktop browser
840 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
]; //smartphone
841 this.selectedPiece
.style
.left
= (offsetX
-this.start
.x
) + "px";
842 this.selectedPiece
.style
.top
= (offsetY
-this.start
.y
) + "px";
845 mouseup: function(e
) {
846 if (!this.selectedPiece
)
848 e
= e
|| window
.event
;
849 // Read drop target (or parentElement, parentNode... if type == "img")
850 this.selectedPiece
.style
.zIndex
= -3000; //HACK to find square from final coordinates
851 const [offsetX
,offsetY
] = !!e
.clientX
852 ? [e
.clientX
,e
.clientY
]
853 : [e
.changedTouches
[0].pageX
, e
.changedTouches
[0].pageY
];
854 let landing
= document
.elementFromPoint(offsetX
, offsetY
);
855 this.selectedPiece
.style
.zIndex
= 3000;
856 while (landing
.tagName
== "IMG") //classList.contains(piece) fails because of mark/highlight
857 landing
= landing
.parentNode
;
858 if (this.start
.id
== landing
.id
) //a click: selectedPiece and possibleMoves already filled
860 // OK: process move attempt
861 let endSquare
= this.getSquareFromId(landing
.id
);
862 let moves
= this.findMatchingMoves(endSquare
);
863 this.possibleMoves
= [];
864 if (moves
.length
> 1)
865 this.choices
= moves
;
866 else if (moves
.length
==1)
868 // Else: impossible move
869 this.selectedPiece
.parentNode
.removeChild(this.selectedPiece
);
870 delete this.selectedPiece
;
871 this.selectedPiece
= null;
873 findMatchingMoves: function(endSquare
) {
874 // Run through moves list and return the matching set (if promotions...)
876 this.possibleMoves
.forEach(function(m
) {
877 if (endSquare
[0] == m
.end
.x
&& endSquare
[1] == m
.end
.y
)
882 animateMove: function(move) {
883 let startSquare
= document
.getElementById(this.getSquareId(move.start
));
884 let endSquare
= document
.getElementById(this.getSquareId(move.end
));
885 let rectStart
= startSquare
.getBoundingClientRect();
886 let rectEnd
= endSquare
.getBoundingClientRect();
887 let translation
= {x:rectEnd
.x
-rectStart
.x
, y:rectEnd
.y
-rectStart
.y
};
889 document
.querySelector("#" + this.getSquareId(move.start
) + " > img.piece");
890 // HACK for animation (with positive translate, image slides "under background"...)
891 // Possible improvement: just alter squares on the piece's way...
892 squares
= document
.getElementsByClassName("board");
893 for (let i
=0; i
<squares
.length
; i
++)
895 let square
= squares
.item(i
);
896 if (square
.id
!= this.getSquareId(move.start
))
897 square
.style
.zIndex
= "-1";
899 movingPiece
.style
.transform
= "translate(" + translation
.x
+ "px," + translation
.y
+ "px)";
900 movingPiece
.style
.transitionDuration
= "0.2s";
901 movingPiece
.style
.zIndex
= "3000";
903 for (let i
=0; i
<squares
.length
; i
++)
904 squares
.item(i
).style
.zIndex
= "auto";
905 movingPiece
.style
= {}; //required e.g. for 0-0 with KR swap
909 play: function(move, programmatic
) {
912 // Navigate after game is over
913 if (this.cursor
>= this.vr
.moves
.length
)
914 return; //already at the end
915 move = this.vr
.moves
[this.cursor
++];
917 if (!!programmatic
) //computer or human opponent
919 this.animateMove(move);
922 this.incheck
= this.vr
.getCheckSquares(move); //is opponent in check?
923 // Not programmatic, or animation is over
924 if (this.mode
== "human" && this.vr
.turn
== this.mycolor
)
925 this.conn
.send(JSON
.stringify({code:"newmove", move:move, oppid:this.oppid
}));
926 new Audio("/sounds/chessmove1.mp3").play().then(() => {}).catch(err
=> {});
927 if (this.mode
!= "idle")
928 this.vr
.play(move, "ingame");
930 VariantRules
.PlayOnBoard(this.vr
.board
, move);
931 if (this.mode
== "human")
932 this.updateStorage(); //after our moves and opponent moves
933 if (this.mode
!= "idle")
935 const eog
= this.vr
.checkGameOver();
939 if (this.mode
== "computer" && this.vr
.turn
!= this.mycolor
)
940 setTimeout(this.playComputerMove
, 500);
943 // Navigate after game is over
944 if (this.cursor
== 0)
945 return; //already at the beginning
946 const move = this.vr
.moves
[--this.cursor
];
947 VariantRules
.UndoOnBoard(this.vr
.board
, move);
948 this.$forceUpdate(); //TODO: ?!