Add ability to choose system as active

This commit is contained in:
Aelita4 2024-11-15 11:40:36 +01:00
parent 40499415be
commit 8f7ca6b9d2
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
3 changed files with 63 additions and 28 deletions

View File

@ -4,6 +4,7 @@ import { getHighestWeightedLanguage, getLocales, getName } from '../lib/utils/la
import { getAllResources } from '../lib/db/resources'; import { getAllResources } from '../lib/db/resources';
import locationManager from '../lib/classes/managers/LocationManager'; import locationManager from '../lib/classes/managers/LocationManager';
import { Resource } from '../lib/classes/managers/abstract/ResourceManager'; import { Resource } from '../lib/classes/managers/abstract/ResourceManager';
import SystemManager from '../lib/classes/managers/SystemManager';
const resourceTypes = await getAllResources(); const resourceTypes = await getAllResources();
@ -11,7 +12,7 @@ const lang = await getLocales(Astro.cookies.get('language')?.value ?? await getH
const planetId = new ObjectId(Astro.cookies.get('currentPlanet')?.value ?? ''); const planetId = new ObjectId(Astro.cookies.get('currentPlanet')?.value ?? '');
const planet = locationManager.getPlanet(planetId); const planet = locationManager.findId(planetId);
if(!planet) return; if(!planet) return;
@ -27,7 +28,7 @@ for(const key of planet.resources.resources) {
<div class="resourcebar-circle-id" data-type="solid"></div> <div class="resourcebar-circle-id" data-type="solid"></div>
</div> </div>
<div class="resourcebar-planetname"> <div class="resourcebar-planetname">
{planet.name} {planet instanceof SystemManager ? <span style="color: red;">{planet.data.name}</span> : planet.name}
</div> </div>
<div id="resourcebar-elements" class="resourcebar-elements"> <div id="resourcebar-elements" class="resourcebar-elements">
{resourceArray.map(res => {resourceArray.map(res =>
@ -148,8 +149,8 @@ for(const key of planet.resources.resources) {
function numWithPrefix(x: number) { function numWithPrefix(x: number) {
x = Math.floor(x); x = Math.floor(x);
if(x < 1_000) return x.toString(); if(x < 1_000) return x.toString();
if(x < 1_000_000) return (x / 1_000).toFixed(2) + "k"; if(x < 1_000_000) return (x / 1_000).toFixed(3) + "k";
if(x < 1_000_000_000) return (x / 1_000_000).toFixed(2) + "M"; if(x < 1_000_000_000) return (x / 1_000_000).toFixed(3) + "M";
return x.toString(); return x.toString();
} }

View File

@ -23,7 +23,7 @@ if(!user) return Astro.redirect('/logout');
const planetId = Astro.cookies.get('currentPlanet')?.value ?? ""; const planetId = Astro.cookies.get('currentPlanet')?.value ?? "";
if(planetId === "") return "No planet selected"; if(planetId === "") return "No planet selected";
const planet = locationManager.getPlanet(new ObjectId(planetId)); const planet = locationManager.findId(new ObjectId(planetId));
if(!planet) return "Planet not found"; if(!planet) return "Planet not found";
const ships = await getAllShips(); const ships = await getAllShips();
@ -47,7 +47,7 @@ if(Astro.request.method === "POST") {
}); });
const fleetData = { const fleetData = {
source: planet._id, source: planet instanceof SystemManager ? planet.data._id : planet._id,
destination: form.get('toSystem') ? form.get('destination-system')?.toString() : form.get('destination-planet')?.toString(), destination: form.get('toSystem') ? form.get('destination-system')?.toString() : form.get('destination-planet')?.toString(),
mission: form.get('mission')?.toString() ?? "NULL", mission: form.get('mission')?.toString() ?? "NULL",
ships: ships.map(ship => { ships: ships.map(ship => {
@ -86,27 +86,40 @@ let own = 0;
let friendly = 0; let friendly = 0;
let enemy = 0; let enemy = 0;
for(const system of userSystems) {
for(const planet of system.planets) {
for(const f of fleet) { for(const f of fleet) {
if(f.source.equals(planet._id) || f.source.equals(system.data._id)) own++; const source = locationManager.findId(f.source);
else if(f.destination.equals(planet._id) || f.destination.equals(system.data._id)) { if(source !== null) {
if(f.mission === 'ATTACK') enemy++; if(source instanceof SystemManager) {
else { if(source.data.ownedBy.id.equals(user.id)) own++;
const sourceObj = locationManager.findId(f.source); else enemy++;
const destinationObj = locationManager.findId(f.destination); } else {
if(source.system.data.ownedBy.id.equals(user.id)) own++;
else enemy++;
}
}
}
if(!sourceObj || !destinationObj) continue; // for(const system of userSystems) {
// for(const planet of system.planets) {
// for(const f of fleet) {
// if(f.source.equals(planet._id) || f.source.equals(system.data._id)) own++;
// else if(f.destination.equals(planet._id) || f.destination.equals(system.data._id)) {
// if(f.mission === 'ATTACK') enemy++;
// else {
// const sourceObj = locationManager.findId(f.source);
// const destinationObj = locationManager.findId(f.destination);
const source = sourceObj instanceof SystemManager ? sourceObj.data.ownedBy.id : sourceObj.system.data.ownedBy.id; // if(!sourceObj || !destinationObj) continue;
const destination = destinationObj instanceof SystemManager ? destinationObj.data.ownedBy.id : destinationObj.system.data.ownedBy.id;
if(!source?.equals(destination)) friendly++; // const source = sourceObj instanceof SystemManager ? sourceObj.data.ownedBy.id : sourceObj.system.data.ownedBy.id;
} // const destination = destinationObj instanceof SystemManager ? destinationObj.data.ownedBy.id : destinationObj.system.data.ownedBy.id;
}
} // if(!source?.equals(destination)) friendly++;
} // }
} // }
// }
// }
// }
const sectorsList = galaxies.map(galaxy => { const sectorsList = galaxies.map(galaxy => {
return { return {
@ -166,7 +179,7 @@ const lang = await getLocales(Astro.cookies.get('language')?.value ?? await getH
</label> </label>
<div class="fleet-send-container"> <div class="fleet-send-container">
<form method="post"> <form method="post">
<h1>Sending fleet from {planet.name}</h1> <h1>Sending fleet from {planet instanceof SystemManager ? planet.data.name : planet.name}</h1>
<hr /> <hr />
<h2>Ships</h2> <h2>Ships</h2>
<div class="fleet-send-ships"> <div class="fleet-send-ships">

View File

@ -35,6 +35,8 @@ if(Astro.request.method === "POST") {
secure: true secure: true
}); });
} }
return Astro.redirect('/game/systemManager');
} }
--- ---
<Layout title="System Manager"> <Layout title="System Manager">
@ -48,10 +50,22 @@ if(Astro.request.method === "POST") {
<a href="/game/systemManager/spaceStations">Space stations</a> <a href="/game/systemManager/spaceStations">Space stations</a>
<a href="/game/systemManager/asteroids">Asteroids</a> <a href="/game/systemManager/asteroids">Asteroids</a>
</div> </div>
<div class="resources">
<p>Resources: {currentSystem.resources.resources.map(r => `${r.id}: ${r.amount}`).join(", ")}</p>
</div>
<div class="planet-list"> <div class="planet-list">
<div class="system-card">
<h2 style="color: red;">{currentSystem.data.name}<div><form method="post"><input type="hidden" name="planetId" value={currentSystem.data._id.toString()} /><input type="submit" value="Select" /></form></div></h2>
<h3>Resources:</h3>
<div>
{currentSystem.resources.resources.length === 0 ? <span>None</span> : currentSystem.resources.resources.map(res => (
<p>{res.data.id} - {res.amount}</p>
))}
</div>
<h3>Ships: </h3>
<div>
{currentSystem.ships.ships.length === 0 ? <span>None</span> : currentSystem.ships.ships.map(ship => (
<p>{ship.data.id} - {ship.amount}</p>
))}
</div>
</div>
{currentSystem.planets.length === 0 ? <span>No planets in this sector</span> : currentSystem.planets.map(planet => ( {currentSystem.planets.length === 0 ? <span>No planets in this sector</span> : currentSystem.planets.map(planet => (
<div class="planet-card"> <div class="planet-card">
<h2>{planet.name}<div><form method="post"><input type="hidden" name="planetId" value={planet._id.toString()} /><input type="submit" value="Select" /></form></div></h2> <h2>{planet.name}<div><form method="post"><input type="hidden" name="planetId" value={planet._id.toString()} /><input type="submit" value="Select" /></form></div></h2>
@ -109,6 +123,13 @@ if(Astro.request.method === "POST") {
margin-bottom: 1rem; margin-bottom: 1rem;
} }
.ships {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 1rem;
}
.planet-list { .planet-list {
display: flex; display: flex;
flex-direction: row; flex-direction: row;