Make DS4Item compile and commen getData methods for now

This commit is contained in:
Johannes Loher 2021-06-30 12:54:39 +02:00
parent 446340c064
commit 918fa5081b
4 changed files with 62 additions and 72 deletions

View file

@ -9,7 +9,6 @@ import { ModifiableDataBaseTotal } from "../../common/common-data";
import { DS4 } from "../../config"; import { DS4 } from "../../config";
import { getCanvas } from "../../helpers"; import { getCanvas } from "../../helpers";
import { DS4Item } from "../../item/item"; import { DS4Item } from "../../item/item";
import { DS4ItemData } from "../../item/item-data-source";
import { getDS4Settings } from "../../settings"; import { getDS4Settings } from "../../settings";
import notifications from "../../ui/notifications"; import notifications from "../../ui/notifications";
import { DS4Actor } from "../actor"; import { DS4Actor } from "../actor";
@ -51,39 +50,39 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
return `${basePath}/${this.actor.data.type}-sheet.hbs`; return `${basePath}/${this.actor.data.type}-sheet.hbs`;
} }
/** // /**
* This method returns the data for the template of the actor sheet. // * This method returns the data for the template of the actor sheet.
* It explicitly adds the items of the object sorted by type in the // * It explicitly adds the items of the object sorted by type in the
* object itemsByType. // * object itemsByType.
* @returns The data fed to the template of the actor sheet // * @returns The data fed to the template of the actor sheet
*/ // */
async getData(): Promise<ActorSheet.Data<DS4Actor>> { // async getData(): Promise<ActorSheet.Data<DS4Actor>> {
const itemsByType = Object.fromEntries( // const itemsByType = Object.fromEntries(
Object.entries(this.actor.itemTypes).map(([itemType, items]) => { // Object.entries(this.actor.itemTypes).map(([itemType, items]) => {
return [itemType, items.map((item) => item.data).sort((a, b) => (a.sort || 0) - (b.sort || 0))]; // return [itemType, items.map((item) => item.data).sort((a, b) => (a.sort || 0) - (b.sort || 0))];
}), // }),
); // );
const data = { // const data = {
...this._addTooltipsToData(await super.getData()), // ...this._addTooltipsToData(await super.getData()),
// Add the localization config to the data: // // Add the localization config to the data:
config: DS4, // config: DS4,
// Add the items explicitly sorted by type to the data: // // Add the items explicitly sorted by type to the data:
itemsByType, // itemsByType,
settings: getDS4Settings(), // settings: getDS4Settings(),
}; // };
return data; // return data;
} // }
protected _addTooltipsToData(data: ActorSheet.Data<DS4Actor>): ActorSheet.Data<DS4Actor> { // protected _addTooltipsToData(data: ActorSheet.Data<DS4Actor>): ActorSheet.Data<DS4Actor> {
const valueGroups = [data.data.attributes, data.data.traits, data.data.combatValues]; // const valueGroups = [data.data.attributes, data.data.traits, data.data.combatValues];
valueGroups.forEach((valueGroup) => { // valueGroups.forEach((valueGroup) => {
Object.values(valueGroup).forEach((attribute: ModifiableDataBaseTotal<number> & { tooltip?: string }) => { // Object.values(valueGroup).forEach((attribute: ModifiableDataBaseTotal<number> & { tooltip?: string }) => {
attribute.tooltip = this._getTooltipForValue(attribute); // attribute.tooltip = this._getTooltipForValue(attribute);
}); // });
}); // });
return data; // return data;
} // }
protected _getTooltipForValue(value: ModifiableDataBaseTotal<number>): string { protected _getTooltipForValue(value: ModifiableDataBaseTotal<number>): string {
return `${value.base} (${game.i18n.localize("DS4.TooltipBaseValue")}) + ${value.mod} (${game.i18n.localize( return `${value.base} (${game.i18n.localize("DS4.TooltipBaseValue")}) + ${value.mod} (${game.i18n.localize(

View file

@ -32,19 +32,17 @@ export class DS4ItemSheet extends ItemSheet {
const basePath = "systems/ds4/templates/sheets/item"; const basePath = "systems/ds4/templates/sheets/item";
return `${basePath}/${this.item.data.type}-sheet.hbs`; return `${basePath}/${this.item.data.type}-sheet.hbs`;
} }
// /** @override */
/** @override */ // async getData(): Promise<ItemSheet.Data<DS4Item>> {
async getData(): Promise<ItemSheet.Data<DS4Item>> { // const data = {
const data = { // ...(await super.getData()),
...(await super.getData()), // config: DS4,
config: DS4, // isOwned: this.item.isOwned,
isOwned: this.item.isOwned, // actor: this.item.actor,
actor: this.item.actor, // isPhysical: isDS4ItemDataTypePhysical(this.item.data.data),
isPhysical: isDS4ItemDataTypePhysical(this.item.data.data), // };
}; // return data;
return data; // }
}
/** @override */ /** @override */
setPosition(options: Partial<Application.Position> = {}): (Application.Position & { height: number }) | undefined { setPosition(options: Partial<Application.Position> = {}): (Application.Position & { height: number }) | undefined {
const position = super.setPosition(options); const position = super.setPosition(options);

View file

@ -3,7 +3,6 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
import { DS4Actor } from "../actor/actor";
import { DS4 } from "../config"; import { DS4 } from "../config";
import { createCheckRoll } from "../rolls/check-factory"; import { createCheckRoll } from "../rolls/check-factory";
import notifications from "../ui/notifications"; import notifications from "../ui/notifications";
@ -20,14 +19,12 @@ declare global {
* The Item class for DS4 * The Item class for DS4
*/ */
export class DS4Item extends Item { export class DS4Item extends Item {
/** /** @override */
* @override
*/
prepareData(): void { prepareData(): void {
super.prepareData(); super.prepareData();
this.prepareDerivedData();
} }
/** @override */
prepareDerivedData(): void { prepareDerivedData(): void {
if (this.data.type === "talent") { if (this.data.type === "talent") {
const data = this.data.data; const data = this.data.data;
@ -68,10 +65,6 @@ export class DS4Item extends Item {
* Roll a check for an action with this item. * Roll a check for an action with this item.
*/ */
async roll(): Promise<void> { async roll(): Promise<void> {
if (!this.isOwnedItem()) {
throw new Error(game.i18n.format("DS4.ErrorCannotRollUnownedItem", { name: this.name, id: this.id }));
}
switch (this.data.type) { switch (this.data.type) {
case "weapon": case "weapon":
return this.rollWeapon(); return this.rollWeapon();
@ -82,7 +75,7 @@ export class DS4Item extends Item {
} }
} }
protected async rollWeapon(this: this & { readonly actor: DS4Actor }): Promise<void> { protected async rollWeapon(): Promise<void> {
if (!(this.data.type === "weapon")) { if (!(this.data.type === "weapon")) {
throw new Error( throw new Error(
game.i18n.format("DS4.ErrorWrongItemType", { game.i18n.format("DS4.ErrorWrongItemType", {
@ -104,8 +97,11 @@ export class DS4Item extends Item {
); );
} }
const actor = this.actor; if (!this.actor) {
const ownerDataData = actor.data.data; throw new Error(game.i18n.format("DS4.ErrorCannotRollUnownedItem", { name: this.name, id: this.id }));
}
const ownerDataData = this.actor.data.data;
const weaponBonus = this.data.data.weaponBonus; const weaponBonus = this.data.data.weaponBonus;
const combatValue = await this.getCombatValueKeyForAttackType(this.data.data.attackType); const combatValue = await this.getCombatValueKeyForAttackType(this.data.data.attackType);
const checkTargetNumber = ownerDataData.combatValues[combatValue].total + weaponBonus; const checkTargetNumber = ownerDataData.combatValues[combatValue].total + weaponBonus;
@ -114,11 +110,11 @@ export class DS4Item extends Item {
rollMode: game.settings.get("core", "rollMode"), rollMode: game.settings.get("core", "rollMode"),
maximumCoupResult: ownerDataData.rolling.maximumCoupResult, maximumCoupResult: ownerDataData.rolling.maximumCoupResult,
minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult, minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult,
flavor: game.i18n.format("DS4.ItemWeaponCheckFlavor", { actor: actor.name, weapon: this.name }), flavor: game.i18n.format("DS4.ItemWeaponCheckFlavor", { actor: this.actor.name, weapon: this.name }),
}); });
} }
protected async rollSpell(this: this & { readonly actor: DS4Actor }): Promise<void> { protected async rollSpell(): Promise<void> {
if (!(this.data.type === "spell")) { if (!(this.data.type === "spell")) {
throw new Error( throw new Error(
game.i18n.format("DS4.ErrorWrongItemType", { game.i18n.format("DS4.ErrorWrongItemType", {
@ -140,8 +136,11 @@ export class DS4Item extends Item {
); );
} }
const actor = this.actor; if (!this.actor) {
const ownerDataData = actor.data.data; throw new Error(game.i18n.format("DS4.ErrorCannotRollUnownedItem", { name: this.name, id: this.id }));
}
const ownerDataData = this.actor.data.data;
const spellBonus = Number.isNumeric(this.data.data.bonus) ? parseInt(this.data.data.bonus) : undefined; const spellBonus = Number.isNumeric(this.data.data.bonus) ? parseInt(this.data.data.bonus) : undefined;
if (spellBonus === undefined) { if (spellBonus === undefined) {
notifications.info( notifications.info(
@ -158,7 +157,7 @@ export class DS4Item extends Item {
rollMode: game.settings.get("core", "rollMode"), rollMode: game.settings.get("core", "rollMode"),
maximumCoupResult: ownerDataData.rolling.maximumCoupResult, maximumCoupResult: ownerDataData.rolling.maximumCoupResult,
minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult, minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult,
flavor: game.i18n.format("DS4.ItemSpellCheckFlavor", { actor: actor.name, spell: this.name }), flavor: game.i18n.format("DS4.ItemSpellCheckFlavor", { actor: this.actor.name, spell: this.name }),
}); });
} }
@ -195,11 +194,4 @@ export class DS4Item extends Item {
return `${attackType}Attack` as const; return `${attackType}Attack` as const;
} }
} }
/**
* Type-guarding variant to check if the item is owned.
*/
isOwnedItem(): this is this & { readonly isOwned: true; readonly actor: DS4Actor; readonly parent: DS4Actor } {
return this.isOwned;
}
} }

View file

@ -3,11 +3,12 @@
"target": "ES2020", "target": "ES2020",
"lib": ["DOM", "ES2020"], "lib": ["DOM", "ES2020"],
"types": ["@league-of-foundry-developers/foundry-vtt-types"], "types": ["@league-of-foundry-developers/foundry-vtt-types"],
"esModuleInterop": true, // "esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
"forceConsistentCasingInFileNames": true, // "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"sourceMap": true "sourceMap": true,
"outDir": "foooo"
}, },
"include": ["src"] "include": ["src"]
} }