refactor: extract active effect application to DS4ActiveEffect

This commit is contained in:
Johannes Loher 2022-11-03 21:41:44 +01:00
parent b1ed05a796
commit ddfab1813e
3 changed files with 46 additions and 44 deletions

View file

@ -3,6 +3,7 @@
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect/active-effect";
import { DS4 } from "../config";
import { mathEvaluator } from "../expression-evaluation/evaluator";
import { getGame } from "../helpers";
@ -16,7 +17,7 @@ import type { DS4Item } from "../item/item";
import type { ItemType } from "../item/item-data-source";
import type { DS4ShieldDataProperties } from "../item/shield/shield-data-properties";
import type { Check } from "./actor-data-properties-base";
import type { EffectChangeData } from "../active-effect/active-effect";
declare global {
interface DocumentClassConfig {
Actor: typeof DS4Actor;
@ -105,7 +106,9 @@ export class DS4Actor extends Actor {
applyActiveEffectsToBaseData(): void {
// reset overrides because our variant of applying active effects does not set them, it only adds overrides
this.overrides = {};
this.applyActiveEffectsFiltered(
DS4ActiveEffect.applyEffetcs(
this,
this.actorEffects,
(change) =>
!this.derivedDataProperties.includes(change.key) &&
!this.finalDerivedDataProperties.includes(change.key),
@ -113,29 +116,9 @@ export class DS4Actor extends Actor {
}
applyActiveEffectsToDerivedData(): void {
this.applyActiveEffectsFiltered((change) => this.derivedDataProperties.includes(change.key));
}
/**
* Apply ActiveEffectChanges to the Actor data which are caused by ActiveEffects and satisfy the given predicate.
*
* @param predicate - The predicate that ActiveEffectChanges need to satisfy in order to be applied
*/
applyActiveEffectsFiltered(predicate: (change: EffectChangeData) => boolean): void {
const overrides: Record<string, unknown> = {};
// Organize non-disabled and -surpressed effects by their application priority
const changesWithEffect = this.actorEffects.flatMap((e) => e.getFactoredChangesWithEffect(predicate));
changesWithEffect.sort((a, b) => (a.change.priority ?? 0) - (b.change.priority ?? 0));
// Apply all changes
for (const changeWithEffect of changesWithEffect) {
const result = changeWithEffect.effect.apply(this, changeWithEffect.change);
if (result !== null) overrides[changeWithEffect.change.key] = result;
}
// Expand the set of final overrides
this.overrides = foundry.utils.expandObject({ ...foundry.utils.flattenObject(this.overrides), ...overrides });
DS4ActiveEffect.applyEffetcs(this, this.actorEffects, (change) =>
this.derivedDataProperties.includes(change.key),
);
}
/**