Improve mining rate module

This commit is contained in:
Aelita4 2025-02-11 12:56:39 +01:00
parent 3d55f7060c
commit 08a2371196
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
9 changed files with 145 additions and 126 deletions

View File

@ -19,43 +19,12 @@ if(!planet) return;
await planet.resources.calculateCurrentAvailableResources();
const resourceArray: (Resource & { capacity: number })[] = [];
const miningRate = planet.resources.getPerHourMiningRate();
const storage = planet.resources.getStorageCapacities();
const resourceArray: (Resource & { capacity: number, rate: number })[] = [];
for(const key of planet.resources.resources) {
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",
"uranium-container": "uranium",
"oil-tank": "oil",
"diesel-tank": "diesel",
"kerosene-tank": "kerosene",
"deuterium-tank": "deuterium",
"tritium-tank": "tritium",
}
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;
}
}
}
resourceArray.push({ ...key, capacity: storage.find(r => r.id === key.id)?.capacity ?? 10_000, rate: miningRate.find(x => x.id === key.id)?.rate ?? 0 });
}
---
<div id="resourcebar">
@ -83,7 +52,7 @@ if(!(planet instanceof SystemManager)) {
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.amount >= res.capacity ? 0 : res.perHourMiningRate}
data-res-mining-rate={res.amount >= res.capacity ? 0 : res.rate}
data-res-capacity={res.capacity}
>
<Image src={resourceTypes.find(x => x.id === res.id)?.icon ?? "#"} alt={res.id} class="icon" width={32} height={32} />
@ -91,7 +60,7 @@ if(!(planet instanceof SystemManager)) {
<div class="resourcebar-item-tooltip">
<span class="resourcebar-item-tooltip-title">{getName(lang, 'resources', res.data.id)}</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.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', '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.rate.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">{res.capacity.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}</span>
</div>
</div>

View File

@ -11,7 +11,7 @@ export default class PlanetResourceManager extends ResourceManager {
this.planet = planet;
}
async init(resourceData: { id: string, amount: number, lastUpdated: Date, perHourMiningRate: number }[]) {
async init(resourceData: { id: string, amount: number, lastUpdated: Date }[]) {
const resources = await getAllResources();
this.resourcesDB = resources;
@ -21,56 +21,47 @@ export default class PlanetResourceManager extends ResourceManager {
{
id: "coal",
amount: 11,
lastUpdated: new Date(),
perHourMiningRate: 11
lastUpdated: new Date()
},
{
id: "iron",
amount: 22,
lastUpdated: new Date(),
perHourMiningRate: 22
lastUpdated: new Date()
},
{
id: "gold",
amount: 33,
lastUpdated: new Date(),
perHourMiningRate: 33
lastUpdated: new Date()
},
{
id: "water",
amount: 44,
lastUpdated: new Date(),
perHourMiningRate: 44
lastUpdated: new Date()
},
{
id: "sulfuric-acid",
amount: 55,
lastUpdated: new Date(),
perHourMiningRate: 55
lastUpdated: new Date()
},
{
id: "liquid-nitrogen",
amount: 66,
lastUpdated: new Date(),
perHourMiningRate: 66
lastUpdated: new Date()
},
{
id: "hydrogen",
amount: 77,
lastUpdated: new Date(),
perHourMiningRate: 77
lastUpdated: new Date()
},
{
id: "oxygen",
amount: 88,
lastUpdated: new Date(),
perHourMiningRate: 88
lastUpdated: new Date()
},
{
id: "helium-3",
amount: 99,
lastUpdated: new Date(),
perHourMiningRate: 99
lastUpdated: new Date()
}
];
@ -83,7 +74,6 @@ export default class PlanetResourceManager extends ResourceManager {
id: resource.id,
amount: resource.amount,
lastUpdated: resource.lastUpdated,
perHourMiningRate: resource.perHourMiningRate,
data: resFromDB
})
});
@ -102,6 +92,12 @@ export default class PlanetResourceManager extends ResourceManager {
"hydrogen-tank": "hydrogen",
"oxygen-tank": "oxygen",
"helium3-tank": "helium-3",
"uranium-container": "uranium",
"oil-tank": "oil",
"diesel-tank": "diesel",
"kerosene-tank": "kerosene",
"deuterium-tank": "deuterium",
"tritium-tank": "tritium",
}
const output: { id: string, capacity: number }[] = [];
@ -118,13 +114,36 @@ export default class PlanetResourceManager extends ResourceManager {
return output;
}
getPerHourMiningRate() {
const mapMinerToResource: { [key: string]: string } = {
"coal-mine": "coal",
"iron-mine": "iron",
"gold-mine": "gold"
}
const output = [];
const buildings = this.planet.buildings.buildings;
for(const building of buildings) {
if(Object.keys(mapMinerToResource).includes(building.data.id)) {
const miningRate = 20 * Math.pow(building.level, 2) + 40 * building.level + 15;
output.push({
id: mapMinerToResource[building.data.id],
rate: miningRate
});
}
}
return output;
}
async sync() {
await updatePlanetResources(this.planet._id, this.resources.map(res => {
return {
id: res.id,
amount: res.amount,
lastUpdated: res.lastUpdated,
perHourMiningRate: res.perHourMiningRate
}
}));
}

