From 0e56421c3a1be8d611942010abb1ef12b63951ec Mon Sep 17 00:00:00 2001 From: ThaumRystra Date: Sun, 1 Dec 2024 21:16:53 +0200 Subject: [PATCH] Tested and fixed critical hits not doubling damage --- .../applyDamageProperty.test.ts | 47 +++++++++++++++++++ .../applyProperties/applyDamageProperty.ts | 2 +- .../userInput/inputProviderForTests.testFn.ts | 15 +++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/app/imports/api/engine/action/applyProperties/applyDamageProperty.test.ts b/app/imports/api/engine/action/applyProperties/applyDamageProperty.test.ts index 146f56a0..97f46eef 100644 --- a/app/imports/api/engine/action/applyProperties/applyDamageProperty.test.ts +++ b/app/imports/api/engine/action/applyProperties/applyDamageProperty.test.ts @@ -6,6 +6,7 @@ import { removeAllCreaturesAndProps, runActionById } from '/imports/api/engine/action/functions/actionEngineTest.testFn'; +import { critInputProvider } from '../functions/userInput/inputProviderForTests.testFn'; const [ creatureId, targetCreatureId, targetCreature2Id, damageTargetId, damageSelfId, targetCreatureHitPointsId, targetCreature2HitPointsId, selfHitPointsId, damageWithEffectsId, effectId, effect2Id, @@ -241,4 +242,50 @@ describe('Apply Damage Properties', function () { ], }]); }); + + it('Doubles damage on a critical hit', async function () { + const [ + creatureId, damageId, actionId + ] = getRandomIds(3); + const testCreature = { + _id: creatureId, + props: [ + { + _id: actionId, + type: 'action', + attackRoll: { calculation: '10' }, + children: [ + { + _id: damageId, + type: 'damage', + target: 'target', + amount: { calculation: '2d6 + 7' } + }, + ] + }, + ], + }; + await createTestCreature(testCreature); + + const action = await runActionById(actionId, [], critInputProvider); + assert.exists(action); + assert.deepEqual(allMutations(action), [{ + 'contents': [{ 'name': 'Action' }], + 'targetIds': [] + }, { + 'contents': [{ + 'inline': true, + 'name': 'Critical Hit!', + 'value': '1d20 [20] + 10\n**30**' + }], + 'targetIds': [], + }, { + 'contents': [{ + 'inline': true, + 'name': 'Damage', + 'value': '2d6 [3, 4, 5, 6] + 7\n**25** critical slashing damage', + }], + 'targetIds': [], + }]); + }); }); diff --git a/app/imports/api/engine/action/applyProperties/applyDamageProperty.ts b/app/imports/api/engine/action/applyProperties/applyDamageProperty.ts index d5190e44..d1e3aab2 100644 --- a/app/imports/api/engine/action/applyProperties/applyDamageProperty.ts +++ b/app/imports/api/engine/action/applyProperties/applyDamageProperty.ts @@ -21,7 +21,7 @@ export default async function applyDamageProperty( task: PropTask, action: EngineAction, result: TaskResult, inputProvider: InputProvider ) { const prop = task.prop; - const scope = getEffectiveActionScope(action); + const scope = await getEffectiveActionScope(action); // Choose target const damageTargets = prop.target === 'self' ? [action.creatureId] : task.targetIds; diff --git a/app/imports/api/engine/action/functions/userInput/inputProviderForTests.testFn.ts b/app/imports/api/engine/action/functions/userInput/inputProviderForTests.testFn.ts index dec48a5d..e91b159f 100644 --- a/app/imports/api/engine/action/functions/userInput/inputProviderForTests.testFn.ts +++ b/app/imports/api/engine/action/functions/userInput/inputProviderForTests.testFn.ts @@ -1,4 +1,4 @@ -import InputProvider from '/imports/api/engine/action/functions/userInput/InputProvider'; +import InputProvider, { CastSpellParams } from '/imports/api/engine/action/functions/userInput/InputProvider'; const inputProviderForTests: InputProvider = { async targetIds(target, currentTargetIds = []) { @@ -43,8 +43,19 @@ const inputProviderForTests: InputProvider = { return suggestedParams; }, async castSpell(suggestedParams) { - return suggestedParams; + return suggestedParams as CastSpellParams; }, } +export const critInputProvider: InputProvider = { + ...inputProviderForTests, + async rollDice(dice = []) { + // when rolling 1d20, crit, otherwise use the normal test roll provider + if (dice.length === 1 && dice[0].diceSize === 20 && dice[0].number === 1) { + return [[20]]; + } + return inputProviderForTests.rollDice(dice); + }, +}; + export default inputProviderForTests;