X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=4907965d041a38575ee7a2959b547b9fe9dac2ab;hp=7de7abb94638189e3d0cc0c7f306e96d9f5ab2cb;hb=f14572c4a22425033735253eabbaa2d8dbb53d05;hpb=0234201fb338fc239d6f613c677fa932c7c3697c diff --git a/server/sockets.js b/server/sockets.js index 7de7abb9..4907965d 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -32,24 +32,13 @@ module.exports = function(wss) { const id = query["id"]; const tmpId = query["tmpId"]; const page = query["page"]; - const notifyRoom = (page,code,obj={}) => { - if (!clients[page]) return; - Object.keys(clients[page]).forEach(k => { - Object.keys(clients[page][k]).forEach(x => { - if (k == sid && x == tmpId) return; - send( - clients[page][k][x].socket, - Object.assign({ code: code, from: sid }, obj) - ); - }); - }); - }; - // For focus events: no need to target self - const notifyAllBut = (page,code,obj={},except) => { + const notifyRoom = (page, code, obj={}, except) => { if (!clients[page]) return; + except = except || []; Object.keys(clients[page]).forEach(k => { if (except.includes(k)) return; Object.keys(clients[page][k]).forEach(x => { + if (k == sid && x == tmpId) return; send( clients[page][k][x].socket, Object.assign({ code: code, from: sid }, obj) @@ -208,14 +197,16 @@ module.exports = function(wss) { case "drawoffer": case "rematchoffer": case "draw": - notifyRoom(page, obj.code, {data: obj.data}); + notifyRoom(page, obj.code, {data: obj.data}, obj.excluded); break; case "rnewgame": // A rematch game started: - // NOTE: no need to explicitely notify Hall: the game will be sent - notifyAllBut(page, "newgame", {data: obj.data}, [sid]); - notifyRoom("/mygames", "newgame", {data: obj.data}); + notifyRoom(page, "newgame", {data: obj.data}); + // Explicitely notify Hall if gametype == corr. + // Live games will be polled from Hall after gconnect event. + if (obj.data.cadence.indexOf('d') >= 0) + notifyRoom("/", "newgame", {data: obj.data}); break; case "newmove": { @@ -284,11 +275,11 @@ module.exports = function(wss) { case "getfocus": case "losefocus": - if (page == "/") notifyAllBut("/", obj.code, { page: "/" }, [sid]); + if (page == "/") notifyRoom("/", obj.code, { page: "/" }, [sid]); else { // Notify game room + Hall: - notifyAllBut(page, obj.code, {}, [sid]); - notifyAllBut("/", obj.code, { page: page }, [sid]); + notifyRoom(page, obj.code, {}, [sid]); + notifyRoom("/", obj.code, { page: page }, [sid]); } break;