+ 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 = 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});
+ if (this.game.type == "corr")
+ sendMove.message = this.corrMsg;
+ const oppsid = this.getOppSid();
+ if (!!oppsid)
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "newmove",
+ target: oppsid,
+ move: sendMove,
+ }));
+ }
+ if (this.game.type == "corr" && this.corrMsg != "")
+ {
+ // Add message to last move in BaseGame:
+ // TODO: not very good style...
+ this.$refs["basegame"].setCurrentMessage(this.corrMsg);
+ }
+ }
+ 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.type == "live" || move.color == this.game.mycolor)
+ {
+ if (this.game.type == "corr")
+ {
+ GameStorage.update(this.gameRef.id,
+ {
+ fen: move.fen,
+ move:
+ {
+ squares: filtered_move,
+ message: this.corrMsg,
+ 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: just this.game.clocks[colorIdx] += addTime;
+ this.$set(this.game.clocks, colorIdx, this.game.clocks[colorIdx] + addTime);
+ this.game.initime[nextIdx] = Date.now();
+ // Finally reset curMoveMessage if needed
+ if (this.game.type == "corr" && move.color == this.game.mycolor)
+ this.corrMsg = "";
+ },
+ gameOver: function(score) {
+ this.game.mode = "analyze";
+ this.game.score = score;
+ GameStorage.update(this.gameRef.id, { score: score });