- loadGame: function(gid) {
- // TODO: ask game to remote peer if this.remoteId is set
- // (or just if game not found locally)
- // NOTE: if it's a corr game, ask it from server
- const game = getGameFromStorage(gid); //, this.gameRef.uid); //uid may be blank
- this.opponent.id = game.oppid; //opponent ID in case of running HH game
- this.opponent.name = game.oppname; //maye be blank (if anonymous)
- this.score = game.score;
- this.mycolor = game.mycolor;
- this.fenStart = game.fenStart;
- this.moves = game.moves;
- this.cursor = game.moves.length-1;
- this.lastMove = (game.moves.length > 0 ? game.moves[this.cursor] : null);
+ // Post-process a move (which was just played)
+ processMove: function(move) {
+ if (!this.game.mycolor)
+ return; //I'm just an observer
+ // Update storage (corr or live)
+ const colorIdx = ["w","b"].indexOf(move.color);
+ // https://stackoverflow.com/a/38750895
+ const allowed_fields = ["appear", "vanish", "start", "end"];
+ const 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 opponent(s) (if ours)
+ let addTime = undefined;
+ if (move.color == this.game.mycolor)
+ {
+ const elapsed = Date.now() - this.game.initime[colorIdx];
+ // elapsed time is measured in milliseconds
+ addTime = this.game.increment - elapsed/1000;
+ this.st.conn.send(JSON.stringify({
+ code: "newmove",
+ target: this.game.oppid,
+ move: Object.assign({}, filtered_move, {addTime: addTime}),
+ }));
+ }
+ else
+ addTime = move.addTime; //supposed transmitted
+ const nextIdx = ["w","b"].indexOf(this.vr.turn);
+ GameStorage.update(this.gameRef.id,
+ {
+ colorIdx: colorIdx,
+ nextIdx: nextIdx,
+ move: filtered_move,
+ fen: move.fen,
+ addTime: addTime,
+ });
+ // Also update current game object:
+ this.game.moves.push(move);
+ this.game.fen = move.fen;
+ //TODO: just this.game.clocks[colorIdx] += (!!addTime ? addTime : 0);
+ this.$set(this.game.clocks, colorIdx,
+ this.game.clocks[colorIdx] + (!!addTime ? addTime : 0));
+ this.game.initime[nextIdx] = Date.now();