Add storage system
This commit is contained in:
parent
d6c6327661
commit
b1e17b16d3
|
@ -5,6 +5,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();
|
||||||
|
|
||||||
|
@ -18,9 +19,37 @@ if(!planet) return;
|
||||||
|
|
||||||
await planet.resources.calculateCurrentAvailableResources();
|
await planet.resources.calculateCurrentAvailableResources();
|
||||||
|
|
||||||
const resourceArray: Resource[] = [];
|
const resourceArray: (Resource & { capacity: number })[] = [];
|
||||||
for(const key of planet.resources.resources) {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
<div id="resourcebar">
|
<div id="resourcebar">
|
||||||
|
@ -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-type={resourceTypes.find(x => x.id === res.id)?.type ?? "solid"}
|
||||||
data-res-id={res.id}
|
data-res-id={res.id}
|
||||||
data-res-amount={res.amount}
|
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}
|
||||||
>
|
>
|
||||||
<Image src={resourceTypes.find(x => x.id === res.id)?.icon ?? "#"} alt={res.id} class="icon" width={32} height={32} />
|
<Image src={resourceTypes.find(x => x.id === res.id)?.icon ?? "#"} alt={res.id} class="icon" width={32} height={32} />
|
||||||
<div class="text" data-resname={res.id}>
|
<div class="text" data-resname={res.id}>
|
||||||
<span class="line1">{getName(lang, 'resources', res.id)}</span>
|
<span class="line1">{getName(lang, 'resources', res.id)}</span>
|
||||||
<span class="line2"></span>
|
<span class={`line2 ${res.amount >= res.capacity ? "prod-full" : res.amount >= (res.capacity * 0.9) ? "prod-almost-full" : ""}`}></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="resourcebar-item-tooltip">
|
<div class="resourcebar-item-tooltip">
|
||||||
<span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'avaliable')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-avaliable">{Math.floor(res.amount).toString()}</span>
|
<span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'avaliable')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-avaliable">{Math.floor(res.amount).toString()}</span>
|
||||||
<span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'production')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-production">{res.perHourMiningRate.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") ?? "0"}</span>
|
<span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'production')}</span><span class={`resourcebar-item-tooltip-amount resourcebar-item-tooltip-production ${res.amount >= 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"}</span>
|
||||||
<span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'capacity')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-capacity">{'21372137'.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</span>
|
<span class="resourcebar-item-tooltip-name">{getName(lang, 'general', 'capacity')}</span><span class="resourcebar-item-tooltip-amount resourcebar-item-tooltip-capacity">{res.capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
@ -155,6 +185,14 @@ for(const key of planet.resources.resources) {
|
||||||
margin-bottom: 5px;
|
margin-bottom: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.prod-almost-full {
|
||||||
|
color: #ff9900;
|
||||||
|
}
|
||||||
|
|
||||||
|
.prod-full {
|
||||||
|
color: #ff0000;
|
||||||
|
}
|
||||||
|
|
||||||
.resourcebar-item .resourcebar-item-tooltip {
|
.resourcebar-item .resourcebar-item-tooltip {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -198,6 +236,17 @@ for(const key of planet.resources.resources) {
|
||||||
item.dataset.resAmount = newAmount.toString();
|
item.dataset.resAmount = newAmount.toString();
|
||||||
(item.querySelector('.line2') as HTMLElement).innerHTML = numWithPrefix(newAmount);
|
(item.querySelector('.line2') as HTMLElement).innerHTML = numWithPrefix(newAmount);
|
||||||
(item.querySelector('.resourcebar-item-tooltip-avaliable') as HTMLElement).innerHTML = numWithSeparator(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');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,33 @@ export default class PlanetResourceManager extends ResourceManager {
|
||||||
return this;
|
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() {
|
async sync() {
|
||||||
await updatePlanetResources(this.planet._id, this.resources.map(res => {
|
await updatePlanetResources(this.planet._id, this.resources.map(res => {
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -73,7 +73,14 @@ export default class SystemResourceManager extends ResourceManager {
|
||||||
return this;
|
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() {
|
async sync() {
|
||||||
await updateSystemResources(this.system.data._id, this.resources.map(res => {
|
await updateSystemResources(this.system.data._id, this.resources.map(res => {
|
||||||
|
|
|
@ -14,17 +14,23 @@ export default abstract class ResourceManager {
|
||||||
|
|
||||||
abstract sync(): Promise<void>;
|
abstract sync(): Promise<void>;
|
||||||
|
|
||||||
|
abstract getStorageCapacities(): { id: string, capacity: number }[];
|
||||||
|
|
||||||
getResourceById(resId: string) {
|
getResourceById(resId: string) {
|
||||||
return this.resources.find(res => res.id === resId);
|
return this.resources.find(res => res.id === resId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async calculateCurrentAvailableResources() {
|
async calculateCurrentAvailableResources() {
|
||||||
|
const storage = this.getStorageCapacities();
|
||||||
for(const res of this.resources) {
|
for(const res of this.resources) {
|
||||||
if(!res.lastUpdated || !res.perHourMiningRate) continue;
|
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 timeDiff = Math.abs((new Date()).getTime() - res.lastUpdated.getTime());
|
||||||
const hours = timeDiff / (1000 * 60 * 60);
|
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.amount += amountToAdd;
|
||||||
res.lastUpdated = new Date();
|
res.lastUpdated = new Date();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue