diff --git a/app/imports/api/creature/creatureProperties/CreatureProperties.ts b/app/imports/api/creature/creatureProperties/CreatureProperties.ts index 3342d643..2bde333a 100644 --- a/app/imports/api/creature/creatureProperties/CreatureProperties.ts +++ b/app/imports/api/creature/creatureProperties/CreatureProperties.ts @@ -147,14 +147,21 @@ export type CreatureProperty = ConvertToUnion; const CreatureProperties = new Mongo.Collection('creatureProperties'); +const genericCreaturePropertySchema = TypedSimpleSchema.from({}) + .extend(CreaturePropertySchema) + .extend(ColorSchema) + .extend(ChildSchema) + .extend(SoftRemovableSchema); + +// Attach the default schema +CreatureProperties.attachSchema(genericCreaturePropertySchema); + +// Attach the schemas for each type let key: keyof typeof propertySchemasIndex; for (key in propertySchemasIndex) { - const schema = new SimpleSchema({}); - schema.extend(propertySchemasIndex[key]); - schema.extend(CreaturePropertySchema); - schema.extend(ColorSchema); - schema.extend(ChildSchema); - schema.extend(SoftRemovableSchema); + const schema = TypedSimpleSchema.from({}) + .extend(propertySchemasIndex[key]) + .extend(genericCreaturePropertySchema) CreatureProperties.attachSchema(schema, { selector: { type: key } }); diff --git a/app/imports/api/engine/action/applyProperties/applyRollProperty.ts b/app/imports/api/engine/action/applyProperties/applyRollProperty.ts index a27f2149..e7bdaec7 100644 --- a/app/imports/api/engine/action/applyProperties/applyRollProperty.ts +++ b/app/imports/api/engine/action/applyProperties/applyRollProperty.ts @@ -44,7 +44,7 @@ export default async function applyRollProperty( } // If we didn't end up with a constant or a number of finite value, give up - if (reduced?.parseType !== 'constant' || !isFiniteNode(reduced)) { + if (!isFiniteNode(reduced)) { return applyDefaultAfterPropTasks(action, prop, task.targetIds, inputProvider); } const value = reduced.value; diff --git a/app/imports/api/engine/action/functions/recalculateCalculation.ts b/app/imports/api/engine/action/functions/recalculateCalculation.ts index 88240692..df8ba004 100644 --- a/app/imports/api/engine/action/functions/recalculateCalculation.ts +++ b/app/imports/api/engine/action/functions/recalculateCalculation.ts @@ -11,10 +11,11 @@ import { CalculatedField } from '/imports/api/properties/subSchemas/computedFiel import InputProvider from '/imports/api/engine/action/functions/userInput/InputProvider'; import { EngineAction } from '/imports/api/engine/action/EngineActions'; import ResolveLevel from '/imports/parser/types/ResolveLevel'; +import constant from '/imports/parser/parseTree/constant'; export default async function recalculateCalculation( calcObj: CalculatedField, - action, + action: EngineAction, parseLevel: ResolveLevel = 'reduce', userInput: InputProvider, ) { @@ -34,11 +35,11 @@ export default async function recalculateCalculation( // Apply all the effects and proficiencies aggregateCalculationEffects( calcObj, - id => getSingleProperty(action.creatureId, id) + (id: string) => getSingleProperty(action.creatureId, id) ); aggregateCalculationProficiencies( calcObj, - id => getSingleProperty(action.creatureId, id), + (id: string) => getSingleProperty(action.creatureId, id), scope['proficiencyBonus']?.value || 0 ); @@ -61,12 +62,13 @@ export async function rollAndReduceCalculation( if (!calcObj) throw new Error('calcObj is required'); const context = new Context(); const scope = await getEffectiveActionScope(action); + // Compile - recalculateCalculation(calcObj, action, 'compile', userInput); - const compiled = calcObj.valueNode; + await recalculateCalculation(calcObj, action, 'compile', userInput); + const compiled = calcObj.valueNode ?? constant.create({ value: 0 }); // Roll - const { result: rolled } = await resolve('roll', calcObj.valueNode, scope, context, userInput); + const { result: rolled } = await resolve('roll', compiled, scope, context, userInput); // Reduce const { result: reduced } = await resolve('reduce', rolled, scope, context, userInput); diff --git a/app/imports/parser/parseTree/constant.ts b/app/imports/parser/parseTree/constant.ts index 36cb78e6..5c6fd1db 100644 --- a/app/imports/parser/parseTree/constant.ts +++ b/app/imports/parser/parseTree/constant.ts @@ -41,12 +41,13 @@ const constant: ConstantFactory = { }, } -export function isFiniteNode(node: ParseNode): node is FiniteNumberConstantNode { +export function isFiniteNode(node: ParseNode | undefined): node is FiniteNumberConstantNode { return node && node.parseType === 'constant' && node.valueType === 'number' && typeof node.value === 'number' - && isFinite(node.value); + && isFinite(node.value) + || false; } export default constant;