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