From 7c7d30854c98247b30a4cbd0ef9d2e0c16b89a9f Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 22 Mar 2021 09:04:45 +0100 Subject: [PATCH] Separate data and _data for DS4Item --- package.json | 2 +- src/module/actor/actor-data.ts | 34 +++++------ src/module/actor/actor.ts | 8 +-- src/module/actor/sheets/actor-sheet.ts | 2 +- src/module/common/common-data.ts | 9 +++ src/module/global.d.ts | 6 ++ src/module/item/item-data.ts | 40 ++++++------- src/module/item/item-prepared-data.ts | 80 ++++++++++++++++++++++++++ src/module/item/item.ts | 9 ++- src/module/migrations.ts | 2 +- src/module/rolls/check-factory.ts | 2 +- yarn.lock | 10 ++-- 12 files changed, 150 insertions(+), 54 deletions(-) create mode 100644 src/module/global.d.ts create mode 100644 src/module/item/item-prepared-data.ts diff --git a/package.json b/package.json index 5ac79213..771fe7f0 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "postinstall": "husky install" }, "devDependencies": { - "@league-of-foundry-developers/foundry-vtt-types": "^0.7.9-3", + "@league-of-foundry-developers/foundry-vtt-types": "^0.7.9-4", "@rollup/plugin-node-resolve": "^11.2.0", "@types/fs-extra": "^9.0.8", "@types/jest": "^26.0.20", diff --git a/src/module/actor/actor-data.ts b/src/module/actor/actor-data.ts index 7fcb8398..10ebedcf 100644 --- a/src/module/actor/actor-data.ts +++ b/src/module/actor/actor-data.ts @@ -1,4 +1,4 @@ -import { ModifiableData, ResourceData, UsableResource } from "../common/common-data"; +import { ModifiableDataTotal, ResourceData, UsableResource } from "../common/common-data"; import { DS4 } from "../config"; import { DS4ItemData } from "../item/item-data"; @@ -21,29 +21,29 @@ interface DS4ActorDataDataBase { } interface DS4ActorDataDataAttributes { - body: ModifiableData; - mobility: ModifiableData; - mind: ModifiableData; + body: ModifiableDataTotal; + mobility: ModifiableDataTotal; + mind: ModifiableDataTotal; } interface DS4ActorDataDataTraits { - strength: ModifiableData; - constitution: ModifiableData; - agility: ModifiableData; - dexterity: ModifiableData; - intellect: ModifiableData; - aura: ModifiableData; + strength: ModifiableDataTotal; + constitution: ModifiableDataTotal; + agility: ModifiableDataTotal; + dexterity: ModifiableDataTotal; + intellect: ModifiableDataTotal; + aura: ModifiableDataTotal; } interface DS4ActorDataDataCombatValues { hitPoints: ResourceData; - defense: ModifiableData; - initiative: ModifiableData; - movement: ModifiableData; - meleeAttack: ModifiableData; - rangedAttack: ModifiableData; - spellcasting: ModifiableData; - targetedSpellcasting: ModifiableData; + defense: ModifiableDataTotal; + initiative: ModifiableDataTotal; + movement: ModifiableDataTotal; + meleeAttack: ModifiableDataTotal; + rangedAttack: ModifiableDataTotal; + spellcasting: ModifiableDataTotal; + targetedSpellcasting: ModifiableDataTotal; } interface DS4ActorDataDataRolling { diff --git a/src/module/actor/actor.ts b/src/module/actor/actor.ts index 0e4cbb0d..b89f03e6 100644 --- a/src/module/actor/actor.ts +++ b/src/module/actor/actor.ts @@ -1,4 +1,4 @@ -import { ModifiableData } from "../common/common-data"; +import { ModifiableDataTotal } from "../common/common-data"; import { DS4 } from "../config"; import { DS4Item } from "../item/item"; import { ItemType } from "../item/item-data"; @@ -32,11 +32,11 @@ export class DS4Actor extends Actor { const attributes = data.data.attributes; Object.values(attributes).forEach( - (attribute: ModifiableData) => (attribute.total = attribute.base + attribute.mod), + (attribute: ModifiableDataTotal) => (attribute.total = attribute.base + attribute.mod), ); const traits = data.data.traits; - Object.values(traits).forEach((trait: ModifiableData) => (trait.total = trait.base + trait.mod)); + Object.values(traits).forEach((trait: ModifiableDataTotal) => (trait.total = trait.base + trait.mod)); } applyActiveEffectsToBaseData(): void { @@ -184,7 +184,7 @@ export class DS4Actor extends Actor { (data.attributes.mind.total ?? 0) + (data.traits.dexterity.total ?? 0) - armorValueOfEquippedItems; Object.values(data.combatValues).forEach( - (combatValue: ModifiableData) => (combatValue.total = combatValue.base + combatValue.mod), + (combatValue: ModifiableDataTotal) => (combatValue.total = combatValue.base + combatValue.mod), ); } diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts index 470f33ea..069fce43 100644 --- a/src/module/actor/sheets/actor-sheet.ts +++ b/src/module/actor/sheets/actor-sheet.ts @@ -160,7 +160,7 @@ export class DS4ActorSheet extends ActorSheet> { ev.preventDefault(); const el: HTMLFormElement = $(ev.currentTarget).get(0); const id = $(ev.currentTarget).parents(".item").data("itemId"); - const item = duplicate(this.actor.getOwnedItem(id)); + const item = duplicate(this.actor.getOwnedItem(id)); const property: string | undefined = $(ev.currentTarget).data("property"); // Early return: diff --git a/src/module/common/common-data.ts b/src/module/common/common-data.ts index d23b61d0..5956d6f1 100644 --- a/src/module/common/common-data.ts +++ b/src/module/common/common-data.ts @@ -1,6 +1,15 @@ export interface ModifiableData { base: T; mod: T; +} + +export interface HasTotal { + total: T; +} + +export interface ModifiableDataTotal { + base: T; + mod: T; total?: T; } diff --git a/src/module/global.d.ts b/src/module/global.d.ts new file mode 100644 index 00000000..c560d623 --- /dev/null +++ b/src/module/global.d.ts @@ -0,0 +1,6 @@ +declare namespace ClientSettings { + interface Values { + "ds4.systemMigrationVersion": number; + "ds4.useSlayingDiceForAutomatedChecks": boolean; + } +} diff --git a/src/module/item/item-data.ts b/src/module/item/item-data.ts index c28fbe86..fbae27e3 100644 --- a/src/module/item/item-data.ts +++ b/src/module/item/item-data.ts @@ -16,7 +16,7 @@ export type DS4ItemData = | DS4AlphabetData | DS4SpecialCreatureAbilityData; -interface DS4ItemDataHelper extends Item.Data { +export interface DS4ItemDataHelper extends Item.Data { type: U; } @@ -34,17 +34,13 @@ type DS4SpecialCreatureAbilityData = DS4ItemDataHelper { +export interface DS4TalentRank extends ModifiableData { max: number; } -interface DS4SpellDataData extends DS4ItemDataDataBase, DS4ItemDataDataEquipable, DS4ItemDataDataRollable { +export interface DS4SpellDataData extends DS4ItemDataDataBase, DS4ItemDataDataEquipable { spellType: "spellcasting" | "targetedSpellcasting"; bonus: string; spellCategory: @@ -81,17 +77,23 @@ interface DS4SpellDataData extends DS4ItemDataDataBase, DS4ItemDataDataEquipable scrollPrice: number; } -interface DS4ShieldDataData +export interface DS4ShieldDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, DS4ItemDataDataEquipable, DS4ItemDataDataProtective {} -interface DS4EquipmentDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, DS4ItemDataDataEquipable {} -interface DS4LootDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical {} -type DS4RacialAbilityDataData = DS4ItemDataDataBase; -type DS4LanguageDataData = DS4ItemDataDataBase; -type DS4AlphabetDataData = DS4ItemDataDataBase; -interface DS4SpecialCreatureAbilityDataData extends DS4ItemDataDataBase { + +export interface DS4EquipmentDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, DS4ItemDataDataEquipable {} + +export interface DS4LootDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical {} + +export type DS4RacialAbilityDataData = DS4ItemDataDataBase; + +export type DS4LanguageDataData = DS4ItemDataDataBase; + +export type DS4AlphabetDataData = DS4ItemDataDataBase; + +export interface DS4SpecialCreatureAbilityDataData extends DS4ItemDataDataBase { experiencePoints: number; } @@ -115,10 +117,6 @@ interface DS4ItemDataDataEquipable { equipped: boolean; } -interface DS4ItemDataDataRollable { - rollable?: boolean; -} - interface DS4ItemDataDataProtective { armorValue: number; } diff --git a/src/module/item/item-prepared-data.ts b/src/module/item/item-prepared-data.ts new file mode 100644 index 00000000..981383f4 --- /dev/null +++ b/src/module/item/item-prepared-data.ts @@ -0,0 +1,80 @@ +import { HasTotal } from "../common/common-data"; +import { + DS4AlphabetDataData, + DS4ArmorDataData, + DS4EquipmentDataData, + DS4ItemDataHelper, + DS4LanguageDataData, + DS4LootDataData, + DS4RacialAbilityDataData, + DS4ShieldDataData, + DS4SpecialCreatureAbilityDataData, + DS4SpellDataData, + DS4TalentDataData, + DS4TalentRank, + DS4WeaponDataData, +} from "./item-data"; + +export type DS4ItemPreparedData = + | DS4WeaponPreparedData + | DS4ArmorPreparedData + | DS4ShieldPreparedData + | DS4SpellPreparedData + | DS4EquipmentPreparedData + | DS4LootPreparedData + | DS4TalentPreparedData + | DS4RacialAbilityPreparedData + | DS4LanguagePreparedData + | DS4AlphabetPreparedData + | DS4SpecialCreatureAbilityPreparedData; + +type DS4WeaponPreparedData = DS4ItemDataHelper; +type DS4ArmorPreparedData = DS4ItemDataHelper; +type DS4ShieldPreparedData = DS4ItemDataHelper; +type DS4SpellPreparedData = DS4ItemDataHelper; +type DS4EquipmentPreparedData = DS4ItemDataHelper; +type DS4LootPreparedData = DS4ItemDataHelper; +type DS4TalentPreparedData = DS4ItemDataHelper; +type DS4RacialAbilityPreparedData = DS4ItemDataHelper; +type DS4LanguagePreparedData = DS4ItemDataHelper; +type DS4AlphabetPreparedData = DS4ItemDataHelper; +type DS4SpecialCreatureAbilityPreparedData = DS4ItemDataHelper< + DS4SpecialCreatureAbilityPreparedDataData, + "specialCreatureAbility" +>; + +interface DS4WeaponPreparedDataData extends DS4WeaponDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4ArmorPreparedDataData extends DS4ArmorDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4ShieldPreparedDataData extends DS4ShieldDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4SpellPreparedDataData extends DS4SpellDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4EquipmentPreparedDataData extends DS4EquipmentDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4LootPreparedDataData extends DS4LootDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4TalentPreparedDataData extends DS4TalentDataData, DS4ItemPreparedDataDataRollable { + rank: DS4TalentPreparedRank; +} + +interface DS4TalentPreparedRank extends DS4TalentRank, HasTotal { + max: number; +} + +interface DS4RacialAbilityPreparedDataData extends DS4RacialAbilityDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4LanguagePreparedDataData extends DS4LanguageDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4AlphabetPreparedDataData extends DS4AlphabetDataData, DS4ItemPreparedDataDataRollable {} + +interface DS4SpecialCreatureAbilityPreparedDataData + extends DS4SpecialCreatureAbilityDataData, + DS4ItemPreparedDataDataRollable {} + +// templates + +interface DS4ItemPreparedDataDataRollable { + rollable: boolean; +} diff --git a/src/module/item/item.ts b/src/module/item/item.ts index 07b5dc47..1c93e68a 100644 --- a/src/module/item/item.ts +++ b/src/module/item/item.ts @@ -3,11 +3,12 @@ import { DS4 } from "../config"; import { createCheckRoll } from "../rolls/check-factory"; import notifications from "../ui/notifications"; import { AttackType, DS4ItemData, ItemType } from "./item-data"; +import { DS4ItemPreparedData } from "./item-prepared-data"; /** * The Item class for DS4 */ -export class DS4Item extends Item { +export class DS4Item extends Item { /** * @override */ @@ -23,6 +24,8 @@ export class DS4Item extends Item { } if (this.data.type === "weapon" || this.data.type === "spell") { this.data.data.rollable = this.data.data.equipped; + } else { + this.data.data.rollable = false; } } @@ -94,7 +97,7 @@ export class DS4Item extends Item { const checkTargetNumber = (ownerDataData.combatValues[combatValue].total as number) + weaponBonus; await createCheckRoll(checkTargetNumber, { - rollMode: game.settings.get("core", "rollMode"), + rollMode: game.settings.get("core", "rollMode") as Const.DiceRollMode, // TODO(types): Type this setting in upstream maximumCoupResult: ownerDataData.rolling.maximumCoupResult, minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult, flavor: game.i18n.format("DS4.ItemWeaponCheckFlavor", { actor: actor.name, weapon: this.name }), @@ -138,7 +141,7 @@ export class DS4Item extends Item { const checkTargetNumber = (ownerDataData.combatValues[spellType].total as number) + (spellBonus ?? 0); await createCheckRoll(checkTargetNumber, { - rollMode: game.settings.get("core", "rollMode"), + rollMode: game.settings.get("core", "rollMode") as Const.DiceRollMode, // TODO(types): Type this setting in upstream maximumCoupResult: ownerDataData.rolling.maximumCoupResult, minimumFumbleResult: ownerDataData.rolling.minimumFumbleResult, flavor: game.i18n.format("DS4.ItemSpellCheckFlavor", { actor: actor.name, spell: this.name }), diff --git a/src/module/migrations.ts b/src/module/migrations.ts index e9a157c7..525400c5 100644 --- a/src/module/migrations.ts +++ b/src/module/migrations.ts @@ -9,7 +9,7 @@ async function migrate(): Promise { return; } - const oldMigrationVersion: number = game.settings.get("ds4", "systemMigrationVersion"); + const oldMigrationVersion = game.settings.get("ds4", "systemMigrationVersion"); const targetMigrationVersion = migrations.length; diff --git a/src/module/rolls/check-factory.ts b/src/module/rolls/check-factory.ts index 5498acab..475c50fc 100644 --- a/src/module/rolls/check-factory.ts +++ b/src/module/rolls/check-factory.ts @@ -79,7 +79,7 @@ export async function createCheckRoll( const newOptions: Partial = { maximumCoupResult: gmModifierData.maximumCoupResult ?? options.maximumCoupResult, minimumFumbleResult: gmModifierData.minimumFumbleResult ?? options.minimumFumbleResult, - useSlayingDice: game.settings.get("ds4", "useSlayingDiceForAutomatedChecks") ?? false, + useSlayingDice: game.settings.get("ds4", "useSlayingDiceForAutomatedChecks"), rollMode: gmModifierData.rollMode ?? options.rollMode, flavor: options.flavor, }; diff --git a/yarn.lock b/yarn.lock index 21e5aac0..e2748004 100644 --- a/yarn.lock +++ b/yarn.lock @@ -647,9 +647,9 @@ __metadata: languageName: node linkType: hard -"@league-of-foundry-developers/foundry-vtt-types@npm:^0.7.9-3": - version: 0.7.9-3 - resolution: "@league-of-foundry-developers/foundry-vtt-types@npm:0.7.9-3" +"@league-of-foundry-developers/foundry-vtt-types@npm:^0.7.9-4": + version: 0.7.9-4 + resolution: "@league-of-foundry-developers/foundry-vtt-types@npm:0.7.9-4" dependencies: "@types/howler": 2.2.1 "@types/jquery": 3.5.1 @@ -659,7 +659,7 @@ __metadata: pixi.js: 5.3.4 tinymce: 5.6.2 typescript: ^4.1.4 - checksum: 75524c7aa78ddb77cad1a9d041af30ae5bbd708f5b26568dabbb3d913a4643aefcc6f2ed80e1e76b3c17050579665eab155f035f840db6397691cf68eeee9b3f + checksum: 9aeee3d49c20dd69e736abd50347e43f5cfdcfdca7fba5af9fb321250cc3ca78758daed14fe6328005f3b606ed0ace49bf2562d7262912c1461132718ab793b8 languageName: node linkType: hard @@ -2891,7 +2891,7 @@ __metadata: version: 0.0.0-use.local resolution: "dungeonslayers4@workspace:." dependencies: - "@league-of-foundry-developers/foundry-vtt-types": ^0.7.9-3 + "@league-of-foundry-developers/foundry-vtt-types": ^0.7.9-4 "@rollup/plugin-node-resolve": ^11.2.0 "@types/fs-extra": ^9.0.8 "@types/jest": ^26.0.20