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