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.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.",

View file

@ -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.",

View file

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

View file

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

View file

@ -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<DS4ActorData, DS4Item, DS4ActorPreparedData>
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<DS4ActorData, DS4Item, DS4ActorPreparedData>
);
}
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'",
}),
);