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