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