+ const mycolor = [undefined, "w", "b"][myIdx + 1]; //undefined for observers
+ if (!game.chats) game.chats = []; //live games don't have chat history
+ if (gtype == "corr") {
+ if (game.players[0].color == "b") {
+ // Adopt the same convention for live and corr games: [0] = white
+ [game.players[0], game.players[1]] = [
+ game.players[1],
+ game.players[0]
+ ];
+ }
+ // corr game: needs to compute the clocks + initime
+ // NOTE: clocks in seconds, initime in milliseconds
+ game.clocks = [tc.mainTime, tc.mainTime];
+ game.moves.sort((m1, m2) => m1.idx - m2.idx); //in case of
+ if (game.score == "*") {
+ //otherwise no need to bother with time
+ game.initime = [0, 0];
+ const L = game.moves.length;
+ if (L >= 3) {
+ let addTime = [0, 0];
+ for (let i = 2; i < L; i++) {
+ addTime[i % 2] +=
+ tc.increment -
+ (game.moves[i].played - game.moves[i - 1].played) / 1000;
+ }
+ for (let i = 0; i <= 1; i++) game.clocks[i] += addTime[i];
+ }
+ if (L >= 1) game.initime[L % 2] = game.moves[L - 1].played;
+ }
+ const reformattedMoves = game.moves.map(m => {
+ const s = m.squares;
+ return {
+ appear: s.appear,
+ vanish: s.vanish,
+ start: s.start,
+ end: s.end
+ };
+ });
+ // Sort chat messages from newest to oldest
+ game.chats.sort((c1, c2) => {
+ return c2.added - c1.added;
+ });
+ if (myIdx >= 0 && game.chats.length > 0) {
+ // TODO: group multi-moves into an array, to deduce color from index
+ // and not need this (also repeated in BaseGame::re_setVariables())
+ let vr_tmp = new V(game.fenStart); //vr is already at end of game
+ for (let i = 0; i < reformattedMoves.length; i++) {
+ game.moves[i].color = vr_tmp.turn;
+ vr_tmp.play(reformattedMoves[i]);
+ }
+ // Blue background on chat button if last chat message arrived after my last move.
+ let dtLastMove = 0;
+ for (let midx = game.moves.length - 1; midx >= 0; midx--) {
+ if (game.moves[midx].color == mycolor) {
+ dtLastMove = game.moves[midx].played;
+ break;
+ }
+ }
+ if (dtLastMove < game.chats[0].added)
+ document.getElementById("chatBtn").classList.add("somethingnew");
+ }
+ // Now that we used idx and played, re-format moves as for live games
+ game.moves = reformattedMoves;
+ }
+ if (gtype == "live" && game.clocks[0] < 0) {
+ //game unstarted
+ game.clocks = [tc.mainTime, tc.mainTime];
+ if (game.score == "*") {
+ game.initime[0] = Date.now();
+ if (myIdx >= 0) {
+ // I play in this live game; corr games don't have clocks+initime
+ GameStorage.update(game.id, {
+ clocks: game.clocks,
+ initime: game.initime
+ });
+ }
+ }
+ }
+ if (game.drawOffer) {
+ if (game.drawOffer == "t")
+ //three repetitions
+ this.drawOffer = "threerep";
+ else {
+ if (myIdx < 0) this.drawOffer = "received";
+ //by any of the players
+ else {
+ // I play in this game:
+ if (
+ (game.drawOffer == "w" && myIdx == 0) ||
+ (game.drawOffer == "b" && myIdx == 1)
+ )
+ this.drawOffer = "sent";
+ //all other cases
+ else this.drawOffer = "received";
+ }
+ }
+ }
+ if (game.scoreMsg) game.scoreMsg = this.st.tr[game.scoreMsg]; //stored in english
+ this.game = Object.assign(
+ {},
+ game,
+ // NOTE: assign mycolor here, since BaseGame could also be VS computer
+ {
+ type: gtype,
+ increment: tc.increment,
+ mycolor: mycolor,
+ // opponent sid not strictly required (or available), but easier
+ // at least oppsid or oppid is available anyway:
+ oppsid: myIdx < 0 ? undefined : game.players[1 - myIdx].sid,
+ oppid: myIdx < 0 ? undefined : game.players[1 - myIdx].uid
+ }
+ );
+ this.re_setClocks();
+ this.$nextTick(() => {
+ this.game.rendered = true;
+ // Did lastate arrive before game was rendered?
+ if (this.lastate) this.processLastate();
+ });
+ this.repeat = {}; //reset: scan past moves' FEN:
+ let repIdx = 0;
+ // NOTE: vr_tmp to obtain FEN strings is redundant with BaseGame
+ let vr_tmp = new V(game.fenStart);
+ game.moves.forEach(m => {
+ vr_tmp.play(m);
+ const fenObj = V.ParseFen(vr_tmp.getFen());
+ repIdx = fenObj.position + "_" + fenObj.turn;
+ if (fenObj.flags) repIdx += "_" + fenObj.flags;
+ this.repeat[repIdx] = this.repeat[repIdx]
+ ? this.repeat[repIdx] + 1
+ : 1;
+ });
+ if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep";
+ if (callback) callback();
+ };
+ if (game) {
+ afterRetrieval(game);
+ return;
+ }
+ if (this.gameRef.rid) {
+ // Remote live game: forgetting about callback func... (TODO: design)
+ this.send("askfullgame", { target: this.gameRef.rid });
+ } else {
+ // Local or corr game
+ GameStorage.get(this.gameRef.id, afterRetrieval);
+ }
+ },
+ re_setClocks: function() {
+ if (this.game.moves.length < 2 || this.game.score != "*") {
+ // 1st move not completed yet, or game over: freeze time
+ this.virtualClocks = this.game.clocks.map(s => ppt(s));
+ return;
+ }
+ const currentTurn = this.vr.turn;
+ const colorIdx = ["w", "b"].indexOf(currentTurn);
+ let countdown =
+ this.game.clocks[colorIdx] -
+ (Date.now() - this.game.initime[colorIdx]) / 1000;
+ this.virtualClocks = [0, 1].map(i => {
+ const removeTime =
+ i == colorIdx ? (Date.now() - this.game.initime[colorIdx]) / 1000 : 0;
+ return ppt(this.game.clocks[i] - removeTime);