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.0
- */
- /*global ScriptAPI, API, items*/
- (function (ScriptAPI, API, items) {
- 'use strict';
- // Configuration variables
- var threshold = 10, // 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
- 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(' ');
- }
- // 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, data),
- comparison = stats[primary] + ' / ' + best[primary];
- ScriptAPI._modules.inventory.lock(item);
- showNotifications[0] && notify('\ud83d\udc4d ' + summarize(item) + ' (' + comparison + ')'); // thumbs up
- return true;
- }
- return false;
- }
- // Sell an item
- function sell(item) {
- if (!item.lock) {
- ScriptAPI.$userService.sellItem(item);
- showNotifications[1] && notify('\ud83d\udcb0 ' + summarize(item)); // money bag
- return true;
- }
- return false;
- }
- // Age an item
- function age(item) {
- var age = now - item.ts;
- // Compare age to the next tier
- if (age > ageTiers[item.ageLevel + 1]) {
- ScriptAPI.$craftingService.ageUpItem(items);
- return true;
- }
- return false;
- }
- // Craft two items together
- function craft(primary, consumed) {
- if (primary.mod === consumed.mod && primary.rarity === consumed.rarity && primary.name === consumed.name) {
- var primaryWasLocked = primary.lock,
- consumedWasLocked = consumed.lock;
- // Unlock items if needed
- primaryWasLocked && ScriptAPI._modules.inventory.unlock(primary);
- consumedWasLocked && ScriptAPI._modules.inventory.unlock(consumed);
- ScriptAPI.$craftingService.craftItems(primary, consumed, function (data) {
- // Lock the item(s) is previously locked
- if (!data.success) {
- primaryWasLocked && ScriptAPI._modules.inventory.lock(primary);
- consumedWasLocked && ScriptAPI._modules.inventory.lock(consumed);
- showNotifications[2] && notify('\u2716 Merge failed...'); // multiplication x
- } else {
- primaryWasLocked && ScriptAPI._modules.inventory.lock(data.newItem);
- 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, data) {
- 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, data),
- 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);
- }
- // Handle incoming items
- items.forEach(function (item) {
- if (isStrongEnough(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 (isStrongEnough(item)) {
- worthyItems.push(item);
- if (!bestItems[item.name] || (item.ts < bestItems[item.name].ts)) {
- bestItems[item.name] = item;
- }
- } else {
- // Disabled, for now... Just in case
- //sell(item);
- }
- });
- worthyItems.forEach(function (item) {
- // Merge items into the best item
- if (bestItems[item.name] !== item) {
- item.subType === 'ARMOR' && craft(bestItems[item.name], item);
- } 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