From b1ab65f095306641abb64c182453946dbb189074 Mon Sep 17 00:00:00 2001 From: ThaumRystra Date: Sat, 28 Sep 2024 12:45:30 +0200 Subject: [PATCH] Tested and fixed buff remover properties --- .../applyProperties/applyBuffProperty.test.ts | 1 - .../applyBuffRemoverProperty.test.ts | 97 +++++++++++++++++++ .../applyBuffRemoverProperty.ts | 22 ++++- 3 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.test.ts diff --git a/app/imports/api/engine/action/applyProperties/applyBuffProperty.test.ts b/app/imports/api/engine/action/applyProperties/applyBuffProperty.test.ts index 989d2a47..f1109e2e 100644 --- a/app/imports/api/engine/action/applyProperties/applyBuffProperty.test.ts +++ b/app/imports/api/engine/action/applyProperties/applyBuffProperty.test.ts @@ -14,7 +14,6 @@ const [ const actionTestCreature = { _id: creatureId, props: [ - // If branch { _id: buffId, type: 'buff', diff --git a/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.test.ts b/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.test.ts new file mode 100644 index 00000000..b3a2bccb --- /dev/null +++ b/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.test.ts @@ -0,0 +1,97 @@ +import { assert } from 'chai'; +import { + allMutations, + createTestCreature, + getRandomIds, + removeAllCreaturesAndProps, + runActionById +} from '/imports/api/engine/action/functions/actionEngineTest.testFn'; + +const [ + creatureId, otherCreatureId, buffId, removeParentBuffId, removeTargetBuffsId, +] = getRandomIds(100); + +const actionTestCreature = { + _id: creatureId, + props: [ + { + _id: buffId, + type: 'buff', + description: { text: 'This buff reduces AC of target by difference between the strength of caster {strength} and the target {~target.strength}' }, + tags: ['some buff'], + children: [ + { + type: 'effect', + stats: ['armor'], + operation: 'add', + amount: { calculation: '~target.strength - strength' }, + }, + { + _id: removeParentBuffId, + type: 'buffRemover', + targetParentBuff: true, + target: 'self', + }, + ], + }, + { + type: 'attribute', + attributeType: 'stat', + variableName: 'strength', + baseValue: { calculation: '18' }, + }, + ], +}; + +const actionOtherCreature = { + _id: otherCreatureId, + props: [ + { + _id: removeTargetBuffsId, + type: 'buffRemover', + target: 'target', + targetTags: ['some buff'] + }, + ], +}; + +describe('Apply Buff Properties', function () { + // Increase timeout + this.timeout(8000); + + beforeEach(async function () { + await removeAllCreaturesAndProps(); + await createTestCreature(actionTestCreature); + await createTestCreature(actionOtherCreature); + }); + + it('removes a parent buff', async function () { + const action = await runActionById(removeParentBuffId); + const mutations = allMutations(action); + assert.deepEqual(mutations, [{ + contents: [{ + name: 'Removed', + value: 'Buff', + }], + removals: [{ + propId: buffId, + }], + targetIds: [] + }]); + }); + + it('removes a tag targeted buff', async function () { + const action = await runActionById(removeTargetBuffsId, [creatureId]); + const mutations = allMutations(action); + assert.deepEqual(mutations, [{ + contents: [{ + name: 'Removed', + value: 'Buff', + }], + removals: [{ + propId: buffId, + }], + targetIds: [creatureId] + }]); + }); +}); diff --git a/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.ts b/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.ts index 68e7d573..c18e13e3 100644 --- a/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.ts +++ b/app/imports/api/engine/action/applyProperties/applyBuffRemoverProperty.ts @@ -4,7 +4,7 @@ import getPropertyTitle from '/imports/api/utility/getPropertyTitle'; import { findLast, filter, difference, intersection } from 'lodash'; import { getPropertiesOfType, getPropertyAncestors } from '/imports/api/engine/loadCreatures'; import getEffectivePropTags from '/imports/api/engine/computation/utility/getEffectivePropTags'; -import { applyDefaultAfterPropTasks } from '/imports/api/engine/action/functions/applyTaskGroups'; +import { applyDefaultAfterPropTasks, applyTaskToEachTarget } from '/imports/api/engine/action/functions/applyTaskGroups'; import { EngineAction } from '/imports/api/engine/action/EngineActions'; import InputProvider from '/imports/api/engine/action/functions/userInput/InputProvider'; @@ -13,13 +13,25 @@ export default async function applyBuffRemoverProperty( ) { const prop = task.prop; - if (prop.name && !prop.silent) { + const targetIds = prop.target === 'self' ? [action.creatureId] : task.targetIds; + + if (prop.name) { // Log Name result.appendLog({ name: getPropertyTitle(prop), + ...prop.silent && { silenced: true }, }, task.targetIds) } + if (targetIds.length > 1) { + return applyTaskToEachTarget(action, task, targetIds, userInput); + } + + if (targetIds.length !== 1) { + throw 'At this step, only a single target is supported' + } + const targetId = targetIds[0]; + // Remove buffs if (prop.targetParentBuff) { // Remove nearest ancestor buff @@ -29,13 +41,13 @@ export default async function applyBuffRemoverProperty( result.appendLog({ name: 'Error', value: 'Buff remover does not have a parent buff to remove', - }, task.targetIds); + }, [targetId]); return; } removeBuff(nearestBuff, prop, result); } else { // Get all the buffs targeted by tags - const allBuffs = getPropertiesOfType(action.creatureId, 'buff'); + const allBuffs = getPropertiesOfType(targetId, 'buff'); const targetedBuffs = filter(allBuffs, buff => { if (buff.inactive) return false; if (buffRemoverMatchTags(prop, buff)) return true; @@ -66,7 +78,7 @@ function removeBuff(buff: any, prop, result: TaskResult) { contents: [{ name: 'Removed', value: `${buff.name || 'Buff'}`, - silenced: prop.silent, + ...prop.silent && { silenced: true }, }], }); }