cleanned code and fix #18

This commit is contained in:
catvayor 2023-06-08 16:50:15 +02:00
parent 68a4789afa
commit c6498211fe

View file

@ -3,8 +3,12 @@
"id" : string,
"pos" : [lat : float, long : float],
"color" : int,
"rooms" : room list,
"shown" : bool
"state" : {
"shown" : bool,
"tracker" : bool,
"tracked" : bool,
"npc" : bool
}
}
Les messages à transmettre par le client :
@ -64,6 +68,8 @@ var server = http.createServer(option, function(req, res){
//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);
@ -93,7 +99,7 @@ var io = new Server(server);
// Everyone in this room is located
// sub-rooms :
// * "tracked" room for all tracked
// * "tracking" room for all tracker
// * "tracker" room for all tracker
// * "npc" room for non-player
// * "%ID" room of a team
//
@ -115,15 +121,49 @@ var tracking = io.to("Tracking");
// }
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];
if (equipe.shown){
tracking.emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos});
} else{
tracking.expect(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": [0,0]});
io.to(team_id).emit('moving', {"id": team_id, "color": equipe.color, "position": equipe.pos});
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});
}
admin.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");
@ -134,29 +174,11 @@ io.on('connection', function(socket){
console.log("connection of " + id + " !");
socket.join("Tracking");
socket.join(id);
if(!(id in equipes)){
var equipe = {};
equipe.shown = true;
equipe.pos = [0,0];
equipe.color = 0;
equipe.id = id;
equipe.rooms = ["tracked"];
equipes[id] = equipe;
}
for(room of equipes[id].rooms)
socket.join(room);
if(!(id in equipes))
equipes[id] = default_team(id);
team_join(id, socket);
socket.on("code", function(d){ /*TODO*/ });
for(i in equipes){
var equipe = equipes[i];
if (equipe.shown){
socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": equipe.pos});
} else{
socket.emit('moving', {"id": equipe.id, "color": equipe.color, "position": [0,0]});
}
}
}
if(socket.handshake.auth.type == "vieux"){