Separate types for data and _data for DS4Actor

This commit is contained in:
Johannes Loher 2021-03-22 10:18:43 +01:00
parent 7c7d30854c
commit 17633ced68
7 changed files with 174 additions and 102 deletions

View file

@ -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 { DS4 } from "../config";
import { DS4ItemData } from "../item/item-data"; import { DS4ItemData } from "../item/item-data";
@ -6,50 +6,48 @@ export type DS4ActorData = DS4CharacterData | DS4CreatureData;
type ActorType = keyof typeof DS4.i18n.actorTypes; type ActorType = keyof typeof DS4.i18n.actorTypes;
interface DS4ActorDataHelper<T, U extends ActorType> extends Actor.Data<T, DS4ItemData> { export interface DS4ActorDataHelper<T, U extends ActorType> extends Actor.Data<T, DS4ItemData> {
type: U; type: U;
} }
type DS4CharacterData = DS4ActorDataHelper<DS4CharacterDataData, "character">; type DS4CharacterData = DS4ActorDataHelper<DS4CharacterDataData, "character">;
type DS4CreatureData = DS4ActorDataHelper<DS4CreatureDataData, "creature">; type DS4CreatureData = DS4ActorDataHelper<DS4CreatureDataData, "creature">;
// templates
interface DS4ActorDataDataBase { interface DS4ActorDataDataBase {
attributes: DS4ActorDataDataAttributes; attributes: DS4ActorDataDataAttributes;
traits: DS4ActorDataDataTraits; traits: DS4ActorDataDataTraits;
combatValues: DS4ActorDataDataCombatValues; combatValues: DS4ActorDataDataCombatValues;
rolling: DS4ActorDataDataRolling;
} }
interface DS4ActorDataDataAttributes { interface DS4ActorDataDataAttributes {
body: ModifiableDataTotal<number>; body: ModifiableDataBase<number>;
mobility: ModifiableDataTotal<number>; mobility: ModifiableDataBase<number>;
mind: ModifiableDataTotal<number>; mind: ModifiableDataBase<number>;
} }
interface DS4ActorDataDataTraits { interface DS4ActorDataDataTraits {
strength: ModifiableDataTotal<number>; strength: ModifiableDataBase<number>;
constitution: ModifiableDataTotal<number>; constitution: ModifiableDataBase<number>;
agility: ModifiableDataTotal<number>; agility: ModifiableDataBase<number>;
dexterity: ModifiableDataTotal<number>; dexterity: ModifiableDataBase<number>;
intellect: ModifiableDataTotal<number>; intellect: ModifiableDataBase<number>;
aura: ModifiableDataTotal<number>; aura: ModifiableDataBase<number>;
} }
interface DS4ActorDataDataCombatValues { interface DS4ActorDataDataCombatValues {
hitPoints: ResourceData<number>; hitPoints: ResourceData<number>;
defense: ModifiableDataTotal<number>; defense: ModifiableData<number>;
initiative: ModifiableDataTotal<number>; initiative: ModifiableData<number>;
movement: ModifiableDataTotal<number>; movement: ModifiableData<number>;
meleeAttack: ModifiableDataTotal<number>; meleeAttack: ModifiableData<number>;
rangedAttack: ModifiableDataTotal<number>; rangedAttack: ModifiableData<number>;
spellcasting: ModifiableDataTotal<number>; spellcasting: ModifiableData<number>;
targetedSpellcasting: ModifiableDataTotal<number>; targetedSpellcasting: ModifiableData<number>;
} }
interface DS4ActorDataDataRolling { // types
maximumCoupResult?: number;
minimumFumbleResult?: number;
}
interface DS4CharacterDataData extends DS4ActorDataDataBase { interface DS4CharacterDataData extends DS4ActorDataDataBase {
baseInfo: DS4CharacterDataDataBaseInfo; baseInfo: DS4CharacterDataDataBaseInfo;
@ -58,25 +56,25 @@ interface DS4CharacterDataData extends DS4ActorDataDataBase {
profile: DS4CharacterDataDataProfile; profile: DS4CharacterDataDataProfile;
currency: DS4CharacterDataDataCurrency; currency: DS4CharacterDataDataCurrency;
} }
interface DS4CharacterDataDataBaseInfo { export interface DS4CharacterDataDataBaseInfo {
race: string; race: string;
class: string; class: string;
heroClass: string; heroClass: string;
culture: string; culture: string;
} }
interface DS4CharacterDataDataProgression { export interface DS4CharacterDataDataProgression {
level: number; level: number;
experiencePoints: number; experiencePoints: number;
talentPoints: UsableResource<number>; talentPoints: UsableResource<number>;
progressPoints: UsableResource<number>; progressPoints: UsableResource<number>;
} }
interface DS4CharacterDataDataLanguage { export interface DS4CharacterDataDataLanguage {
languages: string; languages: string;
alphabets: string; alphabets: string;
} }
interface DS4CharacterDataDataProfile { export interface DS4CharacterDataDataProfile {
biography: string; biography: string;
gender: string; gender: string;
birthday: string; birthday: string;
@ -89,7 +87,7 @@ interface DS4CharacterDataDataProfile {
specialCharacteristics: string; specialCharacteristics: string;
} }
interface DS4CharacterDataDataCurrency { export interface DS4CharacterDataDataCurrency {
gold: number; gold: number;
silver: number; silver: number;
copper: number; copper: number;
@ -99,11 +97,7 @@ interface DS4CreatureDataData extends DS4ActorDataDataBase {
baseInfo: DS4CreatureDataDataBaseInfo; baseInfo: DS4CreatureDataDataBaseInfo;
} }
type CreatureType = "animal" | "construct" | "humanoid" | "magicalEntity" | "plantBeing" | "undead"; export interface DS4CreatureDataDataBaseInfo {
type SizeCategory = "tiny" | "small" | "normal" | "large" | "huge" | "colossal";
interface DS4CreatureDataDataBaseInfo {
loot: string; loot: string;
foeFactor: number; foeFactor: number;
creatureType: CreatureType; creatureType: CreatureType;
@ -111,3 +105,7 @@ interface DS4CreatureDataDataBaseInfo {
experiencePoints: number; experiencePoints: number;
description: string; description: string;
} }
type CreatureType = "animal" | "construct" | "humanoid" | "magicalEntity" | "plantBeing" | "undead";
type SizeCategory = "tiny" | "small" | "normal" | "large" | "huge" | "colossal";

View file

@ -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<DS4CharacterPreparedDataData, "character">;
type DS4CreaturePreparedData = DS4ActorDataHelper<DS4CreaturePreparedDataData, "creature">;
// templates
interface DS4ActorPreparedDataDataBase {
attributes: DS4ActorPreparedDataDataAttributes;
traits: DS4ActorPreparedDataDataTraits;
combatValues: DS4ActorPreparedDataDataCombatValues;
rolling: DS4ActorPreparedDataDataRolling;
}
interface DS4ActorPreparedDataDataAttributes {
body: ModifiableDataBaseTotal<number>;
mobility: ModifiableDataBaseTotal<number>;
mind: ModifiableDataBaseTotal<number>;
}
interface DS4ActorPreparedDataDataTraits {
strength: ModifiableDataBaseTotal<number>;
constitution: ModifiableDataBaseTotal<number>;
agility: ModifiableDataBaseTotal<number>;
dexterity: ModifiableDataBaseTotal<number>;
intellect: ModifiableDataBaseTotal<number>;
aura: ModifiableDataBaseTotal<number>;
}
interface DS4ActorPreparedDataDataCombatValues {
hitPoints: ResourceDataBaseTotalMax<number>;
defense: ModifiableDataBaseTotal<number>;
initiative: ModifiableDataBaseTotal<number>;
movement: ModifiableDataBaseTotal<number>;
meleeAttack: ModifiableDataBaseTotal<number>;
rangedAttack: ModifiableDataBaseTotal<number>;
spellcasting: ModifiableDataBaseTotal<number>;
targetedSpellcasting: ModifiableDataBaseTotal<number>;
}
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;
}

View file

@ -1,16 +1,17 @@
import { ModifiableDataTotal } from "../common/common-data"; import { ModifiableDataBaseTotal } from "../common/common-data";
import { DS4 } from "../config"; import { DS4 } from "../config";
import { DS4Item } from "../item/item"; import { DS4Item } from "../item/item";
import { ItemType } from "../item/item-data"; import { ItemType } from "../item/item-data";
import { DS4ActorData } from "./actor-data"; import { DS4ActorData } from "./actor-data";
import { DS4ActorPreparedData } from "./actor-prepared-data";
/** /**
* The Actor class for DS4 * The Actor class for DS4
*/ */
export class DS4Actor extends Actor<DS4ActorData, DS4Item> { export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData> {
/** @override */ /** @override */
prepareData(): void { 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.img) this.data.img = CONST.DEFAULT_TOKEN;
if (!this.data.name) this.data.name = "New " + this.entity; if (!this.data.name) this.data.name = "New " + this.entity;
this.prepareBaseData(); this.prepareBaseData();
@ -32,11 +33,13 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item> {
const attributes = data.data.attributes; const attributes = data.data.attributes;
Object.values(attributes).forEach( Object.values(attributes).forEach(
(attribute: ModifiableDataTotal<number>) => (attribute.total = attribute.base + attribute.mod), (attribute: ModifiableDataBaseTotal<number>) => (attribute.total = attribute.base + attribute.mod),
); );
const traits = data.data.traits; const traits = data.data.traits;
Object.values(traits).forEach((trait: ModifiableDataTotal<number>) => (trait.total = trait.base + trait.mod)); Object.values(traits).forEach(
(trait: ModifiableDataBaseTotal<number>) => (trait.total = trait.base + trait.mod),
);
} }
applyActiveEffectsToBaseData(): void { applyActiveEffectsToBaseData(): void {
@ -184,7 +187,7 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item> {
(data.attributes.mind.total ?? 0) + (data.traits.dexterity.total ?? 0) - armorValueOfEquippedItems; (data.attributes.mind.total ?? 0) + (data.traits.dexterity.total ?? 0) - armorValueOfEquippedItems;
Object.values(data.combatValues).forEach( Object.values(data.combatValues).forEach(
(combatValue: ModifiableDataTotal<number>) => (combatValue.total = combatValue.base + combatValue.mod), (combatValue: ModifiableDataBaseTotal<number>) => (combatValue.total = combatValue.base + combatValue.mod),
); );
} }

View file

@ -1,4 +1,4 @@
import { ModifiableMaybeData } from "../../common/common-data"; import { ModifiableDataBaseTotal } from "../../common/common-data";
import { DS4 } from "../../config"; import { DS4 } from "../../config";
import { DS4Item } from "../../item/item"; import { DS4Item } from "../../item/item";
import { DS4ItemData } from "../../item/item-data"; import { DS4ItemData } from "../../item/item-data";
@ -75,17 +75,16 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Data<DS4Actor>> {
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( Object.values(valueGroup).forEach((attribute: ModifiableDataBaseTotal<number> & { tooltip?: string }) => {
(attribute: ModifiableMaybeData<number | null> & { tooltip?: string }) => { attribute.tooltip = this._getTooltipForValue(attribute);
attribute.tooltip = this._getTooltipForValue(attribute); });
},
);
}); });
return data; return data;
} }
protected _getTooltipForValue(value: ModifiableMaybeData<number | null>): 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(
"DS4.TooltipModifier", "DS4.TooltipModifier",
)}) ${game.i18n.localize("DS4.TooltipEffects")} ${value.total}`; )}) ${game.i18n.localize("DS4.TooltipEffects")} ${value.total}`;

