beginned to separate admin/vieux/conscrit

This commit is contained in:
catvayor 2023-06-06 19:18:21 +02:00
parent a9bcfc7dbe
commit 68a4789afa
3 changed files with 120 additions and 85 deletions

View file

@ -46,8 +46,9 @@
// SOCKET // SOCKET
id = "%ID"; // %ID will be replaced by the real id. id = "%ID"; // %ID will be replaced by the real id.
setup_socket_common(); type = "conscrit";
setup_socket_not_admin(); setup_socket_not_admin();
setup_socket_common();
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// SETTINGS -- CODE // SETTINGS -- CODE

View file

@ -3,6 +3,7 @@ var server = location.hostname;
var port = location.port; var port = location.port;
var socket; var socket;
var id; var id;
var type;
var markers = {}; var markers = {};
var CircleIcon = L.Icon.extend({ var CircleIcon = L.Icon.extend({
@ -53,9 +54,6 @@ function setup_map(){
// SOCKET // SOCKET
function setup_socket_common(){ function setup_socket_common(){
socket = io.connect({rejectUnauthorized: false, auth: {id: id}},
protocol+"://"+server+":"+port);
socket.on("popup", function(data){ socket.on("popup", function(data){
alert(data.content); alert(data.content);
}); });
@ -71,6 +69,9 @@ function setup_socket_common(){
} }
function setup_socket_not_admin(){ function setup_socket_not_admin(){
socket = io({rejectUnauthorized: false, auth: {id: id, type:type}},
protocol+"//"+server+":"+port);
socket.on("moving", function(data){ socket.on("moving", function(data){
console.log("moving", data); console.log("moving", data);
if(!(data.id in markers)){ if(!(data.id in markers)){

195
traque.js
View file

@ -3,7 +3,7 @@
"id" : string, "id" : string,
"pos" : [lat : float, long : float], "pos" : [lat : float, long : float],
"color" : int, "color" : int,
"room" : socket.io room, "rooms" : room list,
"shown" : bool "shown" : bool
} }
@ -11,8 +11,7 @@ Les messages à transmettre par le client :
- position, HTTP "/log?id=%ID&lat=%LAT&lon=%LON" - position, HTTP "/log?id=%ID&lat=%LAT&lon=%LON"
- code(code) - code(code)
Les messages à transmettre par le serveur : Les messages à transmettre par le serveur :
- moving(id, position) - moving(id, color, position)
- changeColor(id, color)
*/ */
// require = include // require = include
@ -34,60 +33,144 @@ var invisible_delay = 3*60*1000;
var equipes = {}; var equipes = {};
var invisi = {}; 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"); console.log("Setup io server");
const { Server } = require("socket.io"); const { Server } = require("socket.io");
var io = new Server(server); var io = new Server(server);
///////////////// /////////////////
// Tracked namespace // Tracking room
//
// Everyone in this namespace is located
// //
// Everyone in this room is located
// sub-rooms :
// * "tracked" room for all tracked // * "tracked" room for all tracked
// * "tracking" room for all tracker // * "tracking" room for all tracker
// * "npc" room for non-player // * "npc" room for non-player
// * "%ID" room of a team // * "%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) { function emit_update(team_id) {
var equipe = equipes[team_id]; var equipe = equipes[team_id];
if (equipe.shown){ 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{ } else{
team_nsp.expect(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": [0,0]}); tracking.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}); 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"); console.log("Setup handlers");
team_nsp.on('connection', function(socket){ io.on('connection', function(socket){
var id = socket.handshake.auth.id; if(socket.handshake.auth.type == "conscrit" ||
console.log("connection of " + id + " !"); socket.handshake.auth.type == "vieux") {
var id = socket.handshake.auth.id;
console.log("connection of " + id + " !");
socket.join(id); socket.join("Tracking");
if(!(id in equipes)){ socket.join(id);
var equipe = {}; if(!(id in equipes)){
equipe.shown = true; var equipe = {};
equipe.pos = [0,0]; equipe.shown = true;
equipe.color = 0; equipe.pos = [0,0];
equipe.id = id; equipe.color = 0;
equipe.room = team_nsp.to(id); equipe.id = id;
equipe.rooms = ["tracked"]; equipe.rooms = ["tracked"];
equipes[id] = equipe; 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(socket.handshake.auth.type == "Admin"){
if (equipe.shown){ socket.join("Admin");
//TODO
for(i in equipes){
var equipe = equipes[i];
socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": equipe.pos}); 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"); console.log("Launch server");
server.listen(config.port, "::"); server.listen(config.port, "::");
console.log("Running !"); console.log("Running !");