View File

@ -65,7 +65,6 @@ export default class SystemResourceManager extends ResourceManager {
id: resource.id,
amount: resource.amount,
lastUpdated: null,
perHourMiningRate: 0,
data: resFromDB
})
});
@ -74,21 +73,74 @@ export default class SystemResourceManager extends ResourceManager {
}
getStorageCapacities(): { id: string; capacity: number; }[] {
return this.resources.map(res => {
return {
id: res.id,
capacity: 10_000 //TODO: add structure for storage
const output: { id: string, capacity: number }[] = [
{
id: "coal",
capacity: 10_000
}, {
id: "iron",
capacity: 10_000
}, {
id: "gold",
capacity: 10_000
}, {
id: "water",
capacity: 10_000
}, {
id: "sulfuric-acid",
capacity: 10_000
}, {
id: "liquid-nitrogen",
capacity: 10_000
}, {
id: "hydrogen",
capacity: 10_000
}, {
id: "oxygen",
capacity: 10_000
}, {
id: "helium-3",
capacity: 10_000
}, {
id: "uranium",
capacity: 10_000
}, {
id: "oil",
capacity: 10_000
}, {
id: "diesel",
capacity: 10_000
}, {
id: "kerosene",
capacity: 10_000
}, {
id: "deuterium",
capacity: 10_000
}, {
id: "tritium",
capacity: 10_000
}
});
];
const storageStation = this.system.structures.structures.find(s => s.data.id === 'storage-station');
if(typeof storageStation !== 'undefined') {
output.forEach(res => {
res.capacity += storageStation.level * 40_000;
});
}
return output;
}
getPerHourMiningRate() { return []; }
async sync() {
await updateSystemResources(this.system.data._id, this.resources.map(res => {
return {
id: res.id,
amount: res.amount,
lastUpdated: res.lastUpdated,
perHourMiningRate: res.perHourMiningRate
}
}));
}

View File

@ -4,7 +4,6 @@ export type Resource = {
id: string,
amount: number,
lastUpdated: Date | null,
perHourMiningRate: number,
data: DBResource
}
@ -13,7 +12,7 @@ export default abstract class ResourceManager {
resourcesDB: DBResource[] = [];
abstract sync(): Promise<void>;
abstract getPerHourMiningRate(): { id: string, rate: number }[];
abstract getStorageCapacities(): { id: string, capacity: number }[];
getResourceById(resId: string) {
@ -22,13 +21,16 @@ export default abstract class ResourceManager {
async calculateCurrentAvailableResources() {
const storage = this.getStorageCapacities();
const miningRates = this.getPerHourMiningRate();
for(const res of this.resources) {
if(!res.lastUpdated || !res.perHourMiningRate) continue;
if(!res.lastUpdated) continue;
const miningRate = miningRates.find(r => r.id === res.id)?.rate ?? 0;
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);
let amountToAdd = hours * res.perHourMiningRate;
let amountToAdd = hours * miningRate;
if(res.amount + amountToAdd > maxStorage) amountToAdd = Math.max(0, maxStorage - res.amount);
res.amount += amountToAdd;
@ -50,7 +52,6 @@ export default abstract class ResourceManager {
id: res.id,
amount: res.amount - currentRes.amount,
lastUpdated: res.lastUpdated,
perHourMiningRate: res.perHourMiningRate,
data: res.data
});
else difference.push(res);
@ -76,7 +77,6 @@ export default abstract class ResourceManager {
id: res.id,
amount: res.amount,
lastUpdated: new Date(),
perHourMiningRate: 0,
data: this.resourcesDB.find(r => r.id === res.id) as DBResource
});
}
@ -90,7 +90,6 @@ export default abstract class ResourceManager {
id: res.id,
amount: res.amount,
lastUpdated: new Date(),
perHourMiningRate: 0,
data: this.resourcesDB.find(r => r.id === res.id) as DBResource
});
}

