Show 'resize board' button even in no-information variants
[vchess.git] / server / sockets.js
index 1cc47ae..206c780 100644 (file)
@@ -153,7 +153,7 @@ module.exports = function(wss) {
         {
           const pg = obj.page || page; //required for askidentity and askgame
           // In cas askfullgame to wrong SID for example, would crash:
-          if (!!clients[pg][obj.target])
+          if (clients[pg] && clients[pg][obj.target])
           {
             const tmpIds = Object.keys(clients[pg][obj.target]);
             if (obj.target == sid) //targetting myself
@@ -190,8 +190,28 @@ module.exports = function(wss) {
         case "abort":
         case "drawoffer":
         case "draw":
+        {
           notifyRoom(page, obj.code, {data:obj.data});
+          const mygamesPg = "/mygames";
+          if (obj.code == "newmove" && clients[mygamesPg])
+          {
+            // Relay newmove info to myGames page
+            // NOTE: the move itself is not needed (for now at least)
+            const gid = page.split("/")[2]; //format is "/game/gid"
+            obj.data.players.forEach(pSid => {
+              if (clients[mygamesPg][pSid])
+              {
+                Object.keys(clients[mygamesPg][pSid]).forEach(x => {
+                  send(
+                    clients[mygamesPg][pSid][x],
+                    {code:"newmove", gid:gid}
+                  );
+                });
+              }
+            });
+          }
           break;
+        }
 
         case "result":
           // Special case: notify all, 'transroom': Game --> Hall
@@ -207,7 +227,10 @@ module.exports = function(wss) {
         case "lastate":
         {
           const pg = obj.target[2] || page; //required for identity and game
-          send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data});
+          // NOTE: if in game we ask identity to opponent still in Hall,
+          // but leaving Hall, clients[pg] or clients[pg][target] could be ndefined
+          if (clients[pg] && clients[pg][obj.target[0]])
+            send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data});
           break;
         }
       }