mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
Refactored code to remove useless classes, fix #17
This commit is contained in:
@@ -1,21 +1,18 @@
|
||||
import { getDistanceFromLatLon, isInCircle } from "./map_utils.js";
|
||||
import { isInCircle } from "./map_utils.js";
|
||||
import { sendUpdatedTeamInformations, teamBroadcast } from "./team_socket.js";
|
||||
import { GameState } from "./game.js";
|
||||
import { secureAdminBroadcast } from "./admin_socket.js";
|
||||
import { game } from "./index.js";
|
||||
import game, {GameState} from "./game.js";
|
||||
import zone from "./zone_manager.js";
|
||||
|
||||
export class PenaltyController {
|
||||
constructor() {
|
||||
export default {
|
||||
outOfBoundsSince: {},
|
||||
checkIntervalId: null,
|
||||
settings: {
|
||||
allowedTimeOutOfZone: 10,
|
||||
allowedTimeBetweenPositionUpdate: 10,
|
||||
//Number of penalties needed to be eliminated
|
||||
this.game = game;
|
||||
this.outOfBoundsSince = {};
|
||||
this.checkIntervalId = null;
|
||||
this.settings = {
|
||||
allowedTimeOutOfZone: 10,
|
||||
allowedTimeBetweenPositionUpdate: 10,
|
||||
maxPenalties: 3
|
||||
}
|
||||
}
|
||||
maxPenalties: 3
|
||||
},
|
||||
|
||||
start() {
|
||||
this.outOfBoundsSince = {};
|
||||
@@ -24,12 +21,12 @@ export class PenaltyController {
|
||||
}
|
||||
//Watch periodically if all teams need are following the rules
|
||||
this.checkIntervalId = setInterval(() => {
|
||||
if (this.game.state == GameState.PLAYING) {
|
||||
if (game.state == GameState.PLAYING) {
|
||||
this.watchPositionUpdate();
|
||||
this.watchZone();
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
},
|
||||
|
||||
stop() {
|
||||
this.outOfBoundsSince = {};
|
||||
@@ -37,7 +34,7 @@ export class PenaltyController {
|
||||
clearInterval(this.checkIntervalId)
|
||||
this.checkIntervalId = null;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
updateSettings(newSettings) {
|
||||
//Sanitize input
|
||||
@@ -47,14 +44,14 @@ export class PenaltyController {
|
||||
|
||||
this.settings = { ...this.settings, ...newSettings };
|
||||
return true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
addPenalty(teamId) {
|
||||
let team = this.game.getTeam(teamId);
|
||||
let team = game.getTeam(teamId);
|
||||
if (!team) {
|
||||
return;
|
||||
}
|
||||
@@ -63,7 +60,7 @@ export class PenaltyController {
|
||||
}
|
||||
team.penalties++;
|
||||
if (team.penalties >= this.settings.maxPenalties) {
|
||||
this.game.capture(team.id);
|
||||
game.capture(team.id);
|
||||
sendUpdatedTeamInformations(teamId);
|
||||
sendUpdatedTeamInformations(team.chased);
|
||||
teamBroadcast(teamId, "warning", "You have been eliminated (reason: too many penalties)")
|
||||
@@ -72,29 +69,29 @@ export class PenaltyController {
|
||||
teamBroadcast(teamId, "warning", `You recieved a penalty (${team.penalties}/${this.settings.maxPenalties})`)
|
||||
sendUpdatedTeamInformations(teamId);
|
||||
}
|
||||
secureAdminBroadcast("teams", this.game.teams)
|
||||
}
|
||||
secureAdminBroadcast("teams", game.teams)
|
||||
},
|
||||
|
||||
checkPenalties() {
|
||||
for (let team of game.teams) {
|
||||
if (team.penalties >= this.settings.maxPenalties) {
|
||||
this.game.capture(team.id);
|
||||
game.capture(team.id);
|
||||
sendUpdatedTeamInformations(team.id);
|
||||
sendUpdatedTeamInformations(team.chased);
|
||||
teamBroadcast(team.id, "warning", "You have been eliminated (reason: too many penalties)")
|
||||
teamBroadcast(team.chased, "success", "The team you were chasing has been eliminated")
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
watchZone() {
|
||||
this.game.teams.forEach((team) => {
|
||||
game.teams.forEach((team) => {
|
||||
if (team.captured) { return }
|
||||
//All the informations are not ready yet
|
||||
if (team.currentLocation == null || this.game.zone.currentZone == null) {
|
||||
if (team.currentLocation == null || zone.currentZone == null) {
|
||||
return;
|
||||
}
|
||||
if (!isInCircle({ lat: team.currentLocation[0], lng: team.currentLocation[1] }, this.game.zone.currentZone.center, this.game.zone.currentZone.radius)) {
|
||||
if (!isInCircle({ lat: team.currentLocation[0], lng: team.currentLocation[1] }, zone.currentZone.center, zone.currentZone.radius)) {
|
||||
//The team was not previously out of the zone
|
||||
if (!this.outOfBoundsSince[team.id]) {
|
||||
this.outOfBoundsSince[team.id] = new Date();
|
||||
@@ -111,11 +108,11 @@ export class PenaltyController {
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
watchPositionUpdate() {
|
||||
this.game.teams.forEach((team) => {
|
||||
game.teams.forEach((team) => {
|
||||
//If the team has not sent their location for more than the allowed period, automatically send it and add a penalty
|
||||
if (team.captured) { return }
|
||||
if (team.locationSendDeadline == null) {
|
||||
@@ -124,12 +121,12 @@ export class PenaltyController {
|
||||
}
|
||||
if (new Date() > team.locationSendDeadline) {
|
||||
this.addPenalty(team.id);
|
||||
this.game.sendLocation(team.id);
|
||||
game.sendLocation(team.id);
|
||||
sendUpdatedTeamInformations(team.id);
|
||||
secureAdminBroadcast("teams", this.game.teams)
|
||||
}else if(Math.abs(new Date() - team.locationSendDeadline - 60 * 1000) < 100) {
|
||||
secureAdminBroadcast("teams", game.teams)
|
||||
} else if (Math.abs(new Date() - team.locationSendDeadline - 60 * 1000) < 100) {
|
||||
teamBroadcast(team.id, "warning", `You have one minute left to udpate your location.`)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user