Koviko

BattleINF Ultimate Gear Crafter v1.3

Dec 5th, 2015
205
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
  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 = ['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.             } catch (e) {
  191.                 data[item.mod] = data[item.mod] || {};
  192.                 data[item.mod][item.rarity] = data[item.mod][item.rarity] || {};
  193.                 data[item.mod][item.rarity][item.name] = data[item.mod][item.rarity][item.name] || {};
  194.  
  195.                 bestStats = {};
  196.             }
  197.  
  198.             return bestStats;
  199.         }
  200.  
  201.         // Get the primary stat of an item type
  202.         function getPrimaryStat(stats) {
  203.             var primary,
  204.                 i;
  205.  
  206.             for (i in stats) {
  207.                 if (stats.hasOwnProperty(i) && (!primary || stats[i] > stats[primary])) {
  208.                     primary = i;
  209.                 }
  210.             }
  211.  
  212.             return primary;
  213.         }
  214.  
  215.         // Determine if an item's rarity is high enough to preserve
  216.         function isRareEnough(item) {
  217.             return item.mod >= minimumArea && item.rarity >= minimumRarity;
  218.         }
  219.  
  220.         // Determine if an item is strong enough to preserve
  221.         function isStrongEnough(item) {
  222.             var stats = getBaseStats(item),
  223.                 primary = getPrimaryStat(stats),
  224.                 bestStats = getBestStats(item),
  225.                 i;
  226.  
  227.             // Update the storage if the new item is superior
  228.             if (!bestStats.hasOwnProperty(primary) || stats[primary] > bestStats[primary]) {
  229.                 for (i in stats) {
  230.                     if (stats.hasOwnProperty(i)) {
  231.                         data[item.mod][item.rarity][item.name][i] = stats[i];
  232.                     }
  233.                 }
  234.  
  235.                 bestStats = data[item.mod][item.rarity][item.name];
  236.                 saveToStorage(storageKey, data);
  237.             }
  238.  
  239.             // Determine if the new item is good enough
  240.             return (stats[primary] >= (bestStats[primary] - threshold)) && isRareEnough(item);
  241.         }
  242.  
  243.         // Determine if an item can still be used in pure crafting
  244.         function isCraftReady(item) {
  245.             var maxPlus = 5 + (5 * item.rarity);
  246.  
  247.             return item.plus <= (maxPlus - 1);
  248.         }
  249.  
  250.         // Determine if the item is considered worthyless by the config variables
  251.         function isWorthless(item) {
  252.             return worthlessItems.indexOf(item.name) >= 0;
  253.         }
  254.  
  255.         // Combination of strength & lack of worthlessness
  256.         function isWorthy(item) {
  257.             return isStrongEnough(item) && !isWorthless(item);
  258.         }
  259.  
  260.         // Handle incoming items
  261.         items.forEach(function (item) {
  262.             if (isWorthy(item)) {
  263.                 preserve(item);
  264.             } else {
  265.                 sell(item);
  266.             }
  267.         });
  268.  
  269.         // Find the best items
  270.         ScriptAPI.$user.inventory.items.forEach(function (item) {
  271.             // Get all items good enough for merging
  272.             if (isWorthy(item)) {
  273.                 worthyItems.push(item);
  274.  
  275.                 if (isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
  276.                     bestItems[item.name] = item;
  277.                 }
  278.             } else {
  279.                 sell(item, true);
  280.             }
  281.         });
  282.  
  283.         // Check if you are currently wearing your best items
  284.         ScriptAPI.$user.character.equipment.forEach(function (item) {
  285.             if (isWorthy(item) && isCraftReady(item) && (!bestItems[item.name] || (item.ts < bestItems[item.name].ts))) {
  286.                 bestItems[item.name] = item;
  287.             }
  288.         });
  289.  
  290.         // Craft your ultimate items
  291.         worthyItems.forEach(function (item) {
  292.             // Merge items into the best item
  293.             if (bestItems[item.name] !== item) {
  294.                 if (item.plus < 1 && (item.subType === 'ARMOR' || (readyToCraft.indexOf(item.name) > -1))) {
  295.                     craft(bestItems[item.name], item, true);
  296.                 }
  297.             } else {
  298.                 // Only take the time to try to age the best items
  299.                 age(item);
  300.             }
  301.         });
  302.  
  303.         // Age worn items
  304.         ScriptAPI.$user.character.equipment.forEach(age);
  305.     }());
  306. }(ScriptAPI, API, items));
Advertisement
Add Comment
Please, Sign In to add comment