diff --git a/traque-back/admin_socket.js b/traque-back/admin_socket.js index c68f37e..70a4d25 100644 --- a/traque-back/admin_socket.js +++ b/traque-back/admin_socket.js @@ -32,10 +32,11 @@ export function initAdminSocketHandler() { //Admin namespace io.of("admin").on("connection", (socket) => { //Flag to check if the user is logged in, defined for each socket + console.log("Connection of an admin"); let loggedIn = false; socket.on("disconnect", () => { - console.log("user disconnected"); + console.log("Disconnection of an admin"); //Remove the socket from the logged in sockets array loggedInSockets = loggedInSockets.filter(s => s !== socket.id); }); diff --git a/traque-back/game.js b/traque-back/game.js index 5f54088..2568526 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -5,6 +5,7 @@ import { secureAdminBroadcast } from "./admin_socket.js"; import { isInCircle } from "./map_utils.js"; import { playersBroadcast, sendUpdatedTeamInformations } from "./team_socket.js"; +import timeoutHandler from "./timeoutHandler.js"; import penaltyController from "./penalty_controller.js"; import zoneManager from "./zone_manager.js"; @@ -70,6 +71,7 @@ export default { if (newState != GameState.PLAYING) { zoneManager.reset(); penaltyController.stop(); + timeoutHandler.endAllSendPositionTimeout(); } //Game reset if (newState == GameState.SETUP) { @@ -253,7 +255,8 @@ export default { initLastSentLocations() { for (let team of this.teams) { 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; sendUpdatedTeamInformations(team.id); } @@ -277,11 +280,13 @@ export default { return false; } 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; if (this.getTeam(team.chasing) != null) { team.enemyLocation = this.getTeam(team.chasing).lastSentLocation; } + sendUpdatedTeamInformations(team.id); return team; }, @@ -297,6 +302,7 @@ export default { //remove the team from the list this.teams = this.teams.filter(t => t.id !== teamId); this.updateTeamChasing(); + timeoutHandler.endSendPositionTimeout(team.id); return true; }, @@ -325,6 +331,7 @@ export default { */ capture(teamId) { this.getTeam(teamId).captured = true + timeoutHandler.endSendPositionTimeout(teamId); this.updateTeamChasing(); }, @@ -355,6 +362,7 @@ export default { finishGame() { this.setState(GameState.FINISHED); zoneManager.reset(); + timeoutHandler.endAllSendPositionTimeout() playersBroadcast("game_state", this.state); }, } \ No newline at end of file diff --git a/traque-back/penalty_controller.js b/traque-back/penalty_controller.js index 695f0eb..8208f9d 100644 --- a/traque-back/penalty_controller.js +++ b/traque-back/penalty_controller.js @@ -32,7 +32,7 @@ export default { //Watch periodically if all teams need are following the rules this.checkIntervalId = setInterval(() => { if (game.state == GameState.PLAYING) { - this.watchPositionUpdate(); + //this.watchPositionUpdate(); this.watchZone(); } }, 100); diff --git a/traque-back/team_socket.js b/traque-back/team_socket.js index dcec1a1..bf37120 100644 --- a/traque-back/team_socket.js +++ b/traque-back/team_socket.js @@ -62,28 +62,28 @@ export function sendUpdatedTeamInformations(teamId) { }) }) } -export function initTeamSocket() { +export function initTeamSocket() { io.of("player").on("connection", (socket) => { + console.log("Connection of a player"); let teamId = null; - console.log("a user connected"); socket.on("disconnect", () => { - console.log("user disconnected"); + console.log("Disconnection of a player"); logoutPlayer(socket.id) }); socket.on("login", (loginTeamId, callback) => { - if (game.getTeam(loginTeamId) === undefined) { + let team = game.getTeam(loginTeamId); + if (team === undefined) { socket.emit("login_response", false); if (typeof callback === "function") { callback({ isLoggedIn: false, message: "Login denied" }); } } else { logoutPlayer(socket.id) - teamId = loginTeamId; - let team = game.getTeam(loginTeamId); team.sockets.push(socket.id); + teamId = loginTeamId; sendUpdatedTeamInformations(loginTeamId); socket.emit("login_response", true); socket.emit("game_state", game.state) @@ -132,18 +132,17 @@ export function initTeamSocket() { } game.updateTeamChasing(); teamBroadcast(teamId, "update_team", { enemyLocation: team.enemyLocation, locationSendDeadline: team.locationSendDeadline, lastSentLocation: team.lastSentLocation }); - teamBroadcast(teamId, "success", "Position udpated") secureAdminBroadcast("teams", game.teams) }); socket.on("capture", (captureCode, callback) => { - let capturedTeam = game.getTeam(teamId)?.chasing + let capturedTeam = game.getTeam(teamId)?.chasing; if (capturedTeam !== undefined && game.requestCapture(teamId, captureCode)) { sendUpdatedTeamInformations(teamId); sendUpdatedTeamInformations(capturedTeam); secureAdminBroadcast("teams", game.teams); if (typeof callback === "function") { - callback({ hasCaptured : true, message: "Capture succesful" }); + callback({ hasCaptured : true, message: "Capture successful" }); } } else { socket.emit("error", "Incorrect code"); diff --git a/traque-back/timeoutHandler.js b/traque-back/timeoutHandler.js new file mode 100644 index 0000000..ec58d6f --- /dev/null +++ b/traque-back/timeoutHandler.js @@ -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 = []; + } +}