Commit | Line | Data |
---|---|---|
92342261 | 1 | // Game logic on a variant page |
1d184b4c | 2 | Vue.component('my-game', { |
81da2786 | 3 | props: ["gameId"], //to find the game in storage (assumption: it exists) |
1d184b4c BA |
4 | data: function() { |
5 | return { | |
81da2786 BA |
6 | |
7 | // TODO: merge next variables into "game" | |
8 | // if oppid == "computer" then mode = "computer" (otherwise human) | |
3a609580 | 9 | myid: "", //our ID, always set |
81da2786 | 10 | //this.myid = localStorage.getItem("myid") |
1d184b4c | 11 | oppid: "", //opponent ID in case of HH game |
81da2786 BA |
12 | score: "*", //'*' means 'unfinished' |
13 | mycolor: "w", | |
14 | fromChallenge: false, //if true, show chat during game | |
15 | ||
16 | conn: null, //socket connection | |
1d184b4c | 17 | oppConnected: false, |
186516b8 | 18 | seek: false, |
762b7c9c | 19 | fenStart: "", |
3840e240 | 20 | pgnTxt: "", |
a897b421 | 21 | // sound level: 0 = no sound, 1 = sound only on newgame, 2 = always |
e6dcb115 | 22 | sound: parseInt(localStorage["sound"] || "2"), |
643479f8 BA |
23 | // Web worker to play computer moves without freezing interface: |
24 | compWorker: new Worker('/javascripts/playCompMove.js'), | |
25 | timeStart: undefined, //time when computer starts thinking | |
1d184b4c BA |
26 | }; |
27 | }, | |
81da2786 BA |
28 | computed: { |
29 | mode: function() { | |
30 | return (this.game.oppid == "computer" ? "computer" ? "human"); | |
31 | }, | |
32 | showChat: function() { | |
33 | return this.mode=='human' && | |
34 | (this.game.score != '*' || this.game.fromChallenge); | |
35 | }, | |
36 | showMoves: function() { | |
37 | return window.innerWidth >= 768; | |
c794dbb8 BA |
38 | }, |
39 | }, | |
81da2786 BA |
40 | // Modal end of game, and then sub-components |
41 | template: ` | |
42 | <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2"> | |
43 | <input id="modal-eog" type="checkbox" class="modal"/> | |
44 | <div role="dialog" aria-labelledby="eogMessage"> | |
45 | <div class="card smallpad small-modal text-center"> | |
46 | <label for="modal-eog" class="modal-close"></label> | |
47 | <h3 id="eogMessage" class="section">{{ endgameMessage }}</h3> | |
48 | ||
49 | <my-chat v-if="showChat"></my-chat> | |
50 | //TODO: connection + turn indicators en haut à droite (superposé au menu) | |
51 | <my-board></my-board> | |
52 | // TODO: controls: abort, clear, resign, draw (avec confirm box) | |
53 | // et si partie terminée : (mode analyse) just clear, back / play | |
54 | // + flip button toujours disponible | |
55 | ||
56 | <div id="pgn-div" class="section-content"> | |
57 | <a id="download" href: "#"></a> | |
58 | <button id="downloadBtn" @click="download"> | |
59 | {{ translations["Download PGN"] }} | |
60 | </button> | |
61 | ||
62 | <my-move-list v-if="showMoves"></my-move-list> | |
63 | </div> | |
64 | `, | |
1a788978 BA |
65 | computed: { |
66 | endgameMessage: function() { | |
67 | let eogMessage = "Unfinished"; | |
81da2786 | 68 | switch (this.game.score) |
1a788978 BA |
69 | { |
70 | case "1-0": | |
247356cd | 71 | eogMessage = translations["White win"]; |
1a788978 BA |
72 | break; |
73 | case "0-1": | |
247356cd | 74 | eogMessage = translations["Black win"]; |
1a788978 BA |
75 | break; |
76 | case "1/2": | |
247356cd | 77 | eogMessage = translations["Draw"]; |
1a788978 BA |
78 | break; |
79 | } | |
80 | return eogMessage; | |
81 | }, | |
82 | }, | |
1d184b4c BA |
83 | created: function() { |
84 | const url = socketUrl; | |
8d7e2786 | 85 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id); |
81da2786 BA |
86 | // const socketOpenListener = () => { |
87 | // }; | |
88 | ||
89 | // TODO: after game, archive in indexedDB | |
067c675b BA |
90 | |
91 | // TODO: this events listener is central. Refactor ? How ? | |
d35f20e4 | 92 | const socketMessageListener = msg => { |
1d184b4c | 93 | const data = JSON.parse(msg.data); |
edcd679a | 94 | let L = undefined; |
1d184b4c BA |
95 | switch (data.code) |
96 | { | |
1d184b4c | 97 | case "newmove": //..he played! |
8d7e2786 | 98 | this.play(data.move, (variant.name!="Dark" ? "animate" : null)); |
1d184b4c | 99 | break; |
f3802fcd | 100 | case "pong": //received if we sent a ping (game still alive on our side) |
56a683cd BA |
101 | if (this.gameId != data.gameId) |
102 | break; //games IDs don't match: definitely over... | |
1d184b4c | 103 | this.oppConnected = true; |
f3802fcd | 104 | // Send our "last state" informations to opponent |
edcd679a | 105 | L = this.vr.moves.length; |
a29d9d6b | 106 | this.conn.send(JSON.stringify({ |
56a683cd BA |
107 | code: "lastate", |
108 | oppid: this.oppid, | |
109 | gameId: this.gameId, | |
110 | lastMove: (L>0?this.vr.moves[L-1]:undefined), | |
111 | movesCount: L, | |
a29d9d6b | 112 | })); |
1d184b4c | 113 | break; |
56a683cd | 114 | case "lastate": //got opponent infos about last move |
edcd679a | 115 | L = this.vr.moves.length; |
56a683cd BA |
116 | if (this.gameId != data.gameId) |
117 | break; //games IDs don't match: nothing we can do... | |
118 | // OK, opponent still in game (which might be over) | |
edcd679a | 119 | if (this.score != "*") |
a29d9d6b | 120 | { |
56a683cd | 121 | // We finished the game (any result possible) |
a29d9d6b | 122 | this.conn.send(JSON.stringify({ |
56a683cd BA |
123 | code: "lastate", |
124 | oppid: data.oppid, | |
125 | gameId: this.gameId, | |
126 | score: this.score, | |
a29d9d6b BA |
127 | })); |
128 | } | |
56a683cd BA |
129 | else if (!!data.score) //opponent finished the game |
130 | this.endGame(data.score); | |
131 | else if (data.movesCount < L) | |
a29d9d6b BA |
132 | { |
133 | // We must tell last move to opponent | |
a29d9d6b | 134 | this.conn.send(JSON.stringify({ |
56a683cd BA |
135 | code: "lastate", |
136 | oppid: this.oppid, | |
edcd679a | 137 | gameId: this.gameId, |
56a683cd BA |
138 | lastMove: this.vr.moves[L-1], |
139 | movesCount: L, | |
a29d9d6b BA |
140 | })); |
141 | } | |
56a683cd | 142 | else if (data.movesCount > L) //just got last move from him |
a29d9d6b | 143 | this.play(data.lastMove, "animate"); |
ecf44502 | 144 | break; |
1d184b4c | 145 | case "resign": //..you won! |
dfb4afc1 | 146 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); |
1d184b4c | 147 | break; |
f3802fcd | 148 | // TODO: also use (dis)connect info to count online players? |
1d184b4c BA |
149 | case "connect": |
150 | case "disconnect": | |
4f7723a1 | 151 | if (this.mode=="human" && this.oppid == data.id) |
1d184b4c | 152 | this.oppConnected = (data.code == "connect"); |
4f7723a1 | 153 | if (this.oppConnected && this.score != "*") |
3a609580 BA |
154 | { |
155 | // Send our name to the opponent, in case of he hasn't it | |
156 | this.conn.send(JSON.stringify({ | |
157 | code:"myname", name:this.myname, oppid: this.oppid})); | |
158 | } | |
1d184b4c BA |
159 | break; |
160 | } | |
161 | }; | |
067c675b | 162 | |
d35f20e4 | 163 | const socketCloseListener = () => { |
8d7e2786 | 164 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id); |
81da2786 | 165 | //this.conn.addEventListener('open', socketOpenListener); |
d35f20e4 BA |
166 | this.conn.addEventListener('message', socketMessageListener); |
167 | this.conn.addEventListener('close', socketCloseListener); | |
168 | }; | |
81da2786 | 169 | //this.conn.onopen = socketOpenListener; |
d35f20e4 BA |
170 | this.conn.onmessage = socketMessageListener; |
171 | this.conn.onclose = socketCloseListener; | |
81da2786 BA |
172 | |
173 | ||
e64084da | 174 | // Listen to keyboard left/right to navigate in game |
81da2786 | 175 | // TODO: also mouse wheel ! |
e64084da | 176 | document.onkeydown = event => { |
dbcc32e9 | 177 | if (["human","computer"].includes(this.mode) && |
3a609580 | 178 | !!this.vr && this.vr.moves.length > 0 && [37,39].includes(event.keyCode)) |
e64084da BA |
179 | { |
180 | event.preventDefault(); | |
181 | if (event.keyCode == 37) //Back | |
182 | this.undo(); | |
183 | else //Forward (39) | |
184 | this.play(); | |
185 | } | |
186 | }; | |
81da2786 BA |
187 | |
188 | ||
067c675b | 189 | // Computer moves web worker logic: (TODO: also for observers in HH games) |
8d7e2786 | 190 | this.compWorker.postMessage(["scripts",variant.name]); |
643479f8 BA |
191 | const self = this; |
192 | this.compWorker.onmessage = function(e) { | |
aa78cc74 | 193 | let compMove = e.data; |
78bab51e BA |
194 | if (!compMove) |
195 | return; //may happen if MarseilleRules and subTurn==2 (TODO: a bit ugly...) | |
6e62b1c7 BA |
196 | if (!Array.isArray(compMove)) |
197 | compMove = [compMove]; //to deal with MarseilleRules | |
198 | // TODO: imperfect attempt to avoid ghost move: | |
199 | compMove.forEach(m => { m.computer = true; }); | |
643479f8 BA |
200 | // (first move) HACK: small delay to avoid selecting elements |
201 | // before they appear on page: | |
202 | const delay = Math.max(500-(Date.now()-self.timeStart), 0); | |
203 | setTimeout(() => { | |
8d7e2786 | 204 | const animate = (variant.name!="Dark" ? "animate" : null); |
aa78cc74 | 205 | if (self.mode == "computer") //warning: mode could have changed! |
5915f720 | 206 | self.play(compMove[0], animate); |
6e62b1c7 BA |
207 | if (compMove.length == 2) |
208 | setTimeout( () => { | |
209 | if (self.mode == "computer") | |
5915f720 | 210 | self.play(compMove[1], animate); |
78bab51e | 211 | }, 750); |
643479f8 BA |
212 | }, delay); |
213 | } | |
1d184b4c | 214 | }, |
067c675b | 215 | |
067c675b | 216 | |
81da2786 | 217 | methods: { |
01ca2adc | 218 | download: function() { |
edcd679a | 219 | // Variants may have special PGN structure (so next function isn't defined here) |
81da2786 | 220 | const content = V.GetPGN(this.moves, this.mycolor, this.score, this.fenStart, this.mode); |
01ca2adc BA |
221 | // Prepare and trigger download link |
222 | let downloadAnchor = document.getElementById("download"); | |
223 | downloadAnchor.setAttribute("download", "game.pgn"); | |
edcd679a | 224 | downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
01ca2adc BA |
225 | downloadAnchor.click(); |
226 | }, | |
1a788978 | 227 | showScoreMsg: function() { |
ecf44502 | 228 | let modalBox = document.getElementById("modal-eog"); |
186516b8 | 229 | modalBox.checked = true; |
1a788978 BA |
230 | setTimeout(() => { modalBox.checked = false; }, 2000); |
231 | }, | |
232 | endGame: function(score) { | |
233 | this.score = score; | |
56a683cd BA |
234 | if (["human","computer"].includes(this.mode)) |
235 | { | |
236 | const prefix = (this.mode=="computer" ? "comp-" : ""); | |
237 | localStorage.setItem(prefix+"score", score); | |
238 | } | |
1a788978 | 239 | this.showScoreMsg(); |
3a609580 BA |
240 | if (this.mode == "human" && this.oppConnected) |
241 | { | |
242 | // Send our nickname to opponent | |
243 | this.conn.send(JSON.stringify({ | |
244 | code:"myname", name:this.myname, oppid:this.oppid})); | |
245 | } | |
e64084da | 246 | this.cursor = this.vr.moves.length; //to navigate in finished game |
1d184b4c | 247 | }, |
067c675b BA |
248 | resign: function(e) { |
249 | this.getRidOfTooltip(e.currentTarget); | |
250 | if (this.mode == "human" && this.oppConnected) | |
251 | { | |
252 | try { | |
253 | this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid})); | |
254 | } catch (INVALID_STATE_ERR) { | |
255 | return; //socket is not ready (and not yet reconnected) | |
256 | } | |
257 | } | |
258 | this.endGame(this.mycolor=="w"?"0-1":"1-0"); | |
259 | }, | |
1d184b4c | 260 | playComputerMove: function() { |
643479f8 BA |
261 | this.timeStart = Date.now(); |
262 | this.compWorker.postMessage(["askmove"]); | |
1d184b4c | 263 | }, |
067c675b | 264 | // OK, these last functions can stay here (?!) |
1d184b4c | 265 | play: function(move, programmatic) { |
e64084da BA |
266 | if (!move) |
267 | { | |
268 | // Navigate after game is over | |
067c675b | 269 | if (this.cursor >= this.moves.length) |
e64084da | 270 | return; //already at the end |
067c675b | 271 | move = this.moves[this.cursor++]; |
e64084da | 272 | } |
1d184b4c | 273 | if (!!programmatic) //computer or human opponent |
4f7723a1 | 274 | return this.animateMove(move); |
1d184b4c BA |
275 | // Not programmatic, or animation is over |
276 | if (this.mode == "human" && this.vr.turn == this.mycolor) | |
a29d9d6b | 277 | this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid})); |
067c675b BA |
278 | |
279 | ||
280 | // TODO: play move, and stack it on this.moves (if a move was provided; otherwise just navigate) | |
281 | ||
282 | if (this.score == "*") //TODO: I don't like this if() | |
3300df38 | 283 | { |
aa78cc74 BA |
284 | // Emergency check, if human game started "at the same time" |
285 | // TODO: robustify this... | |
286 | if (this.mode == "human" && !!move.computer) | |
287 | return; | |
e64084da | 288 | this.vr.play(move, "ingame"); |
f6dbe8e3 BA |
289 | // Is opponent in check? |
290 | this.incheck = this.vr.getCheckSquares(this.vr.turn); | |
e6dcb115 BA |
291 | if (this.sound == 2) |
292 | new Audio("/sounds/move.mp3").play().catch(err => {}); | |
643479f8 BA |
293 | if (this.mode == "computer") |
294 | { | |
aa78cc74 | 295 | // Send the move to web worker (TODO: including his own moves?!) |
643479f8 BA |
296 | this.compWorker.postMessage(["newmove",move]); |
297 | } | |
067c675b | 298 | const eog = this.vr.getCurrentScore(); |
dbcc32e9 BA |
299 | if (eog != "*") |
300 | { | |
301 | if (["human","computer"].includes(this.mode)) | |
302 | this.endGame(eog); | |
303 | else | |
304 | { | |
305 | // Just show score on screen (allow undo) | |
306 | this.score = eog; | |
307 | this.showScoreMsg(); | |
308 | } | |
309 | } | |
3300df38 | 310 | } |
067c675b BA |
311 | // else |
312 | // { | |
313 | // VariantRules.PlayOnBoard(this.vr.board, move); | |
314 | // this.$forceUpdate(); //TODO: ?! | |
315 | // } | |
4f7723a1 | 316 | if (["human","computer","friend"].includes(this.mode)) |
56a683cd BA |
317 | this.updateStorage(); //after our moves and opponent moves |
318 | if (this.mode == "computer" && this.vr.turn != this.mycolor && this.score == "*") | |
643479f8 | 319 | this.playComputerMove(); |
1d184b4c | 320 | }, |
067c675b | 321 | // TODO: merge two next functions |
e64084da BA |
322 | undo: function() { |
323 | // Navigate after game is over | |
324 | if (this.cursor == 0) | |
325 | return; //already at the beginning | |
3300df38 BA |
326 | if (this.cursor == this.vr.moves.length) |
327 | this.incheck = []; //in case of... | |
e64084da BA |
328 | const move = this.vr.moves[--this.cursor]; |
329 | VariantRules.UndoOnBoard(this.vr.board, move); | |
330 | this.$forceUpdate(); //TODO: ?! | |
2748531f BA |
331 | }, |
332 | undoInGame: function() { | |
333 | const lm = this.vr.lastMove; | |
334 | if (!!lm) | |
b5fb8e69 | 335 | { |
2748531f | 336 | this.vr.undo(lm); |
e6dcb115 BA |
337 | if (this.sound == 2) |
338 | new Audio("/sounds/undo.mp3").play().catch(err => {}); | |
f6dbe8e3 | 339 | this.incheck = this.vr.getCheckSquares(this.vr.turn); |
b5fb8e69 | 340 | } |
2748531f | 341 | }, |
1d184b4c BA |
342 | }, |
343 | }) | |
b6487fb9 | 344 | |
298c42e6 BA |
345 | //// TODO: keep moves list here |
346 | //get lastMove() | |
347 | // { | |
348 | // const L = this.moves.length; | |
349 | // return (L>0 ? this.moves[L-1] : null); | |
350 | // } | |
351 | // | |
352 | //// here too: | |
353 | // move.notation = this.getNotation(move); | |
067c675b BA |
354 | //TODO: confirm dialog with "opponent offers draw", avec possible bouton "prevent future offers" + bouton "proposer nulle" |
355 | //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions, | |
356 | //comme sur lichess |