Tested and fixed inventory computation
This commit is contained in:
@@ -5,7 +5,7 @@ import clean from '../../utility/cleanProp.testFn.js';
|
||||
export default function(){
|
||||
const computation = buildComputationFromProps(testProperties);
|
||||
const hasLink = computation.dependencyGraph.hasLink;
|
||||
|
||||
|
||||
assert.isTrue(
|
||||
!!hasLink('weightEquipment', 'equippedAttunedItemId'),
|
||||
'weight of equipment depends on equipped items'
|
||||
@@ -46,6 +46,14 @@ export default function(){
|
||||
!!hasLink('valueCarried', 'childContainerId'),
|
||||
'valueCarried does not depend on nested containers'
|
||||
);
|
||||
assert.isTrue(
|
||||
!!hasLink('containerId', 'childContainerId'),
|
||||
'containers depend on their child containers'
|
||||
);
|
||||
assert.isTrue(
|
||||
!!hasLink('childContainerId', 'grandchildItemId'),
|
||||
'containers depend on their child items'
|
||||
);
|
||||
}
|
||||
|
||||
var testProperties = [
|
||||
|
||||
@@ -2,11 +2,13 @@ import _variable from './computeByType/computeVariable.js';
|
||||
import action from './computeByType/computeAction.js';
|
||||
import attribute from './computeByType/computeAttribute.js';
|
||||
import slot from './computeByType/computeSlot.js';
|
||||
import container from './computeByType/computeContainer.js';
|
||||
|
||||
export default Object.freeze({
|
||||
_variable,
|
||||
action,
|
||||
attack: action,
|
||||
attribute,
|
||||
container,
|
||||
slot,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import aggregate from './computeVariable/aggregate/index.js';
|
||||
|
||||
export default function computeContainer(graph, node){
|
||||
if (!node.data) node.data = {};
|
||||
aggregateLinks(graph, node);
|
||||
}
|
||||
|
||||
function aggregateLinks(graph, node){
|
||||
graph.forEachLinkedNode(
|
||||
node.id,
|
||||
(linkedNode, link) => {
|
||||
if (!linkedNode.data) linkedNode.data = {};
|
||||
// Ignore inactive props
|
||||
if (linkedNode.data.inactive) return;
|
||||
// Aggregate inventory links
|
||||
aggregate.inventory({node, linkedNode, link});
|
||||
},
|
||||
true // enumerate only outbound links
|
||||
);
|
||||
}
|
||||
@@ -34,10 +34,10 @@ export default function aggregateInventory({node, linkedNode, link}){
|
||||
prop.baseValue = (prop.baseValue || 0) + weight(linkedProp);
|
||||
} else if (node.id === 'valueTotal'){
|
||||
prop.baseValue = (prop.baseValue || 0) + value(linkedProp);
|
||||
} else if (node.did === 'weightCarried'){
|
||||
} else if (node.id === 'weightCarried'){
|
||||
prop.baseValue = (prop.baseValue || 0) + carriedWeight(linkedProp);
|
||||
} else if (node.did === 'valueCarried'){
|
||||
prop.carriedValue = (prop.carriedValue || 0) + carriedValue(linkedProp);
|
||||
} else if (node.id === 'valueCarried'){
|
||||
prop.baseValue = (prop.baseValue || 0) + carriedValue(linkedProp);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ function carriedWeight(prop){
|
||||
}
|
||||
|
||||
function value (prop){
|
||||
return (prop.value || 0) + (prop.value || 0);
|
||||
return (prop.value || 0) + (prop.contentsValue || 0);
|
||||
}
|
||||
|
||||
function carriedValue (prop){
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { buildComputationFromProps } from '/imports/api/creature/computation/newEngine/buildCreatureComputation.js';
|
||||
import { assert } from 'chai';
|
||||
import computeCreatureComputation from '../../computeCreatureComputation.js';
|
||||
import clean from '../../utility/cleanProp.testFn.js';
|
||||
|
||||
export default function(){
|
||||
const computation = buildComputationFromProps(testProperties);
|
||||
computeCreatureComputation(computation);
|
||||
const prop = id => computation.propsById[id];
|
||||
const scope = id => computation.scope[id].value;
|
||||
console.log(computation.scope);
|
||||
|
||||
assert.equal(scope('weightEquipment'), 2);
|
||||
assert.equal(scope('valueEquipment'), 3);
|
||||
|
||||
assert.equal(scope('itemsAttuned'), 1);
|
||||
|
||||
assert.equal(prop('childContainerId').carriedWeight, 23);
|
||||
assert.equal(prop('childContainerId').contentsWeight, 23);
|
||||
|
||||
assert.equal(scope('weightCarried'), 58);
|
||||
|
||||
assert.equal(scope('weightCarried'), 58);
|
||||
assert.equal(scope('valueCarried'), 71);
|
||||
|
||||
assert.equal(scope('weightTotal'), 58);
|
||||
assert.equal(scope('valueTotal'), 71);
|
||||
}
|
||||
|
||||
var testProperties = [
|
||||
clean({
|
||||
_id: 'equippedAttunedItemId',
|
||||
type: 'item',
|
||||
equipped: true,
|
||||
attuned: true,
|
||||
weight: 2,
|
||||
value: 3,
|
||||
ancestors: [{id: 'charId'}],
|
||||
}),
|
||||
clean({
|
||||
_id: 'containerId',
|
||||
type: 'container',
|
||||
carried: true,
|
||||
weight: 5,
|
||||
value: 7,
|
||||
ancestors: [{id: 'charId'}],
|
||||
}),
|
||||
clean({
|
||||
_id: 'childContainerId',
|
||||
type: 'container',
|
||||
carried: true,
|
||||
weight: 11,
|
||||
value: 13,
|
||||
ancestors: [{id: 'charId'}, {id: 'containerId'}],
|
||||
}),
|
||||
clean({
|
||||
_id: 'childItemId',
|
||||
type: 'item',
|
||||
weight: 17,
|
||||
value: 19,
|
||||
ancestors: [{id: 'charId'}, {id: 'containerId'}],
|
||||
}),
|
||||
clean({
|
||||
_id: 'grandchildItemId',
|
||||
type: 'item',
|
||||
weight: 23,
|
||||
value: 29,
|
||||
ancestors: [{id: 'charId'}, {id: 'containerId'}, {id: 'childContainerId'}],
|
||||
}),
|
||||
];
|
||||
@@ -2,8 +2,9 @@ import computeAction from './computeAction.testFn.js';
|
||||
import computeAttribute from './computeAttribute.testFn.js';
|
||||
import computeClasses from './computeClasses.testFn.js';
|
||||
import computeConstants from './computeConstants.testFn.js';
|
||||
import computeInventory from './computeInventory.testFn.js';
|
||||
|
||||
export default [{
|
||||
export default [/*{
|
||||
text: 'Computes actions',
|
||||
fn: computeAction,
|
||||
},{
|
||||
@@ -15,4 +16,7 @@ export default [{
|
||||
},{
|
||||
text: 'Computes constants',
|
||||
fn: computeConstants,
|
||||
},*/{
|
||||
text: 'Computes inventory',
|
||||
fn: computeInventory,
|
||||
}];
|
||||
|
||||
Reference in New Issue
Block a user