diff --git a/static/tracking/conscrit.html b/static/tracking/conscrit.html index 00302f0..f0883be 100644 --- a/static/tracking/conscrit.html +++ b/static/tracking/conscrit.html @@ -46,8 +46,9 @@ // SOCKET id = "%ID"; // %ID will be replaced by the real id. - setup_socket_common(); + type = "conscrit"; setup_socket_not_admin(); + setup_socket_common(); ////////////////////////////////////////////////////////////////////////////// // SETTINGS -- CODE diff --git a/static/utils.js b/static/utils.js index e47ac22..954f094 100644 --- a/static/utils.js +++ b/static/utils.js @@ -3,6 +3,7 @@ var server = location.hostname; var port = location.port; var socket; var id; +var type; var markers = {}; var CircleIcon = L.Icon.extend({ @@ -53,9 +54,6 @@ function setup_map(){ // SOCKET function setup_socket_common(){ - socket = io.connect({rejectUnauthorized: false, auth: {id: id}}, - protocol+"://"+server+":"+port); - socket.on("popup", function(data){ alert(data.content); }); @@ -71,6 +69,9 @@ function setup_socket_common(){ } function setup_socket_not_admin(){ + socket = io({rejectUnauthorized: false, auth: {id: id, type:type}}, + protocol+"//"+server+":"+port); + socket.on("moving", function(data){ console.log("moving", data); if(!(data.id in markers)){ diff --git a/traque.js b/traque.js index a988dd0..5dcf5bf 100644 --- a/traque.js +++ b/traque.js @@ -3,7 +3,7 @@ "id" : string, "pos" : [lat : float, long : float], "color" : int, - "room" : socket.io room, + "rooms" : room list, "shown" : bool } @@ -11,8 +11,7 @@ Les messages à transmettre par le client : - position, HTTP "/log?id=%ID&lat=%LAT&lon=%LON" - code(code) Les messages à transmettre par le serveur : -- moving(id, position) -- changeColor(id, color) +- moving(id, color, position) */ // require = include @@ -34,60 +33,144 @@ var invisible_delay = 3*60*1000; var equipes = {}; var invisi = {}; +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("Setup io server"); const { Server } = require("socket.io"); var io = new Server(server); ///////////////// -// Tracked namespace -// -// Everyone in this namespace is located +// Tracking room // +// Everyone in this room is located +// sub-rooms : // * "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 +// To join : +// auth = { +// type = "conscrit" | "vieux", +// id = "%ID" +// } +// "conscrit" are classical player, "vieux" are npcs (they can become tracker when needed) +var tracking = io.to("Tracking"); +///////////////// +// Admin room +// +// Room for admins +// To join : +// auth = { +// type = "Admin" +// } +var admin = io.to("Admin"); 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}); + tracking.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}); + tracking.expect(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": [0,0]}); + io.to(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos}); } - //TODO admin + admin.emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos}); } console.log("Setup handlers"); -team_nsp.on('connection', function(socket){ - var id = socket.handshake.auth.id; - console.log("connection of " + id + " !"); +io.on('connection', function(socket){ + if(socket.handshake.auth.type == "conscrit" || + socket.handshake.auth.type == "vieux") { + var id = socket.handshake.auth.id; + console.log("connection of " + id + " !"); - socket.join(id); - if(!(id in equipes)){ - var equipe = {}; - equipe.shown = true; - equipe.pos = [0,0]; - equipe.color = 0; - equipe.id = id; - equipe.room = team_nsp.to(id); - equipe.rooms = ["tracked"]; - equipes[id] = equipe; + socket.join("Tracking"); + socket.join(id); + if(!(id in equipes)){ + var equipe = {}; + equipe.shown = true; + equipe.pos = [0,0]; + equipe.color = 0; + equipe.id = 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){ + var equipe = equipes[i]; + 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]}); + } + } } - for(room of equipes[id].rooms) - socket.join(room); - socket.on("code", function(d){ /*TODO*/ }); + if(socket.handshake.auth.type == "vieux"){ + //TODO + } - for(equipe of equipes){ - if (equipe.shown){ + if(socket.handshake.auth.type == "Admin"){ + socket.join("Admin"); + + //TODO + + for(i in equipes){ + var equipe = equipes[i]; 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]}); } } @@ -191,56 +274,6 @@ 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 !");