Fix bug with inconsistent building costs

This commit is contained in:
Aelita4 2024-11-26 12:41:41 +01:00
parent a2b7ae1f94
commit 393bcb1960
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
2 changed files with 4 additions and 4 deletions

View File

@ -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,

View File

@ -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();