Koviko

BattleINF Ultimate Gear Crafter v1.2

Nov 30th, 2015
233
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.2
  7.  */
  8.  
  9. /*global ScriptAPI, API, items*/
  10. (function (ScriptAPI, API, items) {
  11.     'use strict';
  12.  
  13.     // Configuration variables
  14.     var threshold = 15, // 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 = [], // Weapon types that have been prepped for crafting
  19.         worthlessItems = ['Staff', 'Shield'], // 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.         function toggleLock(item, lock) {
  67.             return ScriptAPI._modules.inventory[lock ? 'lock' : 'unlock'](item);
  68.         }
  69.  
  70.         // Save an item for later use
  71.         function preserve(item) {
  72.             // Lock the precious so no harm can come to it
  73.             if (!item.lock) {
  74.                 var primary = getPrimaryStat(item.stats),
  75.                     stats = getBaseStats(item, true),
  76.                     best = getBestStats(item),
  77.                     comparison = stats[primary] + ' / ' + best[primary];
  78.  
  79.                 toggleLock(item, true);
  80.                 showNotifications[0] && notify('\ud83d\udc4d ' + summarize(item) + ' (' + comparison + ')'); // thumbs up
  81.  
  82.                 return true;
  83.             }
  84.  
  85.             return false;
  86.         }
  87.  
  88.         // Sell an item
  89.         function sell(item, bypassLock) {
  90.             var itemIsLocked = item.lock;
  91.  
  92.             if (!itemIsLocked || bypassLock) {
  93.                 itemIsLocked && toggleLock(item, false);
  94.  
  95.                 ScriptAPI.$userService.sellItem(item, function (data) {
  96.                     if (data.success) {
  97.                         showNotifications[1] && notify('\ud83d\udcb0 ' + summarize(item)); // money bag
  98.                     } else if (itemIsLocked) {
  99.                         toggleLock(item, true);
  100.                     }
  101.                 });
  102.  
  103.                 return true;
  104.             }
  105.  
  106.             return false;
  107.         }
  108.  
  109.         // Age an item
  110.         function age(item) {
  111.             // Compare age to the next tier
  112.             if ((now - item.ts) > ageTiers[item.ageLevel + 1]) {
  113.                 ScriptAPI.$craftingService.ageUpItem(item);
  114.  
  115.                 return true;
  116.             }
  117.  
  118.             return false;
  119.         }
  120.  
  121.         // Craft two items together
  122.         function craft(primary, consumed, strict) {
  123.             if (!strict || (primary.mod === consumed.mod && primary.rarity === consumed.rarity && primary.name === consumed.name)) {
  124.                 var primaryWasLocked = primary.lock,
  125.                     consumedWasLocked = consumed.lock;
  126.  
  127.                 // Unlock items if needed
  128.                 primaryWasLocked && toggleLock(primary, false);
  129.                 consumedWasLocked && toggleLock(consumed, false);
  130.  
  131.                 ScriptAPI.$craftingService.craftItems(primary, consumed, function (data) {
  132.                     // Lock the item(s) is previously locked
  133.                     if (!data.success) {
  134.                         primaryWasLocked && toggleLock(primary, true);
  135.                         consumedWasLocked && toggleLock(consumed, true);
  136.                         showNotifications[2] && notify('\u2716 Merge failed...'); // multiplication x
  137.                     } else {
  138.                         primaryWasLocked && toggleLock(data.newItem, true);
  139.                         showNotifications[2] && notify('\u2714 ' + summarize(data.newItem, true)); // check mark
  140.                     }
  141.                 });
  142.  
  143.                 showNotifications[2] && notify('\u22EF ' + summarize(primary) + ' \u2190 ' + summarize(consumed)); // midline ellipsis, left arrow
  144.  
  145.                 return true;
  146.             }
  147.  
  148.             return false;
  149.         }
  150.  
  151.         // Get data from local storage
  152.         function getFromStorage(key) {
  153.             var data = localStorage.getItem(key);
  154.  
  155.             return data ? JSON.parse(data) : {};
  156.         }
  157.  
  158.         // Save data to local storage
  159.         function saveToStorage(key, data) {
  160.             localStorage.setItem(key, JSON.stringify(data));
  161.         }
  162.  
  163.         // Get the base stats of an item
  164.         function getBaseStats(item, allStats) {
  165.             var stats = item.original ? item.original.stats : item.stats,
  166.                 nonZeroStats = {},
  167.                 i;
  168.  
  169.             if (!allStats) {
  170.                 for (i in stats) {
  171.                     if (stats.hasOwnProperty(i) && stats[i] > 0) {
  172.                         nonZeroStats[i] = stats[i];
  173.                     }
  174.                 }
  175.  
  176.                 stats = nonZeroStats;
  177.             }
  178.  
  179.             return stats;
  180.         }
  181.  
  182.         // Get the best stats (based on local storage) for an item type
  183.         function getBestStats(item) {
  184.             var bestStats;
  185.  
  186.             // Make sure the relevant objects exist
  187.             try {
  188.                 bestStats = data[item.mod][item.rarity][item.name];
  189.             } catch (e) {
  190.                 data[item.mod] = data[item.mod] || {};
  191.                 data[item.mod][item.rarity] = data[item.mod][item.rarity] || {};
  192.                 data[item.mod][item.rarity][item.name] = data[item.mod][item.rarity][item.name] || {};
  193.  
  194.                 bestStats = {};
  195.             }
  196.  
  197.             return bestStats;
  198.         }
  199.  
  200.         // Get the primary stat of an item type
  201.         function getPrimaryStat(stats) {
  202.             var primary,
  203.                 i;
  204.  
  205.             for (i in stats) {
  206.                 if (stats.hasOwnProperty(i) && (!primary || stats[i] > stats[primary])) {
  207.                     primary = i;
  208.                 }
  209.             }
  210.  
  211.             return primary;
  212.         }
  213.  
  214.         // Determine if an item's rarity is high enough to preserve
  215.         function isRareEnough(item) {
  216.             return item.mod >= minimumArea && item.rarity >= minimumRarity;
  217.         }
  218.  
  219.         // Determine if an item is strong enough to preserve
  220.         function isStrongEnough(item) {
  221.             var stats = getBaseStats(item),
  222.                 primary = getPrimaryStat(stats),
  223.                 bestStats = getBestStats(item),
  224.                 i;
  225.  
  226.             // Update the storage if the new item is superior
  227.             if (!bestStats.hasOwnProperty(primary) || stats[primary] > bestStats[primary]) {
  228.                 for (i in stats) {
  229.                     if (stats.hasOwnProperty(i)) {
  230.                         data[item.mod][item.rarity][item.name][i] = stats[i];
  231.                     }
  232.                 }
  233.  
  234.                 bestStats = data[item.mod][item.rarity][item.name];
  235.                 saveToStorage(storageKey, data);
  236.             }
  237.  
  238.             // Determine if the new item is good enough
  239.             return (stats[primary] >= (bestStats[primary] - threshold)) && isRareEnough(item);
  240.         }
  241.  
  242.         // Determine if an item can still be used in pure crafting
  243.         function isCraftReady(item) {
  244.             var maxPlus = 5 + (5 * item.rarity);
  245.  
  246.             return item.plus <= (maxPlus - 1);
  247.         }
  248.  
  249.         // Handle incoming items
  250.         items.forEach(function (item) {
  251.             if (isStrongEnough(item) && (worthlessItems.indexOf(item.name) <= -1)) {
  252.                 preserve(item);
  253.             } else {
  254.                 sell(item);
  255.             }
  256.         });
  257.  
  258.         // Find the best items
  259.         ScriptAPI.$user.inventory.items.forEach(function (item) {
  260.             // Get all items good enough for merging
  261.             if (isStrongEnough(item) && (worthlessItems.indexOf(item.name) <= -1)) {
  262.                 worthyItems.push(item);
  263.  
  264.                 if (isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
  265.                     bestItems[item.name] = item;
  266.                 }
  267.             } else {
  268.                 sell(item, true);
  269.             }
  270.         });
  271.  
  272.         worthyItems.forEach(function (item) {
  273.             // Merge items into the best item
  274.             if (bestItems[item.name] !== item) {
  275.                 if (item.plus < 1 && (item.subType === 'ARMOR' || (readyToCraft.indexOf(item.name) > -1))) {
  276.                     craft(bestItems[item.name], item, true);
  277.                 }
  278.             } else {
  279.                 // Only take the time to try to age the best items
  280.                 age(item);
  281.             }
  282.         });
  283.  
  284.         // Age worn items
  285.         ScriptAPI.$user.character.equipment.forEach(age);
  286.     }());
  287. }(ScriptAPI, API, items));
Advertisement
Add Comment
Please, Sign In to add comment