33 lines
864 B
TypeScript
33 lines
864 B
TypeScript
import { enqueueBuilding } from "../utils";
|
|
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;
|
|
}
|
|
|
|
const ongoingUpgrades = V.queue.filter(q => q.id === building.id);
|
|
const level = building.level + 1 + ongoingUpgrades.length;
|
|
|
|
const cost = building.cost(level);
|
|
|
|
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;
|
|
enqueueBuilding(V, building);
|
|
|
|
return true;
|
|
}
|