diff --git a/static/utils.js b/static/utils.js index 0654bd8..e47ac22 100644 --- a/static/utils.js +++ b/static/utils.js @@ -93,9 +93,7 @@ function setup_socket_not_admin(){ else markers[data.id].setIcon(self_icons[data.color]); else - if(data.color == -1) - markers[data.id].setLatLng([0,0]); - else + if(data.color != -1) markers[data.id].setIcon(icons[data.color]); } }); diff --git a/traque.js b/traque.js index f1e0f6a..a988dd0 100644 --- a/traque.js +++ b/traque.js @@ -15,6 +15,12 @@ Les messages à transmettre par le serveur : - changeColor(id, color) */ +// require = include +var http = require('https');//require('http'); +var url = require('url'); +var fs = require('fs'); +var config = require('./config.js'); + // Textes d'interaction avec les conscrits var PWD_TRACKED = "tracked"; var PWD_TRACKER = "tracker"; @@ -26,85 +32,36 @@ var MSG_INVISIBLE = "Les autres équipes ne peuvent plus vous voir !"; var invisible_delay = 3*60*1000; var equipes = {}; -var admins = {}; var invisi = {}; -// require = include -var http = require('https');//require('http'); -var url = require('url'); -var fs = require('fs'); -var config = require('./config.js'); - -console.log("Setup http server"); - -const option = { - key: fs.readFileSync(config.key), - cert: fs.readFileSync(config.cert) -}; - -// The server -var server = http.createServer(option, function(req, res){ - var q = url.parse(req.url, true); - // - // todo : utiliser ?id pour changer des chose dans la page, ou faire ça en fesant croire - // qu'il y a plein de fichiers sous le dossier "conscrit", alors que c'est juste une page html; - // la page conscrit seul renvoyant 404 ou random page erreur "utilise les lien qu'on donne petit conscrit" - // - var filename = "static" + q.pathname; - if(q.pathname.includes("..")) - filename = "static/dotdot.html"; - - if(q.pathname.startsWith("/tracking/")){ - id = q.pathname.substring("/tracking/".length); - return fs.readFile("static/tracking/conscrit.html", 'utf8', function(err, data){ - if(err) - throw new Error("where conscrit.html is !?"); - - res.writeHead(200, {'Content-Type': 'text/html'}); - res.write(data.replaceAll("%ID", id)); - return res.end(); - }); - } - - if(q.pathname == "/log"){ - //position logging - console.log("team " + q.query.id + " moved to (" + q.query.lat + "," + q.query.lon + ")"); - - //current impl (to be changed) - var equipe = equipes[q.query.id]; - var position = [q.query.lat, q.query.lon]; - equipe.pos = position; - if(equipe.shown) - for(i in equipes) - equipes[i].room.emit('moving', {"id": equipe.id, "color": equipe.color, "position": position}); - else { - equipe.room.emit('moving', {"id": equipe.id, "color": equipe.color, "position": position}); -// for(i in admins) -// admins[i].emit('moving', {"id": equipe.id, "position": position}) - } - - - //return empty page - res.writeHead(200, {'Content-Type': 'text/html'}); - return res.end(); - } - fs.readFile(filename, function(err, data) { - if (err) { - console.log("404: ", q.pathname, filename); - res.writeHead(404, {'Content-Type': 'text/html'}); - return res.end("404 Not Found"); - } - res.writeHead(200, {'Content-Type': 'text/html'}); - res.write(data); - return res.end(); - }); -}); - console.log("Setup io server"); const { Server } = require("socket.io"); var io = new Server(server); + +///////////////// +// Tracked namespace +// +// Everyone in this namespace is located +// +// * "tracked" room for all tracked +// * "tracking" room for all tracker +// * "npc" room for non-player +// * "%ID" room of a team +// var team_nsp = io.of("/");//TODO: separate admin and player + +function emit_update(team_id) { + var equipe = equipes[team_id]; + if (equipe.shown){ + team_nsp.emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos}); + } else{ + team_nsp.expect(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": [0,0]}); + team_nsp.to(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos}); + } + //TODO admin +} + console.log("Setup handlers"); team_nsp.on('connection', function(socket){ var id = socket.handshake.auth.id; @@ -118,15 +75,20 @@ team_nsp.on('connection', function(socket){ equipe.color = 0; equipe.id = id; equipe.room = team_nsp.to(id); + equipe.rooms = ["tracked"]; equipes[id] = equipe; } + for(room of equipes[id].rooms) + socket.join(room); socket.on("code", function(d){ /*TODO*/ }); - for(i in equipes){ - if(!equipes[i].shown) - continue; - socket.emit('moving', {"id": i, "color": equipes[i].color, "position": equipes[i].pos}); + for(equipe of equipes){ + if (equipe.shown){ + socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": equipe.pos}); + } else{ + socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": [0,0]}); + } } @@ -229,6 +191,56 @@ team_nsp.on('connection', function(socket){ // } }); +console.log("Setup http server"); + +const option = { + key: fs.readFileSync(config.key), + cert: fs.readFileSync(config.cert) +}; + +// The server +var server = http.createServer(option, function(req, res){ + var q = url.parse(req.url, true); + var filename = "static" + q.pathname; + if(q.pathname.includes("..")) + filename = "static/dotdot.html"; + + if(q.pathname.startsWith("/tracking/")){ + id = q.pathname.substring("/tracking/".length); + //TODO validator for the id + return fs.readFile("static/tracking/conscrit.html", 'utf8', function(err, data){ + if(err) + throw new Error("where conscrit.html is !?"); + + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(data.replaceAll("%ID", id)); + return res.end(); + }); + } + + if(q.pathname == "/log"){ + //position logging + console.log("team " + q.query.id + " moved to (" + q.query.lat + "," + q.query.lon + ")"); + + equipes[q.query.id].pos = [q.query.lat, q.query.lon]; + emit_update(q.query.id); + + //return empty page + res.writeHead(200, {'Content-Type': 'text/html'}); + return res.end(); + } + fs.readFile(filename, function(err, data) { + if (err) { + console.log("404: ", q.pathname, filename); + res.writeHead(404, {'Content-Type': 'text/html'}); + return res.end("404 Not Found"); + } + res.writeHead(200, {'Content-Type': 'text/html'}); + res.write(data); + return res.end(); + }); +}); + console.log("Launch server"); server.listen(config.port, "::"); console.log("Running !");