View File

@ -89,29 +89,12 @@ export const POST: APIRoute = async({ request }) => {
id: res.id,
amount: res.amount,
lastUpdated: res.lastUpdated,
perHourMiningRate: res.perHourMiningRate,
data
}
});
userPlanet.resources.setAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } }));
userPlanet.buildings.addBuilding(building);
if(building.data.category === "mines") {
switch(building.data.id) {
case 'coal-mine':
const coalResource = userPlanet.resources.resources.find(r => r.id === "coal");
if(coalResource) coalResource.perHourMiningRate += 10;
break;
case 'iron-mine':
const ironResource = userPlanet.resources.resources.find(r => r.id === "iron");
if(ironResource) ironResource.perHourMiningRate += 10;
break;
case 'gold-mine':
const goldResource = userPlanet.resources.resources.find(r => r.id === "gold");
if(goldResource) goldResource.perHourMiningRate += 10;
break;
}
}
await userPlanet.buildings.sync();
await userPlanet.resources.sync();

View File

@ -126,7 +126,6 @@ export const POST: APIRoute = async({ request }) => {
id: res.id,
amount: res.amount,
lastUpdated: res.lastUpdated,
perHourMiningRate: res.perHourMiningRate,
data
}
});

View File

@ -110,7 +110,6 @@ export const POST: APIRoute = async({ request }) => {
id: res.id,
amount: res.amount,
lastUpdated: res.lastUpdated,
perHourMiningRate: res.perHourMiningRate,
data
}
});

View File

@ -60,46 +60,45 @@ export const POST: APIRoute = async({ request }) => {
const structure = new Structure(userSystem.structures, structureObj, 1);
// const requirements = await structure.checkRequirements();
// const resources = await building.checkRequiredResources((userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0) + 1);
const requirements = await structure.checkRequirements();
const resources = await structure.checkRequiredResources((userSystem.structures.getStructureById(structureId)?.level ?? 0) + 1);
// if(!requirements.canBuild || !resources) {
// return new Response(
// JSON.stringify({
// code: 400,
// message: "Bad Request",
// error: `${requirements.error} | ${resources ? "" : "Not enough resources"}`
// }), { status: 400 }
// )
// }
if(!requirements.canBuild || !resources) {
return new Response(
JSON.stringify({
code: 400,
message: "Bad Request",
error: `${requirements.error} | ${resources ? "" : "Not enough resources"}`
}), { status: 400 }
)
}
// const resourcesDiff = await userPlanet.resources.getDifference(building.data.requirements.resources.map(res => {
// return {
// id: res.id,
// amount: Math.pow(building.data.multiplier, (userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0) + 1) * res.amount
// }
// }));
const resourcesDiff = await userSystem.resources.getDifference(structure.data.requirements.resources.map(res => {
return {
id: res.id,
amount: Math.pow(structure.data.multiplier, (userSystem.structures.getStructureById(structureId)?.level ?? 0) + 1) * res.amount
}
}));
// const resourceDB = await getAllResources();
// const resourcesAfter = resourcesDiff.map(res => {
// const data = resourceDB.find(r => r.id === res.id);
const resourceDB = await getAllResources();
const resourcesAfter = resourcesDiff.map(res => {
const data = resourceDB.find(r => r.id === res.id);
// if(!data) throw new Error("Resource not found");
if(!data) throw new Error("Resource not found");
// return {
// id: res.id,
// amount: res.amount,
// lastUpdated: res.lastUpdated,
// perHourMiningRate: res.perHourMiningRate,
// data
// }
// });
return {
id: res.id,
amount: res.amount,
lastUpdated: res.lastUpdated,
data
}
});
// userPlanet.resources.update(resourcesAfter);
// userSystem.structures.addStructure(structure);
userSystem.resources.setAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } }));
userSystem.structures.addStructure(structure);
// await userSystem.structures.sync();
// await userPlanet.resources.sync();
await userSystem.structures.sync();
await userSystem.resources.sync();
return new Response(
JSON.stringify({

View File

@ -5,7 +5,7 @@ export default interface DBPlanet {
name: string;
owner: ObjectId; // shouldn't be here
fields: number;
resources: Array<{ id: string, amount: number, lastUpdated: Date, perHourMiningRate: number }>;
resources: Array<{ id: string, amount: number, lastUpdated: Date }>;
buildings: Array<{ id: string, level: number, activePercent?: number }>;
ships: Array<{ id: string, amount: number }>;
defenses: Array<{ id: string, amount: number }>;