From b1e17b16d3d49cad05501052ac3d281608aec706 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Sat, 18 Jan 2025 22:00:42 +0100 Subject: [PATCH] Add storage system --- src/components/ResourceBar.astro | 61 +++++++++++++++++-- .../classes/managers/PlanetResourceManager.ts | 27 ++++++++ .../classes/managers/SystemResourceManager.ts | 9 ++- .../managers/abstract/ResourceManager.ts | 8 ++- 4 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/components/ResourceBar.astro b/src/components/ResourceBar.astro index f3f83c8..9a04107 100644 --- a/src/components/ResourceBar.astro +++ b/src/components/ResourceBar.astro @@ -5,6 +5,7 @@ import { getHighestWeightedLanguage, getLocales, getName } from '../lib/utils/la import { getAllResources } from '../lib/db/resources'; import locationManager from '../lib/classes/managers/LocationManager'; import { Resource } from '../lib/classes/managers/abstract/ResourceManager'; +import SystemManager from '../lib/classes/managers/SystemManager'; const resourceTypes = await getAllResources(); @@ -18,9 +19,37 @@ if(!planet) return; await planet.resources.calculateCurrentAvailableResources(); -const resourceArray: Resource[] = []; +const resourceArray: (Resource & { capacity: number })[] = []; for(const key of planet.resources.resources) { - resourceArray.push(key); + resourceArray.push({ ...key, capacity: 10_000 }); +} + +if(!(planet instanceof SystemManager)) { + const mapStorageToResource: { [key: string]: string } = { + "coal-storage": "coal", + "iron-storage": "iron", + "gold-storage": "gold", + "water-tank": "water", + "acid-tank": "sulfuric-acid", + "nitrogen-tank": "liquid-nitrogen", + "hydrogen-tank": "hydrogen", + "oxygen-tank": "oxygen", + "helium3-tank": "helium-3", + } + + resourceArray.forEach(res => { + res.capacity = 10_000; + }); + + for(const building of planet.buildings.buildings) { + if(building.data.category === 'storage') { + const resource = mapStorageToResource[building.data.id]; + if(resource) { + const res = resourceArray.find(x => x.id === resource); + if(res) res.capacity += building.level * 10_000; + } + } + } } ---
@@ -29,17 +58,18 @@ for(const key of planet.resources.resources) { data-res-type={resourceTypes.find(x => x.id === res.id)?.type ?? "solid"} data-res-id={res.id} data-res-amount={res.amount} - data-res-mining-rate={res.perHourMiningRate} + data-res-mining-rate={res.amount >= res.capacity ? 0 : res.perHourMiningRate} + data-res-capacity={res.capacity} > x.id === res.id)?.icon ?? "#"} alt={res.id} class="icon" width={32} height={32} />
{getName(lang, 'resources', res.id)} - + = res.capacity ? "prod-full" : res.amount >= (res.capacity * 0.9) ? "prod-almost-full" : ""}`}>
{getName(lang, 'general', 'avaliable')}{Math.floor(res.amount).toString()} - {getName(lang, 'general', 'production')}{res.perHourMiningRate.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") ?? "0"} - {getName(lang, 'general', 'capacity')}{'21372137'.replace(/\B(?=(\d{3})+(?!\d))/g, ",")} + {getName(lang, 'general', 'production')}= res.capacity ? "prod-full" : res.amount >= (res.capacity * 0.9) ? "prod-almost-full" : ""}`}>{res.amount >= res.capacity ? "0" : res.perHourMiningRate.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") ?? "0"} + {getName(lang, 'general', 'capacity')}{res.capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}
)} @@ -155,6 +185,14 @@ for(const key of planet.resources.resources) { margin-bottom: 5px; } +.prod-almost-full { + color: #ff9900; +} + +.prod-full { + color: #ff0000; +} + .resourcebar-item .resourcebar-item-tooltip { position: absolute; display: flex; @@ -198,6 +236,17 @@ for(const key of planet.resources.resources) { item.dataset.resAmount = newAmount.toString(); (item.querySelector('.line2') as HTMLElement).innerHTML = numWithPrefix(newAmount); (item.querySelector('.resourcebar-item-tooltip-avaliable') as HTMLElement).innerHTML = numWithSeparator(newAmount); + + if(newAmount >= parseFloat(item.dataset.resCapacity ?? '0')) { + (item.querySelector('.line2') as HTMLElement).classList.remove('prod-almost-full'); + (item.querySelector('.line2') as HTMLElement).classList.add('prod-full'); + (item.querySelector('.resourcebar-item-tooltip-production') as HTMLElement).classList.remove('prod-almost-full'); + (item.querySelector('.resourcebar-item-tooltip-production') as HTMLElement).classList.add('prod-full'); + item.dataset.resMiningRate = '0'; + } else if(newAmount >= parseFloat(item.dataset.resCapacity ?? '0') * 0.9) { + (item.querySelector('.line2') as HTMLElement).classList.add('prod-almost-full'); + (item.querySelector('.resourcebar-item-tooltip-production') as HTMLElement).classList.add('prod-almost-full'); + } }); } diff --git a/src/lib/classes/managers/PlanetResourceManager.ts b/src/lib/classes/managers/PlanetResourceManager.ts index 91be4f5..8f37f0f 100644 --- a/src/lib/classes/managers/PlanetResourceManager.ts +++ b/src/lib/classes/managers/PlanetResourceManager.ts @@ -91,6 +91,33 @@ export default class PlanetResourceManager extends ResourceManager { return this; } + getStorageCapacities() { + const mapStorageToResource: { [key: string]: string } = { + "coal-storage": "coal", + "iron-storage": "iron", + "gold-storage": "gold", + "water-tank": "water", + "acid-tank": "sulfuric-acid", + "nitrogen-tank": "liquid-nitrogen", + "hydrogen-tank": "hydrogen", + "oxygen-tank": "oxygen", + "helium3-tank": "helium-3", + } + + const output: { id: string, capacity: number }[] = []; + + for(const building of this.planet.buildings.buildings) { + if(building.data.category === 'storage') { + const resource = mapStorageToResource[building.data.id]; + if(resource) { + output.push({ id: resource, capacity: building.level * 10_000 }); + } + } + } + + return output; + } + async sync() { await updatePlanetResources(this.planet._id, this.resources.map(res => { return { diff --git a/src/lib/classes/managers/SystemResourceManager.ts b/src/lib/classes/managers/SystemResourceManager.ts index 64e859d..ac5ce52 100644 --- a/src/lib/classes/managers/SystemResourceManager.ts +++ b/src/lib/classes/managers/SystemResourceManager.ts @@ -73,7 +73,14 @@ export default class SystemResourceManager extends ResourceManager { return this; } - + getStorageCapacities(): { id: string; capacity: number; }[] { + return this.resources.map(res => { + return { + id: res.id, + capacity: 10_000 //TODO: add structure for storage + } + }); + } async sync() { await updateSystemResources(this.system.data._id, this.resources.map(res => { diff --git a/src/lib/classes/managers/abstract/ResourceManager.ts b/src/lib/classes/managers/abstract/ResourceManager.ts index df5df74..28fe504 100644 --- a/src/lib/classes/managers/abstract/ResourceManager.ts +++ b/src/lib/classes/managers/abstract/ResourceManager.ts @@ -14,17 +14,23 @@ export default abstract class ResourceManager { abstract sync(): Promise; + abstract getStorageCapacities(): { id: string, capacity: number }[]; + getResourceById(resId: string) { return this.resources.find(res => res.id === resId); } async calculateCurrentAvailableResources() { + const storage = this.getStorageCapacities(); for(const res of this.resources) { if(!res.lastUpdated || !res.perHourMiningRate) continue; + const maxStorage = 10_000 + (storage.find(s => s.id === res.id)?.capacity ?? 0); const timeDiff = Math.abs((new Date()).getTime() - res.lastUpdated.getTime()); const hours = timeDiff / (1000 * 60 * 60); - const amountToAdd = hours * res.perHourMiningRate; + let amountToAdd = hours * res.perHourMiningRate; + if(res.amount + amountToAdd > maxStorage) amountToAdd = Math.max(0, maxStorage - res.amount); + res.amount += amountToAdd; res.lastUpdated = new Date(); };