Some more cleaning + fixes
[vchess.git] / server / sockets.js
index ba14df9..1cc47ae 100644 (file)
@@ -152,15 +152,22 @@ module.exports = function(wss) {
         case "askfullgame":
         {
           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][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;
         }
 
@@ -186,6 +193,11 @@ module.exports = function(wss) {
           notifyRoom(page, obj.code, {data:obj.data});
           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,
         // but target is fully identified (sid + tmpId)
         case "challenge":
@@ -201,7 +213,7 @@ module.exports = function(wss) {
       }
     };
     const closeListener = () => {
-      // For tab or browser closing:
+      // For browser or tab closing (including page reload):
       doDisconnect();
     };
     // Update clients object: add new connexion