From 347bd8e4766d41e37c9ac0c35e71819d44f2deff Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Thu, 23 Sep 2021 11:41:15 +0200 Subject: [PATCH] Tested and fixed class level computations --- .../buildComputation/linkTypeDependencies.js | 2 +- .../computeByType/computeVariable.js | 2 +- .../aggregate/aggregateClassLevel.js | 3 +- .../computeVariable/computeVariableAsClass.js | 5 +- .../tests/computeClasses.testFn.js | 60 +++++++++++++++++++ .../computeComputation/tests/index.js | 6 +- .../newEngine/computeCreatureComputation.js | 4 +- .../computedOnlyPropertySchemasIndex.js | 2 +- .../computedPropertySchemasIndex.js | 2 +- .../api/properties/propertySchemasIndex.js | 2 +- app/imports/constants/PROPERTIES.js | 2 +- 11 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 app/imports/api/creature/computation/newEngine/computeComputation/tests/computeClasses.testFn.js diff --git a/app/imports/api/creature/computation/newEngine/buildComputation/linkTypeDependencies.js b/app/imports/api/creature/computation/newEngine/buildComputation/linkTypeDependencies.js index 01f1d0b7..d58edd84 100644 --- a/app/imports/api/creature/computation/newEngine/buildComputation/linkTypeDependencies.js +++ b/app/imports/api/creature/computation/newEngine/buildComputation/linkTypeDependencies.js @@ -2,7 +2,7 @@ const linkDependenciesByType = { action: linkResources, attack: linkResources, attribute: linkAttribute, - characterClass: linkVariableName, + class: linkVariableName, classLevel: linkClassLevel, constant: linkVariableName, damageMultiplier: linkDamageMultiplier, diff --git a/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable.js b/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable.js index b66cc075..26ac02de 100644 --- a/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable.js +++ b/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable.js @@ -54,7 +54,7 @@ function computeVariableProp(node, prop, scope){ computeVariableAsSkill(node, prop, scope) } else if (prop.type === 'constant'){ computeVariableAsConstant(node, prop, scope) - } else if (prop.type === 'characterClass'){ + } else if (prop.type === 'class'){ computeVariableAsClass(node, prop, scope) } } diff --git a/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/aggregate/aggregateClassLevel.js b/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/aggregate/aggregateClassLevel.js index 84db29b2..22197dc2 100644 --- a/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/aggregate/aggregateClassLevel.js +++ b/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/aggregate/aggregateClassLevel.js @@ -10,6 +10,7 @@ export default function aggregateClassLevel({node, linkedNode, link}){ if (linkedProp.level > aggregator.level) aggregator.level = linkedProp.level; aggregator.levelsFilled[linkedProp.level] = true; } else if (link.data === 'level'){ - node.baseValue = (node.baseValue || 0) + node.data.classLevelAggregator.level; + node.data.baseValue = (node.data.baseValue || 0) + + linkedNode.data.classLevelAggregator.level; } } diff --git a/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/computeVariableAsClass.js b/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/computeVariableAsClass.js index d48839f2..4a0b4d77 100644 --- a/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/computeVariableAsClass.js +++ b/app/imports/api/creature/computation/newEngine/computeComputation/computeByType/computeVariable/computeVariableAsClass.js @@ -2,11 +2,12 @@ export default function computeVariableAsAttribute(node, prop){ let classLevelAgg = node.data.classLevelAggregator; if (!classLevelAgg) return; prop.level = classLevelAgg.level; - classLevelAgg.levelsFilled.forEach((filled, index) => { + for (let index = 1; index < classLevelAgg.level; index++ ){ + const filled = classLevelAgg.levelsFilled[index]; if (!filled){ if (!prop.missingLevels) prop.missingLevels = []; prop.missingLevels.push(index); } - }); + } prop.missingLevels?.sort((a, b) => a - b); } diff --git a/app/imports/api/creature/computation/newEngine/computeComputation/tests/computeClasses.testFn.js b/app/imports/api/creature/computation/newEngine/computeComputation/tests/computeClasses.testFn.js new file mode 100644 index 00000000..d49011e2 --- /dev/null +++ b/app/imports/api/creature/computation/newEngine/computeComputation/tests/computeClasses.testFn.js @@ -0,0 +1,60 @@ +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 scope = id => computation.scope[id]; + const prop = id => computation.propsById[id]; + assert.equal(scope('level').value, 5); + assert.equal(scope('wizard').level, 4); + assert.equal(prop('wizzardId').level, 4); + assert.deepEqual(prop('wizzardId').missingLevels, [3]); +} + +var testProperties = [ + clean({ + _id: 'wizzardId', + type: 'class', + variableName: 'wizard', + classType: 'startingClass', + ancestors: [{id: 'charId'}], + }), + clean({ + _id: 'rangerId', + type: 'class', + variableName: 'ranger', + classType: 'multiClass', + ancestors: [{id: 'charId'}], + }), + clean({ + _id: 'wiz1Id', + type: 'classLevel', + variableName: 'wizard', + level: 1, + ancestors: [{id: 'charId'}], + }), + clean({ + _id: 'wiz2Id', + type: 'classLevel', + variableName: 'wizard', + level: 2, + ancestors: [{id: 'charId'}], + }), + clean({ + _id: 'wiz4Id', + type: 'classLevel', + variableName: 'wizard', + level: 4, + ancestors: [{id: 'charId'}], + }), + clean({ + _id: 'rang1Id', + type: 'classLevel', + variableName: 'ranger', + level: 1, + ancestors: [{id: 'charId'}], + }), +]; diff --git a/app/imports/api/creature/computation/newEngine/computeComputation/tests/index.js b/app/imports/api/creature/computation/newEngine/computeComputation/tests/index.js index 8c92682e..1ab60e82 100644 --- a/app/imports/api/creature/computation/newEngine/computeComputation/tests/index.js +++ b/app/imports/api/creature/computation/newEngine/computeComputation/tests/index.js @@ -1,5 +1,6 @@ import computeAction from './computeAction.testFn.js'; import computeAttribute from './computeAttribute.testFn.js'; +import computeClasses from './computeClasses.testFn.js'; export default [{ text: 'Computes actions', @@ -7,4 +8,7 @@ export default [{ },{ text: 'Computes attributes', fn: computeAttribute, -},] +},{ + text: 'Computes classes', + fn: computeClasses, +}]; diff --git a/app/imports/api/creature/computation/newEngine/computeCreatureComputation.js b/app/imports/api/creature/computation/newEngine/computeCreatureComputation.js index df8d39a7..a230f30c 100644 --- a/app/imports/api/creature/computation/newEngine/computeCreatureComputation.js +++ b/app/imports/api/creature/computation/newEngine/computeCreatureComputation.js @@ -42,5 +42,7 @@ function compute(graph, node, scope){ } function pushDependenciesToStack(nodeId, graph, stack){ - graph.forEachLinkedNode(nodeId, linkedNode => stack.push(linkedNode), true); + graph.forEachLinkedNode(nodeId, linkedNode => { + stack.push(linkedNode); + }, true); } diff --git a/app/imports/api/properties/computedOnlyPropertySchemasIndex.js b/app/imports/api/properties/computedOnlyPropertySchemasIndex.js index 2f7cf1ee..b01bd38d 100644 --- a/app/imports/api/properties/computedOnlyPropertySchemasIndex.js +++ b/app/imports/api/properties/computedOnlyPropertySchemasIndex.js @@ -32,7 +32,7 @@ const propertySchemasIndex = { attack: ComputedOnlyAttackSchema, attribute: ComputedOnlyAttributeSchema, buff: ComputedOnlyBuffSchema, - characterClass: ComputedOnlyClassSchema, + class: ComputedOnlyClassSchema, classLevel: ComputedOnlyClassLevelSchema, constant: ComputedOnlyConstantSchema, container: ComputedOnlyContainerSchema, diff --git a/app/imports/api/properties/computedPropertySchemasIndex.js b/app/imports/api/properties/computedPropertySchemasIndex.js index f30cd1c2..41fe5084 100644 --- a/app/imports/api/properties/computedPropertySchemasIndex.js +++ b/app/imports/api/properties/computedPropertySchemasIndex.js @@ -32,7 +32,7 @@ const propertySchemasIndex = { attack: ComputedAttackSchema, attribute: ComputedAttributeSchema, buff: ComputedBuffSchema, - characterClass: ComputedClassSchema, + class: ComputedClassSchema, classLevel: ClassLevelSchema, constant: ConstantSchema, damage: ComputedDamageSchema, diff --git a/app/imports/api/properties/propertySchemasIndex.js b/app/imports/api/properties/propertySchemasIndex.js index 00cc1644..c4c9eec8 100644 --- a/app/imports/api/properties/propertySchemasIndex.js +++ b/app/imports/api/properties/propertySchemasIndex.js @@ -32,7 +32,7 @@ const propertySchemasIndex = { attack: AttackSchema, attribute: AttributeSchema, buff: BuffSchema, - characterClass: ClassSchema, + class: ClassSchema, classLevel: ClassLevelSchema, constant: ConstantSchema, damage: DamageSchema, diff --git a/app/imports/constants/PROPERTIES.js b/app/imports/constants/PROPERTIES.js index 38692a85..0a4a296f 100644 --- a/app/imports/constants/PROPERTIES.js +++ b/app/imports/constants/PROPERTIES.js @@ -30,7 +30,7 @@ const PROPERTIES = Object.freeze({ helpText: 'When a buff is activated as a child of an action, it will copy the properties under itself onto a target character.', suggestedParents: ['action', 'attack', 'savingThrow', 'spell'], }, - characterClass: { + class: { icon: 'mdi-card-account-details', name: 'Class', helpText: 'Your character should ideally have one starting class. Classes hold class levels',