Advertisement
Koviko

BattleINF Ultimate Gear Crafter v1.0

Nov 27th, 2015
125
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 <koviko.net@gmail.com>
  6.  * @version 1.0
  7.  */
  8.  
  9. /*global ScriptAPI, API, items*/
  10. (function (ScriptAPI, API, items) {
  11.     'use strict';
  12.  
  13.     // Configuration variables
  14.     var threshold = 10, // 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.         showNotifications = [true, false, true]; // [save, sell, craft]
  19.  
  20.     // Business logic
  21.     (function () {
  22.         var storageKey = 'best',
  23.             data = getFromStorage(storageKey),
  24.             now = Date.now(),
  25.             bestItems = {}, // Best items to be merged into
  26.             worthyItems = [],
  27.             ageTiers = [ // Limits for each age, used to reduce number of server calls
  28.                 0, // Worn: 0 minutes
  29.                 900000, // Fine: 15 minutes
  30.                 1800000, // Refined: 30 minutes
  31.                 3600000, // Aged: 1 hour
  32.                 7200000, // Exotic: 2 hours
  33.                 86400000, // Famous: 1 day
  34.                 172800000, // Master: 2 days
  35.                 345600000, // Heroic: 4 days
  36.                 691200000, // Ancient: 8 days
  37.                 1382400000, // Fabled: 16 days
  38.                 2764800000, // Ascended: 32 days
  39.                 5529600000, // Legendary: 64 days
  40.                 11059200000 // Eternal: 128 days
  41.             ];
  42.  
  43.         // Create a notification
  44.         function notify(message, timeout) {
  45.             API.notifications.create(message, timeout || defaultTimeout);
  46.             console.log(message);
  47.         }
  48.  
  49.         // Show a summary of an item
  50.         function summarize(item, showPlus) {
  51.             var parts = [
  52.                 '\ud83c\udfe0' + item.mod, // Area
  53.                 '\u2606' + item.rarity, // Rarity
  54.                 item.name // Name
  55.             ];
  56.  
  57.             if (showPlus) {
  58.                 parts.push('+' + item.plus);
  59.             }
  60.  
  61.             return parts.join(' ');
  62.         }
  63.  
  64.         // Save an item for later use
  65.         function preserve(item) {
  66.             // Lock the precious so no harm can come to it
  67.             if (!item.lock) {
  68.                 var primary = getPrimaryStat(item.stats),
  69.                     stats = getBaseStats(item, true),
  70.                     best = getBestStats(item, data),
  71.                     comparison = stats[primary] + ' / ' + best[primary];
  72.  
  73.                 ScriptAPI._modules.inventory.lock(item);
  74.                 showNotifications[0] && notify('\ud83d\udc4d ' + summarize(item) + ' (' + comparison + ')'); // thumbs up
  75.  
  76.                 return true;
  77.             }
  78.  
  79.             return false;
  80.         }
  81.  
  82.         // Sell an item
  83.         function sell(item) {
  84.             if (!item.lock) {
  85.                 ScriptAPI.$userService.sellItem(item);
  86.                 showNotifications[1] && notify('\ud83d\udcb0 ' + summarize(item)); // money bag
  87.  
  88.                 return true;
  89.             }
  90.  
  91.             return false;
  92.         }
  93.  
  94.         // Age an item
  95.         function age(item) {
  96.             var age = now - item.ts;
  97.  
  98.             // Compare age to the next tier
  99.             if (age > ageTiers[item.ageLevel + 1]) {
  100.                 ScriptAPI.$craftingService.ageUpItem(items);
  101.  
  102.                 return true;
  103.             }
  104.  
  105.             return false;
  106.         }
  107.  
  108.         // Craft two items together
  109.         function craft(primary, consumed) {
  110.             if (primary.mod === consumed.mod && primary.rarity === consumed.rarity && primary.name === consumed.name) {
  111.                 var primaryWasLocked = primary.lock,
  112.                     consumedWasLocked = consumed.lock;
  113.  
  114.                 // Unlock items if needed
  115.                 primaryWasLocked && ScriptAPI._modules.inventory.unlock(primary);
  116.                 consumedWasLocked && ScriptAPI._modules.inventory.unlock(consumed);
  117.  
  118.                 ScriptAPI.$craftingService.craftItems(primary, consumed, function (data) {
  119.                     // Lock the item(s) is previously locked
  120.                     if (!data.success) {
  121.                         primaryWasLocked && ScriptAPI._modules.inventory.lock(primary);
  122.                         consumedWasLocked && ScriptAPI._modules.inventory.lock(consumed);
  123.                         showNotifications[2] && notify('\u2716 Merge failed...'); // multiplication x
  124.                     } else {
  125.                         primaryWasLocked && ScriptAPI._modules.inventory.lock(data.newItem);
  126.                         showNotifications[2] && notify('\u2714 ' + summarize(data.newItem, true)); // check mark
  127.                     }
  128.                 });
  129.  
  130.                 showNotifications[2] && notify('\u22EF ' + summarize(primary) + ' \u2190 ' + summarize(consumed)); // midline ellipsis, left arrow
  131.  
  132.                 return true;
  133.             }
  134.  
  135.             return false;
  136.         }
  137.  
  138.         // Get data from local storage
  139.         function getFromStorage(key) {
  140.             var data = localStorage.getItem(key);
  141.  
  142.             return data ? JSON.parse(data) : {};
  143.         }
  144.  
  145.         // Save data to local storage
  146.         function saveToStorage(key, data) {
  147.             localStorage.setItem(key, JSON.stringify(data));
  148.         }
  149.  
  150.         // Get the base stats of an item
  151.         function getBaseStats(item, allStats) {
  152.             var stats = item.original ? item.original.stats : item.stats,
  153.                 nonZeroStats = {},
  154.                 i;
  155.  
  156.             if (!allStats) {
  157.                 for (i in stats) {
  158.                     if (stats.hasOwnProperty(i) && stats[i] > 0) {
  159.                         nonZeroStats[i] = stats[i];
  160.                     }
  161.                 }
  162.  
  163.                 stats = nonZeroStats;
  164.             }
  165.  
  166.             return stats;
  167.         }
  168.  
  169.         // Get the best stats (based on local storage) for an item type
  170.         function getBestStats(item, data) {
  171.             var bestStats;
  172.  
  173.             // Make sure the relevant objects exist
  174.             try {
  175.                 bestStats = data[item.mod][item.rarity][item.name];
  176.             } catch (e) {
  177.                 data[item.mod] = data[item.mod] || {};
  178.                 data[item.mod][item.rarity] = data[item.mod][item.rarity] || {};
  179.                 data[item.mod][item.rarity][item.name] = data[item.mod][item.rarity][item.name] || {};
  180.  
  181.                 bestStats = {};
  182.             }
  183.  
  184.             return bestStats;
  185.         }
  186.  
  187.         // Get the primary stat of an item type
  188.         function getPrimaryStat(stats) {
  189.             var primary,
  190.                 i;
  191.  
  192.             for (i in stats) {
  193.                 if (stats.hasOwnProperty(i) && (!primary || stats[i] > stats[primary])) {
  194.                     primary = i;
  195.                 }
  196.             }
  197.  
  198.             return primary;
  199.         }
  200.  
  201.         // Determine if an item's rarity is high enough to preserve
  202.         function isRareEnough(item) {
  203.             return item.mod >= minimumArea && item.rarity >= minimumRarity;
  204.         }
  205.  
  206.         // Determine if an item is strong enough to preserve
  207.         function isStrongEnough(item) {
  208.             var stats = getBaseStats(item),
  209.                 primary = getPrimaryStat(stats),
  210.                 bestStats = getBestStats(item, data),
  211.                 i;
  212.  
  213.             // Update the storage if the new item is superior
  214.             if (!bestStats.hasOwnProperty(primary) || stats[primary] > bestStats[primary]) {
  215.                 for (i in stats) {
  216.                     if (stats.hasOwnProperty(i)) {
  217.                         data[item.mod][item.rarity][item.name][i] = stats[i];
  218.                     }
  219.                 }
  220.  
  221.                 bestStats = data[item.mod][item.rarity][item.name];
  222.                 saveToStorage(storageKey, data);
  223.             }
  224.  
  225.             // Determine if the new item is good enough
  226.             return (stats[primary] >= (bestStats[primary] - threshold)) && isRareEnough(item);
  227.         }
  228.  
  229.         // Handle incoming items
  230.         items.forEach(function (item) {
  231.             if (isStrongEnough(item)) {
  232.                 preserve(item);
  233.             } else {
  234.                 sell(item);
  235.             }
  236.         });
  237.  
  238.         // Find the best items
  239.         ScriptAPI.$user.inventory.items.forEach(function (item) {
  240.             // Get all items good enough for merging
  241.             if (isStrongEnough(item)) {
  242.                 worthyItems.push(item);
  243.  
  244.                 if (!bestItems[item.name] || (item.ts < bestItems[item.name].ts)) {
  245.                     bestItems[item.name] = item;
  246.                 }
  247.             } else {
  248.                 // Disabled, for now... Just in case
  249.                 //sell(item);
  250.             }
  251.         });
  252.  
  253.         worthyItems.forEach(function (item) {
  254.             // Merge items into the best item
  255.             if (bestItems[item.name] !== item) {
  256.                 item.subType === 'ARMOR' && craft(bestItems[item.name], item);
  257.             } else {
  258.                 // Only take the time to try to age the best items
  259.                 age(item);
  260.             }
  261.         });
  262.  
  263.         // Age worn items
  264.         ScriptAPI.$user.character.equipment.forEach(age);
  265.     }());
  266. }(ScriptAPI, API, items));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement