tf2-item-format
Version:
Format's TF2 items like backpack.tf does
144 lines • 5.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
exports.getEffectExceptions = getEffectExceptions;
exports.isEffectException = isEffectException;
exports.findEffectExceptions = findEffectExceptions;
const schemaCache_1 = require("../../shared/schemaCache");
const isNumber_1 = __importDefault(require("../../util/isNumber"));
const SCHEMA_CACHE_EFFECT_KEY = 'effectExceptions';
/**
* Iterates over effects object to get matching effect.
*/
// eslint-disable-next-line consistent-return
function default_1(name, attributes) {
const effects = attributes.schema.getEffects();
const effectsKeys = Object.keys(effects);
for (let i = 0; i < effectsKeys.length; i++) {
let effect = effectsKeys[i];
if ((0, isNumber_1.default)(effect)) {
continue;
}
if (!name.includes(`${effect} `)) {
continue;
}
const [exception, replacement] = isEffectException(attributes.schema, effect, name, !!attributes.wear);
if (exception) {
if (replacement) {
return replacement;
}
else {
continue;
}
}
return effect;
}
}
function getEffectExceptions(schema) {
let exceptions = schemaCache_1.cache.get(schema, SCHEMA_CACHE_EFFECT_KEY);
if (exceptions) {
return exceptions;
}
exceptions = findEffectExceptions(schema);
schemaCache_1.cache.save(schema, SCHEMA_CACHE_EFFECT_KEY, exceptions);
return exceptions;
}
function isEffectException(schema, effect, name, hasWear) {
const exceptions = getEffectExceptions(schema);
if (exceptions.effect[effect]) {
for (const overlappingEffect of exceptions.effect[effect]) {
if (name.includes(`${overlappingEffect} `)) {
const [canUseOverlap, _] = isEffectException(schema, overlappingEffect, name, hasWear);
if (canUseOverlap) {
return [true, null];
}
else {
return [true, overlappingEffect];
}
}
}
}
if (exceptions.texture[effect] && !!hasWear) {
for (const texture of exceptions.texture[effect]) {
if (name.includes(`${texture} `)) {
return [true, null];
}
}
}
if (exceptions.item[effect]) {
for (const item of exceptions.item[effect]) {
if (name.includes(item)) {
// TODO: Maybe rather look for multiple occurences of `${effect} `
return [!name.includes(`${effect} ${effect} `), null];
}
}
}
return [false, null];
}
function findEffectExceptions(schema) {
const items = schema.getItems();
const textures = schema.getTextures();
const effects = schema.getEffects();
const itemEffectExceptions = {};
for (const effect of Object.keys(effects)) {
if ((0, isNumber_1.default)(effect)) {
continue;
}
const itemExceptions = items
.filter((i) => i.item_name.includes(`${effect} `))
.map((i) => i.item_name);
if (itemExceptions.length > 0) {
itemEffectExceptions[effect] = itemExceptions;
}
}
const effectEffectExceptions = {};
for (const effect1 of Object.keys(effects)) {
if ((0, isNumber_1.default)(effect1)) {
continue;
}
for (const effect2 of Object.keys(effects)) {
if ((0, isNumber_1.default)(effect2)) {
continue;
}
// It has to be distinct word in said effect
if (effect2.startsWith(`${effect1} `) ||
effect2.includes(` ${effect1} `) ||
effect2.endsWith(` ${effect1}`)) {
if (effectEffectExceptions[effect1]) {
effectEffectExceptions[effect1].push(effect2);
}
else {
effectEffectExceptions[effect1] = [effect2];
}
}
}
}
const textureEffectExceptions = {};
for (const effect of Object.keys(effects)) {
if ((0, isNumber_1.default)(effect)) {
continue;
}
for (const texture of Object.keys(textures)) {
if ((0, isNumber_1.default)(texture)) {
continue;
}
if (texture.includes(effect)) {
if (textureEffectExceptions[effect]) {
textureEffectExceptions[effect].push(texture);
}
else {
textureEffectExceptions[effect] = [texture];
}
}
}
}
return {
item: itemEffectExceptions,
effect: effectEffectExceptions,
texture: textureEffectExceptions,
};
}
//# sourceMappingURL=getEffect.js.map