From bdf4074e3c47d9229d2546cc5543ebd9a395c2eb Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Fri, 15 Jan 2021 17:41:06 +0200 Subject: [PATCH] Added first pass at denormalizing inventory data, needs testing and integration --- .../denormalise/recomputeInventory.js | 88 +++++++++++++++++++ app/imports/api/properties/Containers.js | 4 + 2 files changed, 92 insertions(+) create mode 100644 app/imports/api/creature/denormalise/recomputeInventory.js diff --git a/app/imports/api/creature/denormalise/recomputeInventory.js b/app/imports/api/creature/denormalise/recomputeInventory.js new file mode 100644 index 00000000..6cac3d69 --- /dev/null +++ b/app/imports/api/creature/denormalise/recomputeInventory.js @@ -0,0 +1,88 @@ +import CreatureProperties from '/imports/api/creature/CreatureProperties.js'; +import nodesToTree from '/imports/api/parenting/parenting.js'; + +export default function recomputeInventory(creatureId){ + let inventoryForest = nodesToTree({ + collection: CreatureProperties, + ancestorId: creatureId, + filter: { + type: {$in: ['container', 'item']}, + }, + deactivatedByAncestor: {$ne: true}, + }); + return getChildrenInventoryData(inventoryForest); +} + +function getChildrenInventoryData(forest){ + let data = { + weightTotal: 0, + weightEquipment: 0, + weightCarried: 0, + valueTotal: 0, + valueEquipment: 0, + valueCarried: 0, + } + forest.forEach(tree => { + let treeData = getInventoryData(tree); + for (let key in data){ + data[key] += treeData[key]; + } + }); +} + +function getInventoryData(tree){ + let data = { + weightTotal: 0, + weightEquipment: 0, + weightCarried: 0, + valueTotal: 0, + valueEquipment: 0, + valueCarried: 0, + itemsAttuned: 0, + } + let childData = getChildrenInventoryData(tree.children); + let node = tree.node; + if (node.type === 'container'){ + data.weightTotal += node.weight; + data.valueTotal += node.value; + if (node.carried){ + data.weightCarried += node.weight; + data.valueCarried += node.valueCarried; + } + storeContentsData(node, childData); + } else if (node.type === 'item'){ + data.weightTotal += node.weight * node.quantity; + data.valueTotal += node.value * node.quantity; + data.weightCarried += node.weight * node.quantity; + data.valueCarried += node.valueCarried * node.quantity; + if (node.equipped){ + data.weightEquipment += node.weight * node.quantity; + data.valueEquipment += node.valueCarried * node.quantity; + } + if (node.attuned){ + data.itemsAttuned += 1; + } + } + for (let key in data){ + data[key] += childData[key]; + } + return data +} + +function storeContentsData(node, childData){ + let newContentsWeight; + if (node.contentsWeightless){ + newContentsWeight = 0; + } else { + newContentsWeight = childData.weightCarried + } + if (node.contentsWeight !== newContentsWeight){ + node.contentsWeight = newContentsWeight; + node.contentsWeightChanged = true; + } + let newContentsValue = childData.valueCarried; + if (node.contentsValue !== newContentsValue){ + node.contentsValue = newContentsValue; + node.contentsValueChanged = true; + } +} diff --git a/app/imports/api/properties/Containers.js b/app/imports/api/properties/Containers.js index 94b36000..8e878f70 100644 --- a/app/imports/api/properties/Containers.js +++ b/app/imports/api/properties/Containers.js @@ -38,6 +38,10 @@ const ComputedOnlyContainerSchema = new SimpleSchema({ type: Number, optional: true, }, + contentsValue:{ + type: Number, + optional: true, + }, }); const ComputedContainerSchema = new SimpleSchema()