From 195704ed7aa0c4fd8c25c67a88267d98166207ce Mon Sep 17 00:00:00 2001 From: Thaum Rystra <9525416+ThaumRystra@users.noreply.github.com> Date: Tue, 14 May 2024 10:02:54 +0200 Subject: [PATCH] Tested and fixed triggers in new action engine --- .../applyActionProperty.test.ts | 4 +- .../applyTriggerProperty.test.ts | 119 ++++++++++++++++++ .../applyProperties/applyTriggerProperty.ts | 16 +++ .../engine/action/applyProperties/index.ts | 13 +- .../functions/actionEngineTest.testFn.ts | 4 +- app/imports/api/engine/action/tasks/Task.ts | 2 +- .../writeAlteredProperties.js | 1 + app/imports/api/properties/Triggers.js | 2 +- 8 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 app/imports/api/engine/action/applyProperties/applyTriggerProperty.test.ts create mode 100644 app/imports/api/engine/action/applyProperties/applyTriggerProperty.ts diff --git a/app/imports/api/engine/action/applyProperties/applyActionProperty.test.ts b/app/imports/api/engine/action/applyProperties/applyActionProperty.test.ts index 913feef3..7f03a562 100644 --- a/app/imports/api/engine/action/applyProperties/applyActionProperty.test.ts +++ b/app/imports/api/engine/action/applyProperties/applyActionProperty.test.ts @@ -4,7 +4,7 @@ import { allMutations, allUpdates, createTestCreature, - randomIds, + getRandomIds, removeAllCreaturesAndProps, runActionById } from '/imports/api/engine/action/functions/actionEngineTest.testFn'; @@ -17,7 +17,7 @@ const [ usesActionId, attackMissId, attackNoTargetId, usesResourcesActionId, ammoId, resourceAttId, consumeAmmoId, consumeResourceId, noUsesActionId, insufficientResourcesActionId, attributeResetByEventId, eventActionId, advantageAttackId, advantageEffectId, disadvantageAttackId, disadvantageEffectId, -] = randomIds; +] = getRandomIds(100); const actionTestCreature = { _id: creatureId, diff --git a/app/imports/api/engine/action/applyProperties/applyTriggerProperty.test.ts b/app/imports/api/engine/action/applyProperties/applyTriggerProperty.test.ts new file mode 100644 index 00000000..b92f1c1d --- /dev/null +++ b/app/imports/api/engine/action/applyProperties/applyTriggerProperty.test.ts @@ -0,0 +1,119 @@ +import { assert } from 'chai'; +import { + allLogContent, + createTestCreature, + getRandomIds, + removeAllCreaturesAndProps, + runActionById +} from '/imports/api/engine/action/functions/actionEngineTest.testFn'; +import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties'; + +const [ + creatureId, targetCreatureId, targetCreature2Id, actionWithTriggerId, triggerBeforeActionId, + triggerAfterActionId, triggerAfterActionChildrenId +] = getRandomIds(100); + +const actionTestCreature = { + _id: creatureId, + props: [ + // Action with triggers + { + _id: actionWithTriggerId, + type: 'action', + tags: ['trigger tag'], + children: [ + { + type: 'note', + name: 'Action Child' + } + ], + }, + { + _id: triggerBeforeActionId, + type: 'trigger', + targetTags: ['trigger tag'], + name: 'Before Action Trigger', + event: 'doActionProperty', + actionPropertyType: 'action', + timing: 'before', + }, + { + _id: triggerAfterActionId, + type: 'trigger', + targetTags: ['trigger tag'], + name: 'After Action Trigger', + event: 'doActionProperty', + actionPropertyType: 'action', + timing: 'after', + }, + { + _id: triggerAfterActionChildrenId, + type: 'trigger', + targetTags: ['trigger tag'], + name: 'After Action Children Trigger', + event: 'doActionProperty', + actionPropertyType: 'action', + timing: 'afterChildren', + }, + ], +} + +const actionTargetCreature = { + _id: targetCreatureId, + props: [ + { + type: 'attribute', + attributeType: 'stat', + variableName: 'armor', + baseValue: { calculation: '10' }, + } + ] +} + +const actionTargetCreature2 = { + _id: targetCreature2Id, + props: [ + { + type: 'attribute', + attributeType: 'stat', + variableName: 'armor', + baseValue: { calculation: '10' }, + } + ] +} + +describe('Triggers', function () { + // Increase timeout + this.timeout(8000); + + before(async function () { + await removeAllCreaturesAndProps(); + await createTestCreature(actionTestCreature); + await createTestCreature(actionTargetCreature); + await createTestCreature(actionTargetCreature2); + }); + + it('should run triggers on actions', async function () { + const actionProp = CreatureProperties.findOne(actionWithTriggerId); + assert.deepEqual(actionProp.triggerIds, { + before: [triggerBeforeActionId], + after: [triggerAfterActionId], + afterChildren: [triggerAfterActionChildrenId], + }, 'Prop\'s triggerIds should be set'); + const action = await runActionById(actionWithTriggerId); + assert.exists(action); + assert.deepEqual(allLogContent(action), [ + { + name: 'Before Action Trigger', + }, { + name: 'Action', + }, { + name: 'After Action Trigger', + }, { + name: 'Action Child', + }, { + name: 'After Action Children Trigger', + }, + ]); + }); +}); diff --git a/app/imports/api/engine/action/applyProperties/applyTriggerProperty.ts b/app/imports/api/engine/action/applyProperties/applyTriggerProperty.ts new file mode 100644 index 00000000..8cc271d5 --- /dev/null +++ b/app/imports/api/engine/action/applyProperties/applyTriggerProperty.ts @@ -0,0 +1,16 @@ +import { EngineAction } from '/imports/api/engine/action/EngineActions'; +import { applyDefaultAfterPropTasks } from '/imports/api/engine/action/functions/applyTaskGroups'; +import { PropTask } from '/imports/api/engine/action/tasks/Task'; +import getPropertyTitle from '/imports/api/utility/getPropertyTitle'; + + +export default async function applyTriggerProperty( + task: PropTask, action: EngineAction, result, userInput +): Promise { + const prop = task.prop; + result.appendLog({ + name: getPropertyTitle(prop), + ...prop.silent && { silenced: true }, + }) + return applyDefaultAfterPropTasks(action, prop, task.targetIds, userInput); +} diff --git a/app/imports/api/engine/action/applyProperties/index.ts b/app/imports/api/engine/action/applyProperties/index.ts index 9c8599e8..b835db2b 100644 --- a/app/imports/api/engine/action/applyProperties/index.ts +++ b/app/imports/api/engine/action/applyProperties/index.ts @@ -1,3 +1,8 @@ +import { EngineAction } from '/imports/api/engine/action/EngineActions'; +import InputProvider from '/imports/api/engine/action/functions/userInput/InputProvider'; +import { PropTask } from '/imports/api/engine/action/tasks/Task'; +import TaskResult from '/imports/api/engine/action/tasks/TaskResult'; + import action from './applyActionProperty'; import adjustment from './applyAdjustmentProperty'; import branch from './applyBranchProperty'; @@ -9,8 +14,11 @@ import note from './applyNoteProperty'; import roll from './applyRollProperty'; import savingThrow from './applySavingThrowProperty'; import toggle from './applyToggleProperty'; +import trigger from './applyTriggerProperty'; -export default { +const applyPropertyByType: { + [key: string]: (task: PropTask, action: EngineAction, result: TaskResult, input: InputProvider) => Promise +} = { action, adjustment, branch, @@ -23,4 +31,7 @@ export default { savingThrow, propertySlot: folder, toggle, + trigger, } + +export default applyPropertyByType; \ No newline at end of file diff --git a/app/imports/api/engine/action/functions/actionEngineTest.testFn.ts b/app/imports/api/engine/action/functions/actionEngineTest.testFn.ts index fa474d68..722c0892 100644 --- a/app/imports/api/engine/action/functions/actionEngineTest.testFn.ts +++ b/app/imports/api/engine/action/functions/actionEngineTest.testFn.ts @@ -54,9 +54,9 @@ type TestCreature = { } /** - * A list of 100 random Ids + * get a list of random Ids */ -export const randomIds = new Array(100).fill(undefined).map(() => Random.id()); +export const getRandomIds = (count) => new Array(count).fill(undefined).map(() => Random.id()); /** * Creates a new Engine Action and applies the specified creature property diff --git a/app/imports/api/engine/action/tasks/Task.ts b/app/imports/api/engine/action/tasks/Task.ts index 5239c9c0..676d972d 100644 --- a/app/imports/api/engine/action/tasks/Task.ts +++ b/app/imports/api/engine/action/tasks/Task.ts @@ -5,7 +5,7 @@ type Task = PropTask | DamagePropTask | ItemAsAmmoTask | CheckTask | ResetTask; export default Task; type BaseTask = { - prop: { [key: string]: any }; + prop: { type: string, [key: string]: any }; targetIds: string[]; } diff --git a/app/imports/api/engine/computation/writeComputation/writeAlteredProperties.js b/app/imports/api/engine/computation/writeComputation/writeAlteredProperties.js index 6dbb766a..418f4bc6 100644 --- a/app/imports/api/engine/computation/writeComputation/writeAlteredProperties.js +++ b/app/imports/api/engine/computation/writeComputation/writeAlteredProperties.js @@ -25,6 +25,7 @@ export default function writeAlteredProperties(computation) { 'left', 'right', 'parentId', + 'triggerIds', ...schema.objectKeys(), ]; op = addChangedKeysToOp(op, keys, original, changed); diff --git a/app/imports/api/properties/Triggers.js b/app/imports/api/properties/Triggers.js index 1dea7daa..f2bfa797 100644 --- a/app/imports/api/properties/Triggers.js +++ b/app/imports/api/properties/Triggers.js @@ -134,7 +134,7 @@ const ComputedOnlyTriggerSchema = createPropertySchema({ }, }); -const ComputedTriggerSchema = new SimpleSchema() +const ComputedTriggerSchema = new SimpleSchema({}) .extend(TriggerSchema) .extend(ComputedOnlyTriggerSchema);