Acceptable state, but still issues when a lot of moves arrive quickly on several...
[vchess.git] / server / sockets.js
index 3854ca2..406effe 100644 (file)
@@ -14,7 +14,7 @@ function getJsonFromUrl(url) {
 
 // Helper to safe-send some message through a (web-)socket:
 function send(socket, message) {
-  if (socket && socket.readyState == 1)
+  if (!!socket && socket.readyState == 1)
     socket.send(JSON.stringify(message));
 }
 
@@ -145,7 +145,7 @@ module.exports = function(wss) {
         case "askfullgame": {
           const pg = obj.page || page; //required for askidentity and askgame
           // In cas askfullgame to wrong SID for example, would crash:
-          if (clients[pg] && 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
@@ -178,7 +178,6 @@ module.exports = function(wss) {
         case "newchallenge":
         case "newgame":
         case "deletechallenge":
-        case "newmove":
         case "resign":
         case "abort":
         case "drawoffer":
@@ -186,6 +185,34 @@ module.exports = function(wss) {
           notifyRoom(page, obj.code, {data: obj.data});
           break;
 
+        case "newmove": {
+          const dataWithFrom = {from: [sid,tmpId], data: obj.data};
+          // Special case re-send newmove only to opponent:
+          if (!!obj.target && !!clients[page][obj.target]) {
+            Object.keys(clients[page][obj.target]).forEach(x => {
+              send(
+                clients[page][obj.target][x],
+                Object.assign({code: "newmove"}, dataWithFrom)
+              );
+            });
+          } else {
+            // NOTE: data.from is useful only to opponent
+            notifyRoom(page, "newmove", dataWithFrom);
+          }
+          break;
+        }
+        case "gotmove":
+          if (
+            !!clients[page][obj.target[0]] &&
+            !!clients[page][obj.target[0]][obj.target[1]]
+          ) {
+            send(
+              clients[page][obj.target[0]][obj.target[1]],
+              {code: "gotmove", data: obj.data}
+            );
+          }
+          break;
+
         case "result":
           // Special case: notify all, 'transroom': Game --> Hall
           notifyRoom("/", "result", {gid: obj.gid, score: obj.score});
@@ -222,8 +249,8 @@ module.exports = function(wss) {
         {
           const pg = obj.target[2] || page; //required for identity and game
           // 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]])
+          // but leaving Hall, clients[pg] or clients[pg][target] could be undefined
+          if (!!clients[pg] && !!clients[pg][obj.target[0]])
             send(clients[pg][obj.target[0]][obj.target[1]], {code:obj.code, data:obj.data});
           break;
         }