- 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)
- {
- 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,
- },
- 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]),
- });
+ const currentTurn = this.vr.turn;
+ const currentMovesCount = this.game.moves.length;
+ const colorIdx = ["w", "b"].indexOf(currentTurn);
+ let countdown =
+ this.game.clocks[colorIdx] -
+ (Date.now() - this.game.initime[colorIdx]) / 1000;
+ this.virtualClocks = [0, 1].map(i => {
+ const removeTime =
+ i == colorIdx ? (Date.now() - this.game.initime[colorIdx]) / 1000 : 0;
+ return ppt(this.game.clocks[i] - removeTime);
+ });
+ let clockUpdate = setInterval(() => {
+ if (
+ countdown < 0 ||
+ this.game.moves.length > currentMovesCount ||
+ this.game.score != "*"
+ ) {
+ clearInterval(clockUpdate);
+ if (countdown < 0)
+ this.gameOver(
+ currentTurn == "w" ? "0-1" : "1-0",
+ "Time"
+ );
+ } else
+ this.$set(
+ this.virtualClocks,
+ colorIdx,
+ ppt(Math.max(0, --countdown))
+ );
+ }, 1000);
+ },
+ // Post-process a (potentially partial) move (which was just played in BaseGame)
+ processMove: function(move, data) {
+ const moveCol = this.vr.turn;
+ const doProcessMove = () => {
+ const colorIdx = ["w", "b"].indexOf(moveCol);
+ const nextIdx = 1 - colorIdx;
+ if (this.game.mycolor) {
+ // NOTE: 'var' to see that variable outside this block
+ var filtered_move = getFilteredMove(move);
+ }
+ // Send move ("newmove" event) to people in the room (if our turn)
+ let addTime = data ? data.addTime : 0;
+ if (moveCol == this.game.mycolor) {
+ if (this.drawOffer == "received")
+ // I refuse draw
+ this.drawOffer = "";
+ if (this.game.movesCount >= 2) {
+ const elapsed = Date.now() - this.game.initime[colorIdx];
+ // elapsed time is measured in milliseconds
+ addTime = this.game.increment - elapsed / 1000;
+ }
+ const sendMove = {
+ move: filtered_move,
+ addTime: addTime,
+ cancelDrawOffer: this.drawOffer == "",
+ // Players' SID required for /mygames page
+ // TODO: precompute and add this field to game object?
+ players: this.game.players.map(p => p.sid)
+ };
+ this.send("newmove", { data: sendMove });
+ }
+ // Update current game object (no need for moves stack):
+ playMove(move, this.vr);
+ this.game.movesCount++;
+ // (add)Time indication: useful in case of lastate infos requested
+ this.game.moves.push({move:move, addTime:addTime});
+ this.game.fen = this.vr.getFen();
+ this.game.clocks[colorIdx] += addTime;
+ // data.initime is set only when I receive a "lastate" move from opponent
+ this.game.initime[nextIdx] = (data && data.initime) ? data.initime : Date.now();
+ this.re_setClocks();
+ // If repetition detected, consider that a draw offer was received:
+ const fenObj = V.ParseFen(this.game.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" || moveCol == this.game.mycolor)
+ ) {
+ let drawCode = "";
+ switch (this.drawOffer) {
+ case "threerep":
+ drawCode = "t";
+ break;
+ case "sent":
+ drawCode = this.game.mycolor;
+ break;
+ case "received":
+ drawCode = V.GetOppCol(this.game.mycolor);
+ break;
+ }
+ if (this.game.type == "corr") {
+ GameStorage.update(this.gameRef.id, {
+ fen: this.game.fen,
+ move: {
+ squares: filtered_move,
+ played: Date.now(),
+ idx: this.game.moves.length - 1
+ },
+ // Code "n" for "None" to force reset (otherwise it's ignored)
+ drawOffer: drawCode || "n"
+ });
+ }
+ else {
+ // Live game:
+ GameStorage.update(this.gameRef.id, {
+ fen: this.game.fen,
+ move: filtered_move,
+ clocks: this.game.clocks,
+ initime: this.game.initime,
+ drawOffer: drawCode
+ });
+ }
+ }
+ };
+ if (this.game.type == "corr" && moveCol == this.game.mycolor) {
+ setTimeout(() => {
+ if (
+ !confirm(
+ this.st.tr["Move played:"] +
+ " " +
+ getFullNotation(move) +
+ "\n" +
+ this.st.tr["Are you sure?"]
+ )
+ ) {
+ this.$refs["basegame"].cancelLastMove();
+ return;
+ }
+ doProcessMove();
+ // Let small time to finish drawing current move attempt:
+ }, 500);