From 848f2c37dc3546033e8402e8574d7d5895cc5129 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Tue, 4 Feb 2025 16:47:15 +0100 Subject: [PATCH] Implement fuel consumption mechanic Front-end only for now --- src/components/ResourceBar.astro | 2 +- src/pages/api/fleet/getDistance.ts | 50 ++++++ src/pages/game/fleet.astro | 239 ++++++++++++++++++++++++++--- src/types/db/DBShip.ts | 1 + 4 files changed, 271 insertions(+), 21 deletions(-) create mode 100644 src/pages/api/fleet/getDistance.ts diff --git a/src/components/ResourceBar.astro b/src/components/ResourceBar.astro index c4d367e..bd2eb53 100644 --- a/src/components/ResourceBar.astro +++ b/src/components/ResourceBar.astro @@ -67,7 +67,7 @@ if(!(planet instanceof SystemManager)) { {resourceArray.map(res => -
x.id === res.id)?.type ?? "solid"} data-res-id={res.id} data-res-amount={res.amount} diff --git a/src/pages/api/fleet/getDistance.ts b/src/pages/api/fleet/getDistance.ts new file mode 100644 index 0000000..2803355 --- /dev/null +++ b/src/pages/api/fleet/getDistance.ts @@ -0,0 +1,50 @@ +import { APIRoute } from "astro"; +import parseParams from "../../../lib/utils/parseParams"; +import locationManager, { Sector } from "../../../lib/classes/managers/LocationManager"; +import SystemManager from "../../../lib/classes/managers/SystemManager"; +import { Planet } from "../../../lib/classes/managers/PlanetManager"; +import getDistanceBetween from "../../../lib/utils/getDistanceBetween"; + +export const GET: APIRoute = async({ request }) => { + const URLParams = parseParams(request.url); + if(!URLParams.source || !URLParams.destination) { + return new Response( + JSON.stringify({ + code: 400, + message: "Bad Request", + error: "Missing parameters" + }), { status: 400 } + ); + } + + const source = locationManager.findId(URLParams.source); + if(source === null) { + return new Response( + JSON.stringify({ + code: 404, + message: "Not Found", + error: "Source not found" + }), { status: 404 } + ); + } + + let destination: Planet | SystemManager | Sector | null = locationManager.findId(URLParams.destination); + if(destination === null) destination = locationManager.getSector(URLParams.destination) ?? null; + if(destination === null) { + return new Response( + JSON.stringify({ + code: 404, + message: "Not Found", + error: "Destination not found" + }), { status: 404 } + ); + } + + return new Response( + JSON.stringify({ + code: 200, + message: "OK", + distance: getDistanceBetween(source, destination) + }), { status: 200 } + ) +} \ No newline at end of file diff --git a/src/pages/game/fleet.astro b/src/pages/game/fleet.astro index 394a81b..36e8f96 100644 --- a/src/pages/game/fleet.astro +++ b/src/pages/game/fleet.astro @@ -11,6 +11,7 @@ import { getName } from '../../lib/utils/langDriver'; const { token, user, lang } = Astro.locals; const active: SystemManager | Planet = Astro.locals.active; +const activeId = active instanceof SystemManager ? active.data._id : active._id; const ships = await getAllShips(); const url = Astro.url.origin; @@ -82,10 +83,10 @@ for(const f of fleet) { if(source !== null) { if(source instanceof SystemManager) { if(source.data.ownedBy.id.equals(user.id)) own++; - else enemy++; + else if(!f.returning) enemy++; } else { if(source.system.data.ownedBy.id.equals(user.id)) own++; - else enemy++; + else if(!f.returning) enemy++; } } } @@ -152,14 +153,14 @@ const sectorsList = galaxies.map(galaxy => {
-
+

Sending fleet from {active instanceof SystemManager ? active.data.name : active.name}


Ships

{active.ships.ships.map(ship =>

{getName(lang, 'ships', ship.data.id)} - {ship.amount}

- +
)}

@@ -171,24 +172,37 @@ const sectorsList = galaxies.map(galaxy => {
-

Send to:

+

Send to

-

Galaxy

- -

Sector

- -

System

- -
-

Planet

- -
+
+

Galaxy

+ +
+
+

Sector

+ +
+
+

System

+ +
+
+
+

Planet

+ +
+

Cargo

-
+
+ Solid: 0
+ Liquid: 0
+ Gas: 0
+
+