+ const gtype = (game.timeControl.indexOf('d') >= 0 ? "corr" : "live");
+ const tc = extractTime(game.timeControl);
+ 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;
+ if (game.drawOffer)
+ this.drawOffer = "received";
+ }
+ // Now that we used idx and played, re-format moves as for live games
+ game.moves = game.moves.map( (m) => {
+ const s = m.squares;
+ return {
+ appear: s.appear,
+ vanish: s.vanish,
+ start: s.start,
+ end: s.end,
+ };
+ });
+ // Also sort chat messages (if any)
+ game.chats.sort( (c1,c2) => { return c2.added - c1.added; });
+ }
+ const myIdx = game.players.findIndex(p => {
+ return p.sid == this.st.user.sid || p.uid == this.st.user.id;
+ });
+ 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,
+ });
+ }
+ }
+ }