refactor: convert to ECMAScript where necessary

Also drop @league-of-foundry-developers/foundry-vtt-types.
This commit is contained in:
Johannes Loher 2022-11-17 00:12:29 +01:00
parent df4538f6ed
commit 6277e27056
69 changed files with 1077 additions and 1679 deletions

View file

@ -0,0 +1,85 @@
// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { createCheckRoll } from "../../../dice/check-factory";
import { notifications } from "../../../ui/notifications";
import { getGame } from "../../../utils/utils";
import { DS4Item } from "../item";
import { calculateSpellPrice } from "./calculate-spell-price";
export class DS4Spell extends DS4Item {
/** @override */
prepareDerivedData() {
this.data.data.rollable = this.data.data.equipped;
this.data.data.price = calculateSpellPrice(this.data.data);
if (this.data.data.allowsDefense) {
this.data.data.opponentDefense = 0;
}
}
/** @override */
async roll(options = {}) {
const game = getGame();
if (!this.data.data.equipped) {
return notifications.warn(
game.i18n.format("DS4.WarningItemMustBeEquippedToBeRolled", {
name: this.name,
id: this.id,
type: this.data.type,
}),
);
}
if (!this.actor) {
throw new Error(game.i18n.format("DS4.ErrorCannotRollUnownedItem", { name: this.name, id: this.id }));
}
const ownerDataData = this.actor.data.data;
const hasComplexModifier = this.data.data.spellModifier.complex !== "";
if (hasComplexModifier === undefined) {
notifications.info(
game.i18n.format("DS4.InfoManuallyEnterSpellModifier", {
name: this.name,
spellModifier: this.data.data.spellModifier.complex,
}),
);
}
const spellType = this.data.data.spellType;
const opponentDefense = this.data.data.opponentDefense;
const checkTargetNumber =
ownerDataData.combatValues[spellType].total +
(hasComplexModifier ? 0 : this.data.data.spellModifier.numerical);
const speaker = ChatMessage.getSpeaker({ actor: this.actor, ...options.speaker });
const flavor =
opponentDefense !== undefined && opponentDefense !== 0
? "DS4.ItemSpellCheckFlavorWithOpponentDefense"
: "DS4.ItemSpellCheckFlavor";
/** @type {import("../../../dice/check-factory").DS4CheckFactoryOptions["flavorData"]} */
const flavorData = {
actor: speaker.alias ?? this.actor.name,
spell: this.name,
};
if (opponentDefense !== undefined && opponentDefense !== 0) {
flavorData.opponentDefense = (opponentDefense < 0 ? "" : "+") + opponentDefense;
}
await createCheckRoll(checkTargetNumber, {
rollMode: game.settings.get("core", "rollMode"),
maximumCoupResult: ownerDataData.rolling.maximumCoupResult,
minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult,
flavor: flavor,
flavorData: flavorData,
speaker,
});
/**
* A hook event that fires after an item is rolled.
* @function ds4.rollItem
* @memberof hookEvents
* @param {DS4Item} item Item being rolled.
*/
Hooks.callAll("ds4.rollItem", this);
}
}