2021-07-13 02:03:10 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
import { DS4Actor } from "./actor/actor";
|
2021-07-23 12:29:01 +02:00
|
|
|
import { getGame } from "./helpers";
|
2021-07-13 02:03:10 +02:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface DocumentClassConfig {
|
|
|
|
ActiveEffect: typeof DS4ActiveEffect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:29:01 +02:00
|
|
|
type PromisedType<T> = T extends Promise<infer U> ? U : T;
|
2021-07-13 02:03:10 +02:00
|
|
|
export class DS4ActiveEffect extends ActiveEffect {
|
2021-07-23 12:29:01 +02:00
|
|
|
/**
|
|
|
|
* A cached reference to the source document to avoid recurring database lookups
|
|
|
|
*/
|
|
|
|
protected source: PromisedType<ReturnType<typeof fromUuid>> | undefined = undefined;
|
|
|
|
|
2021-07-13 02:03:10 +02:00
|
|
|
/** @override */
|
|
|
|
apply(actor: DS4Actor, change: foundry.data.ActiveEffectData["changes"][number]): unknown {
|
|
|
|
change.value = Roll.replaceFormulaData(change.value, actor.data);
|
|
|
|
try {
|
|
|
|
change.value = Roll.safeEval(change.value).toString();
|
|
|
|
} catch (e) {
|
|
|
|
// this is a valid case, e.g., if the effect change simply is a string
|
|
|
|
}
|
|
|
|
return super.apply(actor, change);
|
|
|
|
}
|
2021-07-20 02:35:55 +02:00
|
|
|
|
|
|
|
/**
|
2021-07-23 12:29:01 +02:00
|
|
|
* Gets the current source name based on the cached source object.
|
|
|
|
*/
|
|
|
|
async getCurrentSourceName(): Promise<string> {
|
|
|
|
const game = getGame();
|
|
|
|
const origin = await this.getSource();
|
|
|
|
if (origin === null) return game.i18n.localize("None");
|
|
|
|
return origin.name ?? game.i18n.localize("Unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the source document for this effect. Uses the cached {@link DS4ActiveEffect#origin} if it has already been
|
|
|
|
* set.
|
2021-07-20 02:35:55 +02:00
|
|
|
*/
|
2021-07-23 12:29:01 +02:00
|
|
|
protected async getSource(): ReturnType<typeof fromUuid> {
|
|
|
|
if (this.source === undefined) {
|
|
|
|
this.source = this.data.origin !== undefined ? await fromUuid(this.data.origin) : null;
|
|
|
|
}
|
|
|
|
return this.source;
|
2021-07-20 02:35:55 +02:00
|
|
|
}
|
2021-07-13 02:03:10 +02:00
|
|
|
}
|