From 4f7e38a9537576822b53bcee35310c35de326c34 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Fri, 8 May 2020 11:38:31 +0300 Subject: [PATCH] Fix commitments calculation --- src/VillageState.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/VillageState.ts b/src/VillageState.ts index 93ff4ae..c9fa108 100644 --- a/src/VillageState.ts +++ b/src/VillageState.ts @@ -27,7 +27,13 @@ interface VillageOwnStateDictionary { } export interface VillageState extends VillageOwnState { + /** + * Resource commitments of this village to other + */ commitments: Resources; + /** + * List of village id, from which resources ship to this village + */ shipment: Array; } @@ -88,10 +94,12 @@ function createVillageState( scheduler: Scheduler ): VillageState { const villageIds = scheduler.getResourceShipmentVillageIds(state.id); - const commitments = villageIds.reduce((res, villageId) => { - const villageRequired = ownStates[villageId].required; - const missing = villageRequired.balance.min(Resources.zero()); - return res.add(missing); + const commitments = villageIds.reduce((memo, shipmentVillageId) => { + const shipmentVillageState = ownStates[shipmentVillageId]; + const shipmentVillageRequired = shipmentVillageState.required; + const shipmentVillageIncoming = shipmentVillageState.incomingResources; + const targetVillageMissing = shipmentVillageRequired.balance.add(shipmentVillageIncoming).min(Resources.zero()); + return memo.add(targetVillageMissing); }, Resources.zero()); return { ...state, commitments, shipment: villageIds }; }