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