Fixes
[vchess.git] / client / src / views / Game.vue
index 35cec3a..0072bd4 100644 (file)
@@ -152,10 +152,13 @@ export default {
       this.st.conn.send(JSON.stringify({code:"pollclients"}));
     },
     isConnected: function(index) {
-      const name = this.game.players[index].name;
-      if (this.st.user.name == name)
+      const player = this.game.players[index];
+      // Is it me ?
+      if (this.st.user.sid == player.sid || this.st.user.id == player.uid)
         return true;
-      return Object.values(this.people).some(p => p.name == name);
+      // Try to find a match in people:
+      return Object.keys(this.people).some(sid => sid == player.sid) ||
+        Object.values(this.people).some(p => p.id == player.uid);
     },
     socketMessageListener: function(msg) {
       const data = JSON.parse(msg.data);
@@ -209,7 +212,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)
-              || this.game.score != "*")
+              || this.game.score != "*" || this.drawOffer == "sent")
           {
             // Send our "last state" informations to opponent
             const L = this.game.moves.length;
@@ -246,6 +249,7 @@ export default {
               players: this.game.players,
               vid: this.game.vid,
               timeControl: this.game.timeControl,
+              score: this.game.score,
             };
             this.st.conn.send(JSON.stringify({code:"game",
               game:myGame, target:data.from}));
@@ -253,7 +257,12 @@ export default {
           break;
         case "newmove":
           if (!!data.move.cancelDrawOffer) //opponent refuses draw
+          {
             this.drawOffer = "";
+            // NOTE for corr games: drawOffer reset by player in turn
+            if (this.game.type == "live" && !!this.game.mycolor)
+              GameStorage.update(this.gameRef.id, {drawOffer: ""});
+          }
           this.$set(this.game, "moveToPlay", data.move);
           break;
         case "newchat":
@@ -309,11 +318,10 @@ export default {
       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}));
       }
+      if (data.drawSent)
+        this.drawOffer = "received";
       if (data.score != "*")
       {
         this.drawOffer = "";
@@ -554,7 +562,12 @@ export default {
           // 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)
           {
@@ -562,7 +575,6 @@ export default {
               code: "newmove",
               target: sid,
               move: sendMove,
-              cancelDrawOffer: this.drawOffer=="",
             }));
           }
         });
@@ -618,7 +630,7 @@ export default {
               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