View file

@ -1,29 +1,30 @@
export interface ModifiableData<T> { export interface ModifiableData<T> {
base: T;
mod: T; mod: T;
} }
export interface HasBase<T> {
base: T;
}
export interface ModifiableDataBase<T> extends ModifiableData<T>, HasBase<T> {}
export interface HasTotal<T> { export interface HasTotal<T> {
total: T; total: T;
} }
export interface ModifiableDataTotal<T> { export interface ModifiableDataTotal<T> extends ModifiableData<T>, HasTotal<T> {}
base: T;
mod: T;
total?: T;
}
export interface ModifiableMaybeData<T> { export interface ModifiableDataBaseTotal<T> extends ModifiableDataBase<T>, HasTotal<T> {}
base?: T;
mod: T;
total?: T;
}
export interface ResourceData<T> extends ModifiableMaybeData<T> { export interface ResourceData<T> extends ModifiableData<T> {
value: T; value: T;
max?: T;
} }
export interface HasMax<T> {
max: T;
}
export interface ResourceDataBaseTotalMax<T> extends ResourceData<T>, HasBase<T>, HasTotal<T>, HasMax<T> {}
export interface UsableResource<T> { export interface UsableResource<T> {
total: T; total: T;
used: T; used: T;

View file

@ -1,4 +1,4 @@
import { ModifiableData } from "../common/common-data"; import { ModifiableDataBase } from "../common/common-data";
import { DS4 } from "../config"; import { DS4 } from "../config";
export type ItemType = keyof typeof DS4.i18n.itemTypes; export type ItemType = keyof typeof DS4.i18n.itemTypes;
@ -32,7 +32,38 @@ type DS4LanguageData = DS4ItemDataHelper<DS4LanguageDataData, "language">;
type DS4AlphabetData = DS4ItemDataHelper<DS4AlphabetDataData, "alphabet">; type DS4AlphabetData = DS4ItemDataHelper<DS4AlphabetDataData, "alphabet">;
type DS4SpecialCreatureAbilityData = DS4ItemDataHelper<DS4SpecialCreatureAbilityDataData, "specialCreatureAbility">; type DS4SpecialCreatureAbilityData = DS4ItemDataHelper<DS4SpecialCreatureAbilityDataData, "specialCreatureAbility">;
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<UnitType> {
value: string;
unit: UnitType;
}
type TemporalUnit = "rounds" | "minutes" | "hours" | "days" | "custom";
type DistanceUnit = "meter" | "kilometer" | "custom";
// types
export interface DS4WeaponDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, DS4ItemDataDataEquipable { export interface DS4WeaponDataData extends DS4ItemDataDataBase, DS4ItemDataDataPhysical, DS4ItemDataDataEquipable {
attackType: AttackType; attackType: AttackType;
@ -40,6 +71,8 @@ export interface DS4WeaponDataData extends DS4ItemDataDataBase, DS4ItemDataDataP
opponentDefense: number; opponentDefense: number;
} }
export type AttackType = keyof typeof DS4["i18n"]["attackTypes"];
export interface DS4ArmorDataData export interface DS4ArmorDataData
extends DS4ItemDataDataBase, extends DS4ItemDataDataBase,
DS4ItemDataDataPhysical, DS4ItemDataDataPhysical,
@ -53,7 +86,7 @@ export interface DS4TalentDataData extends DS4ItemDataDataBase {
rank: DS4TalentRank; rank: DS4TalentRank;
} }
export interface DS4TalentRank extends ModifiableData<number> { export interface DS4TalentRank extends ModifiableDataBase<number> {
max: number; max: number;
} }
@ -96,34 +129,3 @@ export type DS4AlphabetDataData = DS4ItemDataDataBase;
export interface DS4SpecialCreatureAbilityDataData extends DS4ItemDataDataBase { export interface DS4SpecialCreatureAbilityDataData extends DS4ItemDataDataBase {
experiencePoints: number; 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<UnitType> {
value: string;
unit: UnitType;
}
type TemporalUnit = "rounds" | "minutes" | "hours" | "days" | "custom";
type DistanceUnit = "meter" | "kilometer" | "custom";

View file

@ -43,6 +43,14 @@ type DS4SpecialCreatureAbilityPreparedData = DS4ItemDataHelper<
"specialCreatureAbility" "specialCreatureAbility"
>; >;
// templates
interface DS4ItemPreparedDataDataRollable {
rollable: boolean;
}
//types
interface DS4WeaponPreparedDataData extends DS4WeaponDataData, DS4ItemPreparedDataDataRollable {} interface DS4WeaponPreparedDataData extends DS4WeaponDataData, DS4ItemPreparedDataDataRollable {}
interface DS4ArmorPreparedDataData extends DS4ArmorDataData, DS4ItemPreparedDataDataRollable {} interface DS4ArmorPreparedDataData extends DS4ArmorDataData, DS4ItemPreparedDataDataRollable {}
@ -59,9 +67,7 @@ interface DS4TalentPreparedDataData extends DS4TalentDataData, DS4ItemPreparedDa
rank: DS4TalentPreparedRank; rank: DS4TalentPreparedRank;
} }
interface DS4TalentPreparedRank extends DS4TalentRank, HasTotal<number> { interface DS4TalentPreparedRank extends DS4TalentRank, HasTotal<number> {}
max: number;
}
interface DS4RacialAbilityPreparedDataData extends DS4RacialAbilityDataData, DS4ItemPreparedDataDataRollable {} interface DS4RacialAbilityPreparedDataData extends DS4RacialAbilityDataData, DS4ItemPreparedDataDataRollable {}
@ -72,9 +78,3 @@ interface DS4AlphabetPreparedDataData extends DS4AlphabetDataData, DS4ItemPrepar
interface DS4SpecialCreatureAbilityPreparedDataData interface DS4SpecialCreatureAbilityPreparedDataData
extends DS4SpecialCreatureAbilityDataData, extends DS4SpecialCreatureAbilityDataData,
DS4ItemPreparedDataDataRollable {} DS4ItemPreparedDataDataRollable {}
// templates
interface DS4ItemPreparedDataDataRollable {
rollable: boolean;
}