+ const gtype = game.cadence.indexOf("d") >= 0 ? "corr" : "live";
+ const tc = extractTime(game.cadence);
+ const myIdx = game.players.findIndex(p => {
+ return p.sid == this.st.user.sid || p.uid == this.st.user.id;
+ });
+ 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: need 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;
+ }
+ // Sort chat messages from newest to oldest
+ game.chats.sort((c1, c2) => {
+ return c2.added - c1.added;
+ });
+ if (myIdx >= 0 && game.score == "*" && game.chats.length > 0) {
+ // Did a chat message arrive after my last move?
+ let vr_tmp = new V(game.fen); //start from last position
+ const flags = V.ParseFen(game.fen).flags; //may be undefined
+ let dtLastMove = 0;
+ for (let midx = game.moves.length - 1; midx >= 0; midx--) {
+ // NOTE: flags could be wrong, but since our only concern is turn,
+ // this should be enough. (TODO?)
+ vr_tmp.undo(Object.assign({flags:JSON.stringify(flags)}, game.moves[midx].squares));
+ if (vr_tmp.turn == 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 = game.moves.map(m => m.squares);
+ }
+ if (gtype == "live" && game.clocks[0] < 0) {
+ // Game is 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 {
+ // Draw offered by any of the players:
+ if (myIdx < 0) this.drawOffer = "received";
+ else {
+ // I play in this game:
+ if (
+ (game.drawOffer == "w" && myIdx == 0) ||
+ (game.drawOffer == "b" && myIdx == 1)
+ )
+ this.drawOffer = "sent";
+ else this.drawOffer = "received";
+ }
+ }
+ }
+ 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);
+ let movesCount = -1;
+ let curTurn = "n";
+ game.moves.forEach(m => {
+ if (vr_tmp.turn != curTurn)
+ {
+ movesCount++;
+ curTurn = vr_tmp.turn;
+ }
+ 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 (vr_tmp.turn != curTurn)
+ movesCount++;
+ if (this.repeat[repIdx] >= 3) this.drawOffer = "threerep";
+ this.game = Object.assign(
+ {},