X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fsockets.js;h=7de7abb94638189e3d0cc0c7f306e96d9f5ab2cb;hb=0234201fb338fc239d6f613c677fa932c7c3697c;hp=467036e816a49143db297bee5dbd4c38e59f2d4a;hpb=e01e086d96f3c0f04761d269e6a34ba0b6014a56;p=vchess.git diff --git a/server/sockets.js b/server/sockets.js index 467036e8..7de7abb9 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -21,11 +21,15 @@ function send(socket, message) { module.exports = function(wss) { // Associative array page --> sid --> tmpId --> socket // "page" is either "/" for hall or "/game/some_gid" for Game, + // 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={}) => { @@ -34,7 +38,20 @@ module.exports = function(wss) { Object.keys(clients[page][k]).forEach(x => { if (k == sid && x == tmpId) return; send( - clients[page][k][x], + 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) => { + if (!clients[page]) return; + Object.keys(clients[page]).forEach(k => { + if (except.includes(k)) return; + Object.keys(clients[page][k]).forEach(x => { + send( + clients[page][k][x].socket, Object.assign({ code: code, from: sid }, obj) ); }); @@ -46,14 +63,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) @@ -80,7 +105,7 @@ module.exports = function(wss) { // Self multi-connect: manual removal + disconnect const doKill = (pg) => { Object.keys(clients[pg][obj.sid]).forEach(x => { - send(clients[pg][obj.sid][x], {code: "killed"}); + send(clients[pg][obj.sid][x].socket, { code: "killed" }); }); delete clients[pg][obj.sid]; }; @@ -89,7 +114,7 @@ module.exports = function(wss) { if (k != obj.sid) { Object.keys(clients[pg][k]).forEach(x => { send( - clients[pg][k][x], + clients[pg][k][x].socket, Object.assign({ code: code, from: obj.sid }, o) ); }); @@ -125,7 +150,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 }); @@ -140,7 +165,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 @@ -154,7 +179,7 @@ module.exports = function(wss) { } const tmpId_idx = Math.floor(Math.random() * tmpIds.length); send( - clients[pg][obj.target][tmpIds[tmpId_idx]], + clients[pg][obj.target][tmpIds[tmpId_idx]].socket, { code: obj.code, from: [sid,tmpId,page] } ); } @@ -167,7 +192,7 @@ module.exports = function(wss) { Object.keys(clients[page][obj.target]).forEach(x => { if (obj.target != sid || x != tmpId) send( - clients[page][obj.target][x], + clients[page][obj.target][x].socket, { code: obj.code, data: obj.data } ); }); @@ -176,22 +201,30 @@ module.exports = function(wss) { // Notify all room: mostly game events case "newchat": case "newchallenge": + case "deletechallenge_s": case "newgame": - case "deletechallenge": case "resign": case "abort": case "drawoffer": + case "rematchoffer": case "draw": notifyRoom(page, obj.code, {data: obj.data}); 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}); + 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], + clients[page][obj.target][x].socket, Object.assign({ code: "newmove" }, dataWithFrom) ); }); @@ -207,7 +240,7 @@ module.exports = function(wss) { !!clients[page][obj.target[0]][obj.target[1]] ) { send( - clients[page][obj.target[0]][obj.target[1]], + clients[page][obj.target[0]][obj.target[1]].socket, { code: "gotmove" } ); } @@ -218,32 +251,12 @@ 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], - { 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]) { Object.keys(clients[gamePg][obj.target]).forEach(x => { send( - clients[gamePg][obj.target][x], + clients[gamePg][obj.target][x].socket, { code: "abort" } ); }); @@ -251,9 +264,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]); + else { + // Notify game room + Hall: + notifyAllBut(page, obj.code, {}, [sid]); + notifyAllBut("/", 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": @@ -264,7 +305,7 @@ module.exports = function(wss) { // 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]], + clients[pg][obj.target[0]][obj.target[1]].socket, { code:obj.code, data:obj.data } ); } @@ -277,12 +318,18 @@ module.exports = function(wss) { doDisconnect(); }; // Update clients object: add new connexion + const newElt = { socket: socket, focus: true }; if (!clients[page]) - clients[page] = { [sid]: {[tmpId]: socket } }; + clients[page] = { [sid]: {[tmpId]: newElt } }; else if (!clients[page][sid]) - clients[page][sid] = { [tmpId]: socket }; + clients[page][sid] = { [tmpId]: newElt }; else - clients[page][sid][tmpId] = socket; + 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); });