Merge branch 'main' of github.com:quentinrsl/traque

This commit is contained in:
Quentin Roussel
2024-04-20 15:16:01 +02:00
5 changed files with 44 additions and 5 deletions

View File

@@ -46,6 +46,11 @@ export function initAdminSocketHandler() {
socket.emit("game_state", game.state) socket.emit("game_state", game.state)
//Other settings that need initialization //Other settings that need initialization
socket.emit("zone_settings", game.zone.zoneSettings) 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 { } else {
//Attempt unsuccessful //Attempt unsuccessful

View File

@@ -1,5 +1,7 @@
import { secureAdminBroadcast } from "./admin_socket.js";
import { penaltyController } from "./index.js";
import { isInCircle } from "./map_utils.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"; import { ZoneManager } from "./zone_manager.js";
export const GameState = { export const GameState = {
@@ -35,6 +37,15 @@ export default class Game {
} }
if (newState != GameState.PLAYING) { if (newState != GameState.PLAYING) {
this.zone.reset(); 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; this.state = newState;
return true; return true;
@@ -86,7 +97,10 @@ export default class Game {
} }
updateTeamChasing() { updateTeamChasing() {
if (this.playingTeamCount() <= 1) { if (this.playingTeamCount() <= 2) {
if(this.state == GameState.PLAYING) {
this.finishGame()
}
return false; return false;
} }
let firstTeam = null; let firstTeam = null;
@@ -106,6 +120,7 @@ export default class Game {
this.getTeam(firstTeam).chased = previousTeam; this.getTeam(firstTeam).chased = previousTeam;
this.getTeam(previousTeam).chasing = firstTeam; this.getTeam(previousTeam).chasing = firstTeam;
this.getTeam(previousTeam).enemyName = this.getTeam(firstTeam).name; this.getTeam(previousTeam).enemyName = this.getTeam(firstTeam).name;
secureAdminBroadcast("teams", this.teams)
return true; return true;
} }
@@ -187,7 +202,7 @@ export default class Game {
requestCapture(teamId, captureCode) { requestCapture(teamId, captureCode) {
let enemyTeam = this.getTeam(this.getTeam(teamId).chasing) let enemyTeam = this.getTeam(this.getTeam(teamId).chasing)
if (enemyTeam && enemyTeam.captureCode == captureCode) { if (enemyTeam && enemyTeam.captureCode == captureCode) {
this.capture(enemyTeam); this.capture(enemyTeam.id);
this.updateTeamChasing(); this.updateTeamChasing();
return true; return true;
} }
@@ -216,4 +231,10 @@ export default class Game {
} }
return this.zone.udpateSettings(newSettings) return this.zone.udpateSettings(newSettings)
} }
finishGame() {
this.setState(GameState.FINISHED);
this.zone.reset();
playersBroadcast("game_state", this.state);
}
} }

View File

@@ -48,7 +48,7 @@ function onUpdateZone(zone) {
export const game = new Game(onUpdateZone, onUpdateNewZone); export const game = new Game(onUpdateZone, onUpdateNewZone);
const penaltyController = new PenaltyController(game); export const penaltyController = new PenaltyController();
penaltyController.init() penaltyController.init()

View File

@@ -3,10 +3,11 @@ import { getDistanceFromLatLon, isInCircle } from "./map_utils.js";
import { sendUpdatedTeamInformations, teamBroadcast } from "./team_socket.js"; import { sendUpdatedTeamInformations, teamBroadcast } from "./team_socket.js";
import { GameState } from "./game.js"; import { GameState } from "./game.js";
import { secureAdminBroadcast } from "./admin_socket.js"; import { secureAdminBroadcast } from "./admin_socket.js";
import { game } from "./index.js";
config() config()
export class PenaltyController { export class PenaltyController {
constructor(game) { constructor() {
//Number of penalties needed to be eliminated //Number of penalties needed to be eliminated
this.game = game; this.game = game;
this.outOfBoundsSince = {}; this.outOfBoundsSince = {};
@@ -27,6 +28,13 @@ export class PenaltyController {
}, 100); }, 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 * 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 * @param {Number} teamId The team that will recieve a penalty

View File

@@ -75,6 +75,11 @@ export function initTeamSocket() {
sendUpdatedTeamInformations(loginTeamId); sendUpdatedTeamInformations(loginTeamId);
socket.emit("login_response", true); socket.emit("login_response", true);
socket.emit("game_state", game.state) 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", () => { socket.on("logout", () => {