Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions src/parseString/Attributes/getEffect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cache } from '../../shared/schemaCache';
import { ISchema } from '../../types/schema';
import isNumber from '../../util/isNumber';

import Attributes from '../Attributes';

Expand All @@ -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;
}
Expand Down Expand Up @@ -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<string, string[]> = {};
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);
Expand All @@ -130,15 +121,7 @@ export function findEffectExceptions(schema: ISchema): SchemaEffectExceptions {

const effectEffectExceptions: Record<string, string[]> = {};
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} `) ||
Expand All @@ -156,15 +139,7 @@ export function findEffectExceptions(schema: ISchema): SchemaEffectExceptions {

const textureEffectExceptions: Record<string, string[]> = {};
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);
Expand Down
4 changes: 2 additions & 2 deletions src/parseString/Attributes/getQuality/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/parseString/Attributes/getUsableItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
32 changes: 3 additions & 29 deletions src/shared/getTexture.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import isNumber from '../util/isNumber';

import { ISchema } from '../types/schema';
import { cache } from './schemaCache';

Expand All @@ -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;
}
Expand Down Expand Up @@ -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<string, string[]> = {};
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);
Expand All @@ -134,15 +124,7 @@ export function findTextureExceptions(

const textureTextureExceptions: Record<string, string[]> = {};
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} `) ||
Expand All @@ -160,15 +142,7 @@ export function findTextureExceptions(

const effectTextureExceptions: Record<string, string[]> = {};
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);
Expand Down
38 changes: 37 additions & 1 deletion src/static/schema.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading