+ if (!this.game.mycolor)
+ return; //I'm just an observer
+ // Update storage (corr or live)
+ const colorIdx = ["w","b","g","r"].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)
+ // (otherwise move.elapsed is supposed to be already transmitted)
+ let addTime = undefined;
+ if (move.color == this.game.mycolor)
+ {
+ const elapsed = Date.now() - GameStorage.getInitime();
+ this.game.players.forEach(p => {
+ if (p.sid != this.st.user.sid)
+ {
+ this.st.conn.send(JSON.stringify({
+ code: "newmove",
+ target: p.sid,
+ move: Object.assign({}, filtered_move, {elapsed: elapsed}),
+ }));
+ }
+ });
+ move.elapsed = elapsed;
+ // elapsed time is measured in milliseconds
+ addTime = this.game.increment - elapsed/1000;
+ }
+ GameStorage.update({
+ colorIdx: colorIdx,
+ move: filtered_move,
+ fen: move.fen,
+ addTime: addTime,
+ initime: (this.vr.turn == this.game.mycolor), //my turn now?
+ });
+ },
+ // TODO: this update function should also work for corr games
+ gameOver: function(score) {
+ this.game.mode = "analyze";
+ GameStorage.update({
+ score: score,
+ });