From 393bcb1960ba2baf5ece0b19137aca7ebb002358 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Tue, 26 Nov 2024 12:41:41 +0100 Subject: [PATCH] Fix bug with inconsistent building costs --- src/lib/classes/managers/abstract/ResourceManager.ts | 2 +- src/pages/api/build/createBuilding.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/classes/managers/abstract/ResourceManager.ts b/src/lib/classes/managers/abstract/ResourceManager.ts index 65b2b9b..2e4829a 100644 --- a/src/lib/classes/managers/abstract/ResourceManager.ts +++ b/src/lib/classes/managers/abstract/ResourceManager.ts @@ -65,7 +65,7 @@ export default abstract class ResourceManager { await this.calculateCurrentAvailableResources(); for(const res of resources) { const resource = this.resources.find(r => r.id === res.id); - if(resource) resource.amount = res.amount; + if(resource) resource.amount += res.amount; else this.resources.push({ id: res.id, amount: res.amount, diff --git a/src/pages/api/build/createBuilding.ts b/src/pages/api/build/createBuilding.ts index 967cf56..9c703f6 100644 --- a/src/pages/api/build/createBuilding.ts +++ b/src/pages/api/build/createBuilding.ts @@ -60,7 +60,7 @@ export const POST: APIRoute = async({ request }) => { const building = new Building(userPlanet.buildings, buildingObj, 1); const requirements = await building.checkRequirements(); - const resources = await building.checkRequiredResources((userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0) + 1); + const resources = await building.checkRequiredResources((userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0)); if(!requirements.canBuild || !resources) { return new Response( @@ -75,7 +75,7 @@ export const POST: APIRoute = async({ request }) => { 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 + amount: Math.pow(building.data.multiplier, (userPlanet.buildings.getBuildingById(buildingId)?.level ?? 0)) * res.amount } })); @@ -94,7 +94,7 @@ export const POST: APIRoute = async({ request }) => { } }); - userPlanet.resources.updateAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } })); + userPlanet.resources.setAmount(resourcesAfter.map(res => { return { id: res.id, amount: res.amount } })); userPlanet.buildings.addBuilding(building); await userPlanet.buildings.sync();