- }
- const colorIdx = ["w","b"].indexOf(move.color);
- const nextIdx = ["w","b"].indexOf(this.vr.turn);
- // 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.drawOffer == "received") //I refuse draw
- this.drawOffer = "";
- 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;
- }
- const sendMove = Object.assign({},
- filtered_move,
- {
- addTime: addTime,
- cancelDrawOffer: this.drawOffer=="",
- });
- this.send("newmove", {data: sendMove});
- // (Add)Time indication: useful in case of lastate infos requested
- move.addTime = addTime;
- }
- else
- addTime = move.addTime; //supposed transmitted
- // Update current game object:
- this.game.moves.push(move);
- this.game.fen = move.fen;
- this.game.clocks[colorIdx] += addTime;
- // move.initime is set only when I receive a "lastate" move from opponent
- this.game.initime[nextIdx] = move.initime || Date.now();
- this.re_setClocks();
- // 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";
- else if (this.drawOffer == "threerep")
- this.drawOffer = "";
- // 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))
- {
- let drawCode = "";
- switch (this.drawOffer)
- {
- case "threerep":
- drawCode = "t";
- break;
- case "sent":
- drawCode = this.game.mycolor;
- break;
- case "received":
- drawCode = this.vr.turn;
- break;
- }
- if (this.game.type == "corr")
- {
- GameStorage.update(this.gameRef.id,
- {
- fen: move.fen,
- move:
- {
- squares: filtered_move,
- played: Date.now(),
- idx: this.game.moves.length - 1,
- },
- drawOffer: drawCode || "n", //"n" for "None" to force reset (otherwise it's ignored)
- });
- }
- else //live
- {
- GameStorage.update(this.gameRef.id,
- {
- fen: move.fen,