Fix commitments calculation
This commit is contained in:
parent
f8173a90c0
commit
4f7e38a953
@ -27,7 +27,13 @@ interface VillageOwnStateDictionary {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface VillageState extends VillageOwnState {
|
export interface VillageState extends VillageOwnState {
|
||||||
|
/**
|
||||||
|
* Resource commitments of this village to other
|
||||||
|
*/
|
||||||
commitments: Resources;
|
commitments: Resources;
|
||||||
|
/**
|
||||||
|
* List of village id, from which resources ship to this village
|
||||||
|
*/
|
||||||
shipment: Array<number>;
|
shipment: Array<number>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,10 +94,12 @@ function createVillageState(
|
|||||||
scheduler: Scheduler
|
scheduler: Scheduler
|
||||||
): VillageState {
|
): VillageState {
|
||||||
const villageIds = scheduler.getResourceShipmentVillageIds(state.id);
|
const villageIds = scheduler.getResourceShipmentVillageIds(state.id);
|
||||||
const commitments = villageIds.reduce((res, villageId) => {
|
const commitments = villageIds.reduce((memo, shipmentVillageId) => {
|
||||||
const villageRequired = ownStates[villageId].required;
|
const shipmentVillageState = ownStates[shipmentVillageId];
|
||||||
const missing = villageRequired.balance.min(Resources.zero());
|
const shipmentVillageRequired = shipmentVillageState.required;
|
||||||
return res.add(missing);
|
const shipmentVillageIncoming = shipmentVillageState.incomingResources;
|
||||||
|
const targetVillageMissing = shipmentVillageRequired.balance.add(shipmentVillageIncoming).min(Resources.zero());
|
||||||
|
return memo.add(targetVillageMissing);
|
||||||
}, Resources.zero());
|
}, Resources.zero());
|
||||||
return { ...state, commitments, shipment: villageIds };
|
return { ...state, commitments, shipment: villageIds };
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user