Merge branch 'version-2' into version-2-dev

This commit is contained in:
Stefan Zermatten
2022-08-19 09:18:55 +02:00
36 changed files with 858 additions and 197 deletions

View File

@@ -2,6 +2,7 @@ import action from './applyPropertyByType/applyAction.js';
import adjustment from './applyPropertyByType/applyAdjustment.js';
import branch from './applyPropertyByType/applyBranch.js';
import buff from './applyPropertyByType/applyBuff.js';
import buffRemover from './applyPropertyByType/applyBuffRemover.js';
import damage from './applyPropertyByType/applyDamage.js';
import note from './applyPropertyByType/applyNote.js';
import roll from './applyPropertyByType/applyRoll.js';
@@ -13,6 +14,7 @@ const applyPropertyByType = {
adjustment,
branch,
buff,
buffRemover,
damage,
note,
roll,

View File

@@ -11,8 +11,8 @@ import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js'
export default function applyAction(node, actionContext) {
applyNodeTriggers(node, 'before', actionContext);
const prop = node.node;
let targets = actionContext.targets;
if (prop.target === 'self') targets = [actionContext.creature];
if (prop.target === 'self') actionContext.targets = [actionContext.creature];
const targets = actionContext.targets;
// Log the name and summary
let content = { name: prop.name };

View File

@@ -21,10 +21,14 @@ export default function applyBuff(node, actionContext){
// Then copy the decendants of the buff to the targets
let propList = [prop];
function addChildrenToPropList(children){
function addChildrenToPropList(children, { skipCrystalize } = {}){
children.forEach(child => {
if (skipCrystalize) child.node._skipCrystalize = true;
propList.push(child.node);
addChildrenToPropList(child.children);
// recursively add the child's children, but don't crystalize nested buffs
addChildrenToPropList(child.children, {
skipCrystalize: skipCrystalize || child.node.type === 'buff'
});
});
}
addChildrenToPropList(node.children);
@@ -88,6 +92,10 @@ function copyNodeListToTarget(propList, target, oldParent){
*/
function crystalizeVariables({propList, actionContext}){
propList.forEach(prop => {
if (prop._skipCrystalize) {
delete prop._skipCrystalize;
return;
}
computedSchemas[prop.type].computedFields().forEach( calcKey => {
applyFnToKey(prop, calcKey, (prop, key) => {
const calcObj = get(prop, key);

View File

@@ -0,0 +1,101 @@
import { findLast, difference, intersection, filter } from 'lodash';
import applyProperty from '../applyProperty.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { getProperyAncestors, getPropertiesOfType } from '/imports/api/engine/loadCreatures.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import { softRemove } from '/imports/api/parenting/softRemove.js';
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
export default function applyBuffRemover(node, actionContext) {
// Apply triggers
applyNodeTriggers(node, 'before', actionContext);
const prop = node.node;
// Log Name
if (prop.name){
actionContext.addLog({ name: prop.name });
}
// Remove buffs
if (prop.targetParentBuff) {
// Remove nearest ancestor buff
const ancestors = getProperyAncestors(actionContext.creature._id, prop._id);
const nearestBuff = findLast(ancestors, ancestor => ancestor.type === 'buff');
if (!nearestBuff) {
actionContext.addLog({
name: 'Error',
value: 'Buff remover does not have a parent buff to remove',
});
return;
}
removeBuff(nearestBuff, actionContext);
} else {
// Get all the buffs targeted by tags
const allBuffs = getPropertiesOfType(actionContext.creature._id, 'buff');
const targetedBuffs = filter(allBuffs, buff => {
if (buff.inactive) return false;
if (buffRemoverMatchTags(prop, buff)) return true;
});
// Remove the buffs
if (prop.removeAll) {
// Remove all matching buffs
targetedBuffs.forEach(buff => {
removeBuff(buff, actionContext);
});
} else {
// Sort in reverse order
targetedBuffs.sort((a, b) => b.order - a.order);
// Remove the one with the highest order
const buff = targetedBuffs[0];
if (buff) {
removeBuff(buff, actionContext);
}
}
}
// Apply triggers
applyNodeTriggers(node, 'after', actionContext);
// Apply children
node.children.forEach(child => applyProperty(child, actionContext));
}
function removeBuff(buff, actionContext) {
actionContext.addLog({
name: 'Removed',
value: `${buff.name || 'Buff'}`
});
softRemove({ _id: buff._id, collection: CreatureProperties });
}
function buffRemoverMatchTags(buffRemover, prop) {
let matched = false;
const propTags = getEffectivePropTags(prop);
// Check the target tags
if (
!buffRemover.targetTags?.length ||
difference(buffRemover.targetTags, propTags).length === 0
) {
matched = true;
}
// Check the extra tags
buffRemover.extraTags?.forEach(extra => {
if (extra.operation === 'OR') {
if (matched) return;
if (
!extra.tags.length ||
difference(extra.tags, propTags).length === 0
) {
matched = true;
}
} else if (extra.operation === 'NOT') {
if (
extra.tags.length &&
intersection(extra.tags, propTags)
) {
return false;
}
}
});
return matched;
}

View File

@@ -10,8 +10,10 @@ export default function computeToggleDependencies(node, dependencyGraph){
prop.enabled
) return;
walkDown(node.children, child => {
child.node._computationDetails.toggleAncestors.push(prop);
// Only for children that aren't inactive
if (child.node.inactive) return;
// The child nodes depend on the toggle condition compuation
child.node._computationDetails.toggleAncestors.push(prop);
dependencyGraph.addLink(child.node._id, prop._id, 'toggle');
});
}

View File

@@ -105,7 +105,8 @@ function linkBuff(dependencyGraph, prop){
dependOnCalc({dependencyGraph, prop, key: 'duration'});
}
function linkClassLevel(dependencyGraph, prop){
function linkClassLevel(dependencyGraph, prop) {
if (prop.inactive) return;
// The variableName of the prop depends on the prop
if (prop.variableName && prop.level){
dependencyGraph.addLink(prop.variableName, prop._id, 'classLevel');
@@ -121,11 +122,16 @@ function linkDamage(dependencyGraph, prop){
dependOnCalc({dependencyGraph, prop, key: 'amount'});
}
function linkEffects(dependencyGraph, prop, computation){
function linkEffects(dependencyGraph, prop, computation) {
// The effect depends on its amount calculation
dependOnCalc({dependencyGraph, prop, key: 'amount'});
dependOnCalc({ dependencyGraph, prop, key: 'amount' });
// Inactive effects aren't going to impact their targeted stats
if (prop.inactive) return;
// The stats depend on the effect
if (prop.targetByTags){
if (prop.inactive) {
// Inactive effects apply to no stats
return;
} else if (prop.targetByTags){
getEffectTagTargets(prop, computation).forEach(targetId => {
const targetProp = computation.propsById[targetId];
if (
@@ -221,13 +227,14 @@ function linkRoll(dependencyGraph, prop){
}
function linkVariableName(dependencyGraph, prop){
// The variableName of the prop depends on the prop
if (prop.variableName){
// The variableName of the prop depends on the prop if the prop is active
if (prop.variableName && !prop.inactive){
dependencyGraph.addLink(prop.variableName, prop._id, 'definition');
}
}
function linkDamageMultiplier(dependencyGraph, prop){
function linkDamageMultiplier(dependencyGraph, prop) {
if (prop.inactive) return;
prop.damageTypes.forEach(damageType => {
// Remove all non-letter characters from the damage name
const damageName = damageType.replace(/[^a-z]/gi, '')
@@ -237,6 +244,7 @@ function linkDamageMultiplier(dependencyGraph, prop){
function linkProficiencies(dependencyGraph, prop){
// The stats depend on the proficiency
if (prop.inactive) return;
prop.stats.forEach(statName => {
if (!statName) return;
dependencyGraph.addLink(statName, prop._id, prop.type);
@@ -248,6 +256,10 @@ function linkSavingThrow(dependencyGraph, prop){
}
function linkSkill(dependencyGraph, prop){
// Depends on base value
dependOnCalc({ dependencyGraph, prop, key: 'baseValue' });
// Link dependents
if (prop.inactive) return;
linkVariableName(dependencyGraph, prop);
// The prop depends on the variable references as the ability
if (prop.ability){
@@ -255,9 +267,6 @@ function linkSkill(dependencyGraph, prop){
}
// Skills depend on the creature's proficiencyBonus
dependencyGraph.addLink(prop._id, 'proficiencyBonus', 'skillProficiencyBonus');
// Depends on base value
dependOnCalc({dependencyGraph, prop, key: 'baseValue'});
}
function linkSlot(dependencyGraph, prop){

View File

@@ -89,6 +89,10 @@ export function buildComputationFromProps(properties, creature, variables){
// Walk the property trees computing things that need to be inherited
walkDown(forest, node => {
computeInactiveStatus(node);
});
// Inactive status must be complete for the whole tree before toggle deps
// are calculated
walkDown(forest, node => {
computeToggleDependencies(node, dependencyGraph);
computeSlotQuantityFilled(node, dependencyGraph);
});

View File

@@ -29,7 +29,7 @@ export default function getAggregatorResult(node){
if (aggregator.set !== undefined) {
result = aggregator.set;
}
if (!node.definingProp?.decimal && Number.isFinite(result)){
if (!node.data.definingProp?.decimal && Number.isFinite(result)){
result = Math.floor(result);
} else if (Number.isFinite(result)){
result = stripFloatingPointOddities(result);

View File

@@ -51,11 +51,22 @@ function compute(computation, node){
function pushDependenciesToStack(nodeId, graph, stack, computation){
graph.forEachLinkedNode(nodeId, linkedNode => {
if (linkedNode._visitedChildren && !linkedNode._visited){
const pather = path.nba(graph, {
oriented: true
});
const loop = pather.find(nodeId, nodeId);
if (linkedNode._visitedChildren && !linkedNode._visited) {
// This is a dependency loop, find a path from the node to itself
// and store that path as a dependency loop error
const pather = path.nba(graph, { oriented: true });
let loop = [];
// Pather doesn't like going from a node to iteself, so find all the
// paths going from the next node back to the original node
// and return the shortest one
graph.forEachLinkedNode(nodeId, nextNode => {
const newLoop = pather.find(nextNode.id, nodeId);
if (!newLoop.length) return;
if (!loop.length || newLoop.length < loop.length - 1) {
loop = [linkedNode, ...newLoop];
}
}, true);
if (loop.length) {
computation.errors.push({
type: 'dependencyLoop',

View File

@@ -46,7 +46,6 @@ export function getSingleProperty(creatureId, propertyId) {
'removed': {$ne: true},
}, {
sort: { order: 1 },
fields: { icon: 0 },
});
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return prop;
@@ -65,7 +64,6 @@ export function getProperties(creatureId) {
'removed': {$ne: true},
}, {
sort: { order: 1 },
fields: { icon: 0 },
}).fetch();
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return props;
@@ -90,7 +88,6 @@ export function getPropertiesOfType(creatureId, propType) {
'type': propType,
}, {
sort: { order: 1 },
fields: { icon: 0 },
}).fetch();
// console.timeEnd(`Cache miss on creature properties: ${creatureId}`);
return props;
@@ -100,14 +97,13 @@ export function getCreature(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const creature = loadedCreature.creature;
if (creature) return creature;
if (creature) {
const cloneCreature = EJSON.clone(creature);
return cloneCreature;
}
}
// console.time(`Cache miss on Creature: ${creatureId}`);
const creature = Creatures.findOne(creatureId, {
denormalizedStats: 1,
variables: 1,
dirty: 1,
});
const creature = Creatures.findOne(creatureId);
// console.timeEnd(`Cache miss on Creature: ${creatureId}`);
return creature;
}
@@ -116,7 +112,10 @@ export function getVariables(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const variables = loadedCreature.variables;
if (variables) return variables;
if (variables) {
const cloneVarables = EJSON.clone(variables);
return cloneVarables;
}
}
// console.time(`Cache miss on variables: ${creatureId}`);
const variables = CreatureVariables.findOne({_creatureId: creatureId});
@@ -149,6 +148,7 @@ export function getProperyAncestors(creatureId, propertyId) {
// Fetch from database
return CreatureProperties.find({
_id: { $in: ancestorIds },
removed: {$ne: true},
}, {
sort: { order: 1 },
}).fetch();
@@ -175,6 +175,8 @@ export function getPropertyDecendants(creatureId, propertyId) {
return CreatureProperties.find({
'ancestors.id': propertyId,
removed: { $ne: true },
}, {
sort: { order: 1 },
}).fetch();
}
}
@@ -199,7 +201,6 @@ class LoadedCreature {
removed: { $ne: true },
}, {
sort: { order: 1 },
fields: { icon: 0 },
}).observeChanges({
added(id, fields) {
fields._id = id;

View File

@@ -0,0 +1,79 @@
import SimpleSchema from 'simpl-schema';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
let BuffRemoverSchema = createPropertySchema({
name: {
type: String,
optional: true,
max: STORAGE_LIMITS.name,
},
// This will remove just the nearest ancestor buff
targetParentBuff: {
type: Boolean,
optional: true,
},
// The following only applies when not targeting the parent buff
// Which character to remove buffs from
target: {
type: String,
allowedValues: [
'self',
'target',
],
defaultValue: 'target',
},
// remove 1 or remove all
removeAll: {
type: Boolean,
optional: true,
defaultValue: true,
},
// Buffs to remove based on tags:
targetTags: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.tagCount,
},
'targetTags.$': {
type: String,
max: STORAGE_LIMITS.tagLength,
},
extraTags: {
type: Array,
optional: true,
maxCount: STORAGE_LIMITS.extraTagsCount,
},
'extraTags.$': {
type: Object,
},
'extraTags.$._id': {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue(){
if (!this.isSet) return Random.id();
}
},
'extraTags.$.operation': {
type: String,
allowedValues: ['OR', 'NOT'],
defaultValue: 'OR',
},
'extraTags.$.tags': {
type: Array,
defaultValue: [],
maxCount: STORAGE_LIMITS.tagCount,
},
'extraTags.$.tags.$': {
type: String,
max: STORAGE_LIMITS.tagLength,
},
});
let ComputedOnlyBuffRemoverSchema = createPropertySchema({});
const ComputedBuffRemoverSchema = new SimpleSchema()
.extend(BuffRemoverSchema)
.extend(ComputedOnlyBuffRemoverSchema);
export { BuffRemoverSchema, ComputedOnlyBuffRemoverSchema, ComputedBuffRemoverSchema };

View File

@@ -12,6 +12,10 @@ let BuffSchema = createPropertySchema({
type: 'inlineCalculationFieldToCompute',
optional: true,
},
hideRemoveButton: {
type: Boolean,
optional: true,
},
// How many rounds this buff lasts
duration: {
type: 'fieldToCompute',

View File

@@ -1,15 +1,15 @@
import SimpleSchema from 'simpl-schema';
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
import createPropertySchema from '/imports/api/properties/subSchemas/createPropertySchema.js';
// Folders organize a character sheet into a tree, particularly to group things
// like 'race' and 'background'
let FolderSchema = new SimpleSchema({
let FolderSchema = new createPropertySchema({
name: {
type: String,
max: STORAGE_LIMITS.name,
},
});
const ComputedOnlyFolderSchema = new SimpleSchema({});
const ComputedOnlyFolderSchema = new createPropertySchema({});
export { FolderSchema, ComputedOnlyFolderSchema };

View File

@@ -25,6 +25,7 @@ const actionPropertyTypeOptions = {
adjustment: 'Attribute damage',
branch: 'Branch',
buff: 'Buff',
buffRemover: 'Buff Removed',
damage: 'Damage',
note: 'Note',
roll: 'Roll',

View File

@@ -3,6 +3,7 @@ import { ComputedOnlyActionSchema } from '/imports/api/properties/Actions.js';
import { ComputedOnlyAdjustmentSchema } from '/imports/api/properties/Adjustments.js';
import { ComputedOnlyAttributeSchema } from '/imports/api/properties/Attributes.js';
import { ComputedOnlyBuffSchema } from '/imports/api/properties/Buffs.js';
import { ComputedOnlyBuffRemoverSchema } from '/imports/api/properties/BuffRemovers.js';
import { ComputedOnlyBranchSchema } from '/imports/api/properties/Branches.js';
import { ComputedOnlyClassSchema } from '/imports/api/properties/Classes.js';
import { ComputedOnlyClassLevelSchema } from '/imports/api/properties/ClassLevels.js';
@@ -33,6 +34,7 @@ const propertySchemasIndex = {
adjustment: ComputedOnlyAdjustmentSchema,
attribute: ComputedOnlyAttributeSchema,
buff: ComputedOnlyBuffSchema,
buffRemover: ComputedOnlyBuffRemoverSchema,
branch: ComputedOnlyBranchSchema,
class: ComputedOnlyClassSchema,
classLevel: ComputedOnlyClassLevelSchema,

View File

@@ -3,6 +3,7 @@ import { ComputedActionSchema } from '/imports/api/properties/Actions.js';
import { ComputedAdjustmentSchema } from '/imports/api/properties/Adjustments.js';
import { ComputedAttributeSchema } from '/imports/api/properties/Attributes.js';
import { ComputedBuffSchema } from '/imports/api/properties/Buffs.js';
import { ComputedBuffRemoverSchema } from '/imports/api/properties/BuffRemovers.js';
import { ComputedBranchSchema } from '/imports/api/properties/Branches.js';
import { ComputedClassSchema } from '/imports/api/properties/Classes.js';
import { ComputedClassLevelSchema } from '/imports/api/properties/ClassLevels.js';
@@ -33,6 +34,7 @@ const propertySchemasIndex = {
adjustment: ComputedAdjustmentSchema,
attribute: ComputedAttributeSchema,
buff: ComputedBuffSchema,
buffRemover: ComputedBuffRemoverSchema,
branch: ComputedBranchSchema,
class: ComputedClassSchema,
classLevel: ComputedClassLevelSchema,

View File

@@ -3,6 +3,7 @@ import { ActionSchema } from '/imports/api/properties/Actions.js';
import { AdjustmentSchema } from '/imports/api/properties/Adjustments.js';
import { AttributeSchema } from '/imports/api/properties/Attributes.js';
import { BuffSchema } from '/imports/api/properties/Buffs.js';
import { BuffRemoverSchema } from '/imports/api/properties/BuffRemovers.js';
import { BranchSchema } from '/imports/api/properties/Branches.js';
import { ClassSchema } from '/imports/api/properties/Classes.js';
import { ClassLevelSchema } from '/imports/api/properties/ClassLevels.js';
@@ -33,6 +34,7 @@ const propertySchemasIndex = {
adjustment: AdjustmentSchema,
attribute: AttributeSchema,
buff: BuffSchema,
buffRemover: BuffRemoverSchema,
branch: BranchSchema,
class: ClassSchema,
classLevel: ClassLevelSchema,