From 4edfe1bcb9052249135a1e1f89955e81c322f196 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 3 Mar 2022 15:53:58 +0200 Subject: [PATCH] Fixed inventory calculation to include item quantities --- .../aggregate/aggregateInventory.js | 16 ++++++++++++---- .../tests/computeInventory.testFn.js | 19 +++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/imports/api/engine/computation/computeComputation/computeByType/computeVariable/aggregate/aggregateInventory.js b/app/imports/api/engine/computation/computeComputation/computeByType/computeVariable/aggregate/aggregateInventory.js index dc36e1d8..6dd01ba2 100644 --- a/app/imports/api/engine/computation/computeComputation/computeByType/computeVariable/aggregate/aggregateInventory.js +++ b/app/imports/api/engine/computation/computeComputation/computeByType/computeVariable/aggregate/aggregateInventory.js @@ -43,18 +43,26 @@ export default function aggregateInventory({node, linkedNode, link}){ } } +function quantity(prop){ + if (typeof prop.quantity === 'number'){ + return prop.quantity; + } else { + return 1; + } +} + function weight(prop){ - return (prop.weight || 0) + (prop.contentsWeight || 0); + return (prop.weight || 0) * quantity(prop) + (prop.contentsWeight || 0); } function carriedWeight(prop){ - return (prop.weight || 0) + (prop.carriedWeight || 0); + return (prop.weight || 0) * quantity(prop) + (prop.carriedWeight || 0); } function value (prop){ - return (prop.value || 0) + (prop.contentsValue || 0); + return (prop.value || 0) * quantity(prop) + (prop.contentsValue || 0); } function carriedValue (prop){ - return (prop.value || 0) + (prop.carriedValue || 0); + return (prop.value || 0) * quantity(prop) + (prop.carriedValue || 0); } diff --git a/app/imports/api/engine/computation/computeComputation/tests/computeInventory.testFn.js b/app/imports/api/engine/computation/computeComputation/tests/computeInventory.testFn.js index d2cc2f1d..13b76e4a 100644 --- a/app/imports/api/engine/computation/computeComputation/tests/computeInventory.testFn.js +++ b/app/imports/api/engine/computation/computeComputation/tests/computeInventory.testFn.js @@ -14,16 +14,14 @@ export default function(){ assert.equal(scope('itemsAttuned'), 1); - assert.equal(prop('childContainerId').carriedWeight, 23); - assert.equal(prop('childContainerId').contentsWeight, 23); + assert.equal(prop('childContainerId').carriedWeight, 69); + assert.equal(prop('childContainerId').contentsWeight, 69); - assert.equal(scope('weightCarried'), 58); + assert.equal(scope('weightCarried'), 104); + assert.equal(scope('valueCarried'), 129); - assert.equal(scope('weightCarried'), 58); - assert.equal(scope('valueCarried'), 71); - - assert.equal(scope('weightTotal'), 58); - assert.equal(scope('valueTotal'), 71); + assert.equal(scope('weightTotal'), 104); + assert.equal(scope('valueTotal'), 129); } var testProperties = [ @@ -62,8 +60,9 @@ var testProperties = [ clean({ _id: 'grandchildItemId', type: 'item', - weight: 23, - value: 29, + weight: 23, // 69 total + value: 29, // 87 total + quantity: 3, ancestors: [{id: 'charId'}, {id: 'containerId'}, {id: 'childContainerId'}], }), ];