2024-10-24 13:12:21 +00:00
|
|
|
import type { Building, Production } from "./types";
|
2024-10-22 15:15:35 +00:00
|
|
|
import type { VillageState } from "./village";
|
|
|
|
|
|
|
|
|
|
|
|
function _reduceResources(acc: Production, item: Production): Production {
|
|
|
|
return {
|
|
|
|
wood: acc.wood + item.wood,
|
|
|
|
stone: acc.stone + item.stone,
|
|
|
|
iron: acc.iron + item.iron,
|
|
|
|
food: acc.food + item.food,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getEmptyResources(): Production {
|
|
|
|
return {
|
|
|
|
wood: 0,
|
|
|
|
stone: 0,
|
|
|
|
iron: 0,
|
|
|
|
food: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getProduction(villageState: VillageState): Production {
|
|
|
|
return villageState.buildings
|
|
|
|
.filter(b => b.behavior.production)
|
|
|
|
.map(b => {
|
|
|
|
if (b.behavior.production) {
|
|
|
|
return b.behavior.production(villageState, b);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.reduce(_reduceResources, getEmptyResources());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getStorage(villageState: VillageState): Production {
|
|
|
|
return villageState.buildings
|
|
|
|
.filter(b => b.behavior.storage)
|
|
|
|
.map(b => {
|
|
|
|
if (b.behavior.storage) {
|
|
|
|
return b.behavior.storage(villageState, b);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.reduce(_reduceResources, getEmptyResources());
|
|
|
|
}
|
2024-10-24 09:32:31 +00:00
|
|
|
|
|
|
|
|
2024-10-24 13:12:21 +00:00
|
|
|
export function getBuilding(V: VillageState, buildingId: number): Building {
|
|
|
|
const building = V.buildings.find(b => b.id === buildingId);
|
|
|
|
if (!building) {
|
|
|
|
throw new Error(`Cannot find building with id "${buildingId}"`);
|
|
|
|
}
|
|
|
|
return building;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-24 09:32:31 +00:00
|
|
|
export function getKeysAsNumbers(dict: Object): Array<number> {
|
|
|
|
return Object.keys(dict).map(i => parseInt(i)).sort((a, b) => a - b);
|
|
|
|
}
|