Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * BattleINF Ultimate Gear Crafter
- * This script is designed to craft the ultimate gear and sell all of the rest.
- *
- * @author Koviko <koviko.net@gmail.com>
- * @version 1.3
- */
- /*global ScriptAPI, API, items*/
- (function (ScriptAPI, API, items) {
- 'use strict';
- // Configuration variables
- var threshold = 15, // Difference in which an item is still good enough to preserve
- minimumArea = 6, // Minimum area of items to preserve
- minimumRarity = 6, // Minimum rarity of items to preserve
- defaultTimeout = 30, // Timeout for notifications
- readyToCraft = ['Bow', '2-handed Sword'], // Weapon types that have been prepped for crafting
- worthlessItems = ['Staff', 'Shield', 'Wand', 'Sword', 'Crossbow'], // Weapon types you no longer need
- showNotifications = [true, false, true]; // [save, sell, craft]
- // Business logic
- (function () {
- var storageKey = 'best',
- data = getFromStorage(storageKey),
- now = Date.now(),
- bestItems = {}, // Best items to be merged into
- worthyItems = [],
- ageTiers = [ // Limits for each age, used to reduce number of server calls
- 0, // Worn: 0 minutes
- 900000, // Fine: 15 minutes
- 1800000, // Refined: 30 minutes
- 3600000, // Aged: 1 hour
- 7200000, // Exotic: 2 hours
- 86400000, // Famous: 1 day
- 172800000, // Master: 2 days
- 345600000, // Heroic: 4 days
- 691200000, // Ancient: 8 days
- 1382400000, // Fabled: 16 days
- 2764800000, // Ascended: 32 days
- 5529600000, // Legendary: 64 days
- 11059200000 // Eternal: 128 days
- ];
- // Create a notification
- function notify(message, timeout) {
- API.notifications.create(message, timeout || defaultTimeout);
- console.log(message);
- }
- // Show a summary of an item
- function summarize(item, showPlus) {
- var parts = [
- '\ud83c\udfe0' + item.mod, // Area
- '\u2606' + item.rarity, // Rarity
- item.name // Name
- ];
- if (showPlus) {
- parts.push('+' + item.plus);
- }
- return parts.join(' ');
- }
- // Lock or unlock an item
- function toggleLock(item, lock) {
- return ScriptAPI._modules.inventory[lock ? 'lock' : 'unlock'](item);
- }
- // Save an item for later use
- function preserve(item) {
- // Lock the precious so no harm can come to it
- if (!item.lock) {
- var primary = getPrimaryStat(item.stats),
- stats = getBaseStats(item, true),
- best = getBestStats(item),
- comparison = stats[primary] + ' / ' + best[primary];
- toggleLock(item, true);
- showNotifications[0] && notify('\ud83d\udc4d ' + summarize(item) + ' (' + comparison + ')'); // thumbs up
- return true;
- }
- return false;
- }
- // Sell an item
- function sell(item, bypassLock) {
- var itemIsLocked = item.lock;
- if (!itemIsLocked || bypassLock) {
- itemIsLocked && toggleLock(item, false);
- ScriptAPI.$userService.sellItem(item, function (data) {
- if (data.money) {
- showNotifications[1] && notify('\ud83d\udcb0 ' + summarize(item)); // money bag
- } else if (itemIsLocked) {
- toggleLock(item, true);
- }
- });
- return true;
- }
- return false;
- }
- // Age an item
- function age(item) {
- // Compare age to the next tier
- if ((now - item.ts) > ageTiers[item.ageLevel + 1]) {
- ScriptAPI.$craftingService.ageUpItem(item);
- return true;
- }
- return false;
- }
- // Craft two items together
- function craft(primary, consumed, strict) {
- if (!strict || (primary.mod === consumed.mod && primary.rarity === consumed.rarity && primary.name === consumed.name)) {
- var primaryWasLocked = primary.lock,
- consumedWasLocked = consumed.lock;
- // Unlock items if needed
- primaryWasLocked && toggleLock(primary, false);
- consumedWasLocked && toggleLock(consumed, false);
- ScriptAPI.$craftingService.craftItems(primary, consumed, function (data) {
- // Lock the item(s) is previously locked
- if (!data.success) {
- primaryWasLocked && toggleLock(primary, true);
- consumedWasLocked && toggleLock(consumed, true);
- showNotifications[2] && notify('\u2716 Merge failed...'); // multiplication x
- } else {
- primaryWasLocked && toggleLock(data.newItem, true);
- showNotifications[2] && notify('\u2714 ' + summarize(data.newItem, true)); // check mark
- }
- });
- showNotifications[2] && notify('\u22EF ' + summarize(primary) + ' \u2190 ' + summarize(consumed)); // midline ellipsis, left arrow
- return true;
- }
- return false;
- }
- // Get data from local storage
- function getFromStorage(key) {
- var data = localStorage.getItem(key);
- return data ? JSON.parse(data) : {};
- }
- // Save data to local storage
- function saveToStorage(key, data) {
- localStorage.setItem(key, JSON.stringify(data));
- }
- // Get the base stats of an item
- function getBaseStats(item, allStats) {
- var stats = item.original ? item.original.stats : item.stats,
- nonZeroStats = {},
- i;
- if (!allStats) {
- for (i in stats) {
- if (stats.hasOwnProperty(i) && stats[i] > 0) {
- nonZeroStats[i] = stats[i];
- }
- }
- stats = nonZeroStats;
- }
- return stats;
- }
- // Get the best stats (based on local storage) for an item type
- function getBestStats(item) {
- var bestStats;
- // Make sure the relevant objects exist
- try {
- bestStats = data[item.mod][item.rarity][item.name];
- } catch (e) {
- data[item.mod] = data[item.mod] || {};
- data[item.mod][item.rarity] = data[item.mod][item.rarity] || {};
- data[item.mod][item.rarity][item.name] = data[item.mod][item.rarity][item.name] || {};
- bestStats = {};
- }
- return bestStats;
- }
- // Get the primary stat of an item type
- function getPrimaryStat(stats) {
- var primary,
- i;
- for (i in stats) {
- if (stats.hasOwnProperty(i) && (!primary || stats[i] > stats[primary])) {
- primary = i;
- }
- }
- return primary;
- }
- // Determine if an item's rarity is high enough to preserve
- function isRareEnough(item) {
- return item.mod >= minimumArea && item.rarity >= minimumRarity;
- }
- // Determine if an item is strong enough to preserve
- function isStrongEnough(item) {
- var stats = getBaseStats(item),
- primary = getPrimaryStat(stats),
- bestStats = getBestStats(item),
- i;
- // Update the storage if the new item is superior
- if (!bestStats.hasOwnProperty(primary) || stats[primary] > bestStats[primary]) {
- for (i in stats) {
- if (stats.hasOwnProperty(i)) {
- data[item.mod][item.rarity][item.name][i] = stats[i];
- }
- }
- bestStats = data[item.mod][item.rarity][item.name];
- saveToStorage(storageKey, data);
- }
- // Determine if the new item is good enough
- return (stats[primary] >= (bestStats[primary] - threshold)) && isRareEnough(item);
- }
- // Determine if an item can still be used in pure crafting
- function isCraftReady(item) {
- var maxPlus = 5 + (5 * item.rarity);
- return item.plus <= (maxPlus - 1);
- }
- // Determine if the item is considered worthyless by the config variables
- function isWorthless(item) {
- return worthlessItems.indexOf(item.name) >= 0;
- }
- // Combination of strength & lack of worthlessness
- function isWorthy(item) {
- return isStrongEnough(item) && !isWorthless(item);
- }
- // Handle incoming items
- items.forEach(function (item) {
- if (isWorthy(item)) {
- preserve(item);
- } else {
- sell(item);
- }
- });
- // Find the best items
- ScriptAPI.$user.inventory.items.forEach(function (item) {
- // Get all items good enough for merging
- if (isWorthy(item)) {
- worthyItems.push(item);
- if (isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
- bestItems[item.name] = item;
- }
- } else {
- sell(item, true);
- }
- });
- // Check if you are currently wearing your best items
- ScriptAPI.$user.character.equipment.forEach(function (item) {
- if (isWorthy(item) && isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
- bestItems[item.name] = item;
- }
- });
- // Craft your ultimate items
- worthyItems.forEach(function (item) {
- // Merge items into the best item
- if (bestItems[item.name] !== item) {
- if (item.plus < 1 && (item.subType === 'ARMOR' || (readyToCraft.indexOf(item.name) > -1))) {
- craft(bestItems[item.name], item, true);
- }
- } else {
- // Only take the time to try to age the best items
- age(item);
- }
- });
- // Age worn items
- ScriptAPI.$user.character.equipment.forEach(age);
- }());
- }(ScriptAPI, API, items));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement