Fixes
[vchess.git] / client / src / views / Game.vue
index 608ac71..8f57153 100644 (file)
@@ -152,21 +152,25 @@ 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);
       switch (data.code)
       {
         case "duplicate":
+          this.st.conn.send(JSON.stringify({code:"duplicate",
+            page:"/game/" + this.game.id}));
           alert(this.st.tr["Warning: multi-tabs not supported"]);
           break;
         // 0.2] Receive clients list (just socket IDs)
         case "pollclients":
-        {
           data.sockIds.forEach(sid => {
             if (!!this.people[sid])
               return;
@@ -175,9 +179,7 @@ export default {
             this.st.conn.send(JSON.stringify({code:"askidentity", target:sid}));
           });
           break;
-        }
         case "askidentity":
-        {
           // Request for identification: reply if I'm not anonymous
           if (this.st.user.id > 0)
           {
@@ -191,9 +193,7 @@ export default {
               target:data.from}));
           }
           break;
-        }
         case "identity":
-        {
           this.$set(this.people, data.user.sid,
             {id: data.user.id, name: data.user.name});
           // Ask potentially missed last state, if opponent and I play
@@ -204,12 +204,10 @@ export default {
             this.st.conn.send(JSON.stringify({code:"asklastate", target:data.user.sid}));
           }
           break;
-        }
         case "asklastate":
-        {
           // 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;
@@ -231,7 +229,6 @@ export default {
             }));
           }
           break;
-        }
         case "askgame":
           // Send current (live) game if I play in (not an observer),
           // and not asked by opponent (!)
@@ -254,7 +251,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":
@@ -263,13 +265,11 @@ export default {
             document.getElementById("chatBtn").style.backgroundColor = "#c5fefe";
           break;
         case "lastate": //got opponent infos about last move
-        {
           this.lastate = data.state;
           if (this.game.rendered) //game is rendered (Board component)
             this.processLastate();
           //else: will be processed when game is ready
           break;
-        }
         case "resign":
           this.gameOver(data.side=="b" ? "1-0" : "0-1", "Resign");
           break;
@@ -292,11 +292,9 @@ export default {
           this.loadGame(data.game, this.roomInit);
           break;
         case "connect":
-        {
           this.$set(this.people, data.from, {name:"", id:0});
           this.st.conn.send(JSON.stringify({code:"askidentity", target:data.from}));
           break;
-        }
         case "disconnect":
           this.$delete(this.people, data.from);
           break;
@@ -310,11 +308,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 = "";
@@ -555,7 +552,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)
           {
@@ -563,7 +565,6 @@ export default {
               code: "newmove",
               target: sid,
               move: sendMove,
-              cancelDrawOffer: this.drawOffer=="",
             }));
           }
         });
@@ -619,7 +620,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