UNPKG

k4-utils

Version:

This node package contains a collection of useful functions for working with the SPT framework. Whether you're a seasoned developer or just starting out, these tools will help you get the job done faster and more efficiently. With a focus on simplicity an

372 lines (348 loc) 13.4 kB
let database = require('./database.json'); let all_names_ngrams = require('./all_names_ngrams.json'); function get_trader_buy_coefficients() { let DB = database.tableData.traders let length = Object.keys(DB).length let array = {} for (let key in DB) { let LL = DB[key].base.loyaltyLevels[0].buy_price_coef array[key] = LL } return array } function can_trader_buy(ID,TraderID) { let DB = database.tableData let handbook = DB.templates.handbook const item = handbook.Items.find(item => item.Id === ID); if (item != undefined) { let ParentID = item.ParentId let HigherParent = handbook.Categories.find(item => item.Id === ParentID).ParentId; let Trader = DB.traders[TraderID].base.sell_category if (Trader.includes(ParentID) || Trader.includes(HigherParent)){ return true } else return false } else return "No item" } function get_trader_by_name(Name) { let array = { "prapor": "54cb50c76803fa8b248b4571", "therapist": "54cb57776803fa99248b456e", "fence": "579dc571d53a0658a154fbec", "skier": "58330581ace78e27b8b10cee", "peacekeeper": "5935c25fb3acc3127c3d8cd9", "mechanic": "5a7c2eca46aef81a7ca2145d", "ragman": "5ac3b934156ae10c4430e83c", "jaeger": "5c0647fdd443bc2504c2d371" } let name = Name.toLowerCase() if (array[name]) return array[name] } function get_handbook_parent(ID) { let DB = database.tableData let handbook = DB.templates.handbook const item = handbook.Items.find(item => item.Id === ID); let ParentID = item.ParentId return ParentID } function get_name(ID, mode = 0, Language = "en") { let localesArray = [ "ch", "cz", "en", "es-mx", "es", "fr", "ge", "hu", "it", "jp", "pl", "po", "ru", "sk", "tu" ] if (localesArray.includes(Language) == false && mode == 0) { throw new Error("Language not found, got " + Language + " expected one of " + localesArray.join(",")) } if (mode == 0) { let DB = database.tableData let locales = DB.locales.global[Language] if (locales.templates[ID] == undefined) { let SimilarID = this.find_most_similar(ID, this.get_all_ids()) throw new Error("ID not found in lanaguage: " + Language + ", ID: " + ID + " Did you mean to use the ID: " + SimilarID + " ?") } let locale = locales.templates[ID] return locale.Name } else { if (database.tableData.templates.items[ID] == undefined) { let SimilarID = this.find_most_similar(ID, this.get_all_ids()) throw new Error("ID not found in lanaguage: " + Language + ", ID: " + ID + " Did you mean to use the ID: " + SimilarID + " ?") } let items = database.tableData.templates.items let name = items[ID]._name return name } } function get_short_name(ID, Language = "en") { let localesArray = [ "ch", "cz", "en", "es-mx", "es", "fr", "ge", "hu", "it", "jp", "pl", "po", "ru", "sk", "tu" ] if (localesArray.includes(Language) == false) { throw new Error("Language not found, got " + Language + " expected one of " + localesArray.join(",")) } let DB = database.tableData let locales = DB.locales.global[Language] if (locales.templates[ID] == undefined) { let SimilarID = this.find_most_similar(ID, this.get_all_ids()) throw new Error("ID not found in lanaguage: " + Language + ", ID: " + ID + " Did you mean to use the ID: " + SimilarID + " ?") } let locale = locales.templates[ID] return locale.ShortName } function get_description(ID, Language = "en") { let localesArray = [ "ch", "cz", "en", "es-mx", "es", "fr", "ge", "hu", "it", "jp", "pl", "po", "ru", "sk", "tu" ] if (localesArray.includes(Language) == false) { throw new Error("Language not found, got " + Language + " expected one of " + localesArray.join(",")) } let DB = database.tableData let locales = DB.locales.global[Language] if (locales.templates[ID] == undefined) { let SimilarID = this.find_most_similar(ID, this.get_all_ids()) throw new Error("ID not found in lanaguage: " + Language + ", ID: " + ID + " Did you mean to use the ID: " + SimilarID + " ?") } let locale = locales.templates[ID] return locale.Description } function get_all_ids() { let DB = database.tableData let items = DB.templates.items let IDs = [] for (let ID in items) { IDs.push(ID) } return IDs } function find_most_similar(mainString, stringArray) { let mostSimilar = stringArray[0]; let lowestDistance = this.levenshteinDistance(mainString, mostSimilar); for (let i = 1; i < stringArray.length; i++) { let distance = this.levenshteinDistance(mainString, stringArray[i]); if (distance < lowestDistance) { lowestDistance = distance; mostSimilar = stringArray[i]; } } return mostSimilar; } function levenshteinDistance(a, b) { const distanceMatrix = Array(b.length + 1).fill(null).map(() => Array(a.length + 1).fill(null)); for (let i = 0; i <= a.length; i++) { distanceMatrix[0][i] = i; } for (let j = 0; j <= b.length; j++) { distanceMatrix[j][0] = j; } for (let j = 1; j <= b.length; j++) { for (let i = 1; i <= a.length; i++) { const indicator = a[i - 1] === b[j - 1] ? 0 : 1; distanceMatrix[j][i] = Math.min( distanceMatrix[j][i - 1] + 1, distanceMatrix[j - 1][i] + 1, distanceMatrix[j - 1][i - 1] + indicator, ); } } return distanceMatrix[b.length][a.length]; } function get_handbook_price(id) { let DB = database.tableData let items = DB.templates.items if (items[id] == undefined) { let SimilarID = this.find_most_similar(id, this.get_all_ids()) throw new Error("The ID" + id + "does not exist in items database.") } let pricesArray = DB.templates.handbook["Items"] for (let i = 0; i < pricesArray.length; i++) { if (pricesArray[i].Id == id) { return pricesArray[i].Price } } } function get_flea_price(id) { let DB = database.tableData let items = DB.templates.items if (items[id] == undefined) { let SimilarID = this.find_most_similar(id, this.get_all_ids()) throw new Error("The ID" + id + "does not exist in items database.") } let pricesArray = DB.templates.prices let price = pricesArray[id] return price } function get_parent(id) { let DB = database.tableData let items = DB.templates.items if (items[id] == undefined) { let SimilarID = this.find_most_similar(id, this.get_all_ids()) throw new Error("The ID" + id + "does not exist in items database.") } if (items[id]._parent != undefined && items[id]._parent != "") { return items[id]._parent } else { return "No parent" } } function arrayID_to_name(array) { let DB = database.tableData let items = DB.templates.items if (array == undefined || array.length == 0) { throw new Error("Array is undefined or empty") } let array_names = [] for (let item in array) { if (items[array[item]] == undefined) { let SimilarID = this.find_most_similar(array[item], this.get_all_ids()) throw new Error("ID is undefined, did you mean to use: " + SimilarID + " ?") } array_names.push(items[array[item]]._name) } return array_names } function get_items_with_parent(parent) { let DB = database.tableData let items = DB.templates.items let items_with_parent = [] for (let item in items) { if (items[item]._parent == parent) { items_with_parent.push(item) } } return items_with_parent } function has_props(ID) { let DB = database.tableData let items = DB.templates.items if (items[ID] == undefined) { let SimilarID = this.find_most_similar(ID, this.get_all_ids()) throw new Error("ID is undefined, did you mean to use: " + SimilarID + " ?") } if (items[ID] != undefined && items[ID]._props != undefined && items[ID]._props != "" && Object.entries(items[ID]._props).length > 5) { return true } else { return false } } function get_date_from_id(id) { const timeStamp = parseInt(id.substring(0, 8), 16); const date = new Date(timeStamp * 1000); return date; } function get_all_ids_names() { let DB = database.tableData let items = DB.templates.items let IDs = [] for (let ID in items) { IDs.push(items[ID]._name) } return IDs } function crash() { console.log("-----------Intentional crash------------") process.exit(0) } function isRequired(ID, SlotID) { let items = database.tableData.templates.items let SlotsFromItem = items[ID]._props.Slots for (let i = 0; i < SlotsFromItem.length; i++) { let current = SlotsFromItem[i] let slotID = current._name if (slotID == SlotID) { if (current._required == true) { return true } } } return false } function get_item(ID) { let items = database.tableData.templates.items let item = items[ID] return item } function get_all_ids_names_from_locale(Language = "en") { let localesArray = [ "ch", "cz", "en", "es-mx", "es", "fr", "ge", "hu", "it", "jp", "pl", "po", "ru", "sk", "tu" ] if (localesArray.includes(Language) == false) { throw new Error("Language not found, got " + Language + " expected one of " + localesArray.join(",")) } let DB = database.tableData let locales = DB.locales.global[Language] let IDs = [] let items = DB.templates.items for (let ID in items) { let locale = locales.templates[ID] if (locale == undefined) { continue } let name = IDs.push(locale.Name) } return IDs } function get_id_from_locale_name(name,Language = "en") { let localesArray = [ "ch", "cz", "en", "es-mx", "es", "fr", "ge", "hu", "it", "jp", "pl", "po", "ru", "sk", "tu" ] if (localesArray.includes(Language) == false) { throw new Error("Language not found, got " + Language + " expected one of " + localesArray.join(",")) } let DB = database.tableData let locales = DB.locales.global[Language] let items = DB.templates.items for (let ID in items) { let locale = locales.templates[ID] if (locale == undefined) { continue } if (locale.Name == name) { return ID } } return undefined } function name_to_tokens(name) { return name.split(" ") } function search_by_name(name) { let tokens = name_to_tokens(name) for (let i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].toLowerCase() } let results = new Map(); for (const [key, value] of Object.entries(all_names_ngrams)) { let intersection = new Set([...value.tokens].filter(x => tokens.includes(x))); let parentMatch = value.parent.filter(x => tokens.includes(x)); let attribuitesMatch = value.attribuites.filter(x => tokens.includes(x)); for (let element of parentMatch) { intersection.delete(element); } if (parentMatch.length > 0 || intersection.size > 0) { results.set(key, parentMatch.length + intersection.size + attribuitesMatch.length); } } let sortedResults = [...results.entries()].sort((a, b) => b[1] - a[1]); return sortedResults } module.exports.get_name = get_name; module.exports.get_short_name = get_short_name; module.exports.get_description = get_description; module.exports.get_all_ids = get_all_ids; module.exports.find_most_similar = find_most_similar; module.exports.get_handbook_price = get_handbook_price; module.exports.get_flea_price = get_flea_price; module.exports.get_parent = get_parent; module.exports.arrayID_to_name = arrayID_to_name; module.exports.get_items_with_parent = get_items_with_parent; module.exports.has_props = has_props; module.exports.get_date_from_id = get_date_from_id; module.exports.get_all_ids_names = get_all_ids_names; module.exports.crash = crash; module.exports.isRequired = isRequired; module.exports.get_item = get_item; module.exports.get_all_ids_names_from_locale = get_all_ids_names_from_locale; module.exports.get_id_from_locale_name = get_id_from_locale_name; module.exports.levenshteinDistance = levenshteinDistance; module.exports.search_by_name = search_by_name; module.exports.get_trader_buy_coefficients = get_trader_buy_coefficients; module.exports.can_trader_buy = can_trader_buy; module.exports.get_trader_by_name = get_trader_by_name; module.exports.get_handbook_parent = get_handbook_parent;