diff --git a/src/lang/de.json b/src/lang/de.json index 76c9b3e7..dc3792f3 100644 --- a/src/lang/de.json +++ b/src/lang/de.json @@ -207,8 +207,8 @@ "DS4.ErrorRollingForItemTypeNotPossible": "Würfeln ist für Items vom Typ '{type}' nicht möglich.", "DS4.ErrorWrongItemType": "Ein Item vom Type '{expectedType}' wurde erwartet aber das Item '{name}' ({id}) ist vom Typ '{actualType}'.", "DS4.ErrorUnexpectedAttackType": "Unerwartete Angriffsart '{actualType}', erwartete Angriffsarten: {expectedTypes}", - "DS4.ErrorUnexpectedAttribute": "Unerwartetes Attribut '{actualType}', erwartete Attribute: {expectedTypes}", - "DS4.ErrorUnexpectedTrait": "Unerwartete Eigenschaft '{actualType}', erwartete Eigenschaften: {expectedTypes}", + "DS4.ErrorUnexpectedAttribute": "Unerwartetes Attribut '{actualAttribute}', erwartete Attribute: {expectedTypes}", + "DS4.ErrorUnexpectedTrait": "Unerwartete Eigenschaft '{actualTrait}', erwartete Eigenschaften: {expectedTypes}", "DS4.ErrorCanvasIsNotInitialized": "Canvas ist noch nicht initialisiert.", "DS4.ErrorCannotDragMissingCheck": "Die Probe '{check}' per Drag & Drop zu ziehen ist nicht möglich, denn sie existiert nicht.", "DS4.WarningItemMustBeEquippedToBeRolled": "Um für das Item '{name}' ({id}) vom Typ '{type}' zu würfeln, muss es ausgerüstet sein.", diff --git a/src/lang/en.json b/src/lang/en.json index 903b13be..cdaf2544 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -207,8 +207,8 @@ "DS4.ErrorRollingForItemTypeNotPossible": "Rolling is not possible for items of type '{type}'.", "DS4.ErrorWrongItemType": "Expected an item of type '{expectedType}' but item '{name}' ({id}) is of type '{actualType}'.", "DS4.ErrorUnexpectedAttackType": "Unexpected attack type '{actualType}', expected it to be one of: {expectedTypes}", - "DS4.ErrorUnexpectedAttribute": "Unexpected attribute '{actualType}', expected it to be one of: {expectedTypes}", - "DS4.ErrorUnexpectedTrait": "Unexpected trait '{actualType}', expected it to be one of: {expectedTypes}", + "DS4.ErrorUnexpectedAttribute": "Unexpected attribute '{actualAttribute}', expected it to be one of: {expectedTypes}", + "DS4.ErrorUnexpectedTrait": "Unexpected trait '{actualTrait}', expected it to be one of: {expectedTypes}", "DS4.ErrorCanvasIsNotInitialized": "Canvas is not initialized yet.", "DS4.ErrorCannotDragMissingCheck": "Trying to drag the check '{check}' but no such check exists.", "DS4.WarningItemMustBeEquippedToBeRolled": "To roll for item '{name}' ({id}) of type '{type}', it needs to be equipped.", diff --git a/src/module/actor/actor-data.ts b/src/module/actor/actor-data.ts index c0684899..9373b5f3 100644 --- a/src/module/actor/actor-data.ts +++ b/src/module/actor/actor-data.ts @@ -21,30 +21,32 @@ interface DS4ActorDataDataBase { combatValues: DS4ActorDataDataCombatValues; } -interface DS4ActorDataDataAttributes { - body: ModifiableDataBase; - mobility: ModifiableDataBase; - mind: ModifiableDataBase; +type DS4ActorDataDataAttributes = { [Key in keyof typeof DS4.i18n.attributes]: ModifiableDataBase }; + +type Attribute = keyof DS4ActorDataDataAttributes; + +export function isAttribute(value: unknown): value is Attribute { + return (Object.keys(DS4.i18n.attributes) as Array).includes(value); } -interface DS4ActorDataDataTraits { - strength: ModifiableDataBase; - constitution: ModifiableDataBase; - agility: ModifiableDataBase; - dexterity: ModifiableDataBase; - intellect: ModifiableDataBase; - aura: ModifiableDataBase; +type DS4ActorDataDataTraits = { [Key in keyof typeof DS4.i18n.traits]: ModifiableDataBase }; + +type Trait = keyof DS4ActorDataDataTraits; + +export function isTrait(value: unknown): value is Trait { + return (Object.keys(DS4.i18n.traits) as Array).includes(value); } -interface DS4ActorDataDataCombatValues { - hitPoints: ResourceData; - defense: ModifiableData; - initiative: ModifiableData; - movement: ModifiableData; - meleeAttack: ModifiableData; - rangedAttack: ModifiableData; - spellcasting: ModifiableData; - targetedSpellcasting: ModifiableData; +type DS4ActorDataDataCombatValues = { + [Key in keyof typeof DS4.i18n.combatValues]: Key extends "hitPoints" + ? ResourceData + : ModifiableData; +}; + +type CombatValue = keyof DS4ActorDataDataCombatValues; + +export function isCombatValue(value: string): value is CombatValue { + return (Object.keys(DS4.i18n.combatValues) as Array).includes(value); } // types diff --git a/src/module/actor/actor-prepared-data.ts b/src/module/actor/actor-prepared-data.ts index 653d023d..845da924 100644 --- a/src/module/actor/actor-prepared-data.ts +++ b/src/module/actor/actor-prepared-data.ts @@ -26,31 +26,17 @@ interface DS4ActorPreparedDataDataBase { checks: DS4ActorPreparedDataDataChecks; } -interface DS4ActorPreparedDataDataAttributes { - body: ModifiableDataBaseTotal; - mobility: ModifiableDataBaseTotal; - mind: ModifiableDataBaseTotal; -} +type DS4ActorPreparedDataDataAttributes = { + [Key in keyof typeof DS4.i18n.attributes]: ModifiableDataBaseTotal; +}; -interface DS4ActorPreparedDataDataTraits { - strength: ModifiableDataBaseTotal; - constitution: ModifiableDataBaseTotal; - agility: ModifiableDataBaseTotal; - dexterity: ModifiableDataBaseTotal; - intellect: ModifiableDataBaseTotal; - aura: ModifiableDataBaseTotal; -} +type DS4ActorPreparedDataDataTraits = { [Key in keyof typeof DS4.i18n.traits]: ModifiableDataBaseTotal }; -interface DS4ActorPreparedDataDataCombatValues { - hitPoints: ResourceDataBaseTotalMax; - defense: ModifiableDataBaseTotal; - initiative: ModifiableDataBaseTotal; - movement: ModifiableDataBaseTotal; - meleeAttack: ModifiableDataBaseTotal; - rangedAttack: ModifiableDataBaseTotal; - spellcasting: ModifiableDataBaseTotal; - targetedSpellcasting: ModifiableDataBaseTotal; -} +type DS4ActorPreparedDataDataCombatValues = { + [Key in keyof typeof DS4.i18n.combatValues]: Key extends "hitPoints" + ? ResourceDataBaseTotalMax + : ModifiableDataBaseTotal; +}; interface DS4ActorPreparedDataDataRolling { maximumCoupResult: number; diff --git a/src/module/actor/actor.ts b/src/module/actor/actor.ts index 00339761..a887838c 100644 --- a/src/module/actor/actor.ts +++ b/src/module/actor/actor.ts @@ -3,7 +3,7 @@ import { DS4 } from "../config"; import { DS4Item } from "../item/item"; import { ItemType } from "../item/item-data"; import { createCheckRoll } from "../rolls/check-factory"; -import { DS4ActorData } from "./actor-data"; +import { DS4ActorData, isAttribute, isTrait } from "./actor-data"; import { Check, DS4ActorPreparedData } from "./actor-prepared-data"; /** @@ -336,7 +336,7 @@ export class DS4Actor extends Actor label: game.i18n.localize("DS4.GenericOkButton"), callback: (html) => { const selectedAttribute = html.find(`#${attributeIdentifier}`).val(); - if (selectedAttribute !== "body" && selectedAttribute !== "mobility" && selectedAttribute !== "mind") { + if (!isAttribute(selectedAttribute)) { throw new Error( game.i18n.format("DS4.ErrorUnexpectedAttribute", { actualAttribute: selectedAttribute, @@ -345,17 +345,10 @@ export class DS4Actor extends Actor ); } const selectedTrait = html.find(`#${traitIdentifier}`).val(); - if ( - selectedTrait !== "strength" && - selectedTrait !== "constitution" && - selectedTrait !== "agility" && - selectedTrait !== "dexterity" && - selectedTrait !== "intellect" && - selectedTrait !== "aura" - ) { + if (!isTrait(selectedTrait)) { throw new Error( game.i18n.format("DS4.ErrorUnexpectedTrait", { - actualAttribute: selectedAttribute, + actualTrait: selectedTrait, expectedTypes: "'strength', 'constitution', 'agility', 'dexterity', 'intellect', 'aura'", }), );