Tested and fixed triggers in new action engine

This commit is contained in:
Thaum Rystra
2024-05-14 10:02:54 +02:00
parent 4a52c3af19
commit 195704ed7a
8 changed files with 154 additions and 7 deletions

View File

@@ -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,

View File

@@ -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',
},
]);
});
});

View File

@@ -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<void> {
const prop = task.prop;
result.appendLog({
name: getPropertyTitle(prop),
...prop.silent && { silenced: true },
})
return applyDefaultAfterPropTasks(action, prop, task.targetIds, userInput);
}

View File

@@ -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<void>
} = {
action,
adjustment,
branch,
@@ -23,4 +31,7 @@ export default {
savingThrow,
propertySlot: folder,
toggle,
trigger,
}
export default applyPropertyByType;

View File

@@ -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

View File

@@ -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[];
}

View File

@@ -25,6 +25,7 @@ export default function writeAlteredProperties(computation) {
'left',
'right',
'parentId',
'triggerIds',
...schema.objectKeys(),
];
op = addChangedKeysToOp(op, keys, original, changed);

View File

@@ -134,7 +134,7 @@ const ComputedOnlyTriggerSchema = createPropertySchema({
},
});
const ComputedTriggerSchema = new SimpleSchema()
const ComputedTriggerSchema = new SimpleSchema({})
.extend(TriggerSchema)
.extend(ComputedOnlyTriggerSchema);