Suppression pénalités d'envoi de position

This commit is contained in:
Sébastien Rivière
2025-06-17 23:46:25 +02:00
parent 41f8caf802
commit c6066bc234
5 changed files with 51 additions and 13 deletions

View File

@@ -32,10 +32,11 @@ export function initAdminSocketHandler() {
//Admin namespace //Admin namespace
io.of("admin").on("connection", (socket) => { io.of("admin").on("connection", (socket) => {
//Flag to check if the user is logged in, defined for each socket //Flag to check if the user is logged in, defined for each socket
console.log("Connection of an admin");
let loggedIn = false; let loggedIn = false;
socket.on("disconnect", () => { socket.on("disconnect", () => {
console.log("user disconnected"); console.log("Disconnection of an admin");
//Remove the socket from the logged in sockets array //Remove the socket from the logged in sockets array
loggedInSockets = loggedInSockets.filter(s => s !== socket.id); loggedInSockets = loggedInSockets.filter(s => s !== socket.id);
}); });

View File

@@ -5,6 +5,7 @@ import { secureAdminBroadcast } from "./admin_socket.js";
import { isInCircle } from "./map_utils.js"; import { isInCircle } from "./map_utils.js";
import { playersBroadcast, sendUpdatedTeamInformations } from "./team_socket.js"; import { playersBroadcast, sendUpdatedTeamInformations } from "./team_socket.js";
import timeoutHandler from "./timeoutHandler.js";
import penaltyController from "./penalty_controller.js"; import penaltyController from "./penalty_controller.js";
import zoneManager from "./zone_manager.js"; import zoneManager from "./zone_manager.js";
@@ -70,6 +71,7 @@ export default {
if (newState != GameState.PLAYING) { if (newState != GameState.PLAYING) {
zoneManager.reset(); zoneManager.reset();
penaltyController.stop(); penaltyController.stop();
timeoutHandler.endAllSendPositionTimeout();
} }
//Game reset //Game reset
if (newState == GameState.SETUP) { if (newState == GameState.SETUP) {
@@ -253,7 +255,8 @@ export default {
initLastSentLocations() { initLastSentLocations() {
for (let team of this.teams) { for (let team of this.teams) {
team.lastSentLocation = team.currentLocation; team.lastSentLocation = team.currentLocation;
team.locationSendDeadline = Number(new Date()) + penaltyController.settings.allowedTimeBetweenPositionUpdate * 60 * 1000; team.locationSendDeadline = Date.now() + penaltyController.settings.allowedTimeBetweenPositionUpdate * 60 * 1000;
timeoutHandler.setSendPositionTimeout(team.id, team.locationSendDeadline);
this.getTeam(team.chasing).enemyLocation = team.lastSentLocation; this.getTeam(team.chasing).enemyLocation = team.lastSentLocation;
sendUpdatedTeamInformations(team.id); sendUpdatedTeamInformations(team.id);
} }
@@ -277,11 +280,13 @@ export default {
return false; return false;
} }
writeSeePosition(Date.now(), teamId, team.chasing); writeSeePosition(Date.now(), teamId, team.chasing);
team.locationSendDeadline = Number(new Date()) + penaltyController.settings.allowedTimeBetweenPositionUpdate * 60 * 1000; team.locationSendDeadline = Date.now() + penaltyController.settings.allowedTimeBetweenPositionUpdate * 60 * 1000;
timeoutHandler.setSendPositionTimeout(team.id, team.locationSendDeadline);
team.lastSentLocation = team.currentLocation; team.lastSentLocation = team.currentLocation;
if (this.getTeam(team.chasing) != null) { if (this.getTeam(team.chasing) != null) {
team.enemyLocation = this.getTeam(team.chasing).lastSentLocation; team.enemyLocation = this.getTeam(team.chasing).lastSentLocation;
} }
sendUpdatedTeamInformations(team.id);
return team; return team;
}, },
@@ -297,6 +302,7 @@ export default {
//remove the team from the list //remove the team from the list
this.teams = this.teams.filter(t => t.id !== teamId); this.teams = this.teams.filter(t => t.id !== teamId);
this.updateTeamChasing(); this.updateTeamChasing();
timeoutHandler.endSendPositionTimeout(team.id);
return true; return true;
}, },
@@ -325,6 +331,7 @@ export default {
*/ */
capture(teamId) { capture(teamId) {
this.getTeam(teamId).captured = true this.getTeam(teamId).captured = true
timeoutHandler.endSendPositionTimeout(teamId);
this.updateTeamChasing(); this.updateTeamChasing();
}, },
@@ -355,6 +362,7 @@ export default {
finishGame() { finishGame() {
this.setState(GameState.FINISHED); this.setState(GameState.FINISHED);
zoneManager.reset(); zoneManager.reset();
timeoutHandler.endAllSendPositionTimeout()
playersBroadcast("game_state", this.state); playersBroadcast("game_state", this.state);
}, },
} }

View File

@@ -32,7 +32,7 @@ export default {
//Watch periodically if all teams need are following the rules //Watch periodically if all teams need are following the rules
this.checkIntervalId = setInterval(() => { this.checkIntervalId = setInterval(() => {
if (game.state == GameState.PLAYING) { if (game.state == GameState.PLAYING) {
this.watchPositionUpdate(); //this.watchPositionUpdate();
this.watchZone(); this.watchZone();
} }
}, 100); }, 100);

View File

@@ -62,28 +62,28 @@ export function sendUpdatedTeamInformations(teamId) {
}) })
}) })
} }
export function initTeamSocket() {
export function initTeamSocket() {
io.of("player").on("connection", (socket) => { io.of("player").on("connection", (socket) => {
console.log("Connection of a player");
let teamId = null; let teamId = null;
console.log("a user connected");
socket.on("disconnect", () => { socket.on("disconnect", () => {
console.log("user disconnected"); console.log("Disconnection of a player");
logoutPlayer(socket.id) logoutPlayer(socket.id)
}); });
socket.on("login", (loginTeamId, callback) => { socket.on("login", (loginTeamId, callback) => {
if (game.getTeam(loginTeamId) === undefined) { let team = game.getTeam(loginTeamId);
if (team === undefined) {
socket.emit("login_response", false); socket.emit("login_response", false);
if (typeof callback === "function") { if (typeof callback === "function") {
callback({ isLoggedIn: false, message: "Login denied" }); callback({ isLoggedIn: false, message: "Login denied" });
} }
} else { } else {
logoutPlayer(socket.id) logoutPlayer(socket.id)
teamId = loginTeamId;
let team = game.getTeam(loginTeamId);
team.sockets.push(socket.id); team.sockets.push(socket.id);
teamId = loginTeamId;
sendUpdatedTeamInformations(loginTeamId); sendUpdatedTeamInformations(loginTeamId);
socket.emit("login_response", true); socket.emit("login_response", true);
socket.emit("game_state", game.state) socket.emit("game_state", game.state)
@@ -132,18 +132,17 @@ export function initTeamSocket() {
} }
game.updateTeamChasing(); game.updateTeamChasing();
teamBroadcast(teamId, "update_team", { enemyLocation: team.enemyLocation, locationSendDeadline: team.locationSendDeadline, lastSentLocation: team.lastSentLocation }); teamBroadcast(teamId, "update_team", { enemyLocation: team.enemyLocation, locationSendDeadline: team.locationSendDeadline, lastSentLocation: team.lastSentLocation });
teamBroadcast(teamId, "success", "Position udpated")
secureAdminBroadcast("teams", game.teams) secureAdminBroadcast("teams", game.teams)
}); });
socket.on("capture", (captureCode, callback) => { socket.on("capture", (captureCode, callback) => {
let capturedTeam = game.getTeam(teamId)?.chasing let capturedTeam = game.getTeam(teamId)?.chasing;
if (capturedTeam !== undefined && game.requestCapture(teamId, captureCode)) { if (capturedTeam !== undefined && game.requestCapture(teamId, captureCode)) {
sendUpdatedTeamInformations(teamId); sendUpdatedTeamInformations(teamId);
sendUpdatedTeamInformations(capturedTeam); sendUpdatedTeamInformations(capturedTeam);
secureAdminBroadcast("teams", game.teams); secureAdminBroadcast("teams", game.teams);
if (typeof callback === "function") { if (typeof callback === "function") {
callback({ hasCaptured : true, message: "Capture succesful" }); callback({ hasCaptured : true, message: "Capture successful" });
} }
} else { } else {
socket.emit("error", "Incorrect code"); socket.emit("error", "Incorrect code");

View File

@@ -0,0 +1,30 @@
import game from "./game.js";
export default {
teams: [],
setSendPositionTimeout(teamID, deadline) {
const foundTeam = this.teams.find(t => t.teamID === teamID);
if (!foundTeam) {
this.teams.push({teamID: teamID, timeoutID: setTimeout(() => game.sendLocation(teamID), deadline - Date.now())});
} else {
clearTimeout(foundTeam.timeoutID);
foundTeam.timeoutID = setTimeout(() => game.sendLocation(teamID), deadline - Date.now());
}
},
endSendPositionTimeout(teamID) {
const foundTeam = this.teams.find(t => t.teamID === teamID);
if (foundTeam) {
clearTimeout(foundTeam.timeoutID);
this.teams = this.teams.filter(t => t.teamID !== teamID);
}
},
endAllSendPositionTimeout() {
for (const team of this.teams) {
clearTimeout(team.timeoutID);
}
this.teams = [];
}
}