diff --git a/src/parseString/Attributes/getEffect.ts b/src/parseString/Attributes/getEffect.ts index f09707f..01a9590 100644 --- a/src/parseString/Attributes/getEffect.ts +++ b/src/parseString/Attributes/getEffect.ts @@ -1,6 +1,5 @@ import { cache } from '../../shared/schemaCache'; import { ISchema } from '../../types/schema'; -import isNumber from '../../util/isNumber'; import Attributes from '../Attributes'; @@ -17,14 +16,10 @@ const SCHEMA_CACHE_EFFECT_KEY = 'effectExceptions'; */ // eslint-disable-next-line consistent-return export default function (name: string, attributes: Attributes): string | void { - const effects = attributes.schema.getEffects(); + const effects = attributes.schema.getEffectNames(); const effectsKeys = Object.keys(effects); for (let i = 0; i < effectsKeys.length; i++) { let effect: string | number = effectsKeys[i]; - if (isNumber(effect)) { - continue; - } - if (!name.includes(`${effect} `)) { continue; } @@ -110,15 +105,11 @@ export function isEffectException( export function findEffectExceptions(schema: ISchema): SchemaEffectExceptions { const items = schema.getItems(); - const textures = schema.getTextures(); - const effects = schema.getEffects(); + const textures = schema.getTextureNames(); + const effects = schema.getEffectNames(); const itemEffectExceptions: Record = {}; for (const effect of Object.keys(effects)) { - if (isNumber(effect)) { - continue; - } - const itemExceptions = items .filter((i) => i.item_name.includes(`${effect} `)) .map((i) => i.item_name); @@ -130,15 +121,7 @@ export function findEffectExceptions(schema: ISchema): SchemaEffectExceptions { const effectEffectExceptions: Record = {}; for (const effect1 of Object.keys(effects)) { - if (isNumber(effect1)) { - continue; - } - for (const effect2 of Object.keys(effects)) { - if (isNumber(effect2)) { - continue; - } - // It has to be distinct word in said effect if ( effect2.startsWith(`${effect1} `) || @@ -156,15 +139,7 @@ export function findEffectExceptions(schema: ISchema): SchemaEffectExceptions { const textureEffectExceptions: Record = {}; for (const effect of Object.keys(effects)) { - if (isNumber(effect)) { - continue; - } - for (const texture of Object.keys(textures)) { - if (isNumber(texture)) { - continue; - } - if (texture.includes(effect)) { if (textureEffectExceptions[effect]) { textureEffectExceptions[effect].push(texture); diff --git a/src/parseString/Attributes/getQuality/exceptions.ts b/src/parseString/Attributes/getQuality/exceptions.ts index 265a203..a063f2a 100644 --- a/src/parseString/Attributes/getQuality/exceptions.ts +++ b/src/parseString/Attributes/getQuality/exceptions.ts @@ -28,8 +28,8 @@ export function getQualityExceptions(schema: ISchema, quality: string): string[] export function findQualityExceptions(schema: ISchema, quality: string): string[] { const items = schema.getItems(); - const effects = schema.getEffects(); - const textures = schema.getTextures(); + const effects = schema.getEffectNames(); + const textures = schema.getTextureNames(); const effectExceptionsForQuality = Object.keys(effects).filter((effect) => effect.includes(quality) diff --git a/src/parseString/Attributes/getUsableItem.ts b/src/parseString/Attributes/getUsableItem.ts index 10a1359..483808d 100644 --- a/src/parseString/Attributes/getUsableItem.ts +++ b/src/parseString/Attributes/getUsableItem.ts @@ -101,8 +101,8 @@ export function getKitExceptions(schema: ISchema): string[] { export function findKitExceptions(schema: ISchema) { const items = schema.getItems(); - const textures = schema.getTextures(); - const effects = schema.getEffects(); + const textures = schema.getTextureNames(); + const effects = schema.getEffectNames(); const effectKitExceptions = Object.keys(effects).filter((effect) => effect.includes('Kit') diff --git a/src/shared/getTexture.ts b/src/shared/getTexture.ts index 6a55d2f..312132b 100644 --- a/src/shared/getTexture.ts +++ b/src/shared/getTexture.ts @@ -1,5 +1,3 @@ -import isNumber from '../util/isNumber'; - import { ISchema } from '../types/schema'; import { cache } from './schemaCache'; @@ -19,14 +17,10 @@ export default function ( name: string, attributes: { wear: any | null; schema: ISchema } ): string | void { - const textures = attributes.schema.getTextures(); + const textures = attributes.schema.getTextureNames(); const textureKeys = Object.keys(textures); for (let i = 0; i < textureKeys.length; i++) { const texture: number | string = textureKeys[i]; - if (isNumber(texture)) { - continue; - } - if (!name.includes(`${texture} `)) { continue; } @@ -114,15 +108,11 @@ export function findTextureExceptions( schema: ISchema ): SchemaTextureExceptions { const items = schema.getItems(); - const textures = schema.getTextures(); - const effects = schema.getEffects(); + const textures = schema.getTextureNames(); + const effects = schema.getEffectNames(); const itemTextureExceptions: Record = {}; for (const texture of Object.keys(textures)) { - if (isNumber(texture)) { - continue; - } - const itemExceptions = items .filter((i) => i.item_name.includes(`${texture} `)) .map((i) => i.item_name); @@ -134,15 +124,7 @@ export function findTextureExceptions( const textureTextureExceptions: Record = {}; for (const texture1 of Object.keys(textures)) { - if (isNumber(texture1)) { - continue; - } - for (const texture2 of Object.keys(textures)) { - if (isNumber(texture2)) { - continue; - } - // It has to be distinct word in said texture if ( texture2.startsWith(`${texture1} `) || @@ -160,15 +142,7 @@ export function findTextureExceptions( const effectTextureExceptions: Record = {}; for (const texture of Object.keys(textures)) { - if (isNumber(texture)) { - continue; - } - for (const effect of Object.keys(effects)) { - if (isNumber(texture)) { - continue; - } - if (effect.includes(texture)) { if (effectTextureExceptions[texture]) { effectTextureExceptions[texture].push(effect); diff --git a/src/static/schema.ts b/src/static/schema.ts index 7fde0bd..7060089 100644 --- a/src/static/schema.ts +++ b/src/static/schema.ts @@ -1,7 +1,7 @@ import { requireStatic, SchemaEnum, DefindexToName } from 'tf2-static-schema'; import isNumber from '../util/isNumber'; -import { ISchema, ItemsGame, SchemaItem } from '../types/schema'; +import { ISchema, ItemsGame, NameToDefindex, SchemaItem } from '../types/schema'; const DEFINDEXES: { [name: string]: number } = { // Local naming @@ -79,9 +79,11 @@ const NAMES: { [defindex: number]: string } = { export class Schema implements ISchema { public effects!: SchemaEnum; + protected effectNames!: NameToDefindex; public wears!: SchemaEnum; public killstreaks!: SchemaEnum; public textures!: SchemaEnum; + protected textureNames!: NameToDefindex; public itemNames!: DefindexToName; public items!: SchemaItem[]; public qualities!: SchemaEnum; @@ -113,6 +115,15 @@ export class Schema implements ISchema { loadEffects(): void { this.effects = requireStatic('effects') as SchemaEnum; + + this.effectNames = {} + for (const effect of Object.keys(this.effects)) { + if (isNumber(effect)) { + continue; + } + + this.effectNames[effect] = this.effects[effect]; + } } loadWears(): void { @@ -125,6 +136,15 @@ export class Schema implements ISchema { loadTextures(): void { this.textures = requireStatic('paint-kits') as SchemaEnum; + + this.textureNames = {} + for (const effect of Object.keys(this.textures)) { + if (isNumber(effect)) { + continue; + } + + this.textureNames[effect] = this.textures[effect]; + } } loadItemNames(): void { @@ -322,6 +342,22 @@ export class Schema implements ISchema { return correctItem; } + + public getTextureNames(): NameToDefindex { + if (!this.textures) { + this.loadTextures(); + } + + return this.textureNames; + } + + public getEffectNames(): NameToDefindex { + if (!this.effects) { + this.loadEffects(); + } + + return this.effectNames; + } } /** diff --git a/src/types/schema.ts b/src/types/schema.ts index a706480..4327402 100644 --- a/src/types/schema.ts +++ b/src/types/schema.ts @@ -37,6 +37,8 @@ export type ISchema = { getTextures(): SchemaEnum; getEffects(): SchemaEnum; getItems(): SchemaItem[]; + getTextureNames(): NameToDefindex; + getEffectNames(): NameToDefindex; isUniqueHat(nameOrDefindex: string | number): boolean; getCrateNumber(defindex: string | number): number;