301 lines
9.1 KiB
JavaScript
301 lines
9.1 KiB
JavaScript
/* struct equipe
|
|
{
|
|
"id" : string,
|
|
"pos" : [lat : float, long : float],
|
|
"color" : int,
|
|
"state" : {
|
|
"shown" : bool,
|
|
"tracker" : bool,
|
|
"tracked" : bool,
|
|
"npc" : bool
|
|
}
|
|
}
|
|
|
|
Les messages à transmettre par le client :
|
|
- position, HTTP "/log?id=%ID&lat=%LAT&lon=%LON"
|
|
- code(code)
|
|
Les messages à transmettre par le serveur :
|
|
- moving(id, color, position)
|
|
*/
|
|
|
|
// 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
|
|
var PWD_TRACKED = "tracked";
|
|
var PWD_TRACKER = "tracker";
|
|
var PWD_INVISIBLE = "invisible";
|
|
var MSG_BAD = "Code Incorrect";
|
|
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 = 3*60*1000;
|
|
|
|
var equipes = {};
|
|
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);
|
|
//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 + ")");
|
|
|
|
if(!(q.query.id in equipes))
|
|
equipes[q.query.id] = default_team(q.query.id);
|
|
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("Setup io server");
|
|
const { Server } = require("socket.io");
|
|
var io = new Server(server);
|
|
|
|
/////////////////
|
|
// Tracking room
|
|
//
|
|
// Everyone in this room is located
|
|
// sub-rooms :
|
|
// * "tracked" room for all tracked
|
|
// * "tracker" room for all tracker
|
|
// * "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");
|
|
|
|
// apparent information of a team, for other only
|
|
function apparent_info(equipe){
|
|
if(equipe.state.shown){
|
|
return {"id": equipe.id, "color": equipe.color, "position": equipe.pos};
|
|
} else {
|
|
return {"id": equipe.id, "color": equipe.color, "position": [0,0]};
|
|
}
|
|
}
|
|
|
|
function emit_update(team_id) {
|
|
var equipe = equipes[team_id];
|
|
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": equipe.color, "position": equipe.pos});
|
|
}
|
|
|
|
// produces a team object populated with default values
|
|
function default_team(team_id) {
|
|
var equipe = {};
|
|
var state = {};
|
|
state.shown = true;
|
|
state.tracker = false;
|
|
state.tracked = true;
|
|
state.npc = false;
|
|
equipe.state = state;
|
|
equipe.pos = [0,0];
|
|
equipe.color = 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_id, socket){
|
|
socket.join(team_id);
|
|
var equipe = equipes[team_id]
|
|
var state = equipe.state;
|
|
if(state.tracker) socket.join("tracker");
|
|
if(state.tracked) socket.join("tracked");
|
|
if(state.npc) socket.join("npc");
|
|
socket.emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos});
|
|
for(other_id in equipes)
|
|
if(other_id != team_id)
|
|
socket.emit('moving', apparent_info(equipes[other_id]));
|
|
}
|
|
|
|
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");
|
|
if(!(id in equipes))
|
|
equipes[id] = default_team(id);
|
|
team_join(id, socket);
|
|
|
|
socket.on("code", function(d){ /*TODO*/ });
|
|
}
|
|
|
|
if(socket.handshake.auth.type == "vieux"){
|
|
//TODO
|
|
}
|
|
|
|
if(socket.handshake.auth.type == "Admin"){
|
|
socket.join("Admin");
|
|
|
|
//TODO
|
|
|
|
for(i in equipes){
|
|
var equipe = equipes[i];
|
|
socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": equipe.pos});
|
|
}
|
|
}
|
|
|
|
|
|
//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 !");
|