Commit | Line | Data |
---|---|---|
a6088c90 | 1 | <template lang="pug"> |
7aa548e7 | 2 | main |
910d631b BA |
3 | input#modalChat.modal( |
4 | type="checkbox" | |
5 | @click="resetChatColor()" | |
6 | ) | |
7 | div#chatWrap( | |
8 | role="dialog" | |
9 | data-checkbox="modalChat" | |
10 | ) | |
a1c48034 BA |
11 | #chat.card |
12 | label.modal-close(for="modalChat") | |
ed06d9e9 | 13 | #participants |
ac8f441c | 14 | span {{ Object.keys(people).length + " " + st.tr["participant(s):"] }} |
910d631b BA |
15 | span( |
16 | v-for="p in Object.values(people)" | |
17 | v-if="!!p.name" | |
18 | ) | |
ed06d9e9 BA |
19 | | {{ p.name }} |
20 | span.anonymous(v-if="Object.values(people).some(p => !p.name)") | |
21 | | + @nonymous | |
910d631b BA |
22 | Chat( |
23 | :players="game.players" | |
24 | :pastChats="game.chats" | |
25 | :newChat="newChat" | |
26 | @mychat="processChat" | |
27 | ) | |
7aa548e7 | 28 | .row |
050ae3b5 | 29 | #aboveBoard.col-sm-12.col-md-9.col-md-offset-3.col-lg-10.col-lg-offset-2 |
2f258c37 BA |
30 | span.variant-cadence {{ game.cadence }} |
31 | span.variant-name {{ game.vname }} | |
910d631b | 32 | button#chatBtn(onClick="window.doClick('modalChat')") Chat |
4f518610 | 33 | #actions(v-if="game.score=='*'") |
910d631b BA |
34 | button( |
35 | @click="clickDraw()" | |
36 | :class="{['draw-' + drawOffer]: true}" | |
37 | ) | |
602d6bef | 38 | | {{ st.tr["Draw"] }} |
910d631b BA |
39 | button( |
40 | v-if="!!game.mycolor" | |
41 | @click="abortGame()" | |
42 | ) | |
43 | | {{ st.tr["Abort"] }} | |
44 | button( | |
45 | v-if="!!game.mycolor" | |
46 | @click="resign()" | |
47 | ) | |
48 | | {{ st.tr["Resign"] }} | |
050ae3b5 BA |
49 | #playersInfo |
50 | p | |
5bcc9b31 BA |
51 | span.name(:class="{connected: isConnected(0)}") |
52 | | {{ game.players[0].name || "@nonymous" }} | |
050ae3b5 BA |
53 | span.time(v-if="game.score=='*'") {{ virtualClocks[0] }} |
54 | span.split-names - | |
5bcc9b31 BA |
55 | span.name(:class="{connected: isConnected(1)}") |
56 | | {{ game.players[1].name || "@nonymous" }} | |
050ae3b5 | 57 | span.time(v-if="game.score=='*'") {{ virtualClocks[1] }} |
910d631b | 58 | BaseGame( |
8477e53d | 59 | ref="basegame" |
910d631b BA |
60 | :game="game" |
61 | :vr="vr" | |
62 | @newmove="processMove" | |
63 | @gameover="gameOver" | |
64 | ) | |
a6088c90 BA |
65 | </template> |
66 | ||
67 | <script> | |
46284a2f | 68 | import BaseGame from "@/components/BaseGame.vue"; |
f21cd6d9 | 69 | import Chat from "@/components/Chat.vue"; |
a6088c90 | 70 | import { store } from "@/store"; |
967a2686 | 71 | import { GameStorage } from "@/utils/gameStorage"; |
5b87454c | 72 | import { ppt } from "@/utils/datetime"; |
66d03f23 | 73 | import { extractTime } from "@/utils/timeControl"; |
51d87b52 | 74 | import { getRandString } from "@/utils/alea"; |
dcd68c41 | 75 | import { processModalClick } from "@/utils/modalClick"; |
77c50966 | 76 | import { getScoreMessage } from "@/utils/scoring"; |
8418f0d7 | 77 | import params from "@/parameters"; |
a6088c90 | 78 | export default { |
6808d7a1 | 79 | name: "my-game", |
a6088c90 BA |
80 | components: { |
81 | BaseGame, | |
6808d7a1 | 82 | Chat |
a6088c90 | 83 | }, |
f7121527 | 84 | // gameRef: to find the game in (potentially remote) storage |
a6088c90 BA |
85 | data: function() { |
86 | return { | |
87 | st: store.state, | |
6808d7a1 BA |
88 | gameRef: { |
89 | //given in URL (rid = remote ID) | |
4b0384fa BA |
90 | id: "", |
91 | rid: "" | |
92 | }, | |
6808d7a1 BA |
93 | game: { |
94 | //passed to BaseGame | |
95 | players: [{ name: "" }, { name: "" }], | |
9a3049f3 | 96 | chats: [], |
6808d7a1 | 97 | rendered: false |
a0c41e7e | 98 | }, |
809ba2aa | 99 | virtualClocks: [0, 0], //initialized with true game.clocks |
6dd02928 | 100 | vr: null, //"variant rules" object initialized from FEN |
dcd68c41 BA |
101 | drawOffer: "", |
102 | people: {}, //players + observers | |
760adbce | 103 | lastate: undefined, //used if opponent send lastate before game is ready |
72ccbd67 | 104 | repeat: {}, //detect position repetition |
ac8f441c | 105 | newChat: "", |
8418f0d7 | 106 | conn: null, |
51d87b52 BA |
107 | connexionString: "", |
108 | // Related to (killing of) self multi-connects: | |
109 | newConnect: {}, | |
6808d7a1 | 110 | killed: {} |
a6088c90 BA |
111 | }; |
112 | }, | |
113 | watch: { | |
6808d7a1 | 114 | $route: function(to) { |
5f131484 BA |
115 | this.gameRef.id = to.params["id"]; |
116 | this.gameRef.rid = to.query["rid"]; | |
117 | this.loadGame(); | |
6808d7a1 | 118 | } |
92a523d1 | 119 | }, |
71468011 | 120 | // NOTE: some redundant code with Hall.vue (mostly related to people array) |
a6088c90 | 121 | created: function() { |
5f131484 BA |
122 | // Always add myself to players' list |
123 | const my = this.st.user; | |
6808d7a1 | 124 | this.$set(this.people, my.sid, { id: my.id, name: my.name }); |
dc284d90 BA |
125 | this.gameRef.id = this.$route.params["id"]; |
126 | this.gameRef.rid = this.$route.query["rid"]; //may be undefined | |
8418f0d7 | 127 | // Initialize connection |
6808d7a1 BA |
128 | this.connexionString = |
129 | params.socketUrl + | |
130 | "/?sid=" + | |
131 | this.st.user.sid + | |
132 | "&tmpId=" + | |
133 | getRandString() + | |
134 | "&page=" + | |
135 | encodeURIComponent(this.$route.path); | |
51d87b52 | 136 | this.conn = new WebSocket(this.connexionString); |
8418f0d7 | 137 | this.conn.onmessage = this.socketMessageListener; |
51d87b52 | 138 | this.conn.onclose = this.socketCloseListener; |
760adbce | 139 | // Socket init required before loading remote game: |
6808d7a1 BA |
140 | const socketInit = callback => { |
141 | if (!!this.conn && this.conn.readyState == 1) | |
8477e53d | 142 | // 1 == OPEN state |
760adbce | 143 | callback(); |
6808d7a1 | 144 | else { |
8477e53d | 145 | // Socket not ready yet (initial loading) |
7f36b53a BA |
146 | // NOTE: it's important to call callback without arguments, |
147 | // otherwise first arg is Websocket object and loadGame fails. | |
6808d7a1 BA |
148 | this.conn.onopen = () => { |
149 | return callback(); | |
150 | }; | |
7f36b53a | 151 | } |
760adbce | 152 | }; |
6808d7a1 | 153 | if (!this.gameRef.rid) |
8477e53d | 154 | // Game stored locally or on server |
760adbce | 155 | this.loadGame(null, () => socketInit(this.roomInit)); |
6808d7a1 | 156 | else { |
8477e53d | 157 | // Game stored remotely: need socket to retrieve it |
760adbce BA |
158 | // NOTE: the callback "roomInit" will be lost, so we don't provide it. |
159 | // --> It will be given when receiving "fullgame" socket event. | |
160 | // A more general approach would be to store it somewhere. | |
161 | socketInit(this.loadGame); | |
162 | } | |
cdb34c93 | 163 | }, |
dcd68c41 | 164 | mounted: function() { |
6808d7a1 BA |
165 | document |
166 | .getElementById("chatWrap") | |
167 | .addEventListener("click", processModalClick); | |
dcd68c41 | 168 | }, |
8418f0d7 | 169 | beforeDestroy: function() { |
71468011 | 170 | this.send("disconnect"); |
8418f0d7 | 171 | }, |
cdb34c93 | 172 | methods: { |
760adbce | 173 | roomInit: function() { |
41c80bb6 BA |
174 | // Notify the room only now that I connected, because |
175 | // messages might be lost otherwise (if game loading is slow) | |
71468011 BA |
176 | this.send("connect"); |
177 | this.send("pollclients"); | |
178 | }, | |
179 | send: function(code, obj) { | |
6808d7a1 BA |
180 | if (this.conn) { |
181 | this.conn.send(JSON.stringify(Object.assign({ code: code }, obj))); | |
51d87b52 | 182 | } |
5f131484 | 183 | }, |
050ae3b5 | 184 | isConnected: function(index) { |
29ced362 BA |
185 | const player = this.game.players[index]; |
186 | // Is it me ? | |
187 | if (this.st.user.sid == player.sid || this.st.user.id == player.uid) | |
050ae3b5 | 188 | return true; |
29ced362 | 189 | // Try to find a match in people: |
6808d7a1 BA |
190 | return ( |
191 | Object.keys(this.people).some(sid => sid == player.sid) || | |
192 | Object.values(this.people).some(p => p.id == player.uid) | |
193 | ); | |
050ae3b5 | 194 | }, |
cdb34c93 | 195 | socketMessageListener: function(msg) { |
6808d7a1 | 196 | if (!this.conn) return; |
a6088c90 | 197 | const data = JSON.parse(msg.data); |
6808d7a1 | 198 | switch (data.code) { |
5f131484 | 199 | case "pollclients": |
5f131484 | 200 | data.sockIds.forEach(sid => { |
6808d7a1 BA |
201 | this.$set(this.people, sid, { id: 0, name: "" }); |
202 | if (sid != this.st.user.sid) { | |
203 | this.send("askidentity", { target: sid }); | |
51d87b52 | 204 | // Ask potentially missed last state, if opponent and I play |
6808d7a1 BA |
205 | if ( |
206 | !!this.game.mycolor && | |
207 | this.game.type == "live" && | |
208 | this.game.score == "*" && | |
209 | this.game.players.some(p => p.sid == sid) | |
210 | ) { | |
211 | this.send("asklastate", { target: sid }); | |
51d87b52 BA |
212 | } |
213 | } | |
5f131484 BA |
214 | }); |
215 | break; | |
71468011 | 216 | case "connect": |
51d87b52 | 217 | if (!this.people[data.from]) |
6808d7a1 BA |
218 | this.$set(this.people, data.from, { name: "", id: 0 }); |
219 | if (!this.people[data.from].name) { | |
51d87b52 | 220 | this.newConnect[data.from] = true; //for self multi-connects tests |
6808d7a1 | 221 | this.send("askidentity", { target: data.from }); |
51d87b52 | 222 | } |
71468011 BA |
223 | break; |
224 | case "disconnect": | |
225 | this.$delete(this.people, data.from); | |
226 | break; | |
51d87b52 BA |
227 | case "killed": |
228 | // I logged in elsewhere: | |
229 | alert(this.st.tr["New connexion detected: tab now offline"]); | |
230 | // TODO: this fails. See https://github.com/websockets/ws/issues/489 | |
231 | //this.conn.removeEventListener("message", this.socketMessageListener); | |
232 | //this.conn.removeEventListener("close", this.socketCloseListener); | |
233 | //this.conn.close(); | |
234 | this.conn = null; | |
235 | break; | |
6808d7a1 | 236 | case "askidentity": { |
51d87b52 BA |
237 | // Request for identification (TODO: anonymous shouldn't need to reply) |
238 | const me = { | |
239 | // Decompose to avoid revealing email | |
240 | name: this.st.user.name, | |
241 | sid: this.st.user.sid, | |
6808d7a1 | 242 | id: this.st.user.id |
51d87b52 | 243 | }; |
6808d7a1 | 244 | this.send("identity", { data: me, target: data.from }); |
5f131484 | 245 | break; |
51d87b52 | 246 | } |
6808d7a1 | 247 | case "identity": { |
71468011 | 248 | const user = data.data; |
6808d7a1 | 249 | if (user.name) { |
51d87b52 | 250 | // If I multi-connect, kill current connexion if no mark (I'm older) |
6808d7a1 BA |
251 | if ( |
252 | this.newConnect[user.sid] && | |
253 | user.id > 0 && | |
254 | user.id == this.st.user.id && | |
255 | user.sid != this.st.user.sid | |
256 | ) { | |
257 | if (!this.killed[this.st.user.sid]) { | |
258 | this.send("killme", { sid: this.st.user.sid }); | |
51d87b52 BA |
259 | this.killed[this.st.user.sid] = true; |
260 | } | |
261 | } | |
6808d7a1 BA |
262 | if (user.sid != this.st.user.sid) { |
263 | //I already know my identity... | |
264 | this.$set(this.people, user.sid, { | |
265 | id: user.id, | |
266 | name: user.name | |
267 | }); | |
51d87b52 | 268 | } |
a0c41e7e | 269 | } |
51d87b52 | 270 | delete this.newConnect[user.sid]; |
a0c41e7e | 271 | break; |
71468011 BA |
272 | } |
273 | case "askgame": | |
274 | // Send current (live) game if not asked by any of the players | |
6808d7a1 BA |
275 | if ( |
276 | this.game.type == "live" && | |
277 | this.game.players.every(p => p.sid != data.from[0]) | |
278 | ) { | |
71468011 BA |
279 | const myGame = { |
280 | id: this.game.id, | |
281 | fen: this.game.fen, | |
282 | players: this.game.players, | |
283 | vid: this.game.vid, | |
284 | cadence: this.game.cadence, | |
285 | score: this.game.score, | |
6808d7a1 | 286 | rid: this.st.user.sid //useful in Hall if I'm an observer |
71468011 | 287 | }; |
6808d7a1 | 288 | this.send("game", { data: myGame, target: data.from }); |
71468011 BA |
289 | } |
290 | break; | |
291 | case "askfullgame": | |
6808d7a1 | 292 | this.send("fullgame", { data: this.game, target: data.from }); |
71468011 BA |
293 | break; |
294 | case "fullgame": | |
295 | // Callback "roomInit" to poll clients only after game is loaded | |
296 | this.loadGame(data.data, this.roomInit); | |
297 | break; | |
a0c41e7e | 298 | case "asklastate": |
a0c41e7e | 299 | // Sending last state if I played a move or score != "*" |
6808d7a1 BA |
300 | if ( |
301 | (this.game.moves.length > 0 && this.vr.turn != this.game.mycolor) || | |
302 | this.game.score != "*" || | |
303 | this.drawOffer == "sent" | |
304 | ) { | |
f41ce580 | 305 | // Send our "last state" informations to opponent |
411d23cd | 306 | const L = this.game.moves.length; |
6808d7a1 | 307 | const myIdx = ["w", "b"].indexOf(this.game.mycolor); |
71468011 BA |
308 | const myLastate = { |
309 | // NOTE: lastMove (when defined) includes addTime | |
6808d7a1 | 310 | lastMove: L > 0 ? this.game.moves[L - 1] : undefined, |
71468011 BA |
311 | // Since we played a move (or abort or resign), |
312 | // only drawOffer=="sent" is possible | |
313 | drawSent: this.drawOffer == "sent", | |
26f3a887 | 314 | score: this.game.score, |
71468011 | 315 | movesCount: L, |
6808d7a1 | 316 | initime: this.game.initime[1 - myIdx] //relevant only if I played |
5fd5fb22 | 317 | }; |
6808d7a1 | 318 | this.send("lastate", { data: myLastate, target: data.from }); |
5fd5fb22 | 319 | } |
c6788ecf | 320 | break; |
71468011 BA |
321 | case "lastate": //got opponent infos about last move |
322 | this.lastate = data.data; | |
6808d7a1 BA |
323 | if (this.game.rendered) |
324 | //game is rendered (Board component) | |
71468011 BA |
325 | this.processLastate(); |
326 | //else: will be processed when game is ready | |
327 | break; | |
6808d7a1 | 328 | case "newmove": { |
71468011 | 329 | const move = data.data; |
6808d7a1 BA |
330 | if (move.cancelDrawOffer) { |
331 | //opponent refuses draw | |
77c50966 | 332 | this.drawOffer = ""; |
c4f6d5a1 BA |
333 | // NOTE for corr games: drawOffer reset by player in turn |
334 | if (this.game.type == "live" && !!this.game.mycolor) | |
6808d7a1 | 335 | GameStorage.update(this.gameRef.id, { drawOffer: "" }); |
633959bf | 336 | } |
8477e53d | 337 | this.$refs["basegame"].play(move, "received"); |
a6088c90 | 338 | break; |
71468011 | 339 | } |
93d1d7a7 | 340 | case "resign": |
8477e53d BA |
341 | const score = data.side == "b" ? "1-0" : "0-1"; |
342 | const side = data.side == "w" ? "White" : "Black"; | |
343 | this.gameOver(score, side + " surrender"); | |
93d1d7a7 | 344 | break; |
93d1d7a7 | 345 | case "abort": |
8477e53d | 346 | this.gameOver("?", "Stop"); |
93d1d7a7 | 347 | break; |
2cc10cdb | 348 | case "draw": |
71468011 | 349 | this.gameOver("1/2", data.data); |
2cc10cdb BA |
350 | break; |
351 | case "drawoffer": | |
41c80bb6 BA |
352 | // NOTE: observers don't know who offered draw |
353 | this.drawOffer = "received"; | |
6d9f4315 | 354 | break; |
71468011 | 355 | case "newchat": |
bd76b456 | 356 | this.newChat = data.data; |
71468011 | 357 | if (!document.getElementById("modalChat").checked) |
2f258c37 | 358 | document.getElementById("chatBtn").classList.add("somethingnew"); |
a6088c90 BA |
359 | break; |
360 | } | |
cdb34c93 | 361 | }, |
51d87b52 BA |
362 | socketCloseListener: function() { |
363 | this.conn = new WebSocket(this.connexionString); | |
6808d7a1 BA |
364 | this.conn.addEventListener("message", this.socketMessageListener); |
365 | this.conn.addEventListener("close", this.socketCloseListener); | |
51d87b52 | 366 | }, |
760adbce BA |
367 | // lastate was received, but maybe game wasn't ready yet: |
368 | processLastate: function() { | |
369 | const data = this.lastate; | |
370 | this.lastate = undefined; //security... | |
371 | const L = this.game.moves.length; | |
6808d7a1 | 372 | if (data.movesCount > L) { |
760adbce | 373 | // Just got last move from him |
8477e53d | 374 | this.$refs["basegame"].play( |
6808d7a1 BA |
375 | Object.assign({ initime: data.initime }, data.lastMove) |
376 | ); | |
a0c41e7e | 377 | } |
6808d7a1 BA |
378 | if (data.drawSent) this.drawOffer = "received"; |
379 | if (data.score != "*") { | |
a0c41e7e | 380 | this.drawOffer = ""; |
6808d7a1 | 381 | if (this.game.score == "*") this.gameOver(data.score); |
760adbce BA |
382 | } |
383 | }, | |
dcd68c41 | 384 | clickDraw: function() { |
6808d7a1 BA |
385 | if (!this.game.mycolor) return; //I'm just spectator |
386 | if (["received", "threerep"].includes(this.drawOffer)) { | |
387 | if (!confirm(this.st.tr["Accept draw?"])) return; | |
388 | const message = | |
389 | this.drawOffer == "received" | |
390 | ? "Mutual agreement" | |
391 | : "Three repetitions"; | |
392 | this.send("draw", { data: message }); | |
77c50966 | 393 | this.gameOver("1/2", message); |
6808d7a1 BA |
394 | } else if (this.drawOffer == "") { |
395 | //no effect if drawOffer == "sent" | |
396 | if (this.game.mycolor != this.vr.turn) { | |
397 | alert(this.st.tr["Draw offer only in your turn"]); | |
6fba6e0c | 398 | return; |
6808d7a1 BA |
399 | } |
400 | if (!confirm(this.st.tr["Offer draw?"])) return; | |
760adbce | 401 | this.drawOffer = "sent"; |
71468011 | 402 | this.send("drawoffer"); |
6808d7a1 | 403 | GameStorage.update(this.gameRef.id, { drawOffer: this.game.mycolor }); |
a6088c90 BA |
404 | } |
405 | }, | |
7f3484bd | 406 | abortGame: function() { |
6808d7a1 | 407 | if (!this.game.mycolor || !confirm(this.st.tr["Terminate game?"])) return; |
8477e53d | 408 | this.gameOver("?", "Stop"); |
71468011 | 409 | this.send("abort"); |
a6088c90 | 410 | }, |
6808d7a1 | 411 | resign: function() { |
77c50966 | 412 | if (!this.game.mycolor || !confirm(this.st.tr["Resign the game?"])) |
a6088c90 | 413 | return; |
6808d7a1 | 414 | this.send("resign", { data: this.game.mycolor }); |
8477e53d BA |
415 | const score = this.game.mycolor == "w" ? "0-1" : "1-0"; |
416 | const side = this.game.mycolor == "w" ? "White" : "Black"; | |
417 | this.gameOver(score, side + " surrender"); | |
a6088c90 | 418 | }, |
967a2686 BA |
419 | // 3 cases for loading a game: |
420 | // - from indexedDB (running or completed live game I play) | |
b196f8ea BA |
421 | // - from server (one correspondance game I play[ed] or not) |
422 | // - from remote peer (one live game I don't play, finished or not) | |
760adbce | 423 | loadGame: function(game, callback) { |
6808d7a1 | 424 | const afterRetrieval = async game => { |
f41ce580 BA |
425 | const vModule = await import("@/variants/" + game.vname + ".js"); |
426 | window.V = vModule.VariantRules; | |
427 | this.vr = new V(game.fen); | |
6808d7a1 | 428 | const gtype = game.cadence.indexOf("d") >= 0 ? "corr" : "live"; |
71468011 | 429 | const tc = extractTime(game.cadence); |
9ef63965 BA |
430 | const myIdx = game.players.findIndex(p => { |
431 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
432 | }); | |
6808d7a1 BA |
433 | const mycolor = [undefined, "w", "b"][myIdx + 1]; //undefined for observers |
434 | if (!game.chats) game.chats = []; //live games don't have chat history | |
435 | if (gtype == "corr") { | |
436 | if (game.players[0].color == "b") { | |
f41ce580 | 437 | // Adopt the same convention for live and corr games: [0] = white |
6808d7a1 BA |
438 | [game.players[0], game.players[1]] = [ |
439 | game.players[1], | |
440 | game.players[0] | |
441 | ]; | |
f41ce580 | 442 | } |
8477e53d | 443 | // corr game: need to compute the clocks + initime |
7f3484bd | 444 | // NOTE: clocks in seconds, initime in milliseconds |
92a523d1 | 445 | game.clocks = [tc.mainTime, tc.mainTime]; |
6808d7a1 BA |
446 | game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of |
447 | if (game.score == "*") { | |
448 | //otherwise no need to bother with time | |
b7cbbda1 BA |
449 | game.initime = [0, 0]; |
450 | const L = game.moves.length; | |
6808d7a1 | 451 | if (L >= 3) { |
b7cbbda1 | 452 | let addTime = [0, 0]; |
6808d7a1 BA |
453 | for (let i = 2; i < L; i++) { |
454 | addTime[i % 2] += | |
455 | tc.increment - | |
456 | (game.moves[i].played - game.moves[i - 1].played) / 1000; | |
b7cbbda1 | 457 | } |
6808d7a1 | 458 | for (let i = 0; i <= 1; i++) game.clocks[i] += addTime[i]; |
5f131484 | 459 | } |
6808d7a1 | 460 | if (L >= 1) game.initime[L % 2] = game.moves[L - 1].played; |
92a523d1 | 461 | } |
9ef63965 | 462 | // Sort chat messages from newest to oldest |
6808d7a1 BA |
463 | game.chats.sort((c1, c2) => { |
464 | return c2.added - c1.added; | |
465 | }); | |
466 | if (myIdx >= 0 && game.chats.length > 0) { | |
8477e53d BA |
467 | // Did a chat message arrive after my last move? |
468 | let vr_tmp = new V(game.fen); //start from last position | |
bc50b249 | 469 | const flags = V.ParseFen(game.fen).flags; //may be undefined |
9ef63965 | 470 | let dtLastMove = 0; |
6808d7a1 | 471 | for (let midx = game.moves.length - 1; midx >= 0; midx--) { |
bc50b249 | 472 | vr_tmp.undo(Object.assign({flags:flags}, game.moves[midx].squares)); |
8477e53d | 473 | if (vr_tmp.turn == mycolor) { |
9ef63965 BA |
474 | dtLastMove = game.moves[midx].played; |
475 | break; | |
476 | } | |
477 | } | |
478 | if (dtLastMove < game.chats[0].added) | |
479 | document.getElementById("chatBtn").classList.add("somethingnew"); | |
480 | } | |
481 | // Now that we used idx and played, re-format moves as for live games | |
8477e53d | 482 | game.moves = game.moves.map(m => m.squares); |
c0b27606 | 483 | } |
6808d7a1 | 484 | if (gtype == "live" && game.clocks[0] < 0) { |
8477e53d | 485 | // Game is unstarted |
66d03f23 | 486 | game.clocks = [tc.mainTime, tc.mainTime]; |
6808d7a1 | 487 | if (game.score == "*") { |
b7cbbda1 | 488 | game.initime[0] = Date.now(); |
6808d7a1 | 489 | if (myIdx >= 0) { |
b7cbbda1 | 490 | // I play in this live game; corr games don't have clocks+initime |
6808d7a1 | 491 | GameStorage.update(game.id, { |
b7cbbda1 | 492 | clocks: game.clocks, |
6808d7a1 | 493 | initime: game.initime |
b7cbbda1 BA |
494 | }); |
495 | } | |
22efa391 | 496 | } |
66d03f23 | 497 | } |
6808d7a1 BA |
498 | if (game.drawOffer) { |
499 | if (game.drawOffer == "t") | |
8477e53d | 500 | // Three repetitions |
77c50966 | 501 | this.drawOffer = "threerep"; |
6808d7a1 | 502 | else { |
8477e53d | 503 | // Draw offered by any of the players: |
6808d7a1 | 504 | if (myIdx < 0) this.drawOffer = "received"; |
6808d7a1 | 505 | else { |
77c50966 | 506 | // I play in this game: |
6808d7a1 BA |
507 | if ( |
508 | (game.drawOffer == "w" && myIdx == 0) || | |
509 | (game.drawOffer == "b" && myIdx == 1) | |
510 | ) | |
77c50966 | 511 | this.drawOffer = "sent"; |
6808d7a1 | 512 | else this.drawOffer = "received"; |
77c50966 BA |
513 | } |
514 | } | |
515 | } | |
725da57f BA |
516 | this.repeat = {}; //reset: scan past moves' FEN: |
517 | let repIdx = 0; | |
518 | // NOTE: vr_tmp to obtain FEN strings is redundant with BaseGame | |
519 | let vr_tmp = new V(game.fenStart); | |
520 | let movesCount = -1; | |
521 | let curTurn = "n"; | |
522 | game.moves.forEach(m => { | |
523 | if (vr_tmp.turn != curTurn) | |
524 | { | |
525 | movesCount++; | |
526 | curTurn = vr_tmp.turn; | |
527 | } | |
528 | vr_tmp.play(m); | |
529 | const fenObj = V.ParseFen(vr_tmp.getFen()); | |
530 | repIdx = fenObj.position + "_" + fenObj.turn; | |
531 | if (fenObj.flags) repIdx += "_" + fenObj.flags; | |
532 | this.repeat[repIdx] = this.repeat[repIdx] | |
533 | ? this.repeat[repIdx] + 1 | |
534 | : 1; | |
535 | }); | |
536 | if (vr_tmp.turn != curTurn) | |
537 | movesCount++; | |
538 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; | |
6808d7a1 BA |
539 | this.game = Object.assign( |
540 | {}, | |
4b0384fa | 541 | game, |
cf742aaf | 542 | // NOTE: assign mycolor here, since BaseGame could also be VS computer |
6fba6e0c | 543 | { |
c0b27606 | 544 | type: gtype, |
66d03f23 | 545 | increment: tc.increment, |
9ef63965 | 546 | mycolor: mycolor, |
5f131484 BA |
547 | // opponent sid not strictly required (or available), but easier |
548 | // at least oppsid or oppid is available anyway: | |
6808d7a1 | 549 | oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid, |
725da57f BA |
550 | oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].uid, |
551 | movesCount: movesCount, | |
6fba6e0c | 552 | } |
4b0384fa | 553 | ); |
9ef63965 | 554 | this.re_setClocks(); |
a0c41e7e BA |
555 | this.$nextTick(() => { |
556 | this.game.rendered = true; | |
557 | // Did lastate arrive before game was rendered? | |
6808d7a1 | 558 | if (this.lastate) this.processLastate(); |
a0c41e7e | 559 | }); |
6808d7a1 | 560 | if (callback) callback(); |
967a2686 | 561 | }; |
6808d7a1 BA |
562 | if (game) { |
563 | afterRetrieval(game); | |
564 | return; | |
967a2686 | 565 | } |
6808d7a1 BA |
566 | if (this.gameRef.rid) { |
567 | // Remote live game: forgetting about callback func... (TODO: design) | |
568 | this.send("askfullgame", { target: this.gameRef.rid }); | |
569 | } else { | |
f41ce580 | 570 | // Local or corr game |
8477e53d | 571 | // NOTE: afterRetrieval() is never called if game not found |
11667c79 | 572 | GameStorage.get(this.gameRef.id, afterRetrieval); |
967a2686 | 573 | } |
a6088c90 | 574 | }, |
9ef63965 | 575 | re_setClocks: function() { |
725da57f | 576 | if (this.game.movesCount < 2 || this.game.score != "*") { |
9ef63965 BA |
577 | // 1st move not completed yet, or game over: freeze time |
578 | this.virtualClocks = this.game.clocks.map(s => ppt(s)); | |
579 | return; | |
580 | } | |
581 | const currentTurn = this.vr.turn; | |
8477e53d | 582 | const currentMovesCount = this.game.moves.length; |
6808d7a1 BA |
583 | const colorIdx = ["w", "b"].indexOf(currentTurn); |
584 | let countdown = | |
585 | this.game.clocks[colorIdx] - | |
586 | (Date.now() - this.game.initime[colorIdx]) / 1000; | |
587 | this.virtualClocks = [0, 1].map(i => { | |
588 | const removeTime = | |
589 | i == colorIdx ? (Date.now() - this.game.initime[colorIdx]) / 1000 : 0; | |
9ef63965 BA |
590 | return ppt(this.game.clocks[i] - removeTime); |
591 | }); | |
592 | let clockUpdate = setInterval(() => { | |
6808d7a1 BA |
593 | if ( |
594 | countdown < 0 || | |
8477e53d | 595 | this.game.moves.length > currentMovesCount || |
6808d7a1 BA |
596 | this.game.score != "*" |
597 | ) { | |
9ef63965 BA |
598 | clearInterval(clockUpdate); |
599 | if (countdown < 0) | |
6808d7a1 | 600 | this.gameOver( |
8477e53d | 601 | currentTurn == "w" ? "0-1" : "1-0", |
6808d7a1 BA |
602 | this.st.tr["Time"] |
603 | ); | |
604 | } else | |
605 | this.$set( | |
606 | this.virtualClocks, | |
607 | colorIdx, | |
608 | ppt(Math.max(0, --countdown)) | |
609 | ); | |
9ef63965 BA |
610 | }, 1000); |
611 | }, | |
8477e53d | 612 | // Post-process a (potentially partial) move (which was just played in BaseGame) |
ce87ac6a | 613 | processMove: function(move) { |
6808d7a1 BA |
614 | if (this.game.type == "corr" && move.color == this.game.mycolor) { |
615 | if ( | |
616 | !confirm( | |
617 | this.st.tr["Move played:"] + | |
618 | " " + | |
619 | move.notation + | |
620 | "\n" + | |
621 | this.st.tr["Are you sure?"] | |
622 | ) | |
623 | ) { | |
8477e53d | 624 | this.$refs["basegame"].undo(move); |
6808d7a1 | 625 | return; |
9ef63965 | 626 | } |
89021f18 | 627 | } |
6808d7a1 BA |
628 | const colorIdx = ["w", "b"].indexOf(move.color); |
629 | const nextIdx = ["w", "b"].indexOf(this.vr.turn); | |
9d54ab89 | 630 | // https://stackoverflow.com/a/38750895 |
6808d7a1 | 631 | if (this.game.mycolor) { |
a1c48034 | 632 | const allowed_fields = ["appear", "vanish", "start", "end"]; |
8477e53d | 633 | // NOTE: 'var' to see that variable outside this block |
a1c48034 BA |
634 | var filtered_move = Object.keys(move) |
635 | .filter(key => allowed_fields.includes(key)) | |
636 | .reduce((obj, key) => { | |
637 | obj[key] = move[key]; | |
638 | return obj; | |
639 | }, {}); | |
640 | } | |
dc284d90 | 641 | // Send move ("newmove" event) to people in the room (if our turn) |
dce792f6 | 642 | let addTime = 0; |
6808d7a1 BA |
643 | if (move.color == this.game.mycolor) { |
644 | if (this.drawOffer == "received") | |
725da57f | 645 | // I refuse draw |
77c50966 | 646 | this.drawOffer = ""; |
725da57f | 647 | if (this.game.movesCount >= 2) { |
dce792f6 BA |
648 | const elapsed = Date.now() - this.game.initime[colorIdx]; |
649 | // elapsed time is measured in milliseconds | |
6808d7a1 | 650 | addTime = this.game.increment - elapsed / 1000; |
dce792f6 | 651 | } |
6808d7a1 BA |
652 | const sendMove = Object.assign({}, filtered_move, { |
653 | addTime: addTime, | |
654 | cancelDrawOffer: this.drawOffer == "" | |
655 | }); | |
656 | this.send("newmove", { data: sendMove }); | |
a0c41e7e BA |
657 | // (Add)Time indication: useful in case of lastate infos requested |
658 | move.addTime = addTime; | |
6808d7a1 | 659 | } else addTime = move.addTime; //supposed transmitted |
77c50966 | 660 | // Update current game object: |
725da57f BA |
661 | if (nextIdx != colorIdx) |
662 | this.game.movesCount++; | |
77c50966 BA |
663 | this.game.moves.push(move); |
664 | this.game.fen = move.fen; | |
9ef63965 | 665 | this.game.clocks[colorIdx] += addTime; |
a0c41e7e BA |
666 | // move.initime is set only when I receive a "lastate" move from opponent |
667 | this.game.initime[nextIdx] = move.initime || Date.now(); | |
725da57f | 668 | this.re_setClocks(); |
77c50966 BA |
669 | // If repetition detected, consider that a draw offer was received: |
670 | const fenObj = V.ParseFen(move.fen); | |
671 | let repIdx = fenObj.position + "_" + fenObj.turn; | |
6808d7a1 BA |
672 | if (fenObj.flags) repIdx += "_" + fenObj.flags; |
673 | this.repeat[repIdx] = this.repeat[repIdx] ? this.repeat[repIdx] + 1 : 1; | |
674 | if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep"; | |
675 | else if (this.drawOffer == "threerep") this.drawOffer = ""; | |
6d68309a BA |
676 | // Since corr games are stored at only one location, update should be |
677 | // done only by one player for each move: | |
6808d7a1 | 678 | if ( |
8477e53d | 679 | this.game.mycolor && |
6808d7a1 BA |
680 | (this.game.type == "live" || move.color == this.game.mycolor) |
681 | ) { | |
77c50966 | 682 | let drawCode = ""; |
6808d7a1 | 683 | switch (this.drawOffer) { |
77c50966 BA |
684 | case "threerep": |
685 | drawCode = "t"; | |
686 | break; | |
687 | case "sent": | |
688 | drawCode = this.game.mycolor; | |
689 | break; | |
690 | case "received": | |
691 | drawCode = this.vr.turn; | |
692 | break; | |
693 | } | |
6808d7a1 BA |
694 | if (this.game.type == "corr") { |
695 | GameStorage.update(this.gameRef.id, { | |
e69f159d | 696 | fen: move.fen, |
6808d7a1 | 697 | move: { |
e69f159d | 698 | squares: filtered_move, |
a0c41e7e | 699 | played: Date.now(), |
6808d7a1 | 700 | idx: this.game.moves.length - 1 |
e69f159d | 701 | }, |
8477e53d BA |
702 | // Code "n" for "None" to force reset (otherwise it's ignored) |
703 | drawOffer: drawCode || "n" | |
e69f159d | 704 | }); |
8477e53d | 705 | } |
6808d7a1 | 706 | else { |
8477e53d | 707 | // Live game: |
6808d7a1 | 708 | GameStorage.update(this.gameRef.id, { |
e69f159d BA |
709 | fen: move.fen, |
710 | move: filtered_move, | |
77c50966 BA |
711 | clocks: this.game.clocks, |
712 | initime: this.game.initime, | |
6808d7a1 | 713 | drawOffer: drawCode |
e69f159d BA |
714 | }); |
715 | } | |
6d68309a | 716 | } |
b4fb1612 | 717 | }, |
bfe9a135 BA |
718 | resetChatColor: function() { |
719 | // TODO: this is called twice, once on opening an once on closing | |
2f258c37 | 720 | document.getElementById("chatBtn").classList.remove("somethingnew"); |
a1c48034 | 721 | }, |
ac8f441c | 722 | processChat: function(chat) { |
6808d7a1 | 723 | this.send("newchat", { data: chat }); |
0e16cb26 BA |
724 | // NOTE: anonymous chats in corr games are not stored on server (TODO?) |
725 | if (this.game.type == "corr" && this.st.user.id > 0) | |
6808d7a1 | 726 | GameStorage.update(this.gameRef.id, { chat: chat }); |
63ca2b89 | 727 | }, |
430a2038 | 728 | gameOver: function(score, scoreMsg) { |
430a2038 | 729 | this.game.score = score; |
8477e53d | 730 | this.$set(this.game, "scoreMsg", scoreMsg || getScoreMessage(score)); |
ab6f48ea BA |
731 | const myIdx = this.game.players.findIndex(p => { |
732 | return p.sid == this.st.user.sid || p.uid == this.st.user.id; | |
733 | }); | |
6808d7a1 | 734 | if (myIdx >= 0) { |
8477e53d | 735 | // OK, I play in this game |
6808d7a1 BA |
736 | GameStorage.update(this.gameRef.id, { |
737 | score: score, | |
738 | scoreMsg: scoreMsg | |
739 | }); | |
48ab808f | 740 | // Notify the score to main Hall. TODO: only one player (currently double send) |
6808d7a1 | 741 | this.send("result", { gid: this.game.id, score: score }); |
dcd68c41 | 742 | } |
6808d7a1 BA |
743 | } |
744 | } | |
a6088c90 BA |
745 | }; |
746 | </script> | |
7e1a1fe9 | 747 | |
41c80bb6 | 748 | <style lang="sass" scoped> |
72ccbd67 | 749 | .connected |
050ae3b5 | 750 | background-color: lightgreen |
72ccbd67 | 751 | |
ed06d9e9 BA |
752 | #participants |
753 | margin-left: 5px | |
754 | ||
755 | .anonymous | |
756 | color: grey | |
757 | font-style: italic | |
758 | ||
ec905cbc BA |
759 | #playersInfo > p |
760 | margin: 0 | |
761 | ||
430a2038 BA |
762 | @media screen and (min-width: 768px) |
763 | #actions | |
764 | width: 300px | |
765 | @media screen and (max-width: 767px) | |
766 | .game | |
767 | width: 100% | |
72ccbd67 | 768 | |
430a2038 | 769 | #actions |
cf94b843 | 770 | display: inline-block |
1d6d7b1d | 771 | margin: 0 |
430a2038 BA |
772 | button |
773 | display: inline-block | |
430a2038 | 774 | margin: 0 |
a1c48034 | 775 | |
050ae3b5 BA |
776 | @media screen and (max-width: 767px) |
777 | #aboveBoard | |
778 | text-align: center | |
885d93a7 BA |
779 | @media screen and (min-width: 768px) |
780 | #aboveBoard | |
781 | margin-left: 30% | |
050ae3b5 | 782 | |
2f258c37 BA |
783 | .variant-cadence |
784 | padding-right: 10px | |
785 | ||
786 | .variant-name | |
8c5f5390 | 787 | font-weight: bold |
77c50966 | 788 | padding-right: 10px |
77c50966 | 789 | |
050ae3b5 BA |
790 | .name |
791 | font-size: 1.5rem | |
792 | padding: 1px | |
793 | ||
794 | .time | |
795 | font-size: 2rem | |
796 | display: inline-block | |
797 | margin-left: 10px | |
798 | ||
799 | .split-names | |
800 | display: inline-block | |
801 | margin: 0 15px | |
802 | ||
430a2038 | 803 | #chat |
a1c48034 | 804 | padding-top: 20px |
a154d45e | 805 | max-width: 767px |
430a2038 | 806 | border: none; |
cf94b843 BA |
807 | |
808 | #chatBtn | |
809 | margin: 0 10px 0 0 | |
dcd68c41 BA |
810 | |
811 | .draw-sent, .draw-sent:hover | |
812 | background-color: lightyellow | |
813 | ||
814 | .draw-received, .draw-received:hover | |
815 | background-color: lightgreen | |
816 | ||
817 | .draw-threerep, .draw-threerep:hover | |
818 | background-color: #e4d1fc | |
2f258c37 BA |
819 | |
820 | .somethingnew | |
821 | background-color: #c5fefe | |
7e1a1fe9 | 822 | </style> |