From 174db483c03512ef19ffc9dabccdcc640327fe58 Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Fri, 29 Mar 2024 15:24:33 +0000 Subject: [PATCH] backend for team capture --- traque-back/game.js | 32 ++++++++++++++++++++++++++------ traque-back/index.js | 14 +++++++++++++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/traque-back/game.js b/traque-back/game.js index f8e610f..57c95ff 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -51,6 +51,7 @@ export default class Game { sockets: [], startingArea: null, ready: false, + captured: false, }); this.updateTeamChasing(); return true; @@ -60,13 +61,21 @@ export default class Game { if(this.teams.length <= 1) { return false; } - this.teams[0].chased = this.teams[this.teams.length - 1].id; - this.teams[this.teams.length - 1].chasing = this.teams[0].id; - - for(let i = 0; i < this.teams.length - 1; i++) { - this.teams[i].chasing = this.teams[i + 1].id; - this.teams[i+1].chased = this.teams[i].id; + let firstTeam = null; + let previousTeam = null + for(let i = 0; i < this.teams.length; i++ ) { + if(!this.teams[i].captured) { + if(previousTeam != null) { + this.teams[i].chased = previousTeam; + this.getTeam(previousTeam).chasing = this.teams[i].id; + }else { + firstTeam = this.teams[i].id; + } + previousTeam = this.teams[i].id + } } + this.getTeam(firstTeam).chased = previousTeam; + this.getTeam(previousTeam).chasing =firstTeam; return true; } @@ -87,6 +96,7 @@ export default class Game { return t; } }) + this.updateTeamChasing(); return true; } @@ -130,4 +140,14 @@ export default class Game { this.updateTeamChasing(); return true; } + + capture(teamId, captureCode) { + let enemyTeam = this.getTeam(this.getTeam(teamId).chasing) + if(enemyTeam.captureCode == captureCode) { + enemyTeam.captured = true; + this.updateTeamChasing(); + return true; + } + return false; + } } \ No newline at end of file diff --git a/traque-back/index.js b/traque-back/index.js index c3165c4..64dfd20 100644 --- a/traque-back/index.js +++ b/traque-back/index.js @@ -184,7 +184,8 @@ function sendUpdatedTeamInformations(teamId) { lastSentLocation: team.lastSentLocation, captureCode: team.captureCode, startingArea: team.startingArea, - ready: team.ready + ready: team.ready, + captured: team.captured }) }) } @@ -228,6 +229,7 @@ io.of("player").on("connection", (socket) => { if (team.sockets.indexOf(socket.id) == 0) { game.updateLocation(teamId, position); teamBroadcast(teamId, "update_team", { currentLocation: team.currentLocation, ready: team.ready }); + secureBroadcast("teams", game.teams); } }); @@ -241,4 +243,14 @@ io.of("player").on("connection", (socket) => { game.updateTeamChasing(); teamBroadcast(teamId, "update_team", { enemyLocation: team.enemyLocation }); }); + + socket.on('capture', (captureCode) => { + if(game.capture(teamId, captureCode)) { + sendUpdatedTeamInformations(teamId) + sendUpdatedTeamInformations(game.getTeam(teamId).chasing) + secureBroadcast("teams", game.teams); + }else { + socket.emit("error", "Incorrect code") + } + }) });