traque/traque.js

374 lines
11 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-11 17:42:48 +02:00
"vieux": bool,
2023-06-08 16:50:15 +02:00
"state" : {
2023-06-16 20:56:58 +02:00
"invisibilty" : bool,
"blurred": bool,
2023-06-08 16:50:15 +02:00
"tracker" : bool,
2023-09-05 14:46:13 +02:00
"npc" : int,
"mallette": bool
2023-06-16 20:56:58 +02:00
},
"codes" : {
"invisiblity" : int,
"blurred" : int
2023-06-08 16:50:15 +02:00
}
}
Les messages à transmettre par le client :
2023-06-05 21:26:13 +02:00
- position, HTTP "/log?id=%ID&lat=%LAT&lon=%LON"
2023-06-16 20:56:58 +02:00
- use(code)
- (vieux) changeState (state)
Les messages à transmettre par le serveur :
- moving(id, color, position)
2023-06-16 20:56:58 +02:00
- setCodes(codes)
- (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
2023-06-16 20:56:58 +02:00
const MSG_BAD = "Code Incorrect";
const MSG_TRACKED = "Vous êtes maintenant traqué.e.s !"
const MSG_TRACKER = "Vous pouvez maintenant traquer !";
const MSG_CODES = {
blurred: "Votre positions est maintenant brouillée !",
invisibility: "Les autres équipes ne peuvent plus vous voir !"
};
const bonus_delay = 10000;// 3*60*1000;
2022-09-13 13:59:20 +02:00
2023-06-05 21:26:13 +02:00
var equipes = {};
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");
}
2023-06-11 17:30:57 +02:00
if(filename.endsWith('.js'))
res.writeHead(200, {'Content-Type': 'text/javascript'});
2023-09-06 18:22:20 +02:00
else if(filename.endsWith('.png'))
res.writeHead(200, {'Content-Type': 'image/png'});
else if(filename.endsWith('.ico'))
res.writeHead(200, {'Content-Type': 'image/ico'});
2023-06-11 17:30:57 +02:00
else
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-09-05 14:46:13 +02:00
async function join_leave(team_id, join, leave){
var sockets = await io.in(team_id).fetchSockets();
for(s of sockets){
for(r of join)
s.join(r);
for(r of leave)
s.leave(r);
}
}
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
2023-09-05 14:46:13 +02:00
// * "Tracker" room for trackers
// * "mallette" room for player with a mallette
2023-06-06 17:20:40 +02:00
// * "%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");
2023-09-05 14:46:13 +02:00
var tracker = io.to("Tracker");
var mallette = io.to("mallette");
/////////////////
// 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){
2023-09-05 14:46:13 +02:00
if(team.state.tracker) return 1;
if(team.state.npc != 0) return team.state.npc - 0 + 2;
return 0;
}
function admin_color(team){
2023-06-16 20:56:58 +02:00
if(team.state.invisibility) return 2;
if(team.state.tracker) return 1;
if(team.state.npc != 0) return team.state.npc - 0 + 2;
return 0;
}
2023-09-05 14:46:13 +02:00
// apparent information of a team, for tracker only
function apparent_info_tracker(equipe){
2023-06-16 20:56:58 +02:00
if(equipe.state.invisibility)
return {"id": equipe.id, "color": color(equipe), "position": [0,0]};
2023-06-16 20:56:58 +02:00
if(equipe.state.blurred)
return {"id": equipe.id, "color": color(equipe),
"position": [parseFloat(equipe.pos[0])+config.lat_ofs*(Math.random()*2-1),
parseFloat(equipe.pos[1])+config.long_ofs*(Math.random()*2-1)]};
return {"id": equipe.id, "color": color(equipe), "position": equipe.pos};
2023-06-08 16:50:15 +02:00
}
2023-09-05 14:46:13 +02:00
// apparent information of a team, for mallette only
function apparent_info_mallette(equipe){
if(equipe.state.npc == 0 && !equipe.state.tracker)
return {"id": equipe.id, "color": color(equipe), "position": equipe.pos};
return apparent_info_agent(equipe);
}
// apparent information of a team, for agent only
function apparent_info_agent(equipe){
if(equipe.state.mallette)
return {"id": equipe.id, "color": color(equipe), "position": equipe.pos};
if(equipe.state.npc == 0 && !equipe.state.tracker)
return {"id": equipe.id, "color": color(equipe), "position": [0,0]};
return apparent_info_tracker(equipe);
}
2023-06-06 17:20:40 +02:00
function emit_update(team_id) {
var equipe = equipes[team_id];
2023-09-05 14:46:13 +02:00
tracker.except(team_id).emit('moving', apparent_info_tracker(equipe));
mallette.except(team_id).emit('moving', apparent_info_mallette(equipe));
tracking.except("Tracker").except("mallette").except(team_id).emit('moving', apparent_info_agent(equipe));
// the team, the npcs and the admins always have the real informations
admin.to("npc").to(team_id)
.emit('moving', {"id": team_id, "color": admin_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 state = {};
2023-06-16 20:56:58 +02:00
state.invisibility = valid != 0;
state.blurred = false;
2023-06-08 16:50:15 +02:00
state.tracker = false;
state.npc = valid;
2023-09-05 14:46:13 +02:00
state.mallette = false;
2023-06-16 20:56:58 +02:00
var codes = {};
codes.blurred = 0;
codes.invisibility = 0;
var equipe = {};
2023-06-08 16:50:15 +02:00
equipe.state = state;
2023-06-16 20:56:58 +02:00
equipe.codes = codes;
2023-06-11 17:42:48 +02:00
equipe.vieux = valid == 1;
2023-06-08 16:50:15 +02:00
equipe.pos = [0,0];
equipe.id = team_id;
return equipe;
}
2023-09-05 14:46:13 +02:00
function send_update(team){
io.to(team.id).emit('moving', {"id": team.id, "color": color(team), "position": team.pos});
for(other_id in equipes)
if(other_id != team.id){
if(team.state.tracker)
io.to(team.id).emit('moving', apparent_info_tracker(equipes[other_id]));
else if(team.state.mallette)
io.to(team.id).emit('moving', apparent_info_mallette(equipes[other_id]));
else if(team.state.npc != 0)
io.to(team.id).emit('moving', {"id": team_id, "color": admin_color(equipe), "position": equipe.pos});
else
io.to(team.id).emit('moving', apparent_info_agent(equipes[other_id]));
}
}
2023-06-08 16:50:15 +02:00
// connect a socket to the room corresponding to its team and send it infos
function team_join(team, socket){
socket.join(team.id);
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)
2023-09-05 14:46:13 +02:00
if(other_id != team.id){
if(team.state.tracker)
socket.emit('moving', apparent_info_tracker(equipes[other_id]));
else if(team.state.mallette)
socket.emit('moving', apparent_info_mallette(equipes[other_id]));
else if(team.state.npc != 0)
socket.emit('moving', {"id": team_id, "color": admin_color(equipe), "position": equipe.pos});
else
socket.emit('moving', apparent_info_agent(equipes[other_id]));
}
2023-06-06 17:20:40 +02:00
}
console.log("Setup handlers");
io.on('connection', function(socket){
2023-06-16 20:56:58 +02:00
if(socket.handshake.auth.type == "conscrit") {
var id = socket.handshake.auth.id;
var equipe = equipes[id]
2023-06-16 20:56:58 +02:00
socket.join("Tracking");
2023-09-05 14:46:13 +02:00
if(equipe.state.tracker)
socket.join("Tracker");
if(equipe.state.mallette)
socket.join("mallette");
team_join(equipe, socket);
2023-06-16 20:56:58 +02:00
socket.on("useCode", function(code){
if(code in equipe.codes && equipe.codes[code] > 0){
equipe.codes[code] -= 1;
equipe.state[code] = true;
io.to(id).emit('popup', {content: MSG_CODES[code]});
io.to(id).emit('setCodes', equipe.codes);
emit_update(id);
2023-06-16 20:56:58 +02:00
setTimeout(function(eq, c){
eq.state[c] = false;
emit_update(eq.id);
2023-06-16 20:56:58 +02:00
}, bonus_delay, equipe, code);
}
});
}
if(socket.handshake.auth.type == "vieux"){
var id = socket.handshake.auth.id;
var equipe = equipes[id]
2023-09-05 14:46:13 +02:00
socket.join("npc");
2023-06-16 20:56:58 +02:00
team_join(equipe, socket);
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){
2023-06-16 20:56:58 +02:00
equipes[d.id].codes[d.code] += 1;
io.to(d.id).emit('setCodes', equipes[d.id].codes);
admin.emit('update', equipes[d.id]);
});
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;
2023-09-05 14:46:13 +02:00
var join = [];
var leave = [];
if(d.state.tracker)
join.push("Tracker");
else
leave.push("Tracker");
if(d.state.mallette){
join.push("mallette");
} else{
leave.push("mallette");
}
join_leave(d.id, join, leave);
send_update(equipes[d.id]);
2023-06-11 17:42:48 +02:00
emit_update(d.id);
if(equipes[d.id].vieux)
io.to(d.id).emit('newState', d.state);
2023-06-11 15:19:03 +02:00
});
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
}
});
console.log("Launch server");
server.listen(config.port, "::");
console.log("Running !");