traque/traque.js

308 lines
8.9 KiB
JavaScript
Raw Normal View History

/* struct equipe
{
2023-06-05 21:26:13 +02:00
"id" : string,
"pos" : [lat : float, long : float],
2023-06-08 16:50:15 +02:00
"state" : {
"shown" : bool,
"tracker" : bool,
"npc" : 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)
- (vieux) changeState (state)
Les messages à transmettre par le serveur :
- moving(id, color, position)
- (vieux) newState(state)
*/
2023-06-06 17:20:40 +02:00
// require = include
var http = require('https');//require('http');
var url = require('url');
var fs = require('fs');
var config = require('./config.js');
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 !";
var invisible_delay = 10000;// 3*60*1000;
2022-09-13 13:59:20 +02:00
2023-06-05 21:26:13 +02:00
var equipes = {};
2022-09-14 17:11:04 +02:00
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);
gpslog = true;
if(id.startsWith("nolog/")){
gpslog = false;
id = id.substring("nolog/".length);
}
2023-06-10 09:46:04 +02:00
var end_path = ["conscrit.html", "vieux.html", "invalid.html"][config.validator(id)];
filename = "static/tracking/" + end_path;
return fs.readFile(filename, 'utf8', function(err, data){
if(err)
2023-06-10 09:46:04 +02:00
throw new Error("where " + end_path + " is !?");
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data.replaceAll("%ID", id).replaceAll("%GPSLOG", gpslog));
return res.end();
});
}
if(q.pathname == "/log"){
//position logging
console.log("team " + q.query.id + " moved to (" + q.query.lat + "," + q.query.lon + ")");
var id = q.query.id;
if(id in equipes){
equipes[id].pos = [q.query.lat, q.query.lon];
emit_update(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);
2023-06-06 17:20:40 +02:00
io.use(function(socket, next){
var id = socket.handshake.auth.id;
var type = socket.handshake.auth.type;
if(type == "Admin")
next();
else{
var valid = config.validator(id);
if(valid == 0 && type == "conscrit" ||
valid == 1 && type == "vieux"){
2023-06-11 17:15:04 +02:00
if(!(id in equipes)){
equipes[id] = default_team(id, valid);
2023-06-11 17:15:04 +02:00
emit_update(id);
}
next();
} else
next(new Error("invalid"));
}
});
2023-06-06 17:20:40 +02:00
/////////////////
// Tracking room
2023-06-06 17:20:40 +02:00
//
// Everyone in this room is located
// sub-rooms :
2023-06-06 17:20:40 +02:00
// * "npc" room for non-player
// * "%ID" room of a team
//
// 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");
2023-06-06 17:20:40 +02:00
// visible color of a team
function color(team){
if(!team.state.shown) return 2;
if(team.state.tracker) return 1;
if(team.state.npc != 0) return team.state.npc - 0 + 2;
return 0;
}
2023-06-08 16:50:15 +02:00
// apparent information of a team, for other only
function apparent_info(equipe){
if(equipe.state.shown){
return {"id": equipe.id, "color": color(equipe), "position": equipe.pos};
2023-06-08 16:50:15 +02:00
} else {
return {"id": equipe.id, "color": color(equipe), "position": [0,0]};
2023-06-08 16:50:15 +02:00
}
}
2023-06-06 17:20:40 +02:00
function emit_update(team_id) {
var equipe = equipes[team_id];
2023-06-08 16:50:15 +02:00
tracking.except(team_id).emit('moving', apparent_info(equipe));
// the team and the admins always have the real informations
admin.to(team_id).emit('moving', {"id": team_id, "color": color(equipe), "position": equipe.pos});
2023-06-11 15:19:03 +02:00
admin.emit('update', equipe);
2023-06-08 16:50:15 +02:00
}
// produces a team object populated with default values
function default_team(team_id, valid) {
2023-06-08 16:50:15 +02:00
var equipe = {};
var state = {};
state.shown = valid == 0;
2023-06-08 16:50:15 +02:00
state.tracker = false;
state.npc = valid;
2023-06-08 16:50:15 +02:00
equipe.state = state;
equipe.pos = [0,0];
equipe.id = team_id;
return equipe;
}
// connect a socket to the room corresponding to its team and send it infos
function team_join(team, socket){
socket.join(team.id);
var state = team.state;
if(state.npc != 0) socket.join("npc");
socket.emit('moving', {"id": team.id, "color": color(team), "position": team.pos});
2023-06-08 16:50:15 +02:00
for(other_id in equipes)
if(other_id != team.id)
2023-06-08 16:50:15 +02:00
socket.emit('moving', apparent_info(equipes[other_id]));
2023-06-06 17:20:40 +02:00
}
console.log("Setup handlers");
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("Tracking");
var equipe = equipes[id]
team_join(equipe, socket);
socket.on("code", function(d){
var code = d.code;
if(code == PWD_TRACKER){
equipe.state.tracker = true;
io.to(id).emit('popup', {"content": MSG_TRACKER});
emit_update(id);
} else if((code in invisi) && invisi[code]){
invisi[code] = false;
equipe.state.shown = false;
io.to(id).emit('popup', {"content": MSG_INVISIBLE});
emit_update(id);
setTimeout(function(eq){
eq.state.shown = true;
emit_update(eq.id);
}, invisible_delay, equipe);
} else {
socket.emit('popup', {"content": MSG_BAD});
return;
}
});
}
if(socket.handshake.auth.type == "vieux"){
var id = socket.handshake.auth.id;
var equipe = equipes[id]
socket.on('changeState', function(d){
equipe.state = d;
io.to(id).emit('newState', d);
emit_update(id);
});
socket.emit('newState', equipe.state);
2023-06-05 21:26:13 +02:00
}
if(socket.handshake.auth.type == "Admin"){
socket.join("Admin");
2022-09-09 17:16:20 +02:00
socket.on('newCode', function(d){
invisi[d.code] = true;
});
socket.on('popup', function(d){
tracking.emit('popup', {"content": d.content});
});
socket.on('newTracker', function(d){
io.emit('newTracker', d);
});
2023-06-11 15:19:03 +02:00
socket.on('setState', function(d){
equipes[d.id].state = d.state;
emit_update(d.id);//TODO update vieux
});
for(i in equipes){
var equipe = equipes[i];
socket.emit('moving', {"id": equipe.id, "color": color(equipe), "position": equipe.pos});
2023-06-11 15:19:03 +02:00
socket.emit('update', equipe);
2023-06-06 17:20:40 +02:00
}
2022-09-09 17:16:20 +02:00
}
2023-06-05 21:26:13 +02:00
//ici essentiellement tout est a refaire
// socket.on('message', function(d){
// d.content = d.content.toLowerCase();
2023-06-05 21:26:13 +02:00
// 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
2023-06-05 21:26:13 +02:00
// 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("disconnect", function(_){
// console.log(socket.id + " disconnect");
// socket.shown = false;
2023-06-05 21:26:13 +02:00
// for(i in equipes)
// equipes[i].emit('remove', {"id": socket.id});
// });
});
console.log("Launch server");
server.listen(config.port, "::");
console.log("Running !");