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