X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fsockets.js;h=f904311cbcf5b92e08f62faa220597437616e165;hb=94b800a43378bb245531dc502d6248954ab74380;hp=c8f3c7f80bd8fae9cbd3cb3eefcc6ca5df077bc8;hpb=28b32b4fc7c23b1c72bed68e1897576c5be46c3d;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index c8f3c7f8..f904311c 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) @@ -166,22 +155,42 @@ module.exports = function(wss) { case "askidentity": case "asklastate": case "askchallenges": - case "askgame": - case "askfullgame": { + case "askgame": { 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]) { - const tmpIds = Object.keys(clients[pg][obj.target]); + let 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]].socket, - { code: obj.code, from: [sid,tmpId,page] } - ); + if (tmpIds.length > 0) { + const ttmpId = tmpIds[Math.floor(Math.random() * tmpIds.length)]; + send( + clients[pg][obj.target][ttmpId].socket, + { code: obj.code, from: [sid,tmpId,page] } + ); + } + } + break; + } + + // Special situation of the previous "case": + // Full game can be asked to any observer. + case "askfullgame": { + if (!!clients[page]) { + let sids = Object.keys(clients[page]).filter(k => k != sid); + if (sids.length > 0) { + // Pick a SID at random in this set, and ask full game: + const rid = sids[Math.floor(Math.random() * sids.length)]; + // ..to a random tmpId: + const tmpIds = Object.keys(clients[page][rid]); + const rtmpId = tmpIds[Math.floor(Math.random() * tmpIds.length)]; + send( + clients[page][rid][rtmpId].socket, + { code: "askfullgame", from: [sid,tmpId] } + ); + } } break; } @@ -208,17 +217,16 @@ module.exports = function(wss) { case "drawoffer": case "rematchoffer": case "draw": - if (!!obj.oppsid) - // "newgame" message from Hall: do not target players - notifyAllBut(page, "newgame", {data: obj.data}, [sid, obj.oppsid]); - else notifyRoom(page, obj.code, {data: obj.data}); + notifyRoom(page, obj.code, {data: obj.data}, obj.excluded); break; case "rnewgame": - // A rematch game started: players are already informed - notifyAllBut(page, "newgame", {data: obj.data}, [sid]); - notifyAllBut("/", "newgame", {data: obj.data}, [sid, obj.oppsid]); - notifyRoom("/mygames", "newgame", {data: obj.data}); + // A rematch game started: + 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": { @@ -272,7 +280,7 @@ module.exports = function(wss) { case "notifynewgame": if (!!clients["/mygames"]) { obj.targets.forEach(t => { - const k = t.sid || idToSid[t.uid]; + const k = t.sid || idToSid[t.id]; if (!!clients["/mygames"][k]) { Object.keys(clients["/mygames"][k]).forEach(x => { send( @@ -287,11 +295,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;