2024-10-24 17:27:33 +00:00
|
|
|
import { enqueueBuilding } from "../utils";
|
2024-10-21 15:35:32 +00:00
|
|
|
import type { VillageState } from "../village";
|
|
|
|
|
|
|
|
|
|
|
|
export default function upgradeBuilding(V: VillageState, buildingId: number) {
|
|
|
|
const building = V.buildings.find(b => b.id === buildingId);
|
|
|
|
if (!building) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-24 17:27:33 +00:00
|
|
|
const ongoingUpgrades = V.queue.filter(q => q.id === building.id);
|
|
|
|
const level = building.level + 1 + ongoingUpgrades.length;
|
|
|
|
|
|
|
|
const cost = building.cost(level);
|
2024-10-21 15:35:32 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
cost.wood > V.resources.wood
|
|
|
|
|| cost.stone > V.resources.stone
|
|
|
|
|| cost.iron > V.resources.iron
|
|
|
|
|| cost.food > V.resources.food
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
V.resources.wood -= cost.wood;
|
|
|
|
V.resources.stone -= cost.stone;
|
|
|
|
V.resources.iron -= cost.iron;
|
|
|
|
V.resources.food -= cost.food;
|
2024-10-24 17:27:33 +00:00
|
|
|
enqueueBuilding(V, building);
|
2024-10-21 15:35:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|