X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fsockets.js;h=4907965d041a38575ee7a2959b547b9fe9dac2ab;hb=059228c9fd737361dc97de69811daed5abbd6254;hp=7395af15109135b102a4bb9e99159321d7ad64bd;hpb=856f995c28ad1e23bec4e606b94a3f8446109323;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index 7395af15..4907965d 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -24,29 +24,21 @@ module.exports = function(wss) { // or "/mygames" for Mygames page (simpler: no 'people' array). // tmpId is required if a same user (browser) has different tabs let clients = {}; + let sidToPages = {}; + let idToSid = {}; wss.on("connection", (socket, req) => { const query = getJsonFromUrl(req.url); const sid = query["sid"]; + 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) @@ -60,14 +52,22 @@ module.exports = function(wss) { delete clients[page][sid][tmpId]; if (Object.keys(clients[page][sid]).length == 0) { delete clients[page][sid]; + const pgIndex = sidToPages[sid].findIndex(pg => pg == page); + sidToPages[sid].splice(pgIndex, 1); if (Object.keys(clients[page]).length == 0) delete clients[page]; + // Am I totally offline? + if (sidToPages[sid].length == 0) { + delete sidToPages[sid]; + delete idToSid[id]; + } } }; const doDisconnect = () => { deleteConnexion(); - if (!clients[page] || !clients[page][sid]) { + // Nothing to notify when disconnecting from MyGames page: + if (page != "/mygames" && (!clients[page] || !clients[page][sid])) { // I effectively disconnected from this page: notifyRoom(page, "disconnect"); if (page.indexOf("/game/") >= 0) @@ -139,7 +139,7 @@ module.exports = function(wss) { }); // NOTE: a "gamer" could also just be an observer Object.keys(clients).forEach(p => { - if (p != "/") { + if (p.indexOf("/game/") >= 0) { Object.keys(clients[p]).forEach(k => { // 'page' indicator is needed for gamers if (k != sid) sockIds.push({ sid:k, page:p }); @@ -154,7 +154,7 @@ module.exports = function(wss) { // but the requested resource can be from any tmpId (except current!) case "askidentity": case "asklastate": - case "askchallenge": + case "askchallenges": case "askgame": case "askfullgame": { const pg = obj.page || page; //required for askidentity and askgame @@ -190,24 +190,23 @@ module.exports = function(wss) { // Notify all room: mostly game events case "newchat": case "newchallenge": - case "deletechallenge": + case "deletechallenge_s": case "newgame": case "resign": case "abort": 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, obj.oppsid]); - 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": { @@ -243,26 +242,6 @@ module.exports = function(wss) { notifyRoom("/", "result", { gid: obj.gid, score: obj.score }); break; - case "mconnect": - // Special case: notify some game rooms that - // I'm watching game state from MyGames - // TODO: this code is ignored for now - obj.gids.forEach(gid => { - const pg = "/game/" + gid; - Object.keys(clients[pg]).forEach(s => { - Object.keys(clients[pg][s]).forEach(x => { - send( - clients[pg][s][x].socket, - { code: "mconnect", from: sid } - ); - }); - }); - }); - break; - case "mdisconnect": - // TODO - // Also TODO: pass newgame to MyGames, and gameover (result) - break; case "mabort": { const gamePg = "/game/" + obj.gid; if (!!clients[gamePg] && !!clients[gamePg][obj.target]) { @@ -276,19 +255,37 @@ module.exports = function(wss) { break; } + case "notifyscore": + case "notifyturn": + case "notifynewgame": + if (!!clients["/mygames"]) { + obj.targets.forEach(t => { + const k = t.sid || idToSid[t.id]; + if (!!clients["/mygames"][k]) { + Object.keys(clients["/mygames"][k]).forEach(x => { + send( + clients["/mygames"][k][x].socket, + { code: obj.code, data: obj.data } + ); + }); + } + }); + } + break; + 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; // Passing, relaying something: from isn't needed, // but target is fully identified (sid + tmpId) - case "challenge": + case "challenges": case "fullgame": case "game": case "identity": @@ -319,6 +316,11 @@ module.exports = function(wss) { clients[page][sid] = { [tmpId]: newElt }; else clients[page][sid][tmpId] = newElt; + // Also update helper correspondances + if (!idToSid[id]) idToSid[id] = sid; + if (!sidToPages[sid]) sidToPages[sid] = []; + const pgIndex = sidToPages[sid].findIndex(pg => pg == page); + if (pgIndex === -1) sidToPages[sid].push(page); socket.on("message", messageListener); socket.on("close", closeListener); });