X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fsockets.js;h=406effeeea9553837274ddec62f784c7a5afeee5;hb=dcff8e82dd8bbc285d9c2d5d5e3b361a9ecfa9ac;hp=3854ca2f533ccc764d90bd6a25e17e0f52af66f7;hpb=1611a25f25911da6a95fc0095b31c4b096a6638e;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index 3854ca2f..406effee 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -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; }