Fix draw logic for corr games
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 7 Feb 2020 12:00:14 +0000 (13:00 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 7 Feb 2020 12:00:14 +0000 (13:00 +0100)
client/src/views/Game.vue
server/models/Game.js

index 608ac71..799f5ab 100644 (file)
@@ -209,7 +209,7 @@ export default {
         {
           // Sending last state if I played a move or score != "*"
           if ((this.game.moves.length > 0 && this.vr.turn != this.game.mycolor)
         {
           // Sending last state if I played a move or score != "*"
           if ((this.game.moves.length > 0 && this.vr.turn != this.game.mycolor)
-              || this.game.score != "*")
+              || this.game.score != "*" || this.drawOffer == "sent")
           {
             // Send our "last state" informations to opponent
             const L = this.game.moves.length;
           {
             // Send our "last state" informations to opponent
             const L = this.game.moves.length;
@@ -254,7 +254,11 @@ export default {
           break;
         case "newmove":
           if (!!data.move.cancelDrawOffer) //opponent refuses draw
           break;
         case "newmove":
           if (!!data.move.cancelDrawOffer) //opponent refuses draw
+          {
             this.drawOffer = "";
             this.drawOffer = "";
+            if (this.game.type == "live") //corr games: reset by player who played
+              GameStorage.update(this.gameRef.id, {drawOffer: ""});
+          }
           this.$set(this.game, "moveToPlay", data.move);
           break;
         case "newchat":
           this.$set(this.game, "moveToPlay", data.move);
           break;
         case "newchat":
@@ -310,11 +314,10 @@ export default {
       if (data.movesCount > L)
       {
         // Just got last move from him
       if (data.movesCount > L)
       {
         // Just got last move from him
-        const myIdx = ["w","b"].indexOf(this.game.mycolor);
-        if (!!data.drawSent)
-          this.drawOffer = "received";
         this.$set(this.game, "moveToPlay", Object.assign({}, data.lastMove, {initime: data.initime}));
       }
         this.$set(this.game, "moveToPlay", Object.assign({}, data.lastMove, {initime: data.initime}));
       }
+      if (data.drawSent)
+        this.drawOffer = "received";
       if (data.score != "*")
       {
         this.drawOffer = "";
       if (data.score != "*")
       {
         this.drawOffer = "";
@@ -555,7 +558,12 @@ export default {
           // elapsed time is measured in milliseconds
           addTime = this.game.increment - elapsed/1000;
         }
           // elapsed time is measured in milliseconds
           addTime = this.game.increment - elapsed/1000;
         }
-        let sendMove = Object.assign({}, filtered_move, {addTime: addTime});
+        const sendMove = Object.assign({},
+          filtered_move,
+          {
+            addTime: addTime,
+            cancelDrawOffer: this.drawOffer=="",
+          });
         Object.keys(this.people).forEach(sid => {
           if (sid != this.st.user.sid)
           {
         Object.keys(this.people).forEach(sid => {
           if (sid != this.st.user.sid)
           {
@@ -563,7 +571,6 @@ export default {
               code: "newmove",
               target: sid,
               move: sendMove,
               code: "newmove",
               target: sid,
               move: sendMove,
-              cancelDrawOffer: this.drawOffer=="",
             }));
           }
         });
             }));
           }
         });
@@ -619,7 +626,7 @@ export default {
               played: Date.now(),
               idx: this.game.moves.length - 1,
             },
               played: Date.now(),
               idx: this.game.moves.length - 1,
             },
-            drawOffer: drawCode,
+            drawOffer: drawCode || "n", //"n" for "None" to force reset (otherwise it's ignored)
           });
         }
         else //live
           });
         }
         else //live
index 017d195..d57aac8 100644 (file)
@@ -81,7 +81,7 @@ const GameModel =
         // NOTE: g.scoreMsg can be NULL
         // (in this case score = "*" and no reason to look at it)
         "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
         // NOTE: g.scoreMsg can be NULL
         // (in this case score = "*" and no reason to look at it)
         "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
-          "g.scoreMsg, v.name AS vname " +
+          "g.scoreMsg, g.drawOffer, v.name AS vname " +
         "FROM Games g " +
         "JOIN Variants v " +
         "  ON g.vid = v.id " +
         "FROM Games g " +
         "JOIN Variants v " +
         "  ON g.vid = v.id " +
@@ -178,6 +178,8 @@ const GameModel =
       if (!obj.move.idx.toString().match(/^[0-9]+$/))
         return "Wrong move index";
     }
       if (!obj.move.idx.toString().match(/^[0-9]+$/))
         return "Wrong move index";
     }
+    if (!!obj.drawOffer && !obj.drawOffer.match(/^[wbtn]$/))
+      return "Wrong draw offer format";
     if (!!obj.fen && !obj.fen.match(/^[a-zA-Z0-9, /-]*$/))
       return "Wrong FEN string";
     if (!!obj.score && !obj.score.match(/^[012?*\/-]+$/))
     if (!!obj.fen && !obj.fen.match(/^[a-zA-Z0-9, /-]*$/))
       return "Wrong FEN string";
     if (!!obj.score && !obj.score.match(/^[012?*\/-]+$/))
@@ -199,10 +201,14 @@ const GameModel =
       let modifs = "";
       if (!!obj.message)
         modifs += "message = message || ' ' || '" + obj.message + "',";
       let modifs = "";
       if (!!obj.message)
         modifs += "message = message || ' ' || '" + obj.message + "',";
-      // NOTE: if drawOffer is true, we should check that it's player's turn
+      // NOTE: if drawOffer is set, we should check that it's player's turn
       // A bit overcomplicated. Let's trust the client on that for now...
       if (!!obj.drawOffer)
       // A bit overcomplicated. Let's trust the client on that for now...
       if (!!obj.drawOffer)
-        modifs += "drawOffer = " + obj.drawOffer + ",";
+      {
+        if (obj.drawOffer == "n") //Special "None" update
+          obj.drawOffer = "";
+        modifs += "drawOffer = '" + obj.drawOffer + "',";
+      }
       if (!!obj.fen)
         modifs += "fen = '" + obj.fen + "',";
       if (!!obj.score)
       if (!!obj.fen)
         modifs += "fen = '" + obj.fen + "',";
       if (!!obj.score)