Still some issues with moves navigation after computer game ends
[vchess.git] / client / src / components / BaseGame.vue
CommitLineData
a6088c90
BA
1<template lang="pug">
2.row
3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
4 input#modalEog.modal(type="checkbox")
5 div(role="dialog" aria-labelledby="eogMessage")
6 .card.smallpad.small-modal.text-center
7 label.modal-close(for="modalEog")
8 h3#eogMessage.section {{ endgameMessage }}
9 Board(:vr="vr" :last-move="lastMove" :analyze="analyze" :user-color="mycolor"
b7c32f1a 10 :orientation="orientation" :vname="vname" @play-move="play")
a6088c90 11 .button-group
37cdcbf3
BA
12 button(@click="() => play()") Play
13 button(@click="() => undo()") Undo
a6088c90
BA
14 button(@click="flip") Flip
15 button(@click="gotoBegin") GotoBegin
16 button(@click="gotoEnd") GotoEnd
17 #fenDiv.section-content(v-if="showFen && !!vr")
18 p#fenString.text-center {{ vr.getFen() }}
19 #pgnDiv.section-content
20 a#download(href="#")
21 .button-group
22 button#downloadBtn(@click="download") {{ st.tr["Download PGN"] }}
23 button Import game
24 //MoveList(v-if="showMoves"
25 :moves="moves" :cursor="cursor" @goto-move="gotoMove")
26</template>
27
28<script>
29import Board from "@/components/Board.vue";
30//import MoveList from "@/components/MoveList.vue";
31import { store } from "@/store";
32import { getSquareId } from "@/utils/squareId";
33
34export default {
35 name: 'my-base-game',
36 components: {
37 Board,
38 //MoveList,
39 },
bc8734ba 40 // "vr": VariantRules object, describing the game state + rules
b7c32f1a 41 props: ["vname","analyze","vr","fenStart","players","mycolor"],
a6088c90
BA
42 data: function() {
43 return {
44 st: store.state,
b7c32f1a 45 // NOTE: all following variables must be reset at the beginning of a game
a6088c90
BA
46 endgameMessage: "",
47 orientation: "w",
48 score: "*", //'*' means 'unfinished'
b7c32f1a 49 moves: [],
a6088c90
BA
50 cursor: -1, //index of the move just played
51 lastMove: null,
52 };
53 },
37cdcbf3
BA
54 watch: {
55 // fenStart changes when a new game starts
56 fenStart: function() {
57 // Reset all variables
58 this.endgameMessage = "";
59 this.orientation = this.mycolor;
60 this.score = "*";
61 this.moves = [];
62 this.cursor = -1;
63 this.lastMove = null;
64 },
65 },
a6088c90
BA
66 computed: {
67 showMoves: function() {
68 return true;
69 //return window.innerWidth >= 768;
70 },
71 showFen: function() {
b7c32f1a 72 return this.vname != "Dark" || this.score != "*";
a6088c90
BA
73 },
74 },
75 methods: {
76 setEndgameMessage: function(score) {
77 let eogMessage = "Undefined";
78 switch (score)
79 {
80 case "1-0":
81 eogMessage = translations["White win"];
82 break;
83 case "0-1":
84 eogMessage = translations["Black win"];
85 break;
86 case "1/2":
87 eogMessage = translations["Draw"];
88 break;
89 case "?":
90 eogMessage = "Unfinished";
91 break;
92 }
93 this.endgameMessage = eogMessage;
94 },
95 download: function() {
96 const content = this.getPgn();
97 // Prepare and trigger download link
98 let downloadAnchor = document.getElementById("download");
99 downloadAnchor.setAttribute("download", "game.pgn");
100 downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content);
101 downloadAnchor.click();
102 },
103 getPgn: function() {
104 let pgn = "";
105 pgn += '[Site "vchess.club"]\n';
b7c32f1a 106 pgn += '[Variant "' + this.vname + '"]\n';
a6088c90
BA
107 pgn += '[Date "' + getDate(new Date()) + '"]\n';
108 pgn += '[White "' + this.players[0] + '"]\n';
109 pgn += '[Black "' + this.players[1] + '"]\n';
110 pgn += '[Fen "' + this.fenStart + '"]\n';
111 pgn += '[Result "' + this.score + '"]\n\n';
112 let counter = 1;
113 let i = 0;
114 while (i < this.moves.length)
115 {
116 pgn += (counter++) + ".";
117 for (let color of ["w","b"])
118 {
119 let move = "";
120 while (i < this.moves.length && this.moves[i].color == color)
121 move += this.moves[i++].notation[0] + ",";
122 move = move.slice(0,-1); //remove last comma
123 pgn += move + (i < this.moves.length-1 ? " " : "");
124 }
125 }
126 return pgn + "\n";
127 },
128 showScoreMsg: function(score) {
129 this.setEndgameMessage(score);
130 let modalBox = document.getElementById("modal-eog");
131 modalBox.checked = true;
132 setTimeout(() => { modalBox.checked = false; }, 2000);
133 },
134 endGame: function(score) {
135 this.score = score;
136 this.showScoreMsg(score);
137 this.$emit("game-over");
138 },
139 animateMove: function(move) {
140 let startSquare = document.getElementById(getSquareId(move.start));
141 let endSquare = document.getElementById(getSquareId(move.end));
142 let rectStart = startSquare.getBoundingClientRect();
143 let rectEnd = endSquare.getBoundingClientRect();
144 let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y};
145 let movingPiece =
146 document.querySelector("#" + getSquareId(move.start) + " > img.piece");
147 // HACK for animation (with positive translate, image slides "under background")
148 // Possible improvement: just alter squares on the piece's way...
149 const squares = document.getElementsByClassName("board");
150 for (let i=0; i<squares.length; i++)
151 {
152 let square = squares.item(i);
153 if (square.id != getSquareId(move.start))
154 square.style.zIndex = "-1";
155 }
156 movingPiece.style.transform = "translate(" + translation.x + "px," +
157 translation.y + "px)";
158 movingPiece.style.transitionDuration = "0.2s";
159 movingPiece.style.zIndex = "3000";
160 setTimeout( () => {
161 for (let i=0; i<squares.length; i++)
162 squares.item(i).style.zIndex = "auto";
163 movingPiece.style = {}; //required e.g. for 0-0 with KR swap
164 this.play(move);
165 }, 250);
166 },
167 play: function(move, programmatic) {
168 let navigate = !move;
169 // Forbid playing outside analyze mode when cursor isn't at moves.length-1
170 // (except if we receive opponent's move, human or computer)
171 if (!navigate && !this.analyze && !programmatic
172 && this.cursor < this.moves.length-1)
173 {
174 return;
175 }
176 if (navigate)
177 {
178 if (this.cursor == this.moves.length-1)
179 return; //no more moves
180 move = this.moves[this.cursor+1];
181 }
182 if (!!programmatic) //computer or (remote) human opponent
183 {
184 if (this.cursor < this.moves.length-1)
185 this.gotoEnd(); //required to play the move
186 return this.animateMove(move);
187 }
188 // Not programmatic, or animation is over
189 if (!move.notation)
190 move.notation = this.vr.getNotation(move);
191 if (!move.color)
192 move.color = this.vr.turn;
193 this.vr.play(move);
194 this.cursor++;
195 this.lastMove = move;
196 if (!move.fen)
197 move.fen = this.vr.getFen();
198 if (this.st.settings.sound == 2)
199 new Audio("/sounds/move.mp3").play().catch(err => {});
a6088c90
BA
200 if (!navigate && (this.score == "*" || this.analyze))
201 {
202 // Stack move on movesList at current cursor
203 if (this.cursor == this.moves.length)
204 this.moves.push(move);
205 else
206 this.moves = this.moves.slice(0,this.cursor).concat([move]);
207 }
208 // Is opponent in check?
209 this.incheck = this.vr.getCheckSquares(this.vr.turn);
210 const score = this.vr.getCurrentScore();
211 if (score != "*")
212 {
213 if (!this.analyze)
214 this.endGame(score);
215 else //just show score on screen (allow undo)
216 this.showScoreMsg(score);
217 }
1acda11c 218 this.$emit("newmove", move); //post-processing (e.g. computer play)
a6088c90
BA
219 },
220 undo: function(move) {
221 let navigate = !move;
222 if (navigate)
223 {
224 if (this.cursor < 0)
225 return; //no more moves
226 move = this.moves[this.cursor];
227 }
228 this.vr.undo(move);
229 this.cursor--;
230 this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined);
231 if (this.st.settings.sound == 2)
232 new Audio("/sounds/undo.mp3").play().catch(err => {});
233 this.incheck = this.vr.getCheckSquares(this.vr.turn);
1acda11c 234 if (this.analyze) //TODO: can this happen?
a6088c90
BA
235 this.moves.pop();
236 },
237 gotoMove: function(index) {
37cdcbf3 238 this.vr.re_init(this.moves[index].fen);
a6088c90
BA
239 this.cursor = index;
240 this.lastMove = this.moves[index];
241 },
242 gotoBegin: function() {
37cdcbf3 243 this.vr.re_init(this.fenStart);
a6088c90
BA
244 this.cursor = -1;
245 this.lastMove = null;
246 },
247 gotoEnd: function() {
248 this.gotoMove(this.moves.length-1);
249 },
250 flip: function() {
251 this.orientation = V.GetNextCol(this.orientation);
252 },
253 },
254};
255</script>