Began the great TypeScript Migration

It's helping a lot to move to the new parenting system
This commit is contained in:
Thaum Rystra
2023-09-28 14:16:34 +02:00
parent 26e4d0bcc7
commit e6963ec865
125 changed files with 607 additions and 389 deletions

View File

@@ -4,7 +4,7 @@ import {
} from '/imports/api/engine/loadCreatures.js';
import { groupBy, remove } from 'lodash';
export default class ActionContext{
export default class ActionContext {
constructor(creatureId, targetIds = [], method) {
// Get the creature
this.creature = getCreature(creatureId)
@@ -64,7 +64,7 @@ export default class ActionContext{
}
}
addLog(content) {
if (content.name || content.value){
if (content.name || content.value) {
this.log.content.push(content);
}
}

View File

@@ -1,35 +0,0 @@
import action from './applyPropertyByType/applyAction.js';
import ammo from './applyPropertyByType/applyItemAsAmmo.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 folder from './applyPropertyByType/applyFolder.js';
import note from './applyPropertyByType/applyNote.js';
import roll from './applyPropertyByType/applyRoll.js';
import savingThrow from './applyPropertyByType/applySavingThrow.js';
import toggle from './applyPropertyByType/applyToggle.js';
const applyPropertyByType = {
action,
ammo,
adjustment,
branch,
buff,
buffRemover,
damage,
folder,
note,
propertySlot: folder,
roll,
savingThrow,
spell: action,
toggle,
};
export default function applyProperty(node, actionContext, ...rest) {
if (node.node.deactivatedByToggle) return;
actionContext.scope[`#${node.node.type}`] = node.node;
applyPropertyByType[node.node.type]?.(node, actionContext, ...rest);
}

View File

@@ -0,0 +1,38 @@
import action from './applyPropertyByType/applyAction';
import ammo from './applyPropertyByType/applyItemAsAmmo'
import adjustment from './applyPropertyByType/applyAdjustment';
import branch from './applyPropertyByType/applyBranch';
import buff from './applyPropertyByType/applyBuff';
import buffRemover from './applyPropertyByType/applyBuffRemover';
import damage from './applyPropertyByType/applyDamage';
import folder from './applyPropertyByType/applyFolder';
import note from './applyPropertyByType/applyNote';
import roll from './applyPropertyByType/applyRoll';
import savingThrow from './applyPropertyByType/applySavingThrow';
import toggle from './applyPropertyByType/applyToggle';
import ActionContext from '/imports/api/engine/actions/ActionContext';
import { TreeNode } from '/imports/api/parenting/parentingFunctions';
import { CreatureProperty } from '/imports/api/creature/creatureProperties/CreatureProperties';
const applyPropertyByType = {
action,
ammo,
adjustment,
branch,
buff,
buffRemover,
damage,
folder,
note,
propertySlot: folder,
roll,
savingThrow,
spell: action,
toggle,
};
export default function applyProperty(node: TreeNode<CreatureProperty>, actionContext: ActionContext, ...rest) {
if (node.doc.deactivatedByToggle) return;
actionContext.scope[`#${node.doc.type}`] = node.doc;
applyPropertyByType[node.doc.type]?.(node, actionContext, ...rest);
}

View File

@@ -1,22 +1,31 @@
import recalculateInlineCalculations from './shared/recalculateInlineCalculations.js';
import recalculateCalculation from './shared/recalculateCalculation.js';
import rollDice from '/imports/parser/rollDice.js';
import applyProperty from '../applyProperty.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import applyProperty from '../applyProperty';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
import numberToSignedString from '/imports/api/utility/numberToSignedString.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import { resetProperties } from '/imports/api/creature/creatures/methods/restCreature.js';
import { TreeNode } from '/imports/api/parenting/parentingFunctions';
import { Action } from '/imports/api/properties/Actions';
import { LogContent } from '/imports/api/creature/log/LogContentSchema.js';
import { Item } from '/imports/api/properties/Items.js';
export default function applyAction(node, actionContext) {
interface Ammo extends Item {
type: 'ammo'
adjustment: number
}
export default function applyAction(node: TreeNode<Action>, actionContext) {
applyNodeTriggers(node, 'before', actionContext);
const prop = node.node;
const prop = node.doc;
if (prop.target === 'self') actionContext.targets = [actionContext.creature];
const targets = actionContext.targets;
// Log the name and summary
let content = { name: prop.name };
const content: LogContent = { name: prop.name, };
if (prop.summary?.text) {
recalculateInlineCalculations(prop.summary, actionContext);
content.value = prop.summary.value;
@@ -27,7 +36,7 @@ export default function applyAction(node, actionContext) {
const failed = spendResources(prop, actionContext);
if (failed) return;
const attack = prop.attackRoll || prop.attackRollBonus;
const attack = prop.attackRoll;
// Attack if there is an attack roll
if (attack && attack.calculation) {
@@ -59,7 +68,7 @@ function applyAttackWithoutTarget({ attack, actionContext }) {
recalculateCalculation(attack, actionContext);
const scope = actionContext.scope;
let {
const {
resultPrefix,
result,
criticalHit,
@@ -96,7 +105,7 @@ function applyAttackToTarget({ attack, target, actionContext }) {
recalculateCalculation(attack, actionContext);
let {
const {
resultPrefix,
result,
criticalHit,
@@ -176,7 +185,7 @@ function applyCrits(value, scope) {
scopeCrit = scopeCrit.value;
}
const criticalHitTarget = scopeCrit || 20;
let criticalHit = value >= criticalHitTarget;
const criticalHit = value >= criticalHitTarget;
let criticalMiss;
if (criticalHit) {
scope['~criticalHit'] = { value: true };
@@ -189,9 +198,9 @@ function applyCrits(value, scope) {
return { criticalHit, criticalMiss };
}
function spendResources(prop, actionContext) {
function spendResources(prop: Action, actionContext) {
// Check Uses
if (prop.usesLeft <= 0) {
if (!prop.usesLeft || prop.usesLeft <= 0) {
if (!prop.silent) actionContext.addLog({
name: 'Error',
value: `${prop.name || 'action'} does not have enough uses left`,
@@ -207,42 +216,45 @@ function spendResources(prop, actionContext) {
return true;
}
// Items
let spendLog = [];
let gainLog = [];
const ammoToApply = [];
const spendLog: string[] = [];
const gainLog: string[] = [];
const ammoToApply: TreeNode<Ammo>[] = [];
try {
prop.resources.itemsConsumed.forEach(itemConsumed => {
recalculateCalculation(itemConsumed.quantity, actionContext);
if (!itemConsumed.itemId) {
throw 'No ammo was selected for this prop';
}
let item = CreatureProperties.findOne(itemConsumed.itemId);
if (!item || item.ancestors[0].id !== prop.ancestors[0].id) {
const item = CreatureProperties.findOne(itemConsumed.itemId) as Item;
if (!item || item.root.id !== prop.root.id) {
throw 'The prop\'s ammo was not found on the creature';
}
if (
!itemConsumed?.quantity?.value ||
!isFinite(itemConsumed.quantity.value)
!isFinite(+itemConsumed.quantity.value)
) return;
const quantityConsumed = +itemConsumed.quantity.value;
let logName = item.name;
if (itemConsumed.quantity.value > 1 || itemConsumed.quantity.value < -1) {
if (quantityConsumed > 1 || quantityConsumed < -1) {
logName = item.plural || logName;
}
if (itemConsumed.quantity.value > 0) {
spendLog.push(logName + ': ' + itemConsumed.quantity.value);
} else if (itemConsumed.quantity.value < 0) {
gainLog.push(logName + ': ' + -itemConsumed.quantity.value);
if (quantityConsumed > 0) {
spendLog.push(logName + ': ' + quantityConsumed);
} else if (quantityConsumed < 0) {
gainLog.push(logName + ': ' + -quantityConsumed);
}
// So long as the item isn't an ancestor of the current prop apply it
// If it was an ancestor this would be an infinite loop
if (!hasAncestorRelationship(item, prop)) {
ammoToApply.push({
node: {
doc: {
...item,
// Use ammo pseudo-type
type: 'ammo',
// Store the adjustment to be applied
adjustment: itemConsumed.quantity.value,
adjustment: quantityConsumed,
},
children: []
});
@@ -263,6 +275,7 @@ function spendResources(prop, actionContext) {
CreatureProperties.update(prop._id, {
$inc: { usesUsed: 1 }
}, {
//@ts-expect-error no typings for collection 2 selector
selector: prop
});
if (!prop.silent) actionContext.addLog({
@@ -277,8 +290,9 @@ function spendResources(prop, actionContext) {
recalculateCalculation(attConsumed.quantity, actionContext);
if (!attConsumed.quantity?.value) return;
const quantityConsumed = +attConsumed.quantity.value;
if (!attConsumed.variableName) return;
let stat = actionContext.scope[attConsumed.variableName];
const stat = actionContext.scope[attConsumed.variableName];
if (!stat) {
spendLog.push(attConsumed.variableName + ': ' + ' not found');
return;
@@ -289,10 +303,10 @@ function spendResources(prop, actionContext) {
value: attConsumed.quantity.value,
actionContext,
});
if (attConsumed.quantity.value > 0) {
spendLog.push(stat.name + ': ' + attConsumed.quantity.value);
} else if (attConsumed.quantity.value < 0) {
gainLog.push(stat.name + ': ' + -attConsumed.quantity.value);
if (quantityConsumed > 0) {
spendLog.push(stat.name + ': ' + quantityConsumed);
} else if (quantityConsumed < 0) {
gainLog.push(stat.name + ': ' + -quantityConsumed);
}
});

View File

@@ -1,7 +1,7 @@
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
import recalculateCalculation from './shared/recalculateCalculation.js';
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
export default function applyAdjustment(node, actionContext) {
applyNodeTriggers(node, 'before', actionContext);

View File

@@ -1,8 +1,8 @@
import applyProperty from '../applyProperty.js';
import applyProperty from '../applyProperty';
import recalculateCalculation from './shared/recalculateCalculation.js';
import rollDice from '/imports/parser/rollDice.js';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
export default function applyBranch(node, actionContext) {
applyNodeTriggers(node, 'before', actionContext);

View File

@@ -1,9 +1,8 @@
import {
setLineageOfDocs,
renewDocIds
} from '/imports/api/parenting/parenting.js';
import { setDocToLastOrder } from '/imports/api/parenting/order.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
} from '/imports/api/parenting/parentingFunctions';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
import applyFnToKey from '/imports/api/engine/computation/utility/applyFnToKey.js';
import { get } from 'lodash';
@@ -12,7 +11,7 @@ import symbol from '/imports/parser/parseTree/symbol.js';
import logErrors from './shared/logErrors.js';
import { insertCreatureLog } from '/imports/api/creature/log/CreatureLogs.js';
import cyrb53 from '/imports/api/engine/computation/utility/cyrb53.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import INLINE_CALCULATION_REGEX from '/imports/constants/INLINE_CALCULTION_REGEX.js';
import recalculateInlineCalculations from './shared/recalculateInlineCalculations.js';
@@ -92,10 +91,12 @@ function copyNodeListToTarget(propList, target, oldParent) {
renewDocIds({
docArray: propList,
});
/*
setDocToLastOrder({
collection: CreatureProperties,
doc: propList[0],
});
*/
CreatureProperties.batchInsert(propList);
}

View File

@@ -1,9 +1,9 @@
import { findLast, difference, intersection, filter } from 'lodash';
import applyProperty from '../applyProperty.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import applyProperty from '../applyProperty';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
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 CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import { softRemove } from '/imports/api/parenting/softRemove';
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';

View File

@@ -8,7 +8,7 @@ import { damagePropertyWork } from '/imports/api/creature/creatureProperties/met
import {
getPropertiesOfType
} from '/imports/api/engine/loadCreatures.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
import applySavingThrow from '/imports/api/engine/actions/applyPropertyByType/applySavingThrow.js';

View File

@@ -1,5 +1,5 @@
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
export default function applyFolder(node, actionContext) {
// Apply triggers

View File

@@ -1,7 +1,7 @@
import { getPropertyDecendants } from '/imports/api/engine/loadCreatures.js';
import applyProperty from '../applyProperty.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import applyProperty from '../applyProperty';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import { docsToForest as nodeArrayToTree } from '/imports/api/parenting/parentingFunctions';
import { adjustQuantityWork } from '/imports/api/creature/creatureProperties/methods/adjustQuantity.js';
export default function applyItemAsAmmo(node, actionContext) {

View File

@@ -1,6 +1,6 @@
import recalculateInlineCalculations from './shared/recalculateInlineCalculations.js';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
export default function applyNote(node, actionContext) {
applyNodeTriggers(node, 'before', actionContext);

View File

@@ -2,7 +2,7 @@ import applyChildren from '/imports/api/engine/actions/applyPropertyByType/share
import logErrors from './shared/logErrors.js';
import applyEffectsToCalculationParseNode from '/imports/api/engine/actions/applyPropertyByType/shared/applyEffectsToCalculationParseNode.js';
import resolve, { toString } from '/imports/parser/resolve.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
export default function applyRoll(node, actionContext) {
applyNodeTriggers(node, 'before', actionContext);

View File

@@ -1,9 +1,9 @@
import rollDice from '/imports/parser/rollDice.js';
import recalculateCalculation from './shared/recalculateCalculation.js';
import applyProperty from '../applyProperty.js';
import applyProperty from '../applyProperty';
import numberToSignedString from '/imports/api/utility/numberToSignedString.js';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import { applyUnresolvedEffects } from '/imports/api/engine/actions/doCheck.js';
export default function applySavingThrow(node, actionContext) {

View File

@@ -1,5 +1,5 @@
import recalculateCalculation from './shared/recalculateCalculation.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import applyChildren from '/imports/api/engine/actions/applyPropertyByType/shared/applyChildren.js';
export default function applyToggle(node, actionContext) {

View File

@@ -1,5 +1,5 @@
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import applyProperty from '/imports/api/engine/actions/applyProperty.js';
import { applyNodeTriggers } from '/imports/api/engine/actions/applyTriggers';
import applyProperty from '/imports/api/engine/actions/applyProperty';
export default function applyChildren(node, actionContext) {
applyNodeTriggers(node, 'after', actionContext);

View File

@@ -1,4 +1,4 @@
import { triggerMatchTags } from '/imports/api/engine/actions/applyTriggers.js';
import { triggerMatchTags } from '/imports/api/engine/actions/applyTriggers';
import clean from '/imports/api/engine/computation/utility/cleanProp.testFn.js';
import { assert } from 'chai';

View File

@@ -1,13 +1,13 @@
import recalculateCalculation from '/imports/api/engine/actions/applyPropertyByType/shared/recalculateCalculation.js';
import recalculateInlineCalculations from '/imports/api/engine/actions/applyPropertyByType/shared/recalculateInlineCalculations.js';
import { getPropertyDecendants } from '/imports/api/engine/loadCreatures.js';
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import applyProperty from '/imports/api/engine/actions/applyProperty.js';
import { TreeNode, docsToForest as nodeArrayToTree } from '/imports/api/parenting/parentingFunctions';
import applyProperty from '/imports/api/engine/actions/applyProperty';
import { difference, intersection } from 'lodash';
import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags.js';
export function applyNodeTriggers(node, timing, actionContext) {
const prop = node.node;
export function applyNodeTriggers(node: TreeNode<any>, timing, actionContext) {
const prop = node.doc;
const type = prop.type;
const triggers = actionContext.triggers?.doActionProperty?.[type]?.[timing];
if (triggers) {

View File

@@ -2,14 +2,14 @@ import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import { docsToForest } from '/imports/api/parenting/parentingFunctions';
import {
getProperyAncestors, getPropertyDecendants
} from '/imports/api/engine/loadCreatures.js';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import applyProperty from './applyProperty.js';
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import applyProperty from './applyProperty';
import ActionContext from '/imports/api/engine/actions/ActionContext';
const doAction = new ValidatedMethod({
name: 'creatureProperties.doAction',
@@ -38,8 +38,9 @@ const doAction = new ValidatedMethod({
},
run({ actionId, targetIds = [], scope }) {
// Get action context
let action = CreatureProperties.findOne(actionId);
const creatureId = action.ancestors[0].id;
const action = CreatureProperties.findOne(actionId);
if (!action) throw new Meteor.Error('not-found', 'The action was not found');
const creatureId = action.root.id;
const actionContext = new ActionContext(creatureId, targetIds, this);
// Check permissions
@@ -74,7 +75,7 @@ export function doActionWork({
}) {
// get the docs
const ancestorScope = getAncestorScope(ancestors);
const propertyForest = nodeArrayToTree(properties);
const propertyForest = docsToForest(properties);
if (propertyForest.length !== 1) {
throw new Meteor.Error(`The action has ${propertyForest.length} top level properties, expected 1`);
}
@@ -92,7 +93,7 @@ export function doActionWork({
// Assumes ancestors are in tree order already
function getAncestorScope(ancestors) {
let scope = {};
const scope = {};
ancestors.forEach(prop => {
scope[`#${prop.type}`] = prop;
});

View File

@@ -3,7 +3,7 @@ import '/imports/api/simpleSchemaConfig.js';
import applyTriggers from '/imports/api/engine/actions/applyTriggers.testFn.js';
import { doActionWork } from './doAction.js';
import { CreatureLogSchema } from '/imports/api/creature/log/CreatureLogs.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
function cleanProp(prop) {

View File

@@ -5,11 +5,11 @@ import Creatures from '/imports/api/creature/creatures/Creatures.js';
import {
getProperyAncestors, getPropertyDecendants
} from '/imports/api/engine/loadCreatures.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
import { damagePropertyWork } from '/imports/api/creature/creatureProperties/methods/damageProperty.js';
import { doActionWork } from '/imports/api/engine/actions/doAction.js';
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
import ActionContext from '/imports/api/engine/actions/ActionContext';
const doAction = new ValidatedMethod({
name: 'creatureProperties.doCastSpell',

View File

@@ -1,12 +1,12 @@
import SimpleSchema from 'simpl-schema';
import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { RateLimiterMixin } from 'ddp-rate-limiter-mixin';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import { assertEditPermission } from '/imports/api/creature/creatures/creaturePermissions.js';
import rollDice from '/imports/parser/rollDice.js';
import numberToSignedString from '/imports/api/utility/numberToSignedString.js';
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers.js';
import ActionContext from '/imports/api/engine/actions/ActionContext.js';
import { applyTriggers } from '/imports/api/engine/actions/applyTriggers';
import ActionContext from '/imports/api/engine/actions/ActionContext';
import evaluateCalculation from '/imports/api/engine/computation/utility/evaluateCalculation.js';
const doCheck = new ValidatedMethod({

View File

@@ -1,7 +1,10 @@
import { CreatureProperty } from '/imports/api/creature/creatureProperties/CreatureProperties';
import walkDown from '/imports/api/engine/computation/utility/walkdown.js';
import { TreeNode } from '/imports/api/parenting/parentingFunctions';
import { isSpell } from '/imports/api/properties/Spells';
export default function computeInactiveStatus(node) {
const prop = node.node;
export default function computeInactiveStatus(node: TreeNode<CreatureProperty>): void {
const prop = node.doc;
if (!isActive(prop)) {
// Mark prop inactive due to self
prop.inactive = true;
@@ -16,16 +19,16 @@ export default function computeInactiveStatus(node) {
}
}
function isActive(prop) {
function isActive(prop: CreatureProperty): boolean {
if (prop.disabled) return false;
switch (prop.type) {
// Unprepared spells are inactive
case 'spell': return !!prop.prepared || !!prop.alwaysPrepared;
default: return true;
if (isSpell(prop)) {
return !!prop.prepared || !!prop.alwaysPrepared;
} else {
return true;
}
}
function childrenActive(prop) {
function childrenActive(prop): boolean {
// Children of disabled properties are always inactive
if (prop.disabled) return false;
switch (prop.type) {

View File

@@ -1,13 +1,13 @@
import { nodeArrayToTree } from '/imports/api/parenting/nodesToTree.js';
import { docsToForest as nodeArrayToTree } from '/imports/api/parenting/parentingFunctions';
import { DenormalisedOnlyCreaturePropertySchema as denormSchema }
from '/imports/api/creature/creatureProperties/CreatureProperties.js';
from '/imports/api/creature/creatureProperties/CreatureProperties';
import { getProperties, getCreature, getVariables } from '/imports/api/engine/loadCreatures.js';
import computedOnlySchemas from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
import computedSchemas from '/imports/api/properties/computedPropertySchemasIndex.js';
import linkInventory from './buildComputation/linkInventory.js';
import walkDown from './utility/walkdown.js';
import parseCalculationFields from './buildComputation/parseCalculationFields.js';
import computeInactiveStatus from './buildComputation/computeInactiveStatus.js';
import computeInactiveStatus from './buildComputation/computeInactiveStatus';
import computeToggleDependencies from './buildComputation/computeToggleDependencies.js';
import linkCalculationDependencies from './buildComputation/linkCalculationDependencies.js';
import linkTypeDependencies from './buildComputation/linkTypeDependencies.js';

View File

@@ -1,7 +1,7 @@
import '/imports/api/simpleSchemaConfig.js';
import { buildComputationFromProps } from './buildCreatureComputation.js';
import { assert } from 'chai';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import computeInactiveStatus from './buildComputation/tests/computeInactiveStatus.testFn.js';
import computeSlotQuantityFilled from './buildComputation/tests/computeSlotQuantityFilled.testFn.js';
import computeToggleDependencies from './buildComputation/tests/computeToggleDependencies.testFn.js';
@@ -9,8 +9,8 @@ import linkCalculationDependencies from './buildComputation/tests/linkCalculatio
import linkInventory from './buildComputation/tests/linkInventory.testFn.js';
import linkTypeDependencies from './buildComputation/tests/linkTypeDependencies.testFn.js';
describe('buildComputation', function(){
it('Builds something at all', function(){
describe('buildComputation', function () {
it('Builds something at all', function () {
let computation = buildComputationFromProps(testProperties);
assert.exists(computation);
});
@@ -37,7 +37,7 @@ var testProperties = [
}),
];
function clean(prop){
function clean(prop) {
let schema = CreatureProperties.simpleSchema(prop);
return schema.clean(prop);
}

View File

@@ -1,11 +1,11 @@
import computeCreatureComputation from './computeCreatureComputation.js';
import { buildComputationFromProps } from './buildCreatureComputation.js';
import { assert } from 'chai';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import computeTests from './computeComputation/tests/index.js';
describe('Compute compuation', function(){
it('Computes something at all', function(){
describe('Compute compuation', function () {
it('Computes something at all', function () {
let computation = buildComputationFromProps(testProperties);
computeCreatureComputation(computation);
assert.exists(computation);
@@ -28,7 +28,7 @@ var testProperties = [
}),
];
function clean(prop){
function clean(prop) {
let schema = CreatureProperties.simpleSchema(prop);
return schema.clean(prop);
}

View File

@@ -1,6 +1,6 @@
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
export default function cleanProp(prop){
export default function cleanProp(prop) {
let schema = CreatureProperties.simpleSchema(prop);
return schema.clean(prop);
}

View File

@@ -1,6 +1,6 @@
import { Meteor } from 'meteor/meteor'
import { EJSON } from 'meteor/ejson';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import propertySchemasIndex from '/imports/api/properties/computedOnlyPropertySchemasIndex.js';
export default function writeAlteredProperties(computation) {

View File

@@ -1,11 +1,12 @@
import { debounce } from 'lodash';
import Creatures from '/imports/api/creature/creatures/Creatures.js';
import CreatureVariables from '/imports/api/creature/creatures/CreatureVariables';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties.js';
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';
import computeCreature from './computeCreature';
const COMPUTE_DEBOUNCE_TIME = 100; // ms
export const loadedCreatures = new Map(); // creatureId => {creature, properties, etc.}
// TODO: migrate to nested sets
export function loadCreature(creatureId, subscription) {
if (!creatureId) throw 'creatureId is required';
@@ -43,7 +44,7 @@ export function getSingleProperty(creatureId, propertyId) {
const prop = CreatureProperties.findOne({
_id: propertyId,
'ancestors.id': creatureId,
'removed': {$ne: true},
'removed': { $ne: true },
}, {
sort: { order: 1 },
});
@@ -61,7 +62,7 @@ export function getProperties(creatureId) {
// console.time(`Cache miss on creature properties: ${creatureId}`)
const props = CreatureProperties.find({
'ancestors.id': creatureId,
'removed': {$ne: true},
'removed': { $ne: true },
}, {
sort: { order: 1 },
}).fetch();
@@ -73,7 +74,7 @@ export function getPropertiesOfType(creatureId, propType) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = []
for (const prop of creature.properties.values()){
for (const prop of creature.properties.values()) {
if (prop.type === propType) {
props.push(prop);
}
@@ -97,7 +98,7 @@ export function getCreature(creatureId) {
if (loadedCreatures.has(creatureId)) {
const loadedCreature = loadedCreatures.get(creatureId);
const creature = loadedCreature.creature;
if (creature) {
if (creature) {
const cloneCreature = EJSON.clone(creature);
return cloneCreature;
}
@@ -118,7 +119,7 @@ export function getVariables(creatureId) {
}
}
// console.time(`Cache miss on variables: ${creatureId}`);
const variables = CreatureVariables.findOne({_creatureId: creatureId});
const variables = CreatureVariables.findOne({ _creatureId: creatureId });
// console.timeEnd(`Cache miss on variables: ${creatureId}`);
return variables;
}
@@ -148,7 +149,7 @@ export function getProperyAncestors(creatureId, propertyId) {
// Fetch from database
return CreatureProperties.find({
_id: { $in: ancestorIds },
removed: {$ne: true},
removed: { $ne: true },
}, {
sort: { order: 1 },
}).fetch();
@@ -164,7 +165,7 @@ export function getPropertyDecendants(creatureId, propertyId) {
if (loadedCreatures.has(creatureId)) {
const creature = loadedCreatures.get(creatureId);
const props = [];
for(const prop of creature.properties.values()){
for (const prop of creature.properties.values()) {
if (prop.ancestors[expectedAncestorPostition]?.id === propertyId) {
props.push(prop);
}
@@ -216,7 +217,7 @@ class LoadedCreature {
compute();
},
});
// Observe the creature itself
self.creatureObserver = Creatures.find({
_id: creatureId,
@@ -239,7 +240,7 @@ class LoadedCreature {
self.variablesObserver = CreatureVariables.find({
_creatureId: creatureId,
}, {
fields: { _creatureId: 0},
fields: { _creatureId: 0 },
}).observeChanges({
added(id, fields) {
fields._id = id;