tf2-item-format
Version:
Format's TF2 items like backpack.tf does
143 lines • 5.04 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.getTextureExceptions = getTextureExceptions;
exports.isTextureException = isTextureException;
exports.findTextureExceptions = findTextureExceptions;
const isNumber_1 = __importDefault(require("../util/isNumber"));
const schemaCache_1 = require("./schemaCache");
const SCHEMA_CACHE_TEXTURE_KEY = 'textureExceptions';
/**
* Iterates over effects object to get matching effect.
*/
// eslint-disable-next-line consistent-return
function default_1(name, attributes) {
const textures = attributes.schema.getTextures();
const textureKeys = Object.keys(textures);
for (let i = 0; i < textureKeys.length; i++) {
const texture = textureKeys[i];
if ((0, isNumber_1.default)(texture)) {
continue;
}
if (!name.includes(`${texture} `)) {
continue;
}
const [exception, replacement] = isTextureException(attributes.schema, name, texture, !!attributes.wear);
if (exception) {
if (replacement) {
return replacement;
}
else {
continue;
}
}
return texture;
}
}
function getTextureExceptions(schema) {
let exceptions = schemaCache_1.cache.get(schema, SCHEMA_CACHE_TEXTURE_KEY);
if (exceptions) {
return exceptions;
}
exceptions = findTextureExceptions(schema);
schemaCache_1.cache.save(schema, SCHEMA_CACHE_TEXTURE_KEY, exceptions);
return exceptions;
}
function isTextureException(schema, name, texture, hasWear) {
const exceptions = getTextureExceptions(schema);
if (exceptions.texture[texture]) {
for (const overlappingTexture of exceptions.texture[texture]) {
if (name.includes(`${overlappingTexture} `)) {
const [canUseOverlap, _] = isTextureException(schema, overlappingTexture, name, hasWear);
if (canUseOverlap) {
return [true, null];
}
else {
return [true, overlappingTexture];
}
}
}
}
if (exceptions.effect[texture] && !hasWear) {
for (const effect of exceptions.effect[texture]) {
if (name.includes(`${effect} `)) {
return [true, null];
}
}
}
if (exceptions.item[texture]) {
for (const item of exceptions.item[texture]) {
if (name.includes(item)) {
return [!name.includes(`${texture} ${texture} `), null];
}
}
}
return [false, null];
}
function findTextureExceptions(schema) {
const items = schema.getItems();
const textures = schema.getTextures();
const effects = schema.getEffects();
const itemTextureExceptions = {};
for (const texture of Object.keys(textures)) {
if ((0, isNumber_1.default)(texture)) {
continue;
}
const itemExceptions = items
.filter((i) => i.item_name.includes(`${texture} `))
.map((i) => i.item_name);
if (itemExceptions.length > 0) {
itemTextureExceptions[texture] = itemExceptions;
}
}
const textureTextureExceptions = {};
for (const texture1 of Object.keys(textures)) {
if ((0, isNumber_1.default)(texture1)) {
continue;
}
for (const texture2 of Object.keys(textures)) {
if ((0, isNumber_1.default)(texture2)) {
continue;
}
// It has to be distinct word in said texture
if (texture2.startsWith(`${texture1} `) ||
texture2.includes(` ${texture1} `) ||
texture2.endsWith(` ${texture1}`)) {
if (textureTextureExceptions[texture1]) {
textureTextureExceptions[texture1].push(texture2);
}
else {
textureTextureExceptions[texture1] = [texture2];
}
}
}
}
const effectTextureExceptions = {};
for (const texture of Object.keys(textures)) {
if ((0, isNumber_1.default)(texture)) {
continue;
}
for (const effect of Object.keys(effects)) {
if ((0, isNumber_1.default)(texture)) {
continue;
}
if (effect.includes(texture)) {
if (effectTextureExceptions[texture]) {
effectTextureExceptions[texture].push(effect);
}
else {
effectTextureExceptions[texture] = [effect];
}
}
}
}
return {
effect: effectTextureExceptions,
texture: textureTextureExceptions,
item: itemTextureExceptions,
};
}
//# sourceMappingURL=getTexture.js.map