X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=server%2Fsockets.js;h=c2b41e88c3ede21b381593db92d819916988f8d3;hp=65e41635d1734ec76aa4182ef5b6656cb5277673;hb=bae751bc6bc548791772c3ff5883a03deeb77264;hpb=7ebc0408a76b4a966273190a2ade49e0f97099be diff --git a/server/sockets.js b/server/sockets.js index 65e41635..c2b41e88 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -1,7 +1,7 @@ -const url = require('url'); +const Discord = require('discord.js'); +const { token, channel } = require('./config/discord.json'); // Node version in Ubuntu 16.04 does not know about URL class -// NOTE: url is already transformed, without ?xxx=yyy... parts function getJsonFromUrl(url) { const query = url.substr(2); //starts with "/?" let result = {}; @@ -27,6 +27,14 @@ module.exports = function(wss) { // NOTE: only purpose of sidToPages = know when to delete keys in idToSid let sidToPages = {}; let idToSid = {}; + const discordClient = new Discord.Client(); + let discordChannel = null; + if (token.length > 0) { + discordClient.login(token); + discordClient.once("ready", () => { + discordChannel = discordClient.channels.cache.get(channel); + }); + } wss.on("connection", (socket, req) => { const query = getJsonFromUrl(req.url); const sid = query["sid"]; @@ -90,47 +98,16 @@ module.exports = function(wss) { // When page changes: doDisconnect(); break; - case "killme": { - // Self multi-connect: manual removal + disconnect - const doKill = (pg) => { - Object.keys(clients[pg][obj.sid]).forEach(x => { - send(clients[pg][obj.sid][x].socket, { code: "killed" }); - }); - delete clients[pg][obj.sid]; - }; - const disconnectFromOtherConnexion = (pg,code,o={}) => { - Object.keys(clients[pg]).forEach(k => { - if (k != obj.sid) { - Object.keys(clients[pg][k]).forEach(x => { - send( - clients[pg][k][x].socket, - Object.assign({ code: code, from: obj.sid }, o) - ); - }); - } - }); - }; - Object.keys(clients).forEach(pg => { - if (clients[pg][obj.sid]) { - doKill(pg); - disconnectFromOtherConnexion(pg, "disconnect"); - if (pg.indexOf("/game/") >= 0 && clients["/"]) - disconnectFromOtherConnexion("/", "gdisconnect", { page: pg }); - } - }); - break; - } case "pollclients": { // From Game let sockIds = {}; Object.keys(clients[page]).forEach(k => { - // Avoid polling myself: no new information to get - if (k != sid) { - sockIds[k] = {}; - Object.keys(clients[page][k]).forEach(x => { + sockIds[k] = {}; + Object.keys(clients[page][k]).forEach(x => { + // Avoid polling my tmpId: no information to get + if (k != sid || x != tmpId) sockIds[k][x] = { focus: clients[page][k][x].focus }; - }); - } + }); }); send(socket, { code: "pollclients", sockIds: sockIds }); break; @@ -139,30 +116,30 @@ module.exports = function(wss) { // From Hall let sockIds = {}; Object.keys(clients["/"]).forEach(k => { - // Avoid polling myself: no new information to get - if (k != sid) { - sockIds[k] = {}; - Object.keys(clients[page][k]).forEach(x => { + sockIds[k] = {}; + Object.keys(clients[page][k]).forEach(x => { + // Avoid polling my tmpId: no information to get + if (k != sid || x != tmpId) { sockIds[k][x] = { page: "/", focus: clients[page][k][x].focus }; - }); - } + } + }); }); // NOTE: a "gamer" could also just be an observer Object.keys(clients).forEach(p => { if (p.indexOf("/game/") >= 0) { Object.keys(clients[p]).forEach(k => { - if (k != sid) { - if (!sockIds[k]) sockIds[k] = {}; - Object.keys(clients[p][k]).forEach(x => { + if (!sockIds[k]) sockIds[k] = {}; + Object.keys(clients[p][k]).forEach(x => { + if (k != sid || x != tmpId) { sockIds[k][x] = { page: p, focus: clients[p][k][x].focus }; - }); - } + } + }); }); } }); @@ -210,6 +187,9 @@ module.exports = function(wss) { clients[page][rid][rtmpId].socket, { code: "askfullgame", from: [sid,tmpId] } ); + } else { + // I'm the only person who have the game for the moment: + send(socket, { code: "fullgame", data: { empty: true } }); } } break; @@ -238,7 +218,23 @@ module.exports = function(wss) { case "rematchoffer": case "draw": // "newgame" message can provide a page (corr Game --> Hall) - notifyRoom(obj.page || page, obj.code, {data: obj.data}, obj.excluded); + if (obj.code == "newchallenge") { + // Filter out targeted challenges and correspondance games: + if (!obj.data.to && obj.data.cadence.indexOf('d') < 0) { + const challMsg = ( + (obj.data.sender || "@nonymous") + " : " + + "**" + obj.data.vname + "** " + + "[" + obj.data.cadence + "] " + ); + if (!!discordChannel) discordChannel.send(challMsg); + else + // Log when running locally (dev, debug): + console.log(challMsg); + } + delete obj.data["sender"]; + } + notifyRoom( + obj.page || page, obj.code, {data: obj.data}, obj.excluded); break; case "rnewgame": @@ -316,7 +312,13 @@ module.exports = function(wss) { case "getfocus": case "losefocus": - clients[page][sid][tmpId].focus = (obj.code == "getfocus"); + if ( + !!clients[page] && + !!clients[page][sid] && + !!clients[page][sid][tmpId] + ) { + clients[page][sid][tmpId].focus = (obj.code == "getfocus"); + } if (page == "/") notifyRoom("/", obj.code, { page: "/" }, [sid]); else { // Notify game room + Hall: @@ -334,8 +336,8 @@ module.exports = function(wss) { case "lastate": { 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 undefined + // NOTE: if in game we ask identity to opponent still in Hall, but + // leaving Hall, clients[pg] or clients[pg][target] could be undef. if (!!clients[pg] && !!clients[pg][obj.target[0]]) { send( clients[pg][obj.target[0]][obj.target[1]].socket,