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> | |
936dc463 BA |
48 | </div> |
49 | </div> | |
81da2786 BA |
50 | |
51 | <my-chat v-if="showChat"></my-chat> | |
52 | //TODO: connection + turn indicators en haut à droite (superposé au menu) | |
53 | <my-board></my-board> | |
54 | // TODO: controls: abort, clear, resign, draw (avec confirm box) | |
55 | // et si partie terminée : (mode analyse) just clear, back / play | |
56 | // + flip button toujours disponible | |
e5dc87e0 BA |
57 | // Show current FEN (just below board, lower right corner) |
58 | // (if mode != Dark ...) | |
59 | elementArray.push( | |
60 | h('div', | |
61 | { | |
62 | attrs: { id: "fen-div" }, | |
63 | "class": { "section-content": true }, | |
64 | }, | |
65 | [ | |
66 | h('p', | |
67 | { | |
68 | attrs: { id: "fen-string" }, | |
69 | domProps: { innerHTML: this.vr.getBaseFen() }, | |
70 | "class": { "text-center": true }, | |
71 | } | |
72 | ) | |
73 | ] | |
74 | ) | |
75 | ); | |
81da2786 BA |
76 | |
77 | <div id="pgn-div" class="section-content"> | |
78 | <a id="download" href: "#"></a> | |
79 | <button id="downloadBtn" @click="download"> | |
80 | {{ translations["Download PGN"] }} | |
81 | </button> | |
82 | ||
83 | <my-move-list v-if="showMoves"></my-move-list> | |
84 | </div> | |
85 | `, | |
1a788978 BA |
86 | computed: { |
87 | endgameMessage: function() { | |
88 | let eogMessage = "Unfinished"; | |
81da2786 | 89 | switch (this.game.score) |
1a788978 BA |
90 | { |
91 | case "1-0": | |
247356cd | 92 | eogMessage = translations["White win"]; |
1a788978 BA |
93 | break; |
94 | case "0-1": | |
247356cd | 95 | eogMessage = translations["Black win"]; |
1a788978 BA |
96 | break; |
97 | case "1/2": | |
247356cd | 98 | eogMessage = translations["Draw"]; |
1a788978 BA |
99 | break; |
100 | } | |
101 | return eogMessage; | |
102 | }, | |
103 | }, | |
1d184b4c BA |
104 | created: function() { |
105 | const url = socketUrl; | |
8d7e2786 | 106 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id); |
81da2786 BA |
107 | // const socketOpenListener = () => { |
108 | // }; | |
109 | ||
110 | // TODO: after game, archive in indexedDB | |
067c675b BA |
111 | |
112 | // TODO: this events listener is central. Refactor ? How ? | |
d35f20e4 | 113 | const socketMessageListener = msg => { |
1d184b4c | 114 | const data = JSON.parse(msg.data); |
edcd679a | 115 | let L = undefined; |
1d184b4c BA |
116 | switch (data.code) |
117 | { | |
1d184b4c | 118 | case "newmove": //..he played! |
8d7e2786 | 119 | this.play(data.move, (variant.name!="Dark" ? "animate" : null)); |
1d184b4c | 120 | break; |
f3802fcd | 121 | case "pong": //received if we sent a ping (game still alive on our side) |
56a683cd BA |
122 | if (this.gameId != data.gameId) |
123 | break; //games IDs don't match: definitely over... | |
1d184b4c | 124 | this.oppConnected = true; |
f3802fcd | 125 | // Send our "last state" informations to opponent |
edcd679a | 126 | L = this.vr.moves.length; |
a29d9d6b | 127 | this.conn.send(JSON.stringify({ |
56a683cd BA |
128 | code: "lastate", |
129 | oppid: this.oppid, | |
130 | gameId: this.gameId, | |
131 | lastMove: (L>0?this.vr.moves[L-1]:undefined), | |
132 | movesCount: L, | |
a29d9d6b | 133 | })); |
1d184b4c | 134 | break; |
56a683cd | 135 | case "lastate": //got opponent infos about last move |
edcd679a | 136 | L = this.vr.moves.length; |
56a683cd BA |
137 | if (this.gameId != data.gameId) |
138 | break; //games IDs don't match: nothing we can do... | |
139 | // OK, opponent still in game (which might be over) | |
edcd679a | 140 | if (this.score != "*") |
a29d9d6b | 141 | { |
56a683cd | 142 | // We finished the game (any result possible) |
a29d9d6b | 143 | this.conn.send(JSON.stringify({ |
56a683cd BA |
144 | code: "lastate", |
145 | oppid: data.oppid, | |
146 | gameId: this.gameId, | |
147 | score: this.score, | |
a29d9d6b BA |
148 | })); |
149 | } | |
56a683cd BA |
150 | else if (!!data.score) //opponent finished the game |
151 | this.endGame(data.score); | |
152 | else if (data.movesCount < L) | |
a29d9d6b BA |
153 | { |
154 | // We must tell last move to opponent | |
a29d9d6b | 155 | this.conn.send(JSON.stringify({ |
56a683cd BA |
156 | code: "lastate", |
157 | oppid: this.oppid, | |
edcd679a | 158 | gameId: this.gameId, |
56a683cd BA |
159 | lastMove: this.vr.moves[L-1], |
160 | movesCount: L, | |
a29d9d6b BA |
161 | })); |
162 | } | |
56a683cd | 163 | else if (data.movesCount > L) //just got last move from him |
a29d9d6b | 164 | this.play(data.lastMove, "animate"); |
ecf44502 | 165 | break; |
1d184b4c | 166 | case "resign": //..you won! |
dfb4afc1 | 167 | this.endGame(this.mycolor=="w"?"1-0":"0-1"); |
1d184b4c | 168 | break; |
f3802fcd | 169 | // TODO: also use (dis)connect info to count online players? |
1d184b4c BA |
170 | case "connect": |
171 | case "disconnect": | |
4f7723a1 | 172 | if (this.mode=="human" && this.oppid == data.id) |
1d184b4c | 173 | this.oppConnected = (data.code == "connect"); |
4f7723a1 | 174 | if (this.oppConnected && this.score != "*") |
3a609580 BA |
175 | { |
176 | // Send our name to the opponent, in case of he hasn't it | |
177 | this.conn.send(JSON.stringify({ | |
178 | code:"myname", name:this.myname, oppid: this.oppid})); | |
179 | } | |
1d184b4c BA |
180 | break; |
181 | } | |
182 | }; | |
067c675b | 183 | |
d35f20e4 | 184 | const socketCloseListener = () => { |
8d7e2786 | 185 | this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id); |
81da2786 | 186 | //this.conn.addEventListener('open', socketOpenListener); |
d35f20e4 BA |
187 | this.conn.addEventListener('message', socketMessageListener); |
188 | this.conn.addEventListener('close', socketCloseListener); | |
189 | }; | |
81da2786 | 190 | //this.conn.onopen = socketOpenListener; |
d35f20e4 BA |
191 | this.conn.onmessage = socketMessageListener; |
192 | this.conn.onclose = socketCloseListener; | |
81da2786 BA |
193 | |
194 | ||
e64084da | 195 | // Listen to keyboard left/right to navigate in game |
81da2786 | 196 | // TODO: also mouse wheel ! |
e64084da | 197 | document.onkeydown = event => { |
dbcc32e9 | 198 | if (["human","computer"].includes(this.mode) && |
3a609580 | 199 | !!this.vr && this.vr.moves.length > 0 && [37,39].includes(event.keyCode)) |
e64084da BA |
200 | { |
201 | event.preventDefault(); | |
202 | if (event.keyCode == 37) //Back | |
203 | this.undo(); | |
204 | else //Forward (39) | |
205 | this.play(); | |
206 | } | |
207 | }; | |
81da2786 BA |
208 | |
209 | ||
067c675b | 210 | // Computer moves web worker logic: (TODO: also for observers in HH games) |
8d7e2786 | 211 | this.compWorker.postMessage(["scripts",variant.name]); |
643479f8 BA |
212 | const self = this; |
213 | this.compWorker.onmessage = function(e) { | |
aa78cc74 | 214 | let compMove = e.data; |
78bab51e BA |
215 | if (!compMove) |
216 | return; //may happen if MarseilleRules and subTurn==2 (TODO: a bit ugly...) | |
6e62b1c7 BA |
217 | if (!Array.isArray(compMove)) |
218 | compMove = [compMove]; //to deal with MarseilleRules | |
219 | // TODO: imperfect attempt to avoid ghost move: | |
220 | compMove.forEach(m => { m.computer = true; }); | |
643479f8 BA |
221 | // (first move) HACK: small delay to avoid selecting elements |
222 | // before they appear on page: | |
223 | const delay = Math.max(500-(Date.now()-self.timeStart), 0); | |
224 | setTimeout(() => { | |
8d7e2786 | 225 | const animate = (variant.name!="Dark" ? "animate" : null); |
aa78cc74 | 226 | if (self.mode == "computer") //warning: mode could have changed! |
5915f720 | 227 | self.play(compMove[0], animate); |
6e62b1c7 BA |
228 | if (compMove.length == 2) |
229 | setTimeout( () => { | |
230 | if (self.mode == "computer") | |
5915f720 | 231 | self.play(compMove[1], animate); |
78bab51e | 232 | }, 750); |
643479f8 BA |
233 | }, delay); |
234 | } | |
1d184b4c | 235 | }, |
067c675b | 236 | |
067c675b | 237 | |
81da2786 | 238 | methods: { |
01ca2adc | 239 | download: function() { |
edcd679a | 240 | // Variants may have special PGN structure (so next function isn't defined here) |
81da2786 | 241 | const content = V.GetPGN(this.moves, this.mycolor, this.score, this.fenStart, this.mode); |
01ca2adc BA |
242 | // Prepare and trigger download link |
243 | let downloadAnchor = document.getElementById("download"); | |
244 | downloadAnchor.setAttribute("download", "game.pgn"); | |
edcd679a | 245 | downloadAnchor.href = "data:text/plain;charset=utf-8," + encodeURIComponent(content); |
01ca2adc BA |
246 | downloadAnchor.click(); |
247 | }, | |
1a788978 | 248 | showScoreMsg: function() { |
ecf44502 | 249 | let modalBox = document.getElementById("modal-eog"); |
186516b8 | 250 | modalBox.checked = true; |
1a788978 BA |
251 | setTimeout(() => { modalBox.checked = false; }, 2000); |
252 | }, | |
253 | endGame: function(score) { | |
254 | this.score = score; | |
56a683cd BA |
255 | if (["human","computer"].includes(this.mode)) |
256 | { | |
257 | const prefix = (this.mode=="computer" ? "comp-" : ""); | |
258 | localStorage.setItem(prefix+"score", score); | |
259 | } | |
1a788978 | 260 | this.showScoreMsg(); |
3a609580 BA |
261 | if (this.mode == "human" && this.oppConnected) |
262 | { | |
263 | // Send our nickname to opponent | |
264 | this.conn.send(JSON.stringify({ | |
265 | code:"myname", name:this.myname, oppid:this.oppid})); | |
266 | } | |
e64084da | 267 | this.cursor = this.vr.moves.length; //to navigate in finished game |
1d184b4c | 268 | }, |
067c675b BA |
269 | resign: function(e) { |
270 | this.getRidOfTooltip(e.currentTarget); | |
271 | if (this.mode == "human" && this.oppConnected) | |
272 | { | |
273 | try { | |
274 | this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid})); | |
275 | } catch (INVALID_STATE_ERR) { | |
276 | return; //socket is not ready (and not yet reconnected) | |
277 | } | |
278 | } | |
279 | this.endGame(this.mycolor=="w"?"0-1":"1-0"); | |
280 | }, | |
1d184b4c | 281 | playComputerMove: function() { |
643479f8 BA |
282 | this.timeStart = Date.now(); |
283 | this.compWorker.postMessage(["askmove"]); | |
1d184b4c | 284 | }, |
067c675b | 285 | // OK, these last functions can stay here (?!) |
1d184b4c BA |
286 | }, |
287 | }) | |
b6487fb9 | 288 | |
298c42e6 BA |
289 | //// TODO: keep moves list here |
290 | //get lastMove() | |
291 | // { | |
292 | // const L = this.moves.length; | |
293 | // return (L>0 ? this.moves[L-1] : null); | |
294 | // } | |
295 | // | |
296 | //// here too: | |
297 | // move.notation = this.getNotation(move); | |
067c675b BA |
298 | //TODO: confirm dialog with "opponent offers draw", avec possible bouton "prevent future offers" + bouton "proposer nulle" |
299 | //+ bouton "abort" avec score == "?" + demander confirmation pour toutes ces actions, | |
300 | //comme sur lichess | |
e5dc87e0 BA |
301 | |
302 | // send move from here: | |
303 | //if (this.mode == "human" && this.vr.turn == this.mycolor) | |
304 | //this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid})); | |
305 | // TODO: play move, and stack it on this.moves (if a move was provided; otherwise just navigate) | |
306 | ||
307 | // if (["human","computer","friend"].includes(this.mode)) | |
308 | // this.updateStorage(); //after our moves and opponent moves | |
309 | // if (this.mode == "computer" && this.vr.turn != this.mycolor && this.score == "*") | |
310 | // this.playComputerMove(); | |
311 | // if (this.mode == "computer") | |
312 | // { | |
313 | // // Send the move to web worker (TODO: including his own moves?!) | |
314 | // this.compWorker.postMessage(["newmove",move]); | |
315 | // } | |
316 | // if (["human","computer"].includes(this.mode)) | |
317 | // this.endGame(eog); | |
318 | // else | |
319 | // { | |
320 | // // Just show score on screen (allow undo) | |
321 | // this.score = eog; | |
322 | // this.showScoreMsg(); | |
323 | // } |