Torn Set Calculator

Calculates prices of plushie and flower sets

2015-08-01 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name Torn Set Calculator
// @version 0.2
// @description Calculates prices of plushie and flower sets
// @author MrHat
// @namespace MrHat.Torn
// @require http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @require http://cdnjs.cloudflare.com/ajax/libs/accounting.js/0.4.1/accounting.min.js
// @require https://greasyfork.org/scripts/11365-library-for-intercepting-ajax-communications/code/Library%20for%20intercepting%20AJAX%20communications.js?version=65323
// @match http://www.torn.com/imarket.php*
// @match https://www.torn.com/imarket.php*
// @grant metadata
// ==/UserScript==

function itemsLoaded(items) {
    // List item ids of sets
    var itemSets = {
        plushies: ["186", "215", "187", "261", "618", "258", "273", "269", "266", "268", "281", "274", "384"],
        flowers: ["260", "617", "263", "272", "264", "271", "267", "277", "282", "276", "385"]
    };

    // Create an array with all items ids we are interested in
    var sets = itemSets.plushies.concat(itemSets.flowers);

    // Loop through response items and calculate the sum of all relevant items
    var sum = 0;
    items.forEach(function(item) {
        if ($.inArray(item.itemID, sets) !== -1) {
            sum += parseInt(item.price);
        }
    });

    // Calculate price of individual points
    var individualPoint = sum / 10;

    // Show results on page (attempt to find container, if it's not there we create it)
    var container = $('#setCalculator');
    if (!container.length) {
        container = $('<div>').attr('id', 'setCalculator').addClass('msg right-round');

        var wrapper = $('<div>').addClass('info-msg border-round').html($('<i>').addClass('info-icon'));
        wrapper.append(container);
        wrapper.prependTo($('.main-market-page'));
    }

    // Clear text
    container.empty();

    // Only show results if we have something to show
    if (sum > 0) {
        var msg = $('<span>').html('1 complete set costs <b>' + accounting.formatMoney(sum, "$", 0) + '</b>. This equals to <b>' + accounting.formatMoney(individualPoint, "$", 0) + '</b> per point.');
        container.append(msg);
    } else {
        var msg = $('<span>').html('No sets available.');
        container.append(msg);
    }
}

// Set up interceptor
var AjaxInterceptor = require("ajax-intercept");
AjaxInterceptor.addResponseCallback(function(xhr) {
    
    // We only intercept requests for the item market
    var marketRegex = /^(http|https):\/\/www.torn.com\/imarket.php\?rfcv=(\d+)$/;
    if (marketRegex.test(xhr.responseURL)) {
        
         // Process the items and their pricess
        var items = JSON.parse(xhr.response);
        itemsLoaded(items);
    }
});

// Will proxify XHR to fire the above callbacks
AjaxInterceptor.wire();