From 6393ee12dc287858895d890869373f0a99492819 Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Sat, 20 Apr 2024 11:22:58 +0000 Subject: [PATCH 1/2] game over detection system --- traque-back/game.js | 17 ++++++++++++++--- traque-back/index.js | 2 +- traque-back/penalty_controller.js | 10 +++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/traque-back/game.js b/traque-back/game.js index e5ed0fa..bcbb82f 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -1,5 +1,6 @@ +import { penaltyController } from "./index.js"; import { isInCircle } from "./map_utils.js"; -import { sendUpdatedTeamInformations } from "./team_socket.js"; +import { playersBroadcast, sendUpdatedTeamInformations } from "./team_socket.js"; import { ZoneManager } from "./zone_manager.js"; export const GameState = { @@ -35,6 +36,7 @@ export default class Game { } if (newState != GameState.PLAYING) { this.zone.reset(); + penaltyController.stop(); } this.state = newState; return true; @@ -86,7 +88,10 @@ export default class Game { } updateTeamChasing() { - if (this.playingTeamCount() <= 1) { + if (this.playingTeamCount() <= 2) { + if(this.state == GameState.PLAYING) { + this.finishGame() + } return false; } let firstTeam = null; @@ -187,7 +192,7 @@ export default class Game { requestCapture(teamId, captureCode) { let enemyTeam = this.getTeam(this.getTeam(teamId).chasing) if (enemyTeam && enemyTeam.captureCode == captureCode) { - this.capture(enemyTeam); + this.capture(enemyTeam.id); this.updateTeamChasing(); return true; } @@ -216,4 +221,10 @@ export default class Game { } return this.zone.udpateSettings(newSettings) } + + finishGame() { + this.setState(GameState.FINISHED); + this.zone.reset(); + playersBroadcast("game_state", this.state); + } } \ No newline at end of file diff --git a/traque-back/index.js b/traque-back/index.js index dabeade..2607865 100644 --- a/traque-back/index.js +++ b/traque-back/index.js @@ -48,7 +48,7 @@ function onUpdateZone(zone) { export const game = new Game(onUpdateZone, onUpdateNewZone); -const penaltyController = new PenaltyController(game); +export const penaltyController = new PenaltyController(); penaltyController.init() diff --git a/traque-back/penalty_controller.js b/traque-back/penalty_controller.js index b72d45c..843e1c5 100644 --- a/traque-back/penalty_controller.js +++ b/traque-back/penalty_controller.js @@ -3,10 +3,11 @@ import { getDistanceFromLatLon, isInCircle } from "./map_utils.js"; import { sendUpdatedTeamInformations, teamBroadcast } from "./team_socket.js"; import { GameState } from "./game.js"; import { secureAdminBroadcast } from "./admin_socket.js"; +import { game } from "./index.js"; config() export class PenaltyController { - constructor(game) { + constructor() { //Number of penalties needed to be eliminated this.game = game; this.outOfBoundsSince = {}; @@ -27,6 +28,13 @@ export class PenaltyController { }, 100); } + stop(){ + this.outOfBoundsSince = {}; + if(this.checkIntervalId) { + clearInterval(this.checkIntervalId) + this.checkIntervalId = null; + } + } /** * Increment the penalty score of a team, send a message to the team and eliminated if necessary * @param {Number} teamId The team that will recieve a penalty From 64748d26b35f5b4abc9d03adf2b3dbff61409b12 Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Sat, 20 Apr 2024 12:58:53 +0000 Subject: [PATCH 2/2] send zone info on socket connexion --- traque-back/admin_socket.js | 5 +++++ traque-back/game.js | 10 ++++++++++ traque-back/team_socket.js | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/traque-back/admin_socket.js b/traque-back/admin_socket.js index 212c111..3be25d6 100644 --- a/traque-back/admin_socket.js +++ b/traque-back/admin_socket.js @@ -46,6 +46,11 @@ export function initAdminSocketHandler() { socket.emit("game_state", game.state) //Other settings that need initialization socket.emit("zone_settings", game.zone.zoneSettings) + socket.emit("zone", game.zone.currentZone) + socket.emit("new_zone", { + begin: game.zone.currentStartZone, + end: game.zone.nextZone + }) } else { //Attempt unsuccessful diff --git a/traque-back/game.js b/traque-back/game.js index bcbb82f..7c381cf 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -1,3 +1,4 @@ +import { secureAdminBroadcast } from "./admin_socket.js"; import { penaltyController } from "./index.js"; import { isInCircle } from "./map_utils.js"; import { playersBroadcast, sendUpdatedTeamInformations } from "./team_socket.js"; @@ -38,6 +39,14 @@ export default class Game { this.zone.reset(); penaltyController.stop(); } + //Game reset + if(newState == GameState.SETUP) { + for(let team of this.teams) { + team.penalties = 0; + team.captured = false; + } + this.updateTeamChasing(); + } this.state = newState; return true; } @@ -111,6 +120,7 @@ export default class Game { this.getTeam(firstTeam).chased = previousTeam; this.getTeam(previousTeam).chasing = firstTeam; this.getTeam(previousTeam).enemyName = this.getTeam(firstTeam).name; + secureAdminBroadcast("teams", this.teams) return true; } diff --git a/traque-back/team_socket.js b/traque-back/team_socket.js index 8ac1056..8fcd726 100644 --- a/traque-back/team_socket.js +++ b/traque-back/team_socket.js @@ -75,6 +75,11 @@ export function initTeamSocket() { sendUpdatedTeamInformations(loginTeamId); socket.emit("login_response", true); socket.emit("game_state", game.state) + socket.emit("zone", game.zone.currentZone) + socket.emit("new_zone", { + begin: game.zone.currentStartZone, + end: game.zone.nextZone + }) }); socket.on("logout", () => {