traque/traque.js

235 lines
7.6 KiB
JavaScript
Raw Normal View History

/* struct equipe
{
2023-06-05 21:26:13 +02:00
"id" : string,
"pos" : [lat : float, long : float],
"color" : int,
2023-06-05 21:26:13 +02:00
"room" : socket.io room,
"shown" : bool
}
Les messages à transmettre par le client :
2023-06-05 21:26:13 +02:00
- position, HTTP "/log?id=%ID&lat=%LAT&lon=%LON"
- code(code)
Les messages à transmettre par le serveur :
- moving(id, position)
- changeColor(id, color)
*/
2022-09-13 13:59:20 +02:00
// Textes d'interaction avec les conscrits
var PWD_TRACKED = "tracked";
var PWD_TRACKER = "tracker";
2022-09-15 09:45:42 +02:00
var PWD_INVISIBLE = "invisible";
2022-09-14 17:11:04 +02:00
var MSG_BAD = "Code Incorrect";
2022-09-13 13:59:20 +02:00
var MSG_TRACKED = "Vous êtes maintenant traqué.e.s !"
var MSG_TRACKER = "Vous pouvez maintenant traquer !";
var MSG_INVISIBLE = "Les autres équipes ne peuvent plus vous voir !";
2022-09-14 18:46:12 +02:00
var invisible_delay = 3*60*1000;
2022-09-13 13:59:20 +02:00
2023-06-05 21:26:13 +02:00
var equipes = {};
var admins = {};
2022-09-14 17:11:04 +02:00
var invisi = {};
// require = include
2022-09-09 13:03:08 +02:00
var http = require('https');//require('http');
var url = require('url');
var fs = require('fs');
var config = require('./config.js');
console.log("Setup http server");
2022-09-09 13:03:08 +02:00
const option = {
key: fs.readFileSync(config.key),
cert: fs.readFileSync(config.cert)
2022-09-09 13:03:08 +02:00
};
// The server
2022-09-09 13:03:08 +02:00
var server = http.createServer(option, function(req, res){
var q = url.parse(req.url, true);
2023-06-05 17:51:32 +02:00
//
// 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";
2023-06-05 17:51:32 +02:00
2023-06-06 15:46:53 +02:00
if(q.pathname.startsWith("/tracking/")){
id = q.pathname.substring("/tracking/".length);
return fs.readFile("static/tracking/conscrit.html", 'utf8', function(err, data){
2023-06-05 21:26:13 +02:00
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();
});
}
2023-06-05 17:51:32 +02:00
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)
2023-06-05 21:26:13 +02:00
var equipe = equipes[q.query.id];
2023-06-05 17:51:32 +02:00
var position = [q.query.lat, q.query.lon];
2023-06-05 21:26:13 +02:00
equipe.pos = position;
if(equipe.shown)
2023-06-05 17:51:32 +02:00
for(i in equipes)
2023-06-06 16:20:35 +02:00
equipes[i].room.emit('moving', {"id": equipe.id, "color": equipe.color, "position": position});
2023-06-05 17:51:32 +02:00
else {
2023-06-06 16:20:35 +02:00
equipe.room.emit('moving', {"id": equipe.id, "color": equipe.color, "position": position});
2023-06-05 21:26:13 +02:00
// for(i in admins)
// admins[i].emit('moving', {"id": equipe.id, "position": position})
2023-06-05 17:51:32 +02:00
}
//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);
2023-06-05 21:26:13 +02:00
var team_nsp = io.of("/");//TODO: separate admin and player
console.log("Setup handlers");
2023-06-05 21:26:13 +02:00
team_nsp.on('connection', function(socket){
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);
equipes[id] = equipe;
}
socket.on("code", function(d){ /*TODO*/ });
2022-09-09 17:16:20 +02:00
for(i in equipes){
if(!equipes[i].shown)
continue;
2023-06-06 16:20:35 +02:00
socket.emit('moving', {"id": i, "color": equipes[i].color, "position": equipes[i].pos});
2022-09-09 17:16:20 +02:00
}
2023-06-05 21:26:13 +02:00
//ici essentiellement tout est a refaire
// socket.id = equipes.length;
// equipes.push(socket);
// socket.shown = true;
// socket.admin = false;
// socket.color = 0;
// socket.position = [0,0];
// socket.on('admin', function(){
// socket.admin = true;
// socket.shown = false;
// admins.push(socket);
// for(i in equipes)
// equipes[i].emit('changeColor', {"id": socket.id, "color": -1});
// socket.on('newTracker', function(d){
// for(i in equipes)
// equipes[i].emit('newTracker', d);
// });
// });
// socket.on('changeColor', function(d){
// socket.color = d.color - 0;
// if(d.color == -1)
// socket.shown = false;
// else{
// if(!socket.shown)
// for(i in equipes)
// equipes[i].emit('moving', {"id": socket.id, "position": socket.position});
// socket.shown = true;
// }
// for(i in equipes)
// equipes[i].emit('changeColor', {"id": socket.id, "color": d.color});
// });
// socket.on('message', function(d){
// d.content = d.content.toLowerCase();
// if(d.content == PWD_TRACKED){
// d.color = 0;
// socket.emit('popup', {"content": MSG_TRACKED});
// } else if(d.content == PWD_TRACKER){
// d.color = 1;
// socket.emit('popup', {"content": MSG_TRACKER});
// } else if(d.content == PWD_INVISIBLE){
// d.color = -1;
// socket.emit('popup', {"content": MSG_INVISIBLE});
// } else if((d.content in invisi) && invisi[d.content]){
// invisi[d.content] = false;
// old_color = socket.color;
// d.color = -1;
// socket.emit('popup', {"content": MSG_INVISIBLE});
// setTimeout(function(s,c){
// for(i in equipes){
// equipes[i].emit('moving', {"id": s.id, "position": s.position});
// equipes[i].emit('changeColor', {"id": s.id, "color": c, "debug": "timeout"});
// }
// s.color = c;
// }, invisible_delay, socket, old_color);
// } else {
// socket.emit('popup', {"content": MSG_BAD});
// return;
// }
// socket.color = d.color - 0;
// if(d.color == -1)
// socket.shown = false;
// else{
// if(!socket.shown)
// for(i in equipes)
// equipes[i].emit('moving', {"id": socket.id, "position": socket.position});
// socket.shown = true;
// }
// for(i in equipes)
// equipes[i].emit('changeColor', {"id": socket.id, "color": d.color});
// });
// socket.on('popup', function(d){
// for(i in equipes)
// equipes[i].emit('popup', {"content": d.content});
// });
// socket.on('setName', function(d){
// socket.name = d.name;
// for(i in equipes)
// equipes[i].emit('setName', {"id": socket.id, "name": d.name});
// });
// socket.on('newCode', function(d){
// invisi[d.code] = true;
// });
// socket.on("disconnect", function(_){
// console.log(socket.id + " disconnect");
// socket.shown = false;
// for(i in equipes)
// equipes[i].emit('remove', {"id": socket.id});
// });
//
// socket.emit('yourId', {"id": socket.id});
// for(i in equipes){
// if(!equipes[i].shown)
// continue;
// socket.emit('setName', {"id": i, "name": equipes[i].name});
// socket.emit('changeColor', {"id": i, "color": equipes[i].color});
// socket.emit('moving', {"id": i, "position": equipes[i].position});
// }
});
console.log("Launch server");
server.listen(config.port, "::");
console.log("Running !");