diff --git a/src/lib/utils/getDistanceBetween.ts b/src/lib/utils/getDistanceBetween.ts new file mode 100644 index 0000000..64335a3 --- /dev/null +++ b/src/lib/utils/getDistanceBetween.ts @@ -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; + } + } +} \ No newline at end of file diff --git a/src/pages/api/fleet/send.ts b/src/pages/api/fleet/send.ts index 8b540b8..7f72581 100644 --- a/src/pages/api/fleet/send.ts +++ b/src/pages/api/fleet/send.ts @@ -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,