Fixes. TODO: autofocus on forms, and understand why email autofill in name field
[vchess.git] / server / sockets.js
index da748f0..206c780 100644 (file)
@@ -52,6 +52,16 @@ module.exports = function(wss) {
           delete clients[page];
       }
     };
+    const doDisconnect = () => {
+      deleteConnexion();
+      if (!clients[page] || !clients[page][sid])
+      {
+        // I effectively disconnected from this page:
+        notifyRoom(page, "disconnect");
+        if (page.indexOf("/game/") >= 0)
+          notifyRoom("/", "gdisconnect", {page:page});
+      }
+    };
     const messageListener = (objtxt) => {
       let obj = JSON.parse(objtxt);
       switch (obj.code)
@@ -68,14 +78,7 @@ module.exports = function(wss) {
         }
         case "disconnect":
           // When page changes:
-          deleteConnexion();
-          if (!clients[page] || !clients[page][sid])
-          {
-            // I effectively disconnected from this page:
-            notifyRoom(page, "disconnect");
-            if (page.indexOf("/game/") >= 0)
-              notifyRoom("/", "gdisconnect", {page:page});
-          }
+          doDisconnect();
           break;
         case "killme":
         {
@@ -148,19 +151,23 @@ module.exports = function(wss) {
         case "askgame":
         case "askfullgame":
         {
-          // DEBUG:
-          //console.log(sid + " " + page + " " + obj.code + " " + obj.target + " " + obj.page);
-          //console.log(clients);
           const pg = obj.page || page; //required for askidentity and askgame
-          const tmpIds = Object.keys(clients[pg][obj.target]);
-          if (obj.target == sid) //targetting myself
+          // In cas askfullgame to wrong SID for example, would crash:
+          if (clients[pg] && clients[pg][obj.target])
           {
-            const idx_myTmpid = tmpIds.findIndex(x => x == tmpId);
-            if (idx_myTmpid >= 0)
-              tmpIds.splice(idx_myTmpid, 1);
+            const tmpIds = Object.keys(clients[pg][obj.target]);
+            if (obj.target == sid) //targetting myself
+            {
+              const idx_myTmpid = tmpIds.findIndex(x => x == tmpId);
+              if (idx_myTmpid >= 0)
+                tmpIds.splice(idx_myTmpid, 1);
+            }
+            const tmpId_idx = Math.floor(Math.random() * tmpIds.length);
+            send(
+              clients[pg][obj.target][tmpIds[tmpId_idx]],
+              {code:obj.code, from:[sid,tmpId,page]}
+            );
           }
-          const tmpId_idx = Math.floor(Math.random() * tmpIds.length);
-          send(clients[pg][obj.target][tmpIds[tmpId_idx]], {code:obj.code, from:[sid,tmpId,page]});
           break;
         }
 
@@ -183,7 +190,32 @@ 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
+          notifyRoom("/", "result", {gid:obj.gid, score:obj.score});
           break;
 
         // Passing, relaying something: from isn't needed,
@@ -195,14 +227,17 @@ 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;
         }
       }
     };
     const closeListener = () => {
-      // For tab or browser closing:
-      deleteConnexion();
+      // For browser or tab closing (including page reload):
+      doDisconnect();
     };
     // Update clients object: add new connexion
     if (!clients[page])