Added first pass at denormalizing inventory data, needs testing and integration

This commit is contained in:
Stefan Zermatten
2021-01-15 17:41:06 +02:00
parent 9492b2d8b8
commit bdf4074e3c
2 changed files with 92 additions and 0 deletions

View File

@@ -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;
}
}

View File

@@ -38,6 +38,10 @@ const ComputedOnlyContainerSchema = new SimpleSchema({
type: Number, type: Number,
optional: true, optional: true,
}, },
contentsValue:{
type: Number,
optional: true,
},
}); });
const ComputedContainerSchema = new SimpleSchema() const ComputedContainerSchema = new SimpleSchema()