fixed in placement zone detection

This commit is contained in:
2024-04-19 22:34:50 +00:00
parent bde37e4676
commit fac8c2002c
3 changed files with 54 additions and 51 deletions

View File

@@ -10,26 +10,26 @@ export const GameState = {
}
export default class Game {
constructor(onUpdateZone,onUpdateNewZone) {
constructor(onUpdateZone, onUpdateNewZone) {
this.teams = [];
this.state = GameState.SETUP;
this.zone = new ZoneManager(onUpdateZone, onUpdateNewZone)
}
setState(newState) {
if(Object.values(GameState).indexOf(newState) == -1) {
if (Object.values(GameState).indexOf(newState) == -1) {
return false;
}
//The game has started
if(newState == GameState.PLAYING) {
if(!this.zone.ready()) {
if (newState == GameState.PLAYING) {
if (!this.zone.ready()) {
return false;
}
this.initLastSentLocations();
this.zone.reset()
this.zone.start()
}
if(newState != GameState.PLAYING) {
if (newState != GameState.PLAYING) {
this.zone.reset();
}
this.state = newState;
@@ -38,7 +38,7 @@ export default class Game {
getNewTeamId() {
let id = null;
while(id === null || this.teams.find(t => t.id === id)) {
while (id === null || this.teams.find(t => t.id === id)) {
id = Math.floor(Math.random() * 1_000_000);
}
return id;
@@ -73,7 +73,7 @@ export default class Game {
playingTeamCount() {
let res = 0;
this.teams.forEach((t) => {
if(!t.captured) {
if (!t.captured) {
res++;
}
})
@@ -81,24 +81,24 @@ export default class Game {
}
updateTeamChasing() {
if(this.playingTeamCount() <= 1) {
if (this.playingTeamCount() <= 1) {
return false;
}
let firstTeam = null;
let previousTeam = null
for(let i = 0; i < this.teams.length; i++ ) {
if(!this.teams[i].captured) {
if(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 {
} else {
firstTeam = this.teams[i].id;
}
previousTeam = this.teams[i].id
}
}
this.getTeam(firstTeam).chased = previousTeam;
this.getTeam(previousTeam).chasing =firstTeam;
this.getTeam(previousTeam).chasing = firstTeam;
return true;
}
@@ -113,25 +113,25 @@ export default class Game {
updateTeam(teamId, newTeam) {
this.teams = this.teams.map((t) => {
if(t.id == teamId) {
return {...t, ...newTeam}
}else {
if (t.id == teamId) {
return { ...t, ...newTeam }
} else {
return t;
}
})
this.updateTeamChasing();
return true;
}
updateLocation(teamId, location) {
let team = this.getTeam(teamId);
if(team == undefined) {
if (team == undefined) {
return false;
}
team.currentLocation = location;
//Update the team ready status if they are in their starting area
if(this.state == GameState.PLACEMENT && team.startingArea && team.startingArea && location) {
team.ready = isInCircle(location, [team.startingArea.center.lat, team.startingArea.center.lng], team.startingArea.radius)
if (this.state == GameState.PLACEMENT && team.startingArea && team.startingArea && location) {
team.ready = isInCircle({ lat: location[0], lng: location[1] }, team.startingArea.center, team.startingArea.radius)
}
return true;
}
@@ -139,7 +139,7 @@ export default class Game {
//Make it so that when a team requests the location of a team that has never sent their locaiton
//Their position at the begining of the game is sent
initLastSentLocations() {
for(let team of this.teams) {
for (let team of this.teams) {
team.lastSentLocation = team.currentLocation;
team.locationSendDeadline = Number(new Date()) + process.env.ALLOWED_TIME_BETWEEN_POSITION_UPDATE_IN_MINUTES * 60 * 1000;
sendUpdatedTeamInformations(team.id);
@@ -148,19 +148,19 @@ export default class Game {
sendLocation(teamId) {
let team = this.getTeam(teamId);
if(team == undefined) {
if (team == undefined) {
return false;
}
team.locationSendDeadline = Number(new Date()) + process.env.ALLOWED_TIME_BETWEEN_POSITION_UPDATE_IN_MINUTES * 60 * 1000;
team.lastSentLocation = team.currentLocation;
if(this.getTeam(team.chasing) != null) {
if (this.getTeam(team.chasing) != null) {
team.enemyLocation = this.getTeam(team.chasing).lastSentLocation;
}
return team;
}
removeTeam(teamId) {
if(this.getTeam(teamId) == undefined) {
if (this.getTeam(teamId) == undefined) {
return false;
}
//remove the team from the list
@@ -179,7 +179,7 @@ export default class Game {
*/
requestCapture(teamId, captureCode) {
let enemyTeam = this.getTeam(this.getTeam(teamId).chasing)
if(enemyTeam.captureCode == captureCode) {
if (enemyTeam.captureCode == captureCode) {
this.capture(enemyTeam);
this.updateTeamChasing();
return true;
@@ -204,7 +204,7 @@ export default class Game {
*/
setZoneSettings(newSettings) {
//cannot change zones while playing
if(this.state == GameState.PLAYING || this.state == GameState.FINISHED) {
if (this.state == GameState.PLAYING || this.state == GameState.FINISHED) {
return false;
}
return this.zone.udpateSettings(newSettings)

View File

@@ -1,19 +1,19 @@
export function getDistanceFromLatLon({lat: lat1, lng: lon1}, {lat: lat2, lng: lon2}) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d * 1000;
export function getDistanceFromLatLon({ lat: lat1, lng: lon1 }, { lat: lat2, lng: lon2 }) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
return d * 1000;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
return deg * (Math.PI / 180)
}
export function isInCircle(position, center, radius) {

View File

@@ -19,7 +19,7 @@ const storage = multer.diskStorage({
});
function clean() {
const files = fs.readdirSync(UPLOAD_DIR);
for (const file of files) {
@@ -31,7 +31,7 @@ function clean() {
const upload = multer({
storage,
fileFilter: function (req, file, callback) {
if ( ALLOWED_MIME.indexOf(file.mimetype) == -1) {
if (ALLOWED_MIME.indexOf(file.mimetype) == -1) {
callback(null, false);
} else if (!game.getTeam(Number(req.query.team))) {
callback(null, false);
@@ -45,25 +45,28 @@ const upload = multer({
export function initPhotoUpload() {
clean();
app.post("/upload", upload.single('file'), (req, res) => {
res.set("Access-Control-Allow-Origin", "*");
console.log("upload", req.query)
res.send("")
})
app.get("/photo/my", (req,res) => {
app.get("/photo/my", (req, res) => {
let team = game.getTeam(Number(req.query.team));
if(team) {
res.set("Content-Type","image/png")
if (team) {
res.set("Content-Type", "image/png")
res.set("Access-Control-Allow-Origin", "*");
res.sendFile(process.cwd() + "/" + UPLOAD_DIR + "/" + team.id);
}else {
res.send(400,"Team not found")
} else {
res.send(400, "Team not found")
}
})
app.get("/photo/enemy", (req,res) => {
app.get("/photo/enemy", (req, res) => {
let team = game.getTeam(Number(req.query.team));
if(team) {
res.set("Content-Type","image/png")
if (team) {
res.set("Content-Type", "image/png")
res.set("Access-Control-Allow-Origin", "*");
res.sendFile(process.cwd() + "/" + UPLOAD_DIR + "/" + team.chasing);
}else {
res.send(400,"Team not found")
} else {
res.send(400, "Team not found")
}
})
}