Commit | Line | Data |
---|---|---|
fd373b27 | 1 | // Game logic on a variant page: 3 modes, analyze, computer or human |
a3ab5fdb BA |
2 | // TODO: envoyer juste "light move", sans FEN ni notation ...etc |
3 | // TODO: if I'm an observer and player(s) disconnect/reconnect, how to find me ? | |
3ca7a846 BA |
4 | // onClick :: ask full game to remote player, and register as an observer in game |
5 | // (use gameId to communicate) | |
6 | // on landing on game :: if gameId not found locally, check remotely | |
7 | // ==> il manque un param dans game : "remoteId" | |
1d184b4c | 8 | Vue.component('my-game', { |
fd373b27 | 9 | // gameId: to find the game in storage (assumption: it exists) |
81bc1102 | 10 | // fen: to start from a FEN without identifiers (analyze mode) |
a3ab5fdb BA |
11 | // subMode: "auto" (game comp vs comp) or "corr" (correspondance game), |
12 | // or "examine" (after human game: TODO) | |
97da8720 BA |
13 | props: ["conn","gameRef","fen","mode","subMode", |
14 | "allowChat","allowMovelist","settings"], | |
1d184b4c BA |
15 | data: function() { |
16 | return { | |
643479f8 BA |
17 | // Web worker to play computer moves without freezing interface: |
18 | compWorker: new Worker('/javascripts/playCompMove.js'), | |
19 | timeStart: undefined, //time when computer starts thinking | |
fd373b27 | 20 | vr: null, //VariantRules object, describing the game state + rules |
d44df0b0 BA |
21 | endgameMessage: "", |
22 | orientation: "w", | |
d337a94c | 23 | lockCompThink: false, //used to avoid some ghost moves |
a3ab5fdb BA |
24 | myname: user.name, //may be anonymous (thus no name) |
25 | opponents: {}, //filled later (potentially 2 or 3 opponents) | |
26 | drawOfferSent: false, //did I just ask for draw? | |
27 | people: {}, //observers | |
59d58d7d | 28 | score: "*", //'*' means 'unfinished' |
582df349 | 29 | // userColor: given by gameId, or fen in problems mode (if no game Id)... |
59d58d7d | 30 | mycolor: "w", |
7d9e99bc | 31 | fenStart: "", |
d44df0b0 | 32 | moves: [], //TODO: initialize if gameId is defined... |
d337a94c | 33 | cursor: -1, //index of the move just played |
59d58d7d | 34 | lastMove: null, |
1d184b4c BA |
35 | }; |
36 | }, | |
fd08ab2c | 37 | watch: { |
60d9063f | 38 | fen: function() { |
d337a94c BA |
39 | // (Security) No effect if a computer move is in progress: |
40 | if (this.mode == "computer" && this.lockCompThink) | |
41 | return this.$emit("computer-think"); | |
60d9063f | 42 | this.newGameFromFen(); |
fd08ab2c | 43 | }, |
97da8720 | 44 | gameRef: function() { |
582df349 BA |
45 | this.loadGame(); |
46 | }, | |
fd08ab2c | 47 | }, |
81da2786 | 48 | computed: { |
81da2786 | 49 | showChat: function() { |
fd373b27 | 50 | return this.allowChat && this.mode=='human' && this.score != '*'; |
81da2786 BA |
51 | }, |
52 | showMoves: function() { | |
7d9e99bc | 53 | return true; |
fd373b27 BA |
54 | return this.allowMovelist && window.innerWidth >= 768; |
55 | }, | |
56 | showFen: function() { | |
57 | return variant.name != "Dark" || this.score != "*"; | |
c794dbb8 BA |
58 | }, |
59 | }, | |
81da2786 BA |
60 | // Modal end of game, and then sub-components |
61 | template: ` | |
62 | <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2"> | |
63 | <input id="modal-eog" type="checkbox" class="modal"/> | |
64 | <div role="dialog" aria-labelledby="eogMessage"> | |
65 | <div class="card smallpad small-modal text-center"> | |
fd373b27 BA |
66 | <label for="modal-eog" class="modal-close"> |
67 | </label> | |
68 | <h3 id="eogMessage" class="section"> | |
69 | {{ endgameMessage }} | |
70 | </h3> | |
936dc463 BA |
71 | </div> |
72 | </div> | |
a3ab5fdb BA |
73 | <my-chat v-if="showChat" :conn="conn" :myname="myname" |
74 | :opponents="opponents" :people="people"> | |
fd373b27 | 75 | </my-chat> |
582df349 BA |
76 | <my-board v-bind:vr="vr" :last-move="lastMove" :mode="mode" |
77 | :orientation="orientation" :user-color="mycolor" :settings="settings" | |
78 | @play-move="play"> | |
fd373b27 | 79 | </my-board> |
7d9e99bc BA |
80 | <div class="button-group"> |
81 | <button @click="() => play()">Play</button> | |
82 | <button @click="() => undo()">Undo</button> | |
83 | <button @click="flip">Flip</button> | |
84 | <button @click="gotoBegin">GotoBegin</button> | |
85 | <button @click="gotoEnd">GotoEnd</button> | |
86 | </div> | |
a3ab5fdb BA |
87 | <div v-if="mode=='human'" class="button-group"> |
88 | <button @click="offerDraw">Draw</button> | |
89 | <button @click="abortGame">Abort</button> | |
90 | <button @click="resign">Resign</button> | |
91 | </div> | |
d337a94c BA |
92 | <div v-if="mode=='human' && subMode=='corr'"> |
93 | <textarea v-show="score=='*' && vr.turn==mycolor" v-model="corrMsg"> | |
94 | </textarea> | |
95 | <div v-show="cursor>=0"> | |
96 | {{ moves[cursor].message }} | |
97 | </div> | |
98 | </div> | |
d44df0b0 | 99 | <div v-if="showFen && !!vr" id="fen-div" class="section-content"> |
fd373b27 BA |
100 | <p id="fen-string" class="text-center"> |
101 | {{ vr.getFen() }} | |
102 | </p> | |
103 | </div> | |
81da2786 | 104 | <div id="pgn-div" class="section-content"> |
4608eed9 | 105 | <a id="download" href="#"> |
fd373b27 | 106 | </a> |
a6403027 BA |
107 | <div class="button-group"> |
108 | <button id="downloadBtn" @click="download"> | |
109 | {{ translate("Download PGN") }} | |
110 | </button> | |
111 | <button>Import game</button> | |
112 | </div> | |
fd373b27 | 113 | </div> |
7d9e99bc | 114 | <my-move-list v-if="showMoves" :moves="moves" :cursor="cursor" @goto-move="gotoMove"> |
fd373b27 | 115 | </my-move-list> |
81da2786 BA |
116 | </div> |
117 | `, | |
1d184b4c | 118 | created: function() { |
97da8720 | 119 | if (!!this.gameRef) |
d44df0b0 BA |
120 | this.loadGame(); |
121 | else if (!!this.fen) | |
7d9e99bc | 122 | { |
ab4f4bf2 | 123 | this.vr = new V(this.fen); |
7d9e99bc BA |
124 | this.fenStart = this.fen; |
125 | } | |
214dfe16 BA |
126 | // TODO: if I'm one of the players in game, then: |
127 | // Send ping to server (answer pong if opponent is connected) | |
97da8720 | 128 | if (true && !!this.conn && !!this.gameRef) |
214dfe16 | 129 | { |
60d9063f BA |
130 | this.conn.onopen = () => { |
131 | this.conn.send(JSON.stringify({ | |
97da8720 | 132 | code:"ping",oppid:this.oppid,gameId:this.gameRef.id})); |
60d9063f | 133 | }; |
214dfe16 | 134 | } |
a3ab5fdb BA |
135 | // TODO: also handle "draw accepted" (use opponents array?) |
136 | // --> must give this info also when sending lastState... | |
137 | // and, if all players agree then OK draw (end game ...etc) | |
d35f20e4 | 138 | const socketMessageListener = msg => { |
1d184b4c | 139 | const data = JSON.parse(msg.data); |
edcd679a | 140 | let L = undefined; |
1d184b4c BA |
141 | switch (data.code) |
142 | { | |
1d184b4c | 143 | case "newmove": //..he played! |
a3ab5fdb | 144 | this.play(data.move, variant.name!="Dark" ? "animate" : null); |
1d184b4c | 145 | break; |
f3802fcd | 146 | case "pong": //received if we sent a ping (game still alive on our side) |
97da8720 | 147 | if (this.gameRef.id != data.gameId) |
56a683cd | 148 | break; //games IDs don't match: definitely over... |
1d184b4c | 149 | this.oppConnected = true; |
a3ab5fdb | 150 | // Send our "last state" informations to opponent(s) |
edcd679a | 151 | L = this.vr.moves.length; |
a3ab5fdb BA |
152 | Object.keys(this.opponents).forEach(oid => { |
153 | this.conn.send(JSON.stringify({ | |
154 | code: "lastate", | |
155 | oppid: oid, | |
97da8720 | 156 | gameId: this.gameRef.id, |
a3ab5fdb BA |
157 | lastMove: (L>0?this.vr.moves[L-1]:undefined), |
158 | movesCount: L, | |
159 | })); | |
160 | }); | |
1d184b4c | 161 | break; |
a3ab5fdb | 162 | // TODO: refactor this, because at 3 or 4 players we may have missed 2 or 3 moves (not just one) |
56a683cd | 163 | case "lastate": //got opponent infos about last move |
edcd679a | 164 | L = this.vr.moves.length; |
97da8720 | 165 | if (this.gameRef.id != data.gameId) |
56a683cd BA |
166 | break; //games IDs don't match: nothing we can do... |
167 | // OK, opponent still in game (which might be over) | |
edcd679a | 168 | if (this.score != "*") |
a29d9d6b | 169 | { |
56a683cd | 170 | // We finished the game (any result possible) |
a29d9d6b | 171 | this.conn.send(JSON.stringify({ |
56a683cd BA |
172 | code: "lastate", |
173 | oppid: data.oppid, | |
97da8720 | 174 | gameId: this.gameRef.id, |
56a683cd | 175 | score: this.score, |
a29d9d6b BA |
176 | })); |
177 | } | |
56a683cd BA |
178 | else if (!!data.score) //opponent finished the game |
179 | this.endGame(data.score); | |
180 | else if (data.movesCount < L) | |
a29d9d6b BA |
181 | { |
182 | // We must tell last move to opponent | |
a29d9d6b | 183 | this.conn.send(JSON.stringify({ |
56a683cd | 184 | code: "lastate", |
a3ab5fdb | 185 | oppid: this.opponent.id, |
97da8720 | 186 | gameId: this.gameRef.id, |
56a683cd BA |
187 | lastMove: this.vr.moves[L-1], |
188 | movesCount: L, | |
a29d9d6b BA |
189 | })); |
190 | } | |
56a683cd | 191 | else if (data.movesCount > L) //just got last move from him |
a29d9d6b | 192 | this.play(data.lastMove, "animate"); |
ecf44502 | 193 | break; |
1d184b4c | 194 | case "resign": //..you won! |
dfb4afc1 | 195 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); |
1d184b4c | 196 | break; |
f3802fcd | 197 | // TODO: also use (dis)connect info to count online players? |
1d184b4c BA |
198 | case "connect": |
199 | case "disconnect": | |
a3ab5fdb | 200 | if (this.mode=="human") |
3a609580 | 201 | { |
a3ab5fdb BA |
202 | const online = (data.code == "connect"); |
203 | // If this is an opponent ? | |
204 | if (!!this.opponents[data.id]) | |
205 | this.opponents[data.id].online = online; | |
206 | else | |
207 | { | |
208 | // Or an observer ? | |
209 | if (!online) | |
210 | delete this.people[data.id]; | |
211 | else | |
212 | this.people[data.id] = data.name; | |
213 | } | |
3a609580 | 214 | } |
1d184b4c BA |
215 | break; |
216 | } | |
217 | }; | |
d35f20e4 | 218 | const socketCloseListener = () => { |
d35f20e4 BA |
219 | this.conn.addEventListener('message', socketMessageListener); |
220 | this.conn.addEventListener('close', socketCloseListener); | |
221 | }; | |
582df349 BA |
222 | if (!!this.conn) |
223 | { | |
224 | this.conn.onmessage = socketMessageListener; | |
225 | this.conn.onclose = socketCloseListener; | |
226 | } | |
a3ab5fdb | 227 | // Computer moves web worker logic: (TODO: also for observers in HH games ?) |
8d7e2786 | 228 | this.compWorker.postMessage(["scripts",variant.name]); |
d337a94c BA |
229 | this.compWorker.onmessage = e => { |
230 | this.lockCompThink = true; //to avoid some ghost moves | |
aa78cc74 | 231 | let compMove = e.data; |
6e62b1c7 BA |
232 | if (!Array.isArray(compMove)) |
233 | compMove = [compMove]; //to deal with MarseilleRules | |
d337a94c BA |
234 | // Small delay for the bot to appear "more human" |
235 | const delay = Math.max(500-(Date.now()-this.timeStart), 0); | |
643479f8 | 236 | setTimeout(() => { |
d337a94c BA |
237 | const animate = variant.name != "Dark"; |
238 | this.play(compMove[0], animate); | |
6e62b1c7 | 239 | if (compMove.length == 2) |
d337a94c BA |
240 | setTimeout( () => { this.play(compMove[1], animate); }, 750); |
241 | else //250 == length of animation (TODO: should be a constant somewhere) | |
242 | setTimeout( () => this.lockCompThink = false, 250); | |
643479f8 BA |
243 | }, delay); |
244 | } | |
1d184b4c | 245 | }, |
a3ab5fdb BA |
246 | // dans variant.js (plutôt room.js) conn gère aussi les challenges |
247 | // et les chats dans chat.js. Puis en webRTC, repenser tout ça. | |
81da2786 | 248 | methods: { |
a3ab5fdb BA |
249 | offerDraw: function() { |
250 | if (!confirm("Offer draw?")) | |
251 | return; | |
252 | // Stay in "draw offer sent" state until next move is played | |
253 | this.drawOfferSent = true; | |
254 | if (this.subMode == "corr") | |
255 | { | |
256 | // TODO: set drawOffer on in game (how ?) | |
257 | } | |
258 | else //live game | |
259 | { | |
260 | this.opponents.forEach(o => { | |
261 | if (!!o.online) | |
262 | { | |
263 | try { | |
264 | this.conn.send(JSON.stringify({code: "draw", oppid: o.id})); | |
265 | } catch (INVALID_STATE_ERR) { | |
266 | return; | |
267 | } | |
268 | } | |
269 | }); | |
270 | } | |
271 | }, | |
272 | // + conn handling: "draw" message ==> agree for draw (if we have "drawOffered" at true) | |
273 | receiveDrawOffer: function() { | |
274 | //if (...) | |
275 | // TODO: ignore if preventDrawOffer is set; otherwise show modal box with option "prevent future offers" | |
276 | // if accept: send message "draw" | |
277 | }, | |
278 | abortGame: function() { | |
279 | if (!confirm("Abort the game?")) | |
280 | return; | |
281 | //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions, | |
282 | //send message: "gameOver" avec score "?" | |
283 | }, | |
284 | resign: function(e) { | |
285 | if (!confirm("Resign the game?")) | |
286 | return; | |
287 | if (this.mode == "human" && this.oppConnected(this.oppid)) | |
288 | { | |
289 | try { | |
290 | this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid})); | |
291 | } catch (INVALID_STATE_ERR) { | |
292 | return; | |
293 | } | |
294 | } | |
295 | this.endGame(this.mycolor=="w"?"0-1":"1-0"); | |
296 | }, | |
d44df0b0 | 297 | translate: translate, |
60d9063f | 298 | newGameFromFen: function() { |
ab4f4bf2 | 299 | this.vr = new V(this.fen); |
a3ab5fdb BA |
300 | this.moves = []; |
301 | this.cursor = -1; | |
60d9063f | 302 | this.fenStart = this.fen; |
a3ab5fdb BA |
303 | this.score = "*"; |
304 | if (this.mode == "analyze") | |
305 | { | |
60d9063f | 306 | this.mycolor = V.ParseFen(this.fen).turn; |
a3ab5fdb BA |
307 | this.orientation = this.mycolor; |
308 | } | |
309 | else if (this.mode == "computer") //only other alternative (HH with gameId) | |
310 | { | |
311 | this.mycolor = (Math.random() < 0.5 ? "w" : "b"); | |
312 | this.orientation = this.mycolor; | |
60d9063f | 313 | this.compWorker.postMessage(["init",this.fen]); |
a3ab5fdb BA |
314 | if (this.mycolor != "w" || this.subMode == "auto") |
315 | this.playComputerMove(); | |
316 | } | |
317 | }, | |
d44df0b0 | 318 | loadGame: function() { |
3ca7a846 BA |
319 | // TODO: ask game to remote peer if this.remoteId is set |
320 | // (or just if game not found locally) | |
321 | // NOTE: if it's a corr game, ask it from server | |
97da8720 | 322 | const game = getGameFromStorage(this.gameRef.id, this.gameRef.uid); //uid may be blank |
a3ab5fdb BA |
323 | this.opponent.id = game.oppid; //opponent ID in case of running HH game |
324 | this.opponent.name = game.oppname; //maye be blank (if anonymous) | |
59d58d7d | 325 | this.score = game.score; |
a3ab5fdb | 326 | this.mycolor = game.mycolor; |
59d58d7d BA |
327 | this.fenStart = game.fenStart; |
328 | this.moves = game.moves; | |
d337a94c BA |
329 | this.cursor = game.moves.length-1; |
330 | this.lastMove = (game.moves.length > 0 ? game.moves[this.cursor] : null); | |
d44df0b0 | 331 | }, |
fd373b27 BA |
332 | setEndgameMessage: function(score) { |
333 | let eogMessage = "Undefined"; | |
334 | switch (score) | |
335 | { | |
336 | case "1-0": | |
337 | eogMessage = translations["White win"]; | |
338 | break; | |
339 | case "0-1": | |
340 | eogMessage = translations["Black win"]; | |
341 | break; | |
342 | case "1/2": | |
343 | eogMessage = translations["Draw"]; | |
344 | break; | |
345 | case "?": | |
346 | eogMessage = "Unfinished"; | |
347 | break; | |
348 | } | |
349 | this.endgameMessage = eogMessage; | |
350 | }, | |
01ca2adc | 351 | download: function() { |
59d58d7d | 352 | const content = this.getPgn(); |
01ca2adc BA |
353 | // Prepare and trigger download link |
354 | let downloadAnchor = document.getElementById("download"); | |
355 | downloadAnchor.setAttribute("download", "game.pgn"); | |
edcd679a | 356 | downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
01ca2adc BA |
357 | downloadAnchor.click(); |
358 | }, | |
59d58d7d BA |
359 | getPgn: function() { |
360 | let pgn = ""; | |
361 | pgn += '[Site "vchess.club"]\n'; | |
362 | const opponent = (this.mode=="human" ? "Anonymous" : "Computer"); | |
363 | pgn += '[Variant "' + variant.name + '"]\n'; | |
364 | pgn += '[Date "' + getDate(new Date()) + '"]\n'; | |
365 | const whiteName = ["human","computer"].includes(this.mode) | |
366 | ? (this.mycolor=='w'?'Myself':opponent) | |
367 | : "analyze"; | |
368 | const blackName = ["human","computer"].includes(this.mode) | |
369 | ? (this.mycolor=='b'?'Myself':opponent) | |
370 | : "analyze"; | |
371 | pgn += '[White "' + whiteName + '"]\n'; | |
372 | pgn += '[Black "' + blackName + '"]\n'; | |
373 | pgn += '[Fen "' + this.fenStart + '"]\n'; | |
374 | pgn += '[Result "' + this.score + '"]\n\n'; | |
375 | let counter = 1; | |
376 | let i = 0; | |
377 | while (i < this.moves.length) | |
378 | { | |
379 | pgn += (counter++) + "."; | |
380 | for (let color of ["w","b"]) | |
381 | { | |
382 | let move = ""; | |
383 | while (i < this.moves.length && this.moves[i].color == color) | |
384 | move += this.moves[i++].notation[0] + ","; | |
385 | move = move.slice(0,-1); //remove last comma | |
386 | pgn += move + (i < this.moves.length-1 ? " " : ""); | |
387 | } | |
388 | } | |
389 | return pgn + "\n"; | |
390 | }, | |
fd373b27 BA |
391 | showScoreMsg: function(score) { |
392 | this.setEndgameMessage(score); | |
ecf44502 | 393 | let modalBox = document.getElementById("modal-eog"); |
186516b8 | 394 | modalBox.checked = true; |
1a788978 BA |
395 | setTimeout(() => { modalBox.checked = false; }, 2000); |
396 | }, | |
397 | endGame: function(score) { | |
398 | this.score = score; | |
fd373b27 | 399 | this.showScoreMsg(score); |
d337a94c | 400 | if (this.mode == "human") |
d337a94c | 401 | localStorage["score"] = score; |
a3ab5fdb | 402 | this.$emit("game-over"); |
1d184b4c | 403 | }, |
a3ab5fdb BA |
404 | oppConnected: function(uid) { |
405 | return this.opponents.any(o => o.id == uidi && o.online); | |
067c675b | 406 | }, |
1d184b4c | 407 | playComputerMove: function() { |
643479f8 BA |
408 | this.timeStart = Date.now(); |
409 | this.compWorker.postMessage(["askmove"]); | |
1d184b4c | 410 | }, |
fd373b27 | 411 | animateMove: function(move) { |
582df349 BA |
412 | let startSquare = document.getElementById(getSquareId(move.start)); |
413 | let endSquare = document.getElementById(getSquareId(move.end)); | |
fd373b27 BA |
414 | let rectStart = startSquare.getBoundingClientRect(); |
415 | let rectEnd = endSquare.getBoundingClientRect(); | |
416 | let translation = {x:rectEnd.x-rectStart.x, y:rectEnd.y-rectStart.y}; | |
417 | let movingPiece = | |
582df349 | 418 | document.querySelector("#" + getSquareId(move.start) + " > img.piece"); |
fd373b27 BA |
419 | // HACK for animation (with positive translate, image slides "under background") |
420 | // Possible improvement: just alter squares on the piece's way... | |
421 | squares = document.getElementsByClassName("board"); | |
422 | for (let i=0; i<squares.length; i++) | |
423 | { | |
424 | let square = squares.item(i); | |
582df349 | 425 | if (square.id != getSquareId(move.start)) |
fd373b27 BA |
426 | square.style.zIndex = "-1"; |
427 | } | |
428 | movingPiece.style.transform = "translate(" + translation.x + "px," + | |
429 | translation.y + "px)"; | |
430 | movingPiece.style.transitionDuration = "0.2s"; | |
431 | movingPiece.style.zIndex = "3000"; | |
432 | setTimeout( () => { | |
433 | for (let i=0; i<squares.length; i++) | |
434 | squares.item(i).style.zIndex = "auto"; | |
435 | movingPiece.style = {}; //required e.g. for 0-0 with KR swap | |
a3ab5fdb | 436 | this.play(move); |
fd373b27 BA |
437 | }, 250); |
438 | }, | |
439 | play: function(move, programmatic) { | |
d337a94c | 440 | let navigate = !move; |
7d9e99bc | 441 | // Forbid playing outside analyze mode when cursor isn't at moves.length-1 |
d337a94c BA |
442 | // (except if we receive opponent's move, human or computer) |
443 | if (!navigate && this.mode != "analyze" && !programmatic | |
444 | && this.cursor < this.moves.length-1) | |
445 | { | |
7d9e99bc | 446 | return; |
d337a94c | 447 | } |
7d9e99bc BA |
448 | if (navigate) |
449 | { | |
d337a94c | 450 | if (this.cursor == this.moves.length-1) |
7d9e99bc | 451 | return; //no more moves |
d337a94c | 452 | move = this.moves[this.cursor+1]; |
7d9e99bc | 453 | } |
d337a94c BA |
454 | if (!!programmatic) //computer or (remote) human opponent |
455 | { | |
456 | if (this.cursor < this.moves.length-1) | |
457 | this.gotoEnd(); //required to play the move | |
fd373b27 | 458 | return this.animateMove(move); |
d337a94c | 459 | } |
fd373b27 | 460 | // Not programmatic, or animation is over |
a3ab5fdb BA |
461 | if (this.mode == "human" && this.subMode == "corr" && this.mycolor == this.vr.turn) |
462 | { | |
463 | // TODO: show confirm box "validate move ?" | |
464 | } | |
fd373b27 BA |
465 | if (!move.notation) |
466 | move.notation = this.vr.getNotation(move); | |
7d9e99bc BA |
467 | if (!move.color) |
468 | move.color = this.vr.turn; | |
fd373b27 | 469 | this.vr.play(move); |
7d9e99bc | 470 | this.cursor++; |
59d58d7d | 471 | this.lastMove = move; |
fd373b27 BA |
472 | if (!move.fen) |
473 | move.fen = this.vr.getFen(); | |
582df349 | 474 | if (this.settings.sound == 2) |
fd373b27 BA |
475 | new Audio("/sounds/move.mp3").play().catch(err => {}); |
476 | if (this.mode == "human") | |
477 | { | |
478 | updateStorage(move); //after our moves and opponent moves | |
582df349 | 479 | if (this.vr.turn == this.mycolor) |
fd373b27 BA |
480 | this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid})); |
481 | } | |
482 | else if (this.mode == "computer") | |
483 | { | |
484 | // Send the move to web worker (including his own moves) | |
485 | this.compWorker.postMessage(["newmove",move]); | |
486 | } | |
7d9e99bc | 487 | if (!navigate && (this.score == "*" || this.mode == "analyze")) |
fd373b27 | 488 | { |
7d9e99bc BA |
489 | // Stack move on movesList at current cursor |
490 | if (this.cursor == this.moves.length) | |
491 | this.moves.push(move); | |
492 | else | |
74ea2e8d | 493 | this.moves = this.moves.slice(0,this.cursor).concat([move]); |
fd373b27 BA |
494 | } |
495 | // Is opponent in check? | |
496 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
497 | const score = this.vr.getCurrentScore(); | |
498 | if (score != "*") | |
499 | { | |
500 | if (["human","computer"].includes(this.mode)) | |
501 | this.endGame(score); | |
502 | else //just show score on screen (allow undo) | |
503 | this.showScoreMsg(score); | |
fd373b27 | 504 | } |
d337a94c BA |
505 | // subTurn condition for Marseille (and Avalanche) rules |
506 | else if ((this.mode == "computer" && (!this.vr.subTurn || this.vr.subTurn <= 1)) | |
507 | && (this.subMode == "auto" || this.vr.turn != this.mycolor)) | |
508 | { | |
fd373b27 | 509 | this.playComputerMove(); |
d337a94c | 510 | } |
59d58d7d | 511 | // https://vuejs.org/v2/guide/list.html#Caveats (also for undo) |
7d9e99bc BA |
512 | if (navigate) |
513 | this.$children[0].$forceUpdate(); //TODO!? | |
fd373b27 BA |
514 | }, |
515 | undo: function(move) { | |
7d9e99bc BA |
516 | let navigate = !move; |
517 | if (navigate) | |
518 | { | |
d337a94c | 519 | if (this.cursor < 0) |
7d9e99bc | 520 | return; //no more moves |
d337a94c | 521 | move = this.moves[this.cursor]; |
7d9e99bc | 522 | } |
fd373b27 | 523 | this.vr.undo(move); |
7d9e99bc | 524 | this.cursor--; |
d337a94c | 525 | this.lastMove = (this.cursor >= 0 ? this.moves[this.cursor] : undefined); |
582df349 | 526 | if (this.settings.sound == 2) |
fd373b27 BA |
527 | new Audio("/sounds/undo.mp3").play().catch(err => {}); |
528 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
7d9e99bc | 529 | if (navigate) |
a3ab5fdb BA |
530 | this.$children[0].$forceUpdate(); //TODO!? |
531 | else if (this.mode == "analyze") //TODO: can this happen? | |
532 | this.moves.pop(); | |
7d9e99bc BA |
533 | }, |
534 | gotoMove: function(index) { | |
ab4f4bf2 | 535 | this.vr = new V(this.moves[index].fen); |
d337a94c | 536 | this.cursor = index; |
59d58d7d | 537 | this.lastMove = this.moves[index]; |
7d9e99bc BA |
538 | }, |
539 | gotoBegin: function() { | |
ab4f4bf2 | 540 | this.vr = new V(this.fenStart); |
d337a94c | 541 | this.cursor = -1; |
59d58d7d | 542 | this.lastMove = null; |
7d9e99bc BA |
543 | }, |
544 | gotoEnd: function() { | |
545 | this.gotoMove(this.moves.length-1); | |
546 | }, | |
547 | flip: function() { | |
548 | this.orientation = V.GetNextCol(this.orientation); | |
fd373b27 | 549 | }, |
1d184b4c BA |
550 | }, |
551 | }) |