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

View File

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

View File

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