X-Git-Url: https://git.auder.net/assets/current/git-favicon.png?a=blobdiff_plain;f=server%2Fsockets.js;h=cf5ea1b82bbc558f9f87c651f4c16c607b91b48e;hb=db1f1f9adb920605c7a16b060a7737e54636ee08;hp=7f0d14ec90b0efad45e18c865001cd56e59cc156;hpb=092de30647eeb223b148a170615b4a715865fd7f;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index 7f0d14ec..cf5ea1b8 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -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] && 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,34 @@ 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 newmoveForMygames = { + 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", data:newmoveForMygames} + ); + }); + } + }); + } + 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,7 +229,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; } }