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