refactor: use subclasses for different item types

This commit is contained in:
Johannes Loher 2022-02-17 00:17:59 +01:00
parent be616e3be8
commit 8d2cff77d7
47 changed files with 766 additions and 456 deletions

63
src/item/spell/spell.ts Normal file
View file

@ -0,0 +1,63 @@
// SPDX-FileCopyrightText: 2022 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { getGame } from "../../helpers";
import { createCheckRoll } from "../../rolls/check-factory";
import notifications from "../../ui/notifications";
import { DS4Item } from "../item";
import { calculateSpellPrice } from "./calculate-spell-price";
export class DS4Spell extends DS4Item {
/** @override */
prepareDerivedData(): void {
this.data.data.rollable = this.data.data.equipped;
this.data.data.price = calculateSpellPrice(this.data.data);
}
/** @override */
async roll(options: { speaker?: { token?: TokenDocument; alias?: string } } = {}): Promise<void> {
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 spellModifier = Number.isNumeric(this.data.data.bonus) ? parseInt(this.data.data.bonus) : undefined;
if (spellModifier === undefined) {
notifications.info(
game.i18n.format("DS4.InfoManuallyEnterSpellModifier", {
name: this.name,
spellModifier: this.data.data.bonus,
}),
);
}
const spellType = this.data.data.spellType;
const checkTargetNumber = ownerDataData.combatValues[spellType].total + (spellModifier ?? 0);
const speaker = ChatMessage.getSpeaker({ actor: this.actor, ...options.speaker });
await createCheckRoll(checkTargetNumber, {
rollMode: game.settings.get("core", "rollMode"),
maximumCoupResult: ownerDataData.rolling.maximumCoupResult,
minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult,
flavor: "DS4.ItemSpellCheckFlavor",
flavorData: { actor: speaker.alias ?? this.actor.name, spell: this.name },
speaker,
});
}
}
export interface DS4Spell {
data: foundry.data.ItemData & { type: "spell"; _source: { type: "spell" } };
}