Simplify checking if something is a valid attribute / trait / combatValue key

This commit is contained in:
Johannes Loher 2021-05-13 22:03:32 +02:00
parent 24725c15f9
commit 549669e9e2
5 changed files with 39 additions and 58 deletions

View file

@ -207,8 +207,8 @@
"DS4.ErrorRollingForItemTypeNotPossible": "Würfeln ist für Items vom Typ '{type}' nicht möglich.", "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.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.ErrorUnexpectedAttackType": "Unerwartete Angriffsart '{actualType}', erwartete Angriffsarten: {expectedTypes}",
"DS4.ErrorUnexpectedAttribute": "Unerwartetes Attribut '{actualType}', erwartete Attribute: {expectedTypes}", "DS4.ErrorUnexpectedAttribute": "Unerwartetes Attribut '{actualAttribute}', erwartete Attribute: {expectedTypes}",
"DS4.ErrorUnexpectedTrait": "Unerwartete Eigenschaft '{actualType}', erwartete Eigenschaften: {expectedTypes}", "DS4.ErrorUnexpectedTrait": "Unerwartete Eigenschaft '{actualTrait}', erwartete Eigenschaften: {expectedTypes}",
"DS4.ErrorCanvasIsNotInitialized": "Canvas ist noch nicht initialisiert.", "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.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.", "DS4.WarningItemMustBeEquippedToBeRolled": "Um für das Item '{name}' ({id}) vom Typ '{type}' zu würfeln, muss es ausgerüstet sein.",

View file

@ -207,8 +207,8 @@
"DS4.ErrorRollingForItemTypeNotPossible": "Rolling is not possible for items of type '{type}'.", "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.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.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.ErrorUnexpectedAttribute": "Unexpected attribute '{actualAttribute}', expected it to be one of: {expectedTypes}",
"DS4.ErrorUnexpectedTrait": "Unexpected trait '{actualType}', 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.ErrorCanvasIsNotInitialized": "Canvas is not initialized yet.",
"DS4.ErrorCannotDragMissingCheck": "Trying to drag the check '{check}' but no such check exists.", "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.", "DS4.WarningItemMustBeEquippedToBeRolled": "To roll for item '{name}' ({id}) of type '{type}', it needs to be equipped.",

View file

@ -21,30 +21,32 @@ interface DS4ActorDataDataBase {
combatValues: DS4ActorDataDataCombatValues; combatValues: DS4ActorDataDataCombatValues;
} }
interface DS4ActorDataDataAttributes { type DS4ActorDataDataAttributes = { [Key in keyof typeof DS4.i18n.attributes]: ModifiableDataBase<number> };
body: ModifiableDataBase<number>;
mobility: ModifiableDataBase<number>; type Attribute = keyof DS4ActorDataDataAttributes;
mind: ModifiableDataBase<number>;
export function isAttribute(value: unknown): value is Attribute {
return (Object.keys(DS4.i18n.attributes) as Array<unknown>).includes(value);
} }
interface DS4ActorDataDataTraits { type DS4ActorDataDataTraits = { [Key in keyof typeof DS4.i18n.traits]: ModifiableDataBase<number> };
strength: ModifiableDataBase<number>;
constitution: ModifiableDataBase<number>; type Trait = keyof DS4ActorDataDataTraits;
agility: ModifiableDataBase<number>;
dexterity: ModifiableDataBase<number>; export function isTrait(value: unknown): value is Trait {
intellect: ModifiableDataBase<number>; return (Object.keys(DS4.i18n.traits) as Array<unknown>).includes(value);
aura: ModifiableDataBase<number>;
} }
interface DS4ActorDataDataCombatValues { type DS4ActorDataDataCombatValues = {
hitPoints: ResourceData<number>; [Key in keyof typeof DS4.i18n.combatValues]: Key extends "hitPoints"
defense: ModifiableData<number>; ? ResourceData<number>
initiative: ModifiableData<number>; : ModifiableData<number>;
movement: ModifiableData<number>; };
meleeAttack: ModifiableData<number>;
rangedAttack: ModifiableData<number>; type CombatValue = keyof DS4ActorDataDataCombatValues;
spellcasting: ModifiableData<number>;
targetedSpellcasting: ModifiableData<number>; export function isCombatValue(value: string): value is CombatValue {
return (Object.keys(DS4.i18n.combatValues) as Array<unknown>).includes(value);
} }
// types // types

View file

@ -26,31 +26,17 @@ interface DS4ActorPreparedDataDataBase {
checks: DS4ActorPreparedDataDataChecks; checks: DS4ActorPreparedDataDataChecks;
} }
interface DS4ActorPreparedDataDataAttributes { type DS4ActorPreparedDataDataAttributes = {
body: ModifiableDataBaseTotal<number>; [Key in keyof typeof DS4.i18n.attributes]: ModifiableDataBaseTotal<number>;
mobility: ModifiableDataBaseTotal<number>; };
mind: ModifiableDataBaseTotal<number>;
}
interface DS4ActorPreparedDataDataTraits { type DS4ActorPreparedDataDataTraits = { [Key in keyof typeof DS4.i18n.traits]: ModifiableDataBaseTotal<number> };
strength: ModifiableDataBaseTotal<number>;
constitution: ModifiableDataBaseTotal<number>;
agility: ModifiableDataBaseTotal<number>;
dexterity: ModifiableDataBaseTotal<number>;
intellect: ModifiableDataBaseTotal<number>;
aura: ModifiableDataBaseTotal<number>;
}
interface DS4ActorPreparedDataDataCombatValues { type DS4ActorPreparedDataDataCombatValues = {
hitPoints: ResourceDataBaseTotalMax<number>; [Key in keyof typeof DS4.i18n.combatValues]: Key extends "hitPoints"
defense: ModifiableDataBaseTotal<number>; ? ResourceDataBaseTotalMax<number>
initiative: ModifiableDataBaseTotal<number>; : ModifiableDataBaseTotal<number>;
movement: ModifiableDataBaseTotal<number>; };
meleeAttack: ModifiableDataBaseTotal<number>;
rangedAttack: ModifiableDataBaseTotal<number>;
spellcasting: ModifiableDataBaseTotal<number>;
targetedSpellcasting: ModifiableDataBaseTotal<number>;
}
interface DS4ActorPreparedDataDataRolling { interface DS4ActorPreparedDataDataRolling {
maximumCoupResult: number; maximumCoupResult: number;

View file

@ -3,7 +3,7 @@ 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 { createCheckRoll } from "../rolls/check-factory"; 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"; import { Check, DS4ActorPreparedData } from "./actor-prepared-data";
/** /**
@ -336,7 +336,7 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData>
label: game.i18n.localize("DS4.GenericOkButton"), label: game.i18n.localize("DS4.GenericOkButton"),
callback: (html) => { callback: (html) => {
const selectedAttribute = html.find(`#${attributeIdentifier}`).val(); const selectedAttribute = html.find(`#${attributeIdentifier}`).val();
if (selectedAttribute !== "body" && selectedAttribute !== "mobility" && selectedAttribute !== "mind") { if (!isAttribute(selectedAttribute)) {
throw new Error( throw new Error(
game.i18n.format("DS4.ErrorUnexpectedAttribute", { game.i18n.format("DS4.ErrorUnexpectedAttribute", {
actualAttribute: selectedAttribute, actualAttribute: selectedAttribute,
@ -345,17 +345,10 @@ export class DS4Actor extends Actor<DS4ActorData, DS4Item, DS4ActorPreparedData>
); );
} }
const selectedTrait = html.find(`#${traitIdentifier}`).val(); const selectedTrait = html.find(`#${traitIdentifier}`).val();
if ( if (!isTrait(selectedTrait)) {
selectedTrait !== "strength" &&
selectedTrait !== "constitution" &&
selectedTrait !== "agility" &&
selectedTrait !== "dexterity" &&
selectedTrait !== "intellect" &&
selectedTrait !== "aura"
) {
throw new Error( throw new Error(
game.i18n.format("DS4.ErrorUnexpectedTrait", { game.i18n.format("DS4.ErrorUnexpectedTrait", {
actualAttribute: selectedAttribute, actualTrait: selectedTrait,
expectedTypes: "'strength', 'constitution', 'agility', 'dexterity', 'intellect', 'aura'", expectedTypes: "'strength', 'constitution', 'agility', 'dexterity', 'intellect', 'aura'",
}), }),
); );