Skip to content

Commit

Permalink
Clean up no longer needed checks for RuleElements
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoschcau committed Jan 7, 2022
1 parent d8ea3ed commit f015bf7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 89 deletions.
31 changes: 15 additions & 16 deletions src/main/typescript/actor/wvActor.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
// vim: foldmethod=marker
import type { ConstructorDataType } from "@league-of-foundry-developers/foundry-vtt-types/src/types/helperTypes";
import type { PlayerCharacterDataSource } from "./../data/actor/source.js";
import type { Resource } from "../data/foundryCommon.js";
import {
CONSTANTS,
isSkillName,
isSpecialName,
SkillName,
SkillNames,
SpecialName,
SpecialNames
} from "../constants.js";
import {
Criticals,
Resistances,
SecondaryStatistics,
Skill,
Skills
} from "../data/actor/properties.js";
import {
CONSTANTS,
SkillName,
SkillNames,
SpecialName,
SpecialNames,
isSpecialName,
isSkillName
} from "../constants.js";
import Formulator from "../formulator.js";
import WvI18n from "../wvI18n.js";
import RuleElement from "../ruleEngine/ruleElement.js";
import type { Resource } from "../data/foundryCommon.js";
import type DragData from "../dragData.js";
import Formulator from "../formulator.js";
import {
getSkillMaxPoints,
getSkillMinPoints,
getSpecialMaxPoints,
getSpecialMinPoints
} from "../helpers.js";
import { getGroundMoveRange, getGroundSprintMoveRange } from "../movement.js";
import type RuleElement from "../ruleEngine/ruleElement.js";
import WvI18n from "../wvI18n.js";
import type { PlayerCharacterDataSource } from "./../data/actor/source.js";

/* eslint-disable @typescript-eslint/member-ordering */

Expand Down Expand Up @@ -124,8 +124,7 @@ export default class WvActor extends Actor {

this.data.items.forEach((item) => {
item.data.data.rules.elements.forEach((rule) => {
if (rule instanceof RuleElement && rule.target === "actor")
rules.push(rule);
if (rule.target === "actor") rules.push(rule);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/main/typescript/applications/item/wvItemSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export default class WvItemSheet extends ItemSheet {
private handleRuleElementSchemaErrors(
index: number,
errors: DefinedError[],
ruleSource: re.UnknownRuleElementSource
ruleSource: object
): void {
const messages = errors.map(this.translateError);
this.ruleElementSchemaErrors[index] = [messages, ruleSource];
Expand Down
6 changes: 3 additions & 3 deletions src/main/typescript/item/wvItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DocumentModificationOptions } from "@league-of-foundry-developers/
import type { ItemDataConstructorData } from "@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/data/data.mjs/itemData";
import type { BaseUser } from "@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/documents.mjs";
import { getGame } from "../foundryHelpers.js";
import RuleElement from "../ruleEngine/ruleElement.js";
import type RuleElement from "../ruleEngine/ruleElement.js";
import { RULE_ELEMENTS } from "../ruleEngine/ruleElements.js";
import type RuleElementSource from "../ruleEngine/ruleElementSource.js";
import { isMappedItemType, TYPE_CONSTRUCTORS } from "../typeMappings.js";
Expand Down Expand Up @@ -59,8 +59,8 @@ export default class WvItem extends Item {

/** Get RuleElements that apply to this Item. */
get applicableRuleElements(): RuleElement[] {
return this.data.data.rules.elements.flatMap((rule) =>
rule instanceof RuleElement && rule.target === "item" ? [rule] : []
return this.data.data.rules.elements.filter(
(rule) => rule.target === "item"
);
}

Expand Down
45 changes: 6 additions & 39 deletions src/main/typescript/ruleEngine/ruleElement.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type WvItem from "../item/wvItem.js";
import type RuleElementSource from "./ruleElementSource.js";
import RuleElementMessage from "./ruleElementMessage.js";
import WrongSelectedTypeMessage from "./messages/wrongSelectedTypeMessage.js";
import ChangedTypeMessage from "./messages/changedTypeMessage.js";
import NotMatchingSelectorMessage from "./messages/notMatchingSelectorMessage.js";
import WrongSelectedTypeMessage from "./messages/wrongSelectedTypeMessage.js";
import WrongValueTypeMessage from "./messages/wrongValueTypeMessage.js";
import ChangedTypeMessage from "./messages/changedTypeMessage.js";
import RuleElementMessage from "./ruleElementMessage.js";
import type RuleElementSource from "./ruleElementSource.js";
import type { RuleElementTarget } from "./ruleElementSource.js";

/**
* A rule engine element, allowing the modification of a data point, specified
* by a selector and a given value. How the data point is modified depends on
* the type of the element.
*/
export default abstract class RuleElement implements RuleElementLike {
export default abstract class RuleElement {
/**
* Create a RuleElement from the given data and owning item.
* @param source - the source data for the RuleElement
Expand Down Expand Up @@ -235,37 +236,3 @@ export function hasErrors(messages: RuleElementMessage[]): boolean {
export function hasWarnings(messages: RuleElementMessage[]): boolean {
return messages.some((message) => message.isWarning());
}

/** The valid values of a RuleElement target property */
export const RULE_ELEMENT_TARGETS = ["item", "actor"] as const;

/** The type of the valid values for a RuleElement target property */
export type RuleElementTarget = typeof RULE_ELEMENT_TARGETS[number];

/**
* A custom typeguard to check whether a given RuleElement target string has a
* valid value.
* @param target - the target string to check
* @returns whether the target string is valid
*/
export function isValidTarget(target?: string): target is RuleElementTarget {
return RULE_ELEMENT_TARGETS.includes(target as RuleElementTarget);
}

/**
* An unknown version of the RuleElement raw data layout, where each key might
* not exist and is of an unknown type.
*/
export type UnknownRuleElementSource = {
[K in keyof RuleElementSource]?: unknown;
};

/**
* An interface that can be used to pass data around, when no RuleElement could
* be created.
*/
export interface RuleElementLike {
item: WvItem;
messages: RuleElementMessage[];
source: UnknownRuleElementSource;
}
15 changes: 10 additions & 5 deletions src/main/typescript/ruleEngine/ruleElementSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { JSONSchemaType } from "ajv";
import type { RuleElementTarget } from "./ruleElement.js";
import type { MappedRuleElementId } from "./ruleElements.js";
import { RuleElementId, RULE_ELEMENTS } from "./ruleElements.js";

/** The RuleElement raw data layout */
export default interface RuleElementSource {
Expand All @@ -22,12 +21,18 @@ export default interface RuleElementSource {
target: RuleElementTarget;

/** The type identifier of the element. */
type: MappedRuleElementId;
type: RuleElementId;

/** The value of the element */
value: boolean | number | string;
}

/** The valid values of a RuleElement target property */
export const RULE_ELEMENT_TARGETS = ["item", "actor"] as const;

/** The type of the valid values for a RuleElement target property */
export type RuleElementTarget = typeof RULE_ELEMENT_TARGETS[number];

/** A JSON schema for RuleElementSource objects */
export const JSON_SCHEMA: JSONSchemaType<RuleElementSource> = {
description: "The RuleElement raw data layout",
Expand Down Expand Up @@ -61,13 +66,13 @@ export const JSON_SCHEMA: JSONSchemaType<RuleElementSource> = {
description:
"Whether the rule element applies to its own item or the owning actor",
type: "string",
enum: ["actor", "item"],
enum: RULE_ELEMENT_TARGETS,
default: "item"
},
type: {
description: "The identifier of the type or rule element to use",
type: "string",
enum: ["WV.RuleElement.FlatModifier", "WV.RuleElement.ReplaceValue"],
enum: Object.keys(RULE_ELEMENTS) as RuleElementId[],
default: "WV.RuleElement.FlatModifier"
},
value: {
Expand Down
28 changes: 3 additions & 25 deletions src/main/typescript/ruleEngine/ruleElements.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import FlatModifier from "./ruleElements/flatModifier.js";
import ReplaceValue from "./ruleElements/replaceValue.js";

/** RuleElement identifier strings */
export const RULE_ELEMENT_IDS = {
FLAT_MODIFIER: "WV.RuleElement.FlatModifier",
REPLACE_VALUE: "WV.RuleElement.ReplaceValue"
} as const;

/** A union type of RuleElement ID strings */
export type RuleElementId = ValueOf<typeof RULE_ELEMENT_IDS>;

/** A mapping of RuleElement IDs to RuleElement constructors. */
export const RULE_ELEMENTS = {
[RULE_ELEMENT_IDS.FLAT_MODIFIER]: FlatModifier,
[RULE_ELEMENT_IDS.REPLACE_VALUE]: ReplaceValue
["WV.RuleElement.FlatModifier"]: FlatModifier,
["WV.RuleElement.ReplaceValue"]: ReplaceValue
} as const;

export type MappedRuleElementId = keyof typeof RULE_ELEMENTS;

/**
* A custom typeguard to check whether a given RuleElement type string is one of
* the mapped RuleElement types.
* @param type - the type string to check
* @returns whether the type string is one of the RuleElement types
*/
export function isMappedRuleElementType(
type?: string
): type is MappedRuleElementId {
if (typeof type !== "string") return false;
return Object.keys(RULE_ELEMENTS).includes(type);
}
export type RuleElementId = keyof typeof RULE_ELEMENTS;

0 comments on commit f015bf7

Please sign in to comment.