2022-09-08 17:48:53 +02:00
|
|
|
/* struct equipe
|
|
|
|
{
|
|
|
|
"name" : string,
|
|
|
|
"pos" : [lat : float, long : float],
|
|
|
|
"color" : int,
|
|
|
|
"socket" : socket
|
|
|
|
"shown" : bool
|
|
|
|
}
|
|
|
|
|
|
|
|
Les messages à transmettre par le client :
|
|
|
|
- geoLoc(position)
|
|
|
|
- changeColor(color)
|
|
|
|
- setName(id)
|
|
|
|
Les messages à transmettre par le serveur :
|
|
|
|
- moving(id, position)
|
|
|
|
- changeColor(id, color)
|
|
|
|
- setName(id, name)
|
|
|
|
*/
|
|
|
|
|
2022-09-13 13:59:20 +02:00
|
|
|
// Textes d'interaction avec les conscrits
|
|
|
|
var PWD_TRACKED = "tracked";
|
|
|
|
var PWD_TRACKER = "tracker";
|
|
|
|
var PWD_INVISIBLE = "invisible";
|
|
|
|
var MSG_BAD = "La base n'a pas compris votre message...";
|
|
|
|
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-08 17:48:53 +02:00
|
|
|
var equipes = [];
|
2022-09-13 23:18:13 +02:00
|
|
|
var admins = [];
|
2022-09-08 17:48:53 +02:00
|
|
|
|
|
|
|
// require = include
|
2022-09-09 13:03:08 +02:00
|
|
|
var http = require('https');//require('http');
|
2022-09-08 17:48:53 +02:00
|
|
|
var url = require('url');
|
|
|
|
var fs = require('fs');
|
2022-09-12 13:49:35 +02:00
|
|
|
var config = require('./config.js');
|
2022-09-08 17:48:53 +02:00
|
|
|
|
|
|
|
console.log("Setup http server");
|
|
|
|
|
2022-09-09 13:03:08 +02:00
|
|
|
const option = {
|
2022-09-12 13:49:35 +02:00
|
|
|
key: fs.readFileSync(config.key),
|
|
|
|
cert: fs.readFileSync(config.cert)
|
2022-09-09 13:03:08 +02:00
|
|
|
};
|
|
|
|
|
2022-09-08 17:48:53 +02:00
|
|
|
// The server
|
2022-09-09 13:03:08 +02:00
|
|
|
var server = http.createServer(option, function(req, res){
|
2022-09-08 17:48:53 +02:00
|
|
|
var q = url.parse(req.url, true);
|
2022-09-12 13:49:35 +02:00
|
|
|
var filename = "static" + q.pathname;
|
|
|
|
if(q.pathname.includes(".."))
|
|
|
|
filename = "static/dotdot.html";
|
2022-09-08 17:48:53 +02:00
|
|
|
if(q.pathname == "/")
|
2022-09-13 13:59:20 +02:00
|
|
|
filename = "static/conscrit.html";
|
2022-09-08 17:48:53 +02:00
|
|
|
fs.readFile(filename, function(err, data) {
|
|
|
|
if (err) {
|
2022-09-12 14:05:17 +02:00
|
|
|
console.log("404: ", q.pathname, filename);
|
|
|
|
res.writeHead(404, {'Content-Type': 'text/html'});
|
2022-09-08 17:48:53 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
console.log("Setup handlers");
|
|
|
|
io.sockets.on('connection', function(socket){
|
2022-09-09 13:03:08 +02:00
|
|
|
console.log("connection !");
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.id = equipes.length;
|
|
|
|
equipes.push(socket);
|
2022-09-09 13:03:08 +02:00
|
|
|
socket.shown = true;
|
2022-09-13 23:18:13 +02:00
|
|
|
socket.admin = false;
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.color = 0;
|
2022-09-09 17:16:20 +02:00
|
|
|
socket.position = [0,0];
|
2022-09-13 23:18:13 +02:00
|
|
|
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});
|
|
|
|
});
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.on('geoLoc', function(d){
|
|
|
|
socket.position = d.position;
|
|
|
|
if(socket.shown)
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('moving', {"id": socket.id, "position": d.position});
|
2022-09-13 23:18:13 +02:00
|
|
|
else {
|
2022-09-12 09:29:28 +02:00
|
|
|
socket.emit('moving', {"id": socket.id, "position": d.position});
|
2022-09-13 23:18:13 +02:00
|
|
|
for(i in admins)
|
|
|
|
admins[i].emit('moving', {"id": socket.id, "position": d.position})
|
|
|
|
}
|
2022-09-08 17:48:53 +02:00
|
|
|
});
|
|
|
|
socket.on('changeColor', function(d){
|
2022-09-09 13:03:08 +02:00
|
|
|
socket.color = d.color - 0;
|
2022-09-08 17:48:53 +02:00
|
|
|
if(d.color == -1)
|
|
|
|
socket.shown = false;
|
|
|
|
else{
|
|
|
|
if(!socket.shown)
|
2022-09-09 17:16:20 +02:00
|
|
|
for(i in equipes)
|
2022-09-08 17:48:53 +02:00
|
|
|
equipes[i].emit('moving', {"id": socket.id, "position": socket.position});
|
2022-09-13 13:59:20 +02:00
|
|
|
socket.shown = true;
|
|
|
|
}
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('changeColor', {"id": socket.id, "color": d.color});
|
|
|
|
});
|
|
|
|
socket.on('message', function(d){
|
|
|
|
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 {
|
|
|
|
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});
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.shown = true;
|
|
|
|
}
|
2022-09-09 17:16:20 +02:00
|
|
|
for(i in equipes)
|
2022-09-08 17:48:53 +02:00
|
|
|
equipes[i].emit('changeColor', {"id": socket.id, "color": d.color});
|
|
|
|
});
|
2022-09-13 23:18:13 +02:00
|
|
|
socket.on('popup', function(d){
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('popup', d.data);
|
|
|
|
});
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.on('setName', function(d){
|
|
|
|
socket.name = d.name;
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('setName', {"id": socket.id, "name": d.name});
|
|
|
|
});
|
2022-09-09 17:37:30 +02:00
|
|
|
socket.on("disconnect", function(_){
|
|
|
|
socket.shown = false;
|
|
|
|
socket.color = -1;
|
|
|
|
for(i in equipes)
|
|
|
|
equipes[i].emit('changeColor', {"id": socket.id, "color": -1});
|
|
|
|
});
|
2022-09-09 17:16:20 +02:00
|
|
|
|
2022-09-08 17:48:53 +02:00
|
|
|
socket.emit('yourId', {"id": socket.id});
|
2022-09-09 17:16:20 +02:00
|
|
|
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});
|
|
|
|
}
|
2022-09-08 17:48:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
console.log("Launch server");
|
2022-09-12 13:49:35 +02:00
|
|
|
server.listen(config.port, "::");
|
2022-09-09 14:43:41 +02:00
|
|
|
console.log("Running !");
|