backend for team capture

This commit is contained in:
2024-03-29 15:24:33 +00:00
parent 9d6f8e136f
commit 174db483c0
2 changed files with 39 additions and 7 deletions

View File

@@ -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;
}
}

View File

@@ -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")
}
})
});