Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize getTexture func #269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 40 additions & 18 deletions src/shared/getTexture.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import isNumber from '../util/isNumber';

import { ISchema } from '../types/schema';
import { SchemaEnum } from 'tf2-static-schema';

const TEXTURE_EXCEPTIONS = [
['Health and Hell', 'Health and Hell (Green)'],
];
const TEXTURE_EXCEPTIONS = [['Health and Hell', 'Health and Hell (Green)']];

//cache the textureKeys since Object.keys() is expensive
let isTextureKeysCached = false;
let textureKeys: string[] = [];

//cache a stripped version of textures
let isTexturesCached = false;
let textures: SchemaEnum = {};

/**
* Iterates over effects object to get matching effect.
Expand All @@ -14,26 +21,41 @@ export default function (
name: string,
attributes: { wear: any | null; schema: ISchema }
): string | void {
const textures = attributes.schema.getTextures();
const textureKeys = Object.keys(textures);
for (let i = 0; i < textureKeys.length; i++) {
const texture: number | string = textureKeys[i];
if (!isTexturesCached) {
const copysrc = attributes.schema.getTextures();

if (
texture === 'Haunted Ghosts' &&
name.includes('Haunted Ghosts') &&
!attributes.wear
) {
continue;
//strip away all properties with numeric keys
//we must also copy this object, as we don't want to modify the original
const keys = Object.keys(copysrc);
for (const key of keys) {
if (!isNumber(key)) {
textures[key] = copysrc[key];
}
}

for (let j = 0; j < TEXTURE_EXCEPTIONS.length; j++) {
const exception = TEXTURE_EXCEPTIONS[j];
if (texture === exception[0] && name.includes(`${exception[1]} `))
return exception[1];
isTexturesCached = true;
}
if (!isTextureKeysCached) {
textureKeys = Object.keys(textures);
isTextureKeysCached = true;
}

for (let j = 0; j < TEXTURE_EXCEPTIONS.length; j++) {
const exception = TEXTURE_EXCEPTIONS[j];
if (name.includes(`${exception[1]} `)) return exception[1];
}

const skipHauntedFlag = name.includes('Haunted Ghosts') && !attributes.wear;

for (let i = 0; i < textureKeys.length; i++) {
const texture = textureKeys[i];

if (!name.includes(`${texture} `)) {
// eslint-disable-next-line no-continue
continue;
}

if (isNumber(texture) || !name.includes(`${texture} `)) {
if (texture === 'Haunted Ghosts' && skipHauntedFlag) {
// eslint-disable-next-line no-continue
continue;
}
Expand Down