Add fleet time calculation script

This commit is contained in:
Aelita4 2024-12-26 18:00:00 +01:00
parent db7abbb997
commit 4fb4a19b0b
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
2 changed files with 111 additions and 1 deletions

View File

@ -0,0 +1,107 @@
import { Sector } from "../classes/managers/LocationManager";
import { Planet } from "../classes/managers/PlanetManager";
import SystemManager from "../classes/managers/SystemManager";
const PLANET_TO_SYSTEM = 120;
const BETWEEN_PLANETS = 300;
const BETWEEN_SYSTEMS = 1000;
const BETWEEN_SECTORS = 10000;
const BETWEEN_GALAXIES = 100000;
export default function getDistanceBetween(a: Planet | SystemManager | Sector, b: Planet | SystemManager | Sector) {
if("fields" in a) { // a is planet
// b is planet
if("fields" in b) {
// same planet
if(a._id.equals(b._id)) return 0;
// same system
if(a.system.data._id.equals(b.system.data._id)) return BETWEEN_PLANETS;
// same sector
if(a.system.data.sector._id.equals(b.system.data.sector._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS;
// same galaxy
if(a.system.data.sector.galaxy._id.equals(b.system.data.sector.galaxy._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
// b is system
else if("planets" in b) {
// same system
if(a.system.data._id.equals(b.data._id)) return PLANET_TO_SYSTEM;
// same sector
if(a.system.data.sector._id.equals(b.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
// same galaxy
if(a.system.data.sector.galaxy._id.equals(b.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
// b is sector
else {
// same sector
if(a.system.data.sector._id.equals(b._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
// same galaxy
if(a.system.data.sector.galaxy._id.equals(b.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
} else if("planets" in a) { // a is system
// b is planet
if("fields" in b) {
// same system
if(a.data._id.equals(b.system.data._id)) return BETWEEN_PLANETS;
// same sector
if(a.data.sector._id.equals(b.system.data.sector._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS;
// same galaxy
if(a.data.sector.galaxy._id.equals(b.system.data.sector.galaxy._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
// b is system
else if("planets" in b) {
// same system
if(a.data._id.equals(b.data._id)) return 0;
// same sector
if(a.data.sector._id.equals(b.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
// same galaxy
if(a.data.sector.galaxy._id.equals(b.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
// b is sector
else {
// same sector
if(a.data.sector._id.equals(b._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
// same galaxy
if(a.data.sector.galaxy._id.equals(b.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
} else { // a is sector
// b is planet
if("fields" in b) {
// same sector
if(a._id.equals(b.system.data.sector._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS;
// same galaxy
if(a.galaxy._id.equals(b.system.data.sector.galaxy._id)) return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return BETWEEN_PLANETS + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
// b is system
else if("planets" in b) {
// same sector
if(a._id.equals(b.data.sector._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS;
// same galaxy
if(a.galaxy._id.equals(b.data.sector.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
// b is sector
else {
// same sector
if(a._id.equals(b._id)) return 0;
// same galaxy
if(a.galaxy._id.equals(b.galaxy._id)) return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS;
// different galaxy
return PLANET_TO_SYSTEM + BETWEEN_SYSTEMS + BETWEEN_SECTORS + BETWEEN_GALAXIES;
}
}
}

View File

@ -10,6 +10,7 @@ import MissionType from "../../../types/MissionType";
import { getAllResources } from "../../../lib/db/resources";
import FleetManager from "../../../lib/classes/managers/FleetManager";
import SystemManager from "../../../lib/classes/managers/SystemManager";
import getDistanceBetween from "../../../lib/utils/getDistanceBetween";
export const POST: APIRoute = async({ request }) => {
const response = await validateAccessToken(request);
@ -74,12 +75,14 @@ export const POST: APIRoute = async({ request }) => {
dest = checkDestination.planetOrSystem;
}
const flightTime = getDistanceBetween(source, dest);
const fleetManager = new FleetManager(
new ObjectId(),
source,
dest,
new Date(),
new Date(Date.now() + 1000 * 30), //TODO: calculate time based on distance
new Date(Date.now() + 1000 * flightTime),
false,
body.mission,
body.ships,