Commit | Line | Data |
---|---|---|
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 }} | |
93d1d7a7 | 9 | // TODO: or "BoardHex" if this.game.vname in "Hexagonal..." |
d4036efe | 10 | Board(:vr="vr" :last-move="lastMove" :analyze="analyze" |
834c202a BA |
11 | :user-color="game.mycolor" :orientation="orientation" |
12 | :vname="game.vname" @play-move="play") | |
a6088c90 | 13 | .button-group |
37cdcbf3 BA |
14 | button(@click="() => play()") Play |
15 | button(@click="() => undo()") Undo | |
a6088c90 BA |
16 | button(@click="flip") Flip |
17 | button(@click="gotoBegin") GotoBegin | |
18 | button(@click="gotoEnd") GotoEnd | |
19 | #fenDiv.section-content(v-if="showFen && !!vr") | |
20 | p#fenString.text-center {{ vr.getFen() }} | |
21 | #pgnDiv.section-content | |
22 | a#download(href="#") | |
23 | .button-group | |
24 | button#downloadBtn(@click="download") {{ st.tr["Download PGN"] }} | |
d4036efe BA |
25 | |
26 | // TODO: Import game button copy game locally in IndexedDB | |
27 | //button Import game | |
28 | ||
29 | ||
30 | // TODO: do not use localStorage for current game, but directly indexedDB | |
31 | // update function is similar | |
32 | // ==> retrieval functions must filter on score, and potential "imported" tag | |
33 | // ==> this should allow several simultaneous games | |
34 | ||
35 | ||
a6088c90 BA |
36 | //MoveList(v-if="showMoves" |
37 | :moves="moves" :cursor="cursor" @goto-move="gotoMove") | |
38 | </template> | |
39 | ||
40 | <script> | |
41 | import Board from "@/components/Board.vue"; | |
42 | //import MoveList from "@/components/MoveList.vue"; | |
43 | import { store } from "@/store"; | |
44 | import { getSquareId } from "@/utils/squareId"; | |
d4036efe | 45 | import { getDate } from "@/utils/datetime"; |
a6088c90 BA |
46 | |
47 | export default { | |
48 | name: 'my-base-game', | |
49 | components: { | |
50 | Board, | |
51 | //MoveList, | |
52 | }, | |
bc8734ba | 53 | // "vr": VariantRules object, describing the game state + rules |
834c202a | 54 | props: ["vr","game"], |
a6088c90 BA |
55 | data: function() { |
56 | return { | |
57 | st: store.state, | |
b7c32f1a | 58 | // NOTE: all following variables must be reset at the beginning of a game |
a6088c90 BA |
59 | endgameMessage: "", |
60 | orientation: "w", | |
61 | score: "*", //'*' means 'unfinished' | |
b7c32f1a | 62 | moves: [], |
a6088c90 BA |
63 | cursor: -1, //index of the move just played |
64 | lastMove: null, | |
65 | }; | |
66 | }, | |
37cdcbf3 | 67 | watch: { |
834c202a BA |
68 | // game initial FEN changes when a new game starts |
69 | "game.fenStart": function() { | |
4b0384fa | 70 | this.re_setVariables(); |
37cdcbf3 BA |
71 | }, |
72 | }, | |
a6088c90 BA |
73 | computed: { |
74 | showMoves: function() { | |
75 | return true; | |
76 | //return window.innerWidth >= 768; | |
77 | }, | |
78 | showFen: function() { | |
834c202a BA |
79 | return this.game.vname != "Dark" || this.score != "*"; |
80 | }, | |
81 | analyze: function() { | |
d4036efe | 82 | return this.game.mode == "analyze" || this.game.score != "*"; |
a6088c90 BA |
83 | }, |
84 | }, | |
4b0384fa BA |
85 | created: function() { |
86 | if (!!this.game.fenStart) | |
87 | this.re_setVariables(); | |
88 | }, | |
a6088c90 | 89 | methods: { |
4b0384fa BA |
90 | re_setVariables: function() { |
91 | this.endgameMessage = ""; | |
92 | this.orientation = this.game.mycolor || "w"; //default orientation for observed games | |
93 | this.score = this.game.score || "*"; //mutable (if initially "*") | |
94 | this.moves = JSON.parse(JSON.stringify(this.game.moves || [])); | |
d4036efe BA |
95 | // Post-processing: decorate each move with color + current FEN: |
96 | // (to be able to jump to any position quickly) | |
97 | this.moves.forEach(move => { | |
98 | // NOTE: this is doing manually what play() function below achieve, | |
99 | // but in a lighter "fast-forward" way | |
100 | move.color = this.vr.turn; | |
101 | move.notation = this.vr.getNotation(move); | |
102 | this.vr.play(move); | |
103 | move.fen = this.vr.getFen(); | |
104 | }); | |
4b0384fa BA |
105 | const L = this.moves.length; |
106 | this.cursor = L-1; | |
107 | this.lastMove = (L > 0 ? this.moves[L-1] : null); | |
108 | }, | |
a6088c90 BA |
109 | download: function() { |
110 | const content = this.getPgn(); | |
111 | // Prepare and trigger download link | |
112 | let downloadAnchor = document.getElementById("download"); | |
113 | downloadAnchor.setAttribute("download", "game.pgn"); | |
114 | downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content); | |
115 | downloadAnchor.click(); | |
116 | }, | |
117 | getPgn: function() { | |
118 | let pgn = ""; | |
119 | pgn += '[Site "vchess.club"]\n'; | |
834c202a | 120 | pgn += '[Variant "' + this.game.vname + '"]\n'; |
a6088c90 | 121 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; |
d4036efe BA |
122 | pgn += '[White "' + this.game.players[0].name + '"]\n'; |
123 | pgn += '[Black "' + this.game.players[1].name + '"]\n'; | |
834c202a | 124 | pgn += '[Fen "' + this.game.fenStart + '"]\n'; |
a6088c90 BA |
125 | pgn += '[Result "' + this.score + '"]\n\n'; |
126 | let counter = 1; | |
127 | let i = 0; | |
128 | while (i < this.moves.length) | |
129 | { | |
130 | pgn += (counter++) + "."; | |
131 | for (let color of ["w","b"]) | |
132 | { | |
133 | let move = ""; | |
134 | while (i < this.moves.length && this.moves[i].color == color) | |
d4036efe | 135 | move += this.moves[i++].notation + ","; |
a6088c90 | 136 | move = move.slice(0,-1); //remove last comma |
d4036efe | 137 | pgn += move + (i < this.moves.length ? " " : ""); |
a6088c90 BA |
138 | } |
139 | } | |
140 | return pgn + "\n"; | |
141 | }, | |
b988c726 BA |
142 | getScoreMessage: function(score) { |
143 | let eogMessage = "Undefined"; | |
144 | switch (score) | |
145 | { | |
146 | case "1-0": | |
147 | eogMessage = this.st.tr["White win"]; | |
148 | break; | |
149 | case "0-1": | |
150 | eogMessage = this.st.tr["Black win"]; | |
151 | break; | |
152 | case "1/2": | |
153 | eogMessage = this.st.tr["Draw"]; | |
154 | break; | |
155 | case "?": | |
156 | eogMessage = this.st.tr["Unfinished"]; | |
157 | break; | |
158 | } | |
159 | return eogMessage; | |
160 | }, | |
161 | showEndgameMsg: function(message) { | |
162 | this.endgameMessage = message; | |
4494c17c | 163 | let modalBox = document.getElementById("modalEog"); |
a6088c90 BA |
164 | modalBox.checked = true; |
165 | setTimeout(() => { modalBox.checked = false; }, 2000); | |
166 | }, | |
b988c726 | 167 | endGame: function(score, message) { |
a6088c90 | 168 | this.score = score; |
b988c726 BA |
169 | if (!message) |
170 | message = this.getScoreMessage(score); | |
171 | this.showEndgameMsg(score + " . " + message); | |
b4fb1612 | 172 | this.$emit("gameover", score); |
a6088c90 BA |
173 | }, |
174 | animateMove: function(move) { | |
175 | let startSquare = document.getElementById(getSquareId(move.start)); | |
176 | let endSquare = document.getElementById(getSquareId(move.end)); | |
177 | let rectStart = startSquare.getBoundingClientRect(); | |
178 | let rectEnd = endSquare.getBoundingClientRect(); | |
179 | let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; | |
180 | let movingPiece = | |
181 | document.querySelector("#" + getSquareId(move.start) + " > img.piece"); | |
182 | // HACK for animation (with positive translate, image slides "under background") | |
183 | // Possible improvement: just alter squares on the piece's way... | |
184 | const squares = document.getElementsByClassName("board"); | |
185 | for (let i=0; i<squares.length; i++) | |
186 | { | |
187 | let square = squares.item(i); | |
188 | if (square.id != getSquareId(move.start)) | |
189 | square.style.zIndex = "-1"; | |
190 | } | |
191 | movingPiece.style.transform = "translate(" + translation.x + "px," + | |
192 | translation.y + "px)"; | |
193 | movingPiece.style.transitionDuration = "0.2s"; | |
194 | movingPiece.style.zIndex = "3000"; | |
195 | setTimeout( () => { | |
196 | for (let i=0; i<squares.length; i++) | |
197 | squares.item(i).style.zIndex = "auto"; | |
198 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap | |
199 | this.play(move); | |
200 | }, 250); | |
201 | }, | |
c4f91d3f | 202 | play: function(move, receive, noanimate) { |
9d54ab89 | 203 | const navigate = !move; |
a6088c90 BA |
204 | // Forbid playing outside analyze mode when cursor isn't at moves.length-1 |
205 | // (except if we receive opponent's move, human or computer) | |
c4f91d3f | 206 | if (!navigate && !this.analyze && !receive |
a6088c90 BA |
207 | && this.cursor < this.moves.length-1) |
208 | { | |
209 | return; | |
210 | } | |
211 | if (navigate) | |
212 | { | |
213 | if (this.cursor == this.moves.length-1) | |
214 | return; //no more moves | |
215 | move = this.moves[this.cursor+1]; | |
216 | } | |
c4f91d3f | 217 | if (!!receive && !noanimate) //opponent move, variant != "Dark" |
a6088c90 BA |
218 | { |
219 | if (this.cursor < this.moves.length-1) | |
220 | this.gotoEnd(); //required to play the move | |
221 | return this.animateMove(move); | |
222 | } | |
c4f91d3f BA |
223 | if (!navigate) |
224 | { | |
225 | move.color = this.vr.turn; | |
226 | move.notation = this.vr.getNotation(move); | |
227 | } | |
a6088c90 | 228 | // Not programmatic, or animation is over |
a6088c90 BA |
229 | this.vr.play(move); |
230 | this.cursor++; | |
231 | this.lastMove = move; | |
a6088c90 BA |
232 | if (this.st.settings.sound == 2) |
233 | new Audio("/sounds/move.mp3").play().catch(err => {}); | |
9d54ab89 | 234 | if (!navigate) |
a6088c90 | 235 | { |
9d54ab89 BA |
236 | move.fen = this.vr.getFen(); |
237 | if (this.score == "*" || this.analyze) | |
238 | { | |
239 | // Stack move on movesList at current cursor | |
240 | if (this.cursor == this.moves.length) | |
241 | this.moves.push(move); | |
242 | else | |
243 | this.moves = this.moves.slice(0,this.cursor).concat([move]); | |
244 | } | |
a6088c90 | 245 | } |
93d1d7a7 | 246 | // Is opponent in check? (TODO: generalize, find all check squares) |
a6088c90 BA |
247 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
248 | const score = this.vr.getCurrentScore(); | |
93d1d7a7 | 249 | if (score != "*") //TODO: generalize score for 3 or 4 players |
a6088c90 BA |
250 | { |
251 | if (!this.analyze) | |
252 | this.endGame(score); | |
b988c726 BA |
253 | else |
254 | { | |
255 | // Just show score on screen (allow undo) | |
256 | const message = this.getScoreMessage(score); | |
257 | this.showEndgameMsg(score + " . " + message); | |
258 | } | |
a6088c90 | 259 | } |
4494c17c BA |
260 | if (!this.analyze) |
261 | this.$emit("newmove", move); //post-processing (e.g. computer play) | |
a6088c90 BA |
262 | }, |
263 | undo: function(move) { | |
9d54ab89 | 264 | const navigate = !move; |
a6088c90 BA |
265 | if (navigate) |
266 | { | |
267 | if (this.cursor < 0) | |
268 | return; //no more moves | |
269 | move = this.moves[this.cursor]; | |
270 | } | |
271 | this.vr.undo(move); | |
272 | this.cursor--; | |
273 | this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined); | |
274 | if (this.st.settings.sound == 2) | |
275 | new Audio("/sounds/undo.mp3").play().catch(err => {}); | |
276 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
4494c17c | 277 | if (!navigate) |
a6088c90 BA |
278 | this.moves.pop(); |
279 | }, | |
280 | gotoMove: function(index) { | |
37cdcbf3 | 281 | this.vr.re_init(this.moves[index].fen); |
a6088c90 BA |
282 | this.cursor = index; |
283 | this.lastMove = this.moves[index]; | |
284 | }, | |
285 | gotoBegin: function() { | |
834c202a | 286 | this.vr.re_init(this.game.fenStart); |
a6088c90 BA |
287 | this.cursor = -1; |
288 | this.lastMove = null; | |
289 | }, | |
290 | gotoEnd: function() { | |
291 | this.gotoMove(this.moves.length-1); | |
292 | }, | |
293 | flip: function() { | |
294 | this.orientation = V.GetNextCol(this.orientation); | |
295 | }, | |
296 | }, | |
297 | }; | |
298 | </script> |