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