From 9afdd5524887d399e8ee511a38922f9a08471cff Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Thu, 28 Mar 2024 23:33:51 +0000 Subject: [PATCH] game state handling in back end --- traque-back/game.js | 47 +++++++++++++++++++++++++++++++------------- traque-back/index.js | 35 +++++++++++---------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/traque-back/game.js b/traque-back/game.js index 7a69e03..bfd6c59 100644 --- a/traque-back/game.js +++ b/traque-back/game.js @@ -1,15 +1,22 @@ +const GameState = { + SETUP: "setup", + PLACEMENT: "placement", + PLAYING: "playing", + FINISHED: "finished" +} + export default class Game { constructor() { this.teams = []; - this.started = false; + this.state = GameState.SETUP; } - start() { - this.started = true; - } - - stop() { - this.started = false; + setState(newState) { + if(Object.values(GameState).indexOf(newState) == -1) { + return false; + } + this.state = newState; + return true; } getNewTeamId() { @@ -35,7 +42,8 @@ export default class Game { lastSentLocation: null, enemyLocation: null, captureCode: this.createCaptureCode(), - sockets: [] + sockets: [], + startingArea: null }); this.updateTeamChasing(); return true; @@ -64,14 +72,25 @@ export default class Game { return this.teams.find(t => t.id === teamId); } - renameTeam(teamId, newName) { - let team = this.getTeam(teamId); - if(team == undefined) { - return false; - } - team.name = newName; + updateTeam(teamId, newTeam) { + this.teams = this.teams.map((t) => { + if(t.id == teamId) { + return {...t, ...newTeam} + }else { + return t; + } + }) + console.log(this.teams) return true; } + // renameTeam(teamId, newName) { + // let team = this.getTeam(teamId); + // if(team == undefined) { + // return false; + // } + // team.name = newName; + // return true; + // } updateLocation(teamId, location) { let team = this.getTeam(teamId); diff --git a/traque-back/index.js b/traque-back/index.js index fb8ecf2..a43ebcc 100644 --- a/traque-back/index.js +++ b/traque-back/index.js @@ -64,6 +64,8 @@ io.of("admin").on("connection", (socket) => { socket.emit("login_response", true); loggedInSockets.push(socket.id); loggedIn = true; + //Send the current state + socket.emit("game_state", game.state) } else { //Attempt unsuccessful socket.emit("login_response", false); @@ -96,29 +98,16 @@ io.of("admin").on("connection", (socket) => { } }); - //User is attempting to start the game - socket.on("start_game", () => { + //User is attempting to change the game state + socket.on("change_state", (state) => { if(!loggedIn) { socket.emit("error", "Not logged in"); return; } - if(game.start()) { - secureBroadcast("game_started", true); + if(game.setState(state)) { + secureBroadcast("game_state", game.state); }else { - socket.emit("error", "Error starting game"); - } - }); - - //User is attempting to stop the game - socket.on("stop_game", () => { - if(!loggedIn) { - socket.emit("error", "Not logged in"); - return; - } - if(game.stop()) { - secureBroadcast("game_started", false); - }else { - socket.emit("error", "Error stopping game"); + socket.emit("error", "Error setting state"); } }); @@ -137,18 +126,15 @@ io.of("admin").on("connection", (socket) => { } }); - //Change the name of a team given its id - socket.on("rename_team", (teamId, newName) => { + socket.on("update_team", (teamId, newTeam) => { if(!loggedIn) { socket.emit("error", "Not logged in"); return; } - if(game.renameTeam(teamId, newName)) { + if(game.updateTeam(teamId, newTeam)) { secureBroadcast("teams", game.teams); - } else { - socket.emit("error", "Error renaming team"); } - }); + }) //Request an update of the team list //We only reply to the sender to prevent spam @@ -191,6 +177,7 @@ io.of("player").on("connection", (socket) => { socket.emit("login_response", true); socket.emit("enemy_position", team.enemyLocation); socket.emit("live_location", team.currentLocation); + socket.emit("name", team.name); }); socket.on("update_position", (position) => {