Company Stock update

Updates company stock based on given numbers or percentages.

目前為 2018-05-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Company Stock update
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Updates company stock based on given numbers or percentages.
// @author       LordBusiness
// @match        https://www.torn.com/companies.php
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        none
// ==/UserScript==


// You need to add the prices you want in order.
var isPercentage = false;
var APIkey = "";
var PriceArray = ["24", "16", "16", "8", "8", "8", "8", "8"];

/* Examples
var isPercentage = false;
var APIkey = "";
var PriceArray = ["560", "4500", "9000", "500", "1000"];

var isPercentage = true;
var APIkey = "UGdo6gia";
var PriceArray = ["20%", "25%", "5%", "11%", "9%", "13%", "14%"];


Note: * If you use percentages, make sure all of them added are below 100.
      * Make sure Price Array has the exact number of things to order. If you don't need to order, leave it as "0".
*/

// a minimal jQuery library for reacting to innerHTML changes
(function($) {
  $.fn.change = function(cb, e) {
    e = e || { subtree:true, childList:true, characterData:true };
    $(this).each(function() {
      function callback(changes) { cb.call(node, changes, this); }
      var node = this;
      (new MutationObserver(callback)).observe(node, e);
    });
  };
})(jQuery);

var storageURL = "https://api.torn.com/company/?selections=detailed&key=" + APIkey;

var flag = 0, pos = 0, maxStorage = 0;
var url = "https://api.torn.com/company/?selections=stock&key=" + APIkey;
var stockItems = [];
$("#ui-id-8").click(function() {
    if(isPercentage) {
        $.getJSON( storageURL )
            .done(function( json ) {
            maxStorage = parseInt(json.company_detailed.upgrades.storage_space);
            var sumPercent = 0;
            for(var i=0;i<PriceArray.length;i++) {
                PriceArray[i] = PriceArray[i].replace('%', '');
                sumPercent += parseInt(PriceArray[i]);
                PriceArray[i] = parseInt(PriceArray[i]) * maxStorage / 100;
                console.log(sumPercent);
            }
            console.log(sumPercent, PriceArray);
            $.getJSON( url )
                .done(function( json ) {
                for (var key in json.company_stock) {
                    console.log(1, key);
                    if(pos == 0) {
                        stockItems = [key];
                        pos = 1;
                        console.log(2, stockItems);
                    }
                    else {
                        for(var i=0; i < stockItems.length; i++) {
                            var stockItemelem = parseInt(json.company_stock[stockItems[i]].rrp);
                            var currentelem = parseInt(json.company_stock[key].rrp);
                            if(stockItemelem >= currentelem) {
                                if((stockItemelem == currentelem) && (String(key).localeCompare(stockItems[i]) == 1))
                                    if(++i == stockItems.length) {
                                        stockItems.push(key);
                                        break;
                                    }
                                stockItems.splice(i, 0, key);
                                console.log(3, stockItems);
                                break;
                            }
                            else if(i == stockItems.length-1) {
                                stockItems.push(key);
                                break;
                            }
                        }
                    }
                }
                for(var j=0; j<stockItems.length; j++) {
                    PriceArray[j] = PriceArray[j] - parseInt(json.company_stock[stockItems[j]].in_stock) - parseInt(json.company_stock[stockItems[j]].on_order);
                }
                console.log(PriceArray);
                if($(".stock-list-wrap").length > 0) {
                    try {
                        var k = 0;
                        $(".quantity > input").each(function() {
                            if(PriceArray[k] > 0) {
                                $(this).focus();
                                $(this).val(PriceArray[k]);
                                $(this).keydown();
                                $(this).keypress();
                                $(this).keyup();
                                $(this).blur();
                            }
                            k++;
                        });
                        k = 0;
                    }
                    catch(err) {
                        console.log("Err:" + err);
                    }
                }
            })
                .fail(function( jqxhr, textStatus, error ) {
                console.log( "Request Failed: " + textStatus + ", " + error );
            });
        })
            .fail(function( jqxhr, textStatus, error ) {
            console.log( "Request Failed: " + textStatus + ", " + error );
        });
    }
    else {
        $('#stock').change(function(changes, observer) {
            if(($(".stock-list-wrap").length > 0) && (flag === 0)) {
                flag = 1;
                console.log("p");
                try {
                    var i = 0;
                    $(".quantity > input").each(function() {
                        if(PriceArray[i] > 0) {
                            $(this).focus();
                            $(this).val(PriceArray[i]);
                            $(this).keydown();
                            $(this).keypress();
                            $(this).keyup();
                            $(this).blur();
                        }
                        i++;
                    });
                    i = 0;
                }
                catch(err) {
                    console.log("Err:" + err);
                }
            }
        });
    }
});