From 17633ced68f1753f7d2d428249a0ffb6060e88f7 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Mon, 22 Mar 2021 10:18:43 +0100 Subject: [PATCH] Separate types for data and _data for DS4Actor --- src/module/actor/actor-data.ts | 64 +++++++++++----------- src/module/actor/actor-prepared-data.ts | 69 ++++++++++++++++++++++++ src/module/actor/actor.ts | 15 +++--- src/module/actor/sheets/actor-sheet.ts | 13 +++-- src/module/common/common-data.ts | 27 +++++----- src/module/item/item-data.ts | 70 +++++++++++++------------ src/module/item/item-prepared-data.ts | 18 +++---- 7 files changed, 174 insertions(+), 102 deletions(-) create mode 100644 src/module/actor/actor-prepared-data.ts diff --git a/src/module/actor/actor-data.ts b/src/module/actor/actor-data.ts index 10ebedcf..dffd84d9 100644 --- a/src/module/actor/actor-data.ts +++ b/src/module/actor/actor-data.ts @@ -1,4 +1,4 @@ -import { ModifiableDataTotal, ResourceData, UsableResource } from "../common/common-data"; +import { ModifiableData, ModifiableDataBase, ResourceData, UsableResource } from "../common/common-data"; import { DS4 } from "../config"; import { DS4ItemData } from "../item/item-data"; @@ -6,50 +6,48 @@ export type DS4ActorData = DS4CharacterData | DS4CreatureData; type ActorType = keyof typeof DS4.i18n.actorTypes; -interface DS4ActorDataHelper extends Actor.Data { +export interface DS4ActorDataHelper extends Actor.Data { type: U; } type DS4CharacterData = DS4ActorDataHelper; type DS4CreatureData = DS4ActorDataHelper; +// templates + interface DS4ActorDataDataBase { attributes: DS4ActorDataDataAttributes; traits: DS4ActorDataDataTraits; combatValues: DS4ActorDataDataCombatValues; - rolling: DS4ActorDataDataRolling; } interface DS4ActorDataDataAttributes { - body: ModifiableDataTotal; - mobility: ModifiableDataTotal; - mind: ModifiableDataTotal; + body: ModifiableDataBase; + mobility: ModifiableDataBase; + mind: ModifiableDataBase; } interface DS4ActorDataDataTraits { - strength: ModifiableDataTotal; - constitution: ModifiableDataTotal; - agility: ModifiableDataTotal; - dexterity: ModifiableDataTotal; - intellect: ModifiableDataTotal; - aura: ModifiableDataTotal; + strength: ModifiableDataBase; + constitution: ModifiableDataBase; + agility: ModifiableDataBase; + dexterity: ModifiableDataBase; + intellect: ModifiableDataBase; + aura: ModifiableDataBase; } interface DS4ActorDataDataCombatValues { hitPoints: ResourceData; - defense: ModifiableDataTotal; - initiative: ModifiableDataTotal; - movement: ModifiableDataTotal; - meleeAttack: ModifiableDataTotal; - rangedAttack: ModifiableDataTotal; - spellcasting: ModifiableDataTotal; - targetedSpellcasting: ModifiableDataTotal; + defense: ModifiableData; + initiative: ModifiableData; + movement: ModifiableData; + meleeAttack: ModifiableData; + rangedAttack: ModifiableData; + spellcasting: ModifiableData; + targetedSpellcasting: ModifiableData; } -interface DS4ActorDataDataRolling { - maximumCoupResult?: number; - minimumFumbleResult?: number; -} +// types interface DS4CharacterDataData extends DS4ActorDataDataBase { baseInfo: DS4CharacterDataDataBaseInfo; @@ -58,25 +56,25 @@ interface DS4CharacterDataData extends DS4ActorDataDataBase { profile: DS4CharacterDataDataProfile; currency: DS4CharacterDataDataCurrency; } -interface DS4CharacterDataDataBaseInfo { +export interface DS4CharacterDataDataBaseInfo { race: string; class: string; heroClass: string; culture: string; } -interface DS4CharacterDataDataProgression { +export interface DS4CharacterDataDataProgression { level: number; experiencePoints: number; talentPoints: UsableResource; progressPoints: UsableResource; } -interface DS4CharacterDataDataLanguage { +export interface DS4CharacterDataDataLanguage { languages: string; alphabets: string; } -interface DS4CharacterDataDataProfile { +export interface DS4CharacterDataDataProfile { biography: string; gender: string; birthday: string; @@ -89,7 +87,7 @@ interface DS4CharacterDataDataProfile { specialCharacteristics: string; } -interface DS4CharacterDataDataCurrency { +export interface DS4CharacterDataDataCurrency { gold: number; silver: number; copper: number; @@ -99,11 +97,7 @@ interface DS4CreatureDataData extends DS4ActorDataDataBase { baseInfo: DS4CreatureDataDataBaseInfo; } -type CreatureType = "animal" | "construct" | "humanoid" | "magicalEntity" | "plantBeing" | "undead"; - -type SizeCategory = "tiny" | "small" | "normal" | "large" | "huge" | "colossal"; - -interface DS4CreatureDataDataBaseInfo { +export interface DS4CreatureDataDataBaseInfo { loot: string; foeFactor: number; creatureType: CreatureType; @@ -111,3 +105,7 @@ interface DS4CreatureDataDataBaseInfo { experiencePoints: number; description: string; } + +type CreatureType = "animal" | "construct" | "humanoid" | "magicalEntity" | "plantBeing" | "undead"; + +type SizeCategory = "tiny" | "small" | "normal" | "large" | "huge" | "colossal"; diff --git a/src/module/actor/actor-prepared-data.ts b/src/module/actor/actor-prepared-data.ts new file mode 100644 index 00000000..fcbe0139 --- /dev/null +++ b/src/module/actor/actor-prepared-data.ts @@ -0,0 +1,69 @@ +import { ModifiableDataBaseTotal, ResourceDataBaseTotalMax } from "../common/common-data"; +import { + DS4ActorDataHelper, + DS4CharacterDataDataBaseInfo, + DS4CharacterDataDataCurrency, + DS4CharacterDataDataLanguage, + DS4CharacterDataDataProfile, + DS4CharacterDataDataProgression, + DS4CreatureDataDataBaseInfo, +} from "./actor-data"; + +export type DS4ActorPreparedData = DS4CharacterPreparedData | DS4CreaturePreparedData; + +type DS4CharacterPreparedData = DS4ActorDataHelper; +type DS4CreaturePreparedData = DS4ActorDataHelper; + +// templates + +interface DS4ActorPreparedDataDataBase { + attributes: DS4ActorPreparedDataDataAttributes; + traits: DS4ActorPreparedDataDataTraits; + combatValues: DS4ActorPreparedDataDataCombatValues; + rolling: DS4ActorPreparedDataDataRolling; +} + +interface DS4ActorPreparedDataDataAttributes { + body: ModifiableDataBaseTotal; + mobility: ModifiableDataBaseTotal; + mind: ModifiableDataBaseTotal; +} + +interface DS4ActorPreparedDataDataTraits { + strength: ModifiableDataBaseTotal; + constitution: ModifiableDataBaseTotal; + agility: ModifiableDataBaseTotal; + dexterity: ModifiableDataBaseTotal; + intellect: ModifiableDataBaseTotal; + aura: ModifiableDataBaseTotal; +} + +interface DS4ActorPreparedDataDataCombatValues { + hitPoints: ResourceDataBaseTotalMax; + defense: ModifiableDataBaseTotal; + initiative: ModifiableDataBaseTotal; + movement: ModifiableDataBaseTotal; + meleeAttack: ModifiableDataBaseTotal; + rangedAttack: ModifiableDataBaseTotal; + spellcasting: ModifiableDataBaseTotal; + targetedSpellcasting: ModifiableDataBaseTotal; +} + +interface DS4ActorPreparedDataDataRolling { + maximumCoupResult: number; + minimumFumbleResult: number; +} + +// types + +interface DS4CharacterPreparedDataData extends DS4ActorPreparedDataDataBase { + baseInfo: DS4CharacterDataDataBaseInfo; + progression: DS4CharacterDataDataProgression; + language: DS4CharacterDataDataLanguage; + profile: DS4CharacterDataDataProfile; + currency: DS4CharacterDataDataCurrency; +} + +interface DS4CreaturePreparedDataData extends DS4ActorPreparedDataDataBase { + baseInfo: DS4CreatureDataDataBaseInfo; +} diff --git a/src/module/actor/actor.ts b/src/module/actor/actor.ts index b89f03e6..bccfe3c3 100644 --- a/src/module/actor/actor.ts +++ b/src/module/actor/actor.ts @@ -1,16 +1,17 @@ -import { ModifiableDataTotal } from "../common/common-data"; +import { ModifiableDataBaseTotal } from "../common/common-data"; import { DS4 } from "../config"; import { DS4Item } from "../item/item"; import { ItemType } from "../item/item-data"; import { DS4ActorData } from "./actor-data"; +import { DS4ActorPreparedData } from "./actor-prepared-data"; /** * The Actor class for DS4 */ -export class DS4Actor extends Actor { +export class DS4Actor extends Actor { /** @override */ prepareData(): void { - this.data = duplicate(this._data) as DS4ActorData; + this.data = duplicate(this._data) as DS4ActorPreparedData; if (!this.data.img) this.data.img = CONST.DEFAULT_TOKEN; if (!this.data.name) this.data.name = "New " + this.entity; this.prepareBaseData(); @@ -32,11 +33,13 @@ export class DS4Actor extends Actor { const attributes = data.data.attributes; Object.values(attributes).forEach( - (attribute: ModifiableDataTotal) => (attribute.total = attribute.base + attribute.mod), + (attribute: ModifiableDataBaseTotal) => (attribute.total = attribute.base + attribute.mod), ); const traits = data.data.traits; - Object.values(traits).forEach((trait: ModifiableDataTotal) => (trait.total = trait.base + trait.mod)); + Object.values(traits).forEach( + (trait: ModifiableDataBaseTotal) => (trait.total = trait.base + trait.mod), + ); } applyActiveEffectsToBaseData(): void { @@ -184,7 +187,7 @@ export class DS4Actor extends Actor { (data.attributes.mind.total ?? 0) + (data.traits.dexterity.total ?? 0) - armorValueOfEquippedItems; Object.values(data.combatValues).forEach( - (combatValue: ModifiableDataTotal) => (combatValue.total = combatValue.base + combatValue.mod), + (combatValue: ModifiableDataBaseTotal) => (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 069fce43..dd8dfd47 100644 --- a/src/module/actor/sheets/actor-sheet.ts +++ b/src/module/actor/sheets/actor-sheet.ts @@ -1,4 +1,4 @@ -import { ModifiableMaybeData } from "../../common/common-data"; +import { ModifiableDataBaseTotal } from "../../common/common-data"; import { DS4 } from "../../config"; import { DS4Item } from "../../item/item"; import { DS4ItemData } from "../../item/item-data"; @@ -75,17 +75,16 @@ export class DS4ActorSheet extends ActorSheet> { protected _addTooltipsToData(data: ActorSheet.Data): ActorSheet.Data { const valueGroups = [data.data.attributes, data.data.traits, data.data.combatValues]; + valueGroups.forEach((valueGroup) => { - Object.values(valueGroup).forEach( - (attribute: ModifiableMaybeData & { tooltip?: string }) => { - attribute.tooltip = this._getTooltipForValue(attribute); - }, - ); + Object.values(valueGroup).forEach((attribute: ModifiableDataBaseTotal & { tooltip?: string }) => { + attribute.tooltip = this._getTooltipForValue(attribute); + }); }); return data; } - protected _getTooltipForValue(value: ModifiableMaybeData): string { + protected _getTooltipForValue(value: ModifiableDataBaseTotal): string { return `${value.base} (${game.i18n.localize("DS4.TooltipBaseValue")}) + ${value.mod} (${game.i18n.localize( "DS4.TooltipModifier", )}) ➞ ${game.i18n.localize("DS4.TooltipEffects")} ➞ ${value.total}`; diff --git a/src/module/common/common-data.ts b/src/module/common/common-data.ts index 5956d6f1..8850062f 100644 --- a/src/module/common/common-data.ts +++ b/src/module/common/common-data.ts @@ -1,29 +1,30 @@ export interface ModifiableData { - base: T; mod: T; } +export interface HasBase { + base: T; +} +export interface ModifiableDataBase extends ModifiableData, HasBase {} + export interface HasTotal { total: T; } -export interface ModifiableDataTotal { - base: T; - mod: T; - total?: T; -} +export interface ModifiableDataTotal extends ModifiableData, HasTotal {} -export interface ModifiableMaybeData { - base?: T; - mod: T; - total?: T; -} +export interface ModifiableDataBaseTotal extends ModifiableDataBase, HasTotal {} -export interface ResourceData extends ModifiableMaybeData { +export interface ResourceData extends ModifiableData { value: T; - max?: T; } +export interface HasMax { + max: T; +} + +export interface ResourceDataBaseTotalMax extends ResourceData, HasBase, HasTotal, HasMax {} + export interface UsableResource { total: T; used: T; diff --git a/src/module/item/item-data.ts b/src/module/item/item-data.ts index fbae27e3..7fa85fab 100644 --- a/src/module/item/item-data.ts +++ b/src/module/item/item-data.ts @@ -1,4 +1,4 @@ -import { ModifiableData } from "../common/common-data"; +import { ModifiableDataBase } from "../common/common-data"; import { DS4 } from "../config"; export type ItemType = keyof typeof DS4.i18n.itemTypes; @@ -32,7 +32,38 @@ type DS4LanguageData = DS4ItemDataHelper; type DS4AlphabetData = DS4ItemDataHelper; type DS4SpecialCreatureAbilityData = DS4ItemDataHelper; -export type AttackType = keyof typeof DS4["i18n"]["attackTypes"]; +// templates + +interface DS4ItemDataDataBase { + description: string; +} +interface DS4ItemDataDataPhysical { + quantity: number; + price: number; + availability: "hamlet" | "village" | "city" | "elves" | "dwarves" | "nowhere" | "unset"; + storageLocation: string; +} + +export function isDS4ItemDataTypePhysical(input: DS4ItemData["data"]): boolean { + return "quantity" in input && "price" in input && "availability" in input && "storageLocation" in input; +} + +interface DS4ItemDataDataEquipable { + equipped: boolean; +} + +interface DS4ItemDataDataProtective { + armorValue: number; +} + +interface UnitData { + value: string; + unit: UnitType; +} +type TemporalUnit = "rounds" | "minutes" | "hours" | "days" | "custom"; +type DistanceUnit = "meter" | "kilometer" | "custom"; + +// types export interface DS4WeaponDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, DS4ItemDataDataEquipable { attackType: AttackType; @@ -40,6 +71,8 @@ export interface DS4WeaponDataData extends DS4ItemDataDataBase, DS4ItemDataDataP opponentDefense: number; } +export type AttackType = keyof typeof DS4["i18n"]["attackTypes"]; + export interface DS4ArmorDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, @@ -53,7 +86,7 @@ export interface DS4TalentDataData extends DS4ItemDataDataBase { rank: DS4TalentRank; } -export interface DS4TalentRank extends ModifiableData { +export interface DS4TalentRank extends ModifiableDataBase { max: number; } @@ -96,34 +129,3 @@ export type DS4AlphabetDataData = DS4ItemDataDataBase; export interface DS4SpecialCreatureAbilityDataData extends DS4ItemDataDataBase { experiencePoints: number; } - -// templates - -interface DS4ItemDataDataBase { - description: string; -} -interface DS4ItemDataDataPhysical { - quantity: number; - price: number; - availability: "hamlet" | "village" | "city" | "elves" | "dwarves" | "nowhere" | "unset"; - storageLocation: string; -} - -export function isDS4ItemDataTypePhysical(input: DS4ItemData["data"]): boolean { - return "quantity" in input && "price" in input && "availability" in input && "storageLocation" in input; -} - -interface DS4ItemDataDataEquipable { - equipped: boolean; -} - -interface DS4ItemDataDataProtective { - armorValue: number; -} - -interface UnitData { - value: string; - unit: UnitType; -} -type TemporalUnit = "rounds" | "minutes" | "hours" | "days" | "custom"; -type DistanceUnit = "meter" | "kilometer" | "custom"; diff --git a/src/module/item/item-prepared-data.ts b/src/module/item/item-prepared-data.ts index 981383f4..4dede57f 100644 --- a/src/module/item/item-prepared-data.ts +++ b/src/module/item/item-prepared-data.ts @@ -43,6 +43,14 @@ type DS4SpecialCreatureAbilityPreparedData = DS4ItemDataHelper< "specialCreatureAbility" >; +// templates + +interface DS4ItemPreparedDataDataRollable { + rollable: boolean; +} + +//types + interface DS4WeaponPreparedDataData extends DS4WeaponDataData, DS4ItemPreparedDataDataRollable {} interface DS4ArmorPreparedDataData extends DS4ArmorDataData, DS4ItemPreparedDataDataRollable {} @@ -59,9 +67,7 @@ interface DS4TalentPreparedDataData extends DS4TalentDataData, DS4ItemPreparedDa rank: DS4TalentPreparedRank; } -interface DS4TalentPreparedRank extends DS4TalentRank, HasTotal { - max: number; -} +interface DS4TalentPreparedRank extends DS4TalentRank, HasTotal {} interface DS4RacialAbilityPreparedDataData extends DS4RacialAbilityDataData, DS4ItemPreparedDataDataRollable {} @@ -72,9 +78,3 @@ interface DS4AlphabetPreparedDataData extends DS4AlphabetDataData, DS4ItemPrepar interface DS4SpecialCreatureAbilityPreparedDataData extends DS4SpecialCreatureAbilityDataData, DS4ItemPreparedDataDataRollable {} - -// templates - -interface DS4ItemPreparedDataDataRollable { - rollable: boolean; -}