Koviko

BattleINF Ultimate Gear Crafter v1.3.1

Dec 10th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * BattleINF Ultimate Gear Crafter
  3.  * This script is designed to craft the ultimate gear and sell all of the rest.
  4.  *
  5.  * @author Koviko <[email protected]>
  6.  * @version 1.3.1
  7.  */
  8.  
  9. /*global ScriptAPI, API, items*/
  10. (function (ScriptAPI, API, items) {
  11.     'use strict';
  12.  
  13.     // Configuration variables
  14.     var threshold = 5, // Difference in which an item is still good enough to preserve
  15.         minimumArea = 6, // Minimum area of items to preserve
  16.         minimumRarity = 6, // Minimum rarity of items to preserve
  17.         defaultTimeout = 30, // Timeout for notifications
  18.         readyToCraft = ['Bow', '2-handed Sword'], // Weapon types that have been prepped for crafting
  19.         worthlessItems = ['Staff', 'Shield', 'Wand', 'Sword', 'Crossbow'], // Weapon types you no longer need
  20.         showNotifications = [true, false, true]; // [save, sell, craft]
  21.  
  22.     // Business logic
  23.     (function () {
  24.         var storageKey = 'best',
  25.             data = getFromStorage(storageKey),
  26.             now = Date.now(),
  27.             bestItems = {}, // Best items to be merged into
  28.             worthyItems = [],
  29.             ageTiers = [ // Limits for each age, used to reduce number of server calls
  30.                 0, // Worn: 0 minutes
  31.                 900000, // Fine: 15 minutes
  32.                 1800000, // Refined: 30 minutes
  33.                 3600000, // Aged: 1 hour
  34.                 7200000, // Exotic: 2 hours
  35.                 86400000, // Famous: 1 day
  36.                 172800000, // Master: 2 days
  37.                 345600000, // Heroic: 4 days
  38.                 691200000, // Ancient: 8 days
  39.                 1382400000, // Fabled: 16 days
  40.                 2764800000, // Ascended: 32 days
  41.                 5529600000, // Legendary: 64 days
  42.                 11059200000 // Eternal: 128 days
  43.             ];
  44.  
  45.         // Create a notification
  46.         function notify(message, timeout) {
  47.             API.notifications.create(message, timeout || defaultTimeout);
  48.             console.log(message);
  49.         }
  50.  
  51.         // Show a summary of an item
  52.         function summarize(item, showPlus) {
  53.             var parts = [
  54.                 '\ud83c\udfe0' + item.mod, // Area
  55.                 '\u2606' + item.rarity, // Rarity
  56.                 item.name // Name
  57.             ];
  58.  
  59.             if (showPlus) {
  60.                 parts.push('+' + item.plus);
  61.             }
  62.  
  63.             return parts.join(' ');
  64.         }
  65.  
  66.         // Lock or unlock an item
  67.         function toggleLock(item, lock) {
  68.             return ScriptAPI._modules.inventory[lock ? 'lock' : 'unlock'](item);
  69.         }
  70.  
  71.         // Save an item for later use
  72.         function preserve(item) {
  73.             // Lock the precious so no harm can come to it
  74.             if (!item.lock) {
  75.                 var primary = getPrimaryStat(item.stats),
  76.                     stats = getBaseStats(item, true),
  77.                     best = getBestStats(item),
  78.                     comparison = stats[primary] + ' / ' + best[primary];
  79.  
  80.                 toggleLock(item, true);
  81.                 showNotifications[0] && notify('\ud83d\udc4d ' + summarize(item) + ' (' + comparison + ')'); // thumbs up
  82.  
  83.                 return true;
  84.             }
  85.  
  86.             return false;
  87.         }
  88.  
  89.         // Sell an item
  90.         function sell(item, bypassLock) {
  91.             var itemIsLocked = item.lock;
  92.  
  93.             if (!itemIsLocked || bypassLock) {
  94.                 itemIsLocked && toggleLock(item, false);
  95.  
  96.                 ScriptAPI.$userService.sellItem(item, function (data) {
  97.                     if (data.money) {
  98.                         showNotifications[1] && notify('\ud83d\udcb0 ' + summarize(item)); // money bag
  99.                     } else if (itemIsLocked) {
  100.                         toggleLock(item, true);
  101.                     }
  102.                 });
  103.  
  104.                 return true;
  105.             }
  106.  
  107.             return false;
  108.         }
  109.  
  110.         // Age an item
  111.         function age(item) {
  112.             // Compare age to the next tier
  113.             if ((now - item.ts) > ageTiers[item.ageLevel + 1]) {
  114.                 ScriptAPI.$craftingService.ageUpItem(item);
  115.  
  116.                 return true;
  117.             }
  118.  
  119.             return false;
  120.         }
  121.  
  122.         // Craft two items together
  123.         function craft(primary, consumed, strict) {
  124.             if (!strict || (primary.mod === consumed.mod && primary.rarity === consumed.rarity && primary.name === consumed.name)) {
  125.                 var primaryWasLocked = primary.lock,
  126.                     consumedWasLocked = consumed.lock;
  127.  
  128.                 // Unlock items if needed
  129.                 primaryWasLocked && toggleLock(primary, false);
  130.                 consumedWasLocked && toggleLock(consumed, false);
  131.  
  132.                 ScriptAPI.$craftingService.craftItems(primary, consumed, function (data) {
  133.                     // Lock the item(s) is previously locked
  134.                     if (!data.success) {
  135.                         primaryWasLocked && toggleLock(primary, true);
  136.                         consumedWasLocked && toggleLock(consumed, true);
  137.                         showNotifications[2] && notify('\u2716 Merge failed...'); // multiplication x
  138.                     } else {
  139.                         primaryWasLocked && toggleLock(data.newItem, true);
  140.                         showNotifications[2] && notify('\u2714 ' + summarize(data.newItem, true)); // check mark
  141.                     }
  142.                 });
  143.  
  144.                 showNotifications[2] && notify('\u22EF ' + summarize(primary) + ' \u2190 ' + summarize(consumed)); // midline ellipsis, left arrow
  145.  
  146.                 return true;
  147.             }
  148.  
  149.             return false;
  150.         }
  151.  
  152.         // Get data from local storage
  153.         function getFromStorage(key) {
  154.             var data = localStorage.getItem(key);
  155.  
  156.             return data ? JSON.parse(data) : {};
  157.         }
  158.  
  159.         // Save data to local storage
  160.         function saveToStorage(key, data) {
  161.             localStorage.setItem(key, JSON.stringify(data));
  162.         }
  163.  
  164.         // Get the base stats of an item
  165.         function getBaseStats(item, allStats) {
  166.             var stats = item.original ? item.original.stats : item.stats,
  167.                 nonZeroStats = {},
  168.                 i;
  169.  
  170.             if (!allStats) {
  171.                 for (i in stats) {
  172.                     if (stats.hasOwnProperty(i) && stats[i] > 0) {
  173.                         nonZeroStats[i] = stats[i];
  174.                     }
  175.                 }
  176.  
  177.                 stats = nonZeroStats;
  178.             }
  179.  
  180.             return stats;
  181.         }
  182.  
  183.         // Get the best stats (based on local storage) for an item type
  184.         function getBestStats(item) {
  185.             var bestStats;
  186.  
  187.             // Make sure the relevant objects exist
  188.             try {
  189.                 bestStats = data[item.mod][item.rarity][item.name];
  190.  
  191.                 if (!bestStats) {
  192.                     throw 'Invalid stats';
  193.                 }
  194.             } catch (e) {
  195.                 data[item.mod] = data[item.mod] || {};
  196.                 data[item.mod][item.rarity] = data[item.mod][item.rarity] || {};
  197.                 data[item.mod][item.rarity][item.name] = data[item.mod][item.rarity][item.name] || {};
  198.  
  199.                 bestStats = {};
  200.             }
  201.  
  202.             return bestStats;
  203.         }
  204.  
  205.         // Get the primary stat of an item type
  206.         function getPrimaryStat(stats) {
  207.             var primary,
  208.                 i;
  209.  
  210.             for (i in stats) {
  211.                 if (stats.hasOwnProperty(i) && (!primary || stats[i] > stats[primary])) {
  212.                     primary = i;
  213.                 }
  214.             }
  215.  
  216.             return primary;
  217.         }
  218.  
  219.         // Determine if an item's rarity is high enough to preserve
  220.         function isRareEnough(item) {
  221.             return item.mod >= minimumArea && item.rarity >= minimumRarity;
  222.         }
  223.  
  224.         // Determine if an item is strong enough to preserve
  225.         function isStrongEnough(item) {
  226.             var stats = getBaseStats(item),
  227.                 primary = getPrimaryStat(stats),
  228.                 bestStats = getBestStats(item),
  229.                 i;
  230.  
  231.             // Update the storage if the new item is superior
  232.             if (!bestStats.hasOwnProperty(primary) || stats[primary] > bestStats[primary]) {
  233.                 for (i in stats) {
  234.                     if (stats.hasOwnProperty(i)) {
  235.                         data[item.mod][item.rarity][item.name][i] = stats[i];
  236.                     }
  237.                 }
  238.  
  239.                 bestStats = data[item.mod][item.rarity][item.name];
  240.                 saveToStorage(storageKey, data);
  241.             }
  242.  
  243.             // Determine if the new item is good enough
  244.             return (stats[primary] >= (bestStats[primary] - threshold)) && isRareEnough(item);
  245.         }
  246.  
  247.         // Determine if an item can still be used in pure crafting
  248.         function isCraftReady(item) {
  249.             var maxPlus = 5 + (5 * item.rarity);
  250.  
  251.             return item.plus <= (maxPlus - 1);
  252.         }
  253.  
  254.         // Determine if the item is considered worthyless by the config variables
  255.         function isWorthless(item) {
  256.             return worthlessItems.indexOf(item.name) >= 0;
  257.         }
  258.  
  259.         // Combination of strength & lack of worthlessness
  260.         function isWorthy(item) {
  261.             return isStrongEnough(item) && !isWorthless(item);
  262.         }
  263.  
  264.         // Handle incoming items
  265.         items.forEach(function (item) {
  266.             if (isWorthy(item)) {
  267.                 preserve(item);
  268.             } else {
  269.                 sell(item);
  270.             }
  271.         });
  272.  
  273.         // Find the best items
  274.         ScriptAPI.$user.inventory.items.forEach(function (item) {
  275.             // Get all items good enough for merging
  276.             if (isWorthy(item)) {
  277.                 worthyItems.push(item);
  278.  
  279.                 if (isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
  280.                     bestItems[item.name] = item;
  281.                 }
  282.             } else {
  283.                 sell(item, true);
  284.             }
  285.         });
  286.  
  287.         // Check if you are currently wearing your best items
  288.         ScriptAPI.$user.character.equipment.forEach(function (item) {
  289.             if (isWorthy(item) && isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
  290.                 bestItems[item.name] = item;
  291.             }
  292.         });
  293.  
  294.         // Craft your ultimate items
  295.         worthyItems.forEach(function (item) {
  296.             // Merge items into the best item
  297.             if (bestItems[item.name] !== item) {
  298.                 if (item.plus < 1 && (item.subType === 'ARMOR' || (readyToCraft.indexOf(item.name) > -1))) {
  299.                     craft(bestItems[item.name], item, true);
  300.                 }
  301.             } else {
  302.                 // Only take the time to try to age the best items
  303.                 age(item);
  304.             }
  305.         });
  306.  
  307.         // Age worn items
  308.         ScriptAPI.$user.character.equipment.forEach(age);
  309.     }());
  310. }(ScriptAPI, API, items));
Advertisement
Add Comment
Please, Sign In to add comment