Skip to content

Commit

Permalink
Tested that triggers fire for all properties.
Browse files Browse the repository at this point in the history
Fixed some action engine issues found.
  • Loading branch information
ThaumRystra committed Nov 6, 2024
1 parent 057772c commit 2c43129
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,51 @@ export default async function applyAdjustmentProperty(

// Get the operation and value and push the damage hooks to the queue
if (!prop.amount) {
result.appendLog({
name: 'Error',
value: 'Attribute damage does not have an amount set',
silenced: prop.silent,
}, damageTargetIds);
return;
}

// Evaluate the amount
await recalculateCalculation(prop.amount, action, 'reduce', userInput);
const value = +prop.amount.value;
if (!isFinite(value)) {
return;
}

if (!damageTargetIds?.length) {
return;
}

if (damageTargetIds.length !== 1) {
throw 'At this step, only a single target is supported'
}
const targetId = damageTargetIds[0];
const statId = getVariables(targetId)?.[prop.stat]?._propId;
const stat = statId && getSingleProperty(targetId, statId);
if (!stat?.type) {
result.appendLog({
name: 'Error',
value: `Could not apply attribute damage, creature does not have \`${prop.stat}\` set`,
value: 'Attribute damage does not have a finite amount set',
silenced: prop.silent,
}, damageTargetIds);
return;
}

if (damageTargetIds.length && damageTargetIds.length !== 1) {
throw new Meteor.Error('1 target Expected', 'At this step, only a single target is supported');
}
const targetId = damageTargetIds[0];
let stat;
if (targetId) {
const statId = getVariables(targetId)?.[prop.stat]?._propId;
stat = statId && getSingleProperty(targetId, statId);
if (!stat?.type) {
result.appendLog({
name: 'Error',
value: `Could not apply attribute damage, creature does not have \`${prop.stat}\` set`,
silenced: prop.silent,
}, damageTargetIds);
return;
}
}
await applyTask(action, {
targetIds: damageTargetIds,
subtaskFn: 'damageProp',
params: {
title: getPropertyTitle(prop),
operation: prop.operation,
value,
targetProp: stat,
targetProp: stat ?? { _id: 'dummyStat', name: prop.stat, type: 'attribute' },
},
}, userInput);
return applyDefaultAfterPropTasks(action, prop, damageTargetIds, userInput);
Expand Down
34 changes: 23 additions & 11 deletions app/imports/api/engine/action/applyProperties/applyBuffProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export default async function applyBuffProperty(
const prop = EJSON.clone(task.prop);
const targetIds = prop.target === 'self' ? [action.creatureId] : task.targetIds;

// Log the buff and return if there are no targets
if (!targetIds.length) {
logBuff(prop, targetIds, action, userInput, result);
await applyAfterTasksSkipChildren(action, prop, targetIds, userInput);
return;
}

// Get the buff and its descendants
const propList = [
EJSON.clone(prop),
Expand Down Expand Up @@ -55,16 +62,7 @@ export default async function applyBuffProperty(
});

//Log the buff
let logValue = prop.description?.value
if (prop.description?.text) {
recalculateInlineCalculations(prop.description, action, 'reduce', userInput);
logValue = prop.description?.value;
}
result.appendLog({
name: getPropertyTitle(prop),
value: logValue,
silenced: prop.silent,
}, [target]);
logBuff(prop, targetIds, action, userInput, result);

// remove all the computed fields
targetPropList = cleanProps(targetPropList);
Expand All @@ -75,7 +73,21 @@ export default async function applyBuffProperty(
// Add the mutation to the results
result.mutations.push(mutation);
});
applyAfterTasksSkipChildren(action, prop, targetIds, userInput);
await applyAfterTasksSkipChildren(action, prop, targetIds, userInput);
}

async function logBuff(prop, targetIds, action, userInput, result) {
//Log the buff
let logValue = prop.description?.value
if (prop.description?.text) {
recalculateInlineCalculations(prop.description, action, 'reduce', userInput);
logValue = prop.description?.value;
}
result.appendLog({
name: getPropertyTitle(prop),
...logValue && { value: logValue },
silenced: prop.silent,
}, targetIds);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default async function applyBuffRemoverProperty(
return applyTaskToEachTarget(action, task, targetIds, userInput);
}

if (!targetIds.length) {
return applyDefaultAfterPropTasks(action, prop, task.targetIds, userInput);
}

if (targetIds.length !== 1) {
throw 'At this step, only a single target is supported'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ export default async function applyDamageProperty(
const prop = task.prop;
const scope = getEffectiveActionScope(action);

// Skip if there is no parse node to work with
if (!prop.amount?.parseNode) return;

// Choose target
const damageTargets = prop.target === 'self' ? [action.creatureId] : task.targetIds;

// Skip if there is no parse node to work with
if (!prop.amount?.valueNode) {
return applyDefaultAfterPropTasks(action, prop, damageTargets, inputProvider);
}

// Determine if the hit is critical
const criticalHit = await getConstantValueFromScope('~criticalHit', scope)
&& prop.damageType !== 'healing'; // Can't critically heal
Expand Down Expand Up @@ -91,9 +94,9 @@ export default async function applyDamageProperty(
}

// Memoise the damage suffix for the log
const suffix = (criticalHit ? ' critical ' : ' ') +
const suffix = (criticalHit ? 'critical ' : '') +
prop.damageType +
(prop.damageType !== 'healing' ? ' damage ' : '');
(prop.damageType !== 'healing' ? ' damage' : '');

// If there is a save, calculate the save damage
let damageOnSave, saveProp, saveRoll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default async function applySavingThrowProperty(
// If there are no save targets, apply all children as if the save both
// succeeded and failed
if (!targetId) {
console.warn('no target, returning early');
result.pushScope = {
['~saveFailed']: { value: true },
['~saveSucceeded']: { value: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,91 @@ import {
import CreatureProperties from '/imports/api/creature/creatureProperties/CreatureProperties';

const [
creatureId, targetCreatureId, targetCreature2Id, actionWithTriggerId, triggerBeforeActionId,
triggerAfterActionId, triggerAfterActionChildrenId
creatureId, targetCreatureId, targetCreature2Id,
] = getRandomIds(100);

const idMap: Record<string, string> = {};

const propsWithTriggers = ([
{ type: 'action' },
{
type: 'adjustment',
target: 'target',
stat: 'strength',
operation: 'increment',
amount: { calculation: '2' }
},
{
type: 'branch',
branchType: 'if',
condition: { calculation: 'true' },
},
{ type: 'buff' },
{ type: 'buffRemover' },
{
type: 'damage',
amount: { calculation: '1d13 + 3' },
},
{ type: 'note' },
{
type: 'roll',
roll: { calculation: '1d20' },
},
{
type: 'savingThrow',
stat: 'strengthSave',
dc: { calculation: '10' },
},
{ type: 'spell' },
] as any[]).map(prop => {
idMap[prop.type] = Random.id();
idMap[prop.type + 'Before'] = Random.id();
idMap[prop.type + 'After'] = Random.id();
idMap[prop.type + 'AfterChildren'] = Random.id();
prop._id = idMap[prop.type];
prop.name = prop.type;
return prop;
});

const actionTestCreature = {
_id: creatureId,
props: [
// Action with triggers
props: propsWithTriggers.map(prop => [
// Props with triggers
{
_id: actionWithTriggerId,
type: 'action',
tags: ['trigger tag'],
children: [
{
type: 'note',
name: 'Action Child'
name: 'Note Child'
}
],
},
{
_id: triggerBeforeActionId,
...prop,
}, {
_id: idMap[prop.type + 'Before'],
type: 'trigger',
targetTags: ['trigger tag'],
name: 'Before Action Trigger',
name: `Before ${prop.type} Trigger`,
event: 'doActionProperty',
actionPropertyType: 'action',
actionPropertyType: prop.type,
timing: 'before',
},
{
_id: triggerAfterActionId,
}, {
_id: idMap[prop.type + 'After'],
type: 'trigger',
targetTags: ['trigger tag'],
name: 'After Action Trigger',
name: `After ${prop.type} Trigger`,
event: 'doActionProperty',
actionPropertyType: 'action',
actionPropertyType: prop.type,
timing: 'after',
},
{
_id: triggerAfterActionChildrenId,
}, {
_id: idMap[prop.type + 'AfterChildren'],
type: 'trigger',
targetTags: ['trigger tag'],
name: 'After Action Children Trigger',
name: `After ${prop.type} Children Trigger`,
event: 'doActionProperty',
actionPropertyType: 'action',
actionPropertyType: prop.type,
timing: 'afterChildren',
},
],
]).flat(),
}

const actionTargetCreature = {
Expand Down Expand Up @@ -93,27 +131,73 @@ describe('Triggers', function () {
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), [
it('should run triggers on all props', async function () {
const expectedLogs: any[] = [
{
name: 'Before Action Trigger',
type: 'action',
content: [{ name: 'action' }],
}, {
name: 'Action',
type: 'adjustment',
content: [{ inline: true, name: 'Attribute damage', value: 'strength 2' }],
}, {
name: 'After Action Trigger',
type: 'branch',
content: [],
}, {
name: 'Action Child',
type: 'buff',
content: [{ name: 'buff' }],
noChildren: true,
}, {
name: 'After Action Children Trigger',
type: 'buffRemover',
content: [{ name: 'buffRemover' }],
}, {
type: 'damage',
content: [{
inline: true, name: 'Damage', value: '1d13 [7] + 3\n**10** slashing damage'
}],
}, {
type: 'note',
content: [{ name: 'note' }],
}, {
type: 'roll',
content: [{
name: 'roll', inline: true, value: '1d20 [10]\n**10**'
}],
}, {
type: 'savingThrow',
content: [{ name: 'savingThrow', inline: true, value: 'DC **10**' }],
}, {
type: 'spell',
content: [{ name: 'spell' }],
},
]);
];
for (const log of expectedLogs) try {
const type = log.type;
const actionProp = CreatureProperties.findOne(idMap[type]);
assert.deepEqual(actionProp.triggerIds, {
before: [idMap[type + 'Before']],
after: [idMap[type + 'After']],
afterChildren: [idMap[type + 'AfterChildren']],
}, 'Prop\'s triggerIds should be set');
const action = await runActionById(idMap[type]);
assert.exists(action);
assert.deepEqual(allLogContent(action), [
{
name: `Before ${type} Trigger`,
},
...log.content,
{
name: `After ${type} Trigger`,
},
...log.noChildren ? [] : [{
name: 'Note Child',
}],
{
name: `After ${type} Children Trigger`,
},
]);
} catch (e) {
console.error(`failed when running ${log.type}`);
throw e
}
});
});
3 changes: 2 additions & 1 deletion app/imports/api/engine/action/applyProperties/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ const applyPropertyByType: {
note,
roll,
savingThrow,
spell: action,
propertySlot: folder,
toggle,
trigger,
}

export default applyPropertyByType;
export default applyPropertyByType;
Loading

0 comments on commit 2c43129

Please sign in to comment.