+ loadGame: function(game, callback) {
+ const afterRetrieval = async (game) => {
+ const vModule = await import("@/variants/" + game.vname + ".js");
+ window.V = vModule.VariantRules;
+ this.vr = new V(game.fen);
+ 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,
+ });
+ }
+ }
+ }
+ this.game = Object.assign({},
+ game,
+ // NOTE: assign mycolor here, since BaseGame could also be VS computer
+ {
+ type: gtype,
+ increment: tc.increment,
+ mycolor: [undefined,"w","b"][myIdx+1],
+ // 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.repeat = {}; //reset
+ if (!!this.lastate) //lastate arrived before game was loaded:
+ this.processLastate();
+ callback();
+ };
+ if (!!game)
+ return afterRetrieval(game);
+ if (!!this.gameRef.rid)
+ {
+ // Remote live game: forgetting about callback func... (TODO: design)
+ this.st.conn.send(JSON.stringify(
+ {code:"askfullgame", target:this.gameRef.rid}));
+ }
+ else
+ {
+ // Local or corr game
+ GameStorage.get(this.gameRef.id, afterRetrieval);
+ }
+ },
+ // Post-process a move (which was just played)
+ processMove: function(move) {
+ // Update storage (corr or live) if I play in the game
+ const colorIdx = ["w","b"].indexOf(move.color);
+ // https://stackoverflow.com/a/38750895
+ if (!!this.game.mycolor)
+ {
+ const allowed_fields = ["appear", "vanish", "start", "end"];
+ // NOTE: 'var' to see this variable outside this block
+ var filtered_move = Object.keys(move)
+ .filter(key => allowed_fields.includes(key))
+ .reduce((obj, key) => {
+ obj[key] = move[key];
+ return obj;
+ }, {});
+ }
+ // Send move ("newmove" event) to people in the room (if our turn)
+ let addTime = 0;
+ if (move.color == this.game.mycolor)
+ {
+ if (this.game.moves.length >= 2) //after first move
+ {
+ const elapsed = Date.now() - this.game.initime[colorIdx];
+ // elapsed time is measured in milliseconds
+ addTime = this.game.increment - elapsed/1000;
+ }
+ let sendMove = Object.assign({}, filtered_move, {addTime: addTime});
+ Object.keys(this.people).forEach(sid => {
+ if (sid != this.st.user.sid)
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "newmove",
+ target: sid,
+ move: sendMove,
+ }));
+ }
+ });
+ }
+ else
+ addTime = move.addTime; //supposed transmitted
+ const nextIdx = ["w","b"].indexOf(this.vr.turn);
+ // Since corr games are stored at only one location, update should be
+ // done only by one player for each move:
+ if (!!this.game.mycolor &&
+ (this.game.type == "live" || move.color == this.game.mycolor))
+ {
+ if (this.game.type == "corr")
+ {
+ GameStorage.update(this.gameRef.id,
+ {
+ fen: move.fen,
+ move:
+ {
+ squares: filtered_move,
+ played: Date.now(), //TODO: on server?
+ idx: this.game.moves.length,
+ },
+ });
+ }
+ else //live
+ {
+ GameStorage.update(this.gameRef.id,
+ {
+ fen: move.fen,
+ move: filtered_move,
+ clocks: this.game.clocks.map((t,i) => i==colorIdx
+ ? this.game.clocks[i] + addTime
+ : this.game.clocks[i]),
+ initime: this.game.initime.map((t,i) => i==nextIdx
+ ? Date.now()
+ : this.game.initime[i]),
+ });
+ }
+ }
+ // Also update current game object:
+ this.game.moves.push(move);
+ this.game.fen = move.fen;
+ //TODO: (Vue3) just this.game.clocks[colorIdx] += addTime;
+ this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime);
+ this.game.initime[nextIdx] = Date.now();
+ // If repetition detected, consider that a draw offer was received:
+ const fenObj = V.ParseFen(move.fen);
+ let 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";