mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 02:10:18 +01:00
game state handling in back end
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user