some reformatting

This commit is contained in:
catvayor 2023-06-06 17:20:40 +02:00
parent 1154f13962
commit a9bcfc7dbe
2 changed files with 89 additions and 79 deletions

View file

@ -93,9 +93,7 @@ function setup_socket_not_admin(){
else else
markers[data.id].setIcon(self_icons[data.color]); markers[data.id].setIcon(self_icons[data.color]);
else else
if(data.color == -1) if(data.color != -1)
markers[data.id].setLatLng([0,0]);
else
markers[data.id].setIcon(icons[data.color]); markers[data.id].setIcon(icons[data.color]);
} }
}); });

164
traque.js
View file

@ -15,6 +15,12 @@ Les messages à transmettre par le serveur :
- changeColor(id, color) - 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 // Textes d'interaction avec les conscrits
var PWD_TRACKED = "tracked"; var PWD_TRACKED = "tracked";
var PWD_TRACKER = "tracker"; 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 invisible_delay = 3*60*1000;
var equipes = {}; var equipes = {};
var admins = {};
var invisi = {}; 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"); 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
//
// 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 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"); console.log("Setup handlers");
team_nsp.on('connection', function(socket){ team_nsp.on('connection', function(socket){
var id = socket.handshake.auth.id; var id = socket.handshake.auth.id;
@ -118,15 +75,20 @@ team_nsp.on('connection', function(socket){
equipe.color = 0; equipe.color = 0;
equipe.id = id; equipe.id = id;
equipe.room = team_nsp.to(id); equipe.room = team_nsp.to(id);
equipe.rooms = ["tracked"];
equipes[id] = equipe; equipes[id] = equipe;
} }
for(room of equipes[id].rooms)
socket.join(room);
socket.on("code", function(d){ /*TODO*/ }); socket.on("code", function(d){ /*TODO*/ });
for(i in equipes){ for(equipe of equipes){
if(!equipes[i].shown) if (equipe.shown){
continue; socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": equipe.pos});
socket.emit('moving', {"id": i, "color": equipes[i].color, "position": equipes[i].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"); console.log("Launch server");
server.listen(config.port, "::"); server.listen(config.port, "::");
console.log("Running !"); console.log("Running !");