TORN: Mission Reward Information

Give some information about mission rewards.

当前为 2020-01-28 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         TORN: Mission Reward Information
// @namespace    dekleinekobini.missionrewardinformatiom
// @version      1.1.0
// @author       DeKleineKobini
// @description  Give some information about mission rewards.
// @match        https://www.torn.com/loader.php?sid=missions*
// @require      https://greasyfork.org/scripts/390917-dkk-torn-utilities/code/DKK%20Torn%20Utilities.js?version=754362
// @connect      api.torn.com
// @connect      script.google.com
// @connect      script.googleusercontent.com
// @grant        GM_xmlhttpRequest
// ==/UserScript==

initScript({
    name: "Mission Reward Information",
    logging: "ALL"
});

addCSS("MRI", ".d .mod-description li:nth-child(3):after, .d .mod-description li:nth-child(4):after { content: ' '; position: absolute; display: block; width: 100%; height: 1px;  bottom: 0; left: 0; border-bottom:1px solid #000; }"
       + ".d .mod-description li:nth-child(3) { margin-left: 3px; }"
       + ".d .mod-description li:nth-child(5):before, .d .mod-description li:nth-child(6):before { content: ' '; position: absolute; display: block; width: 100%; height: 1px; top: 0; left: 0;  border-top: 1px solid #323232 }");

var spreadsheet;
var apiAmmo;

loadSpreadsheet().then(response => {
    dkklog.info("Spreadsheet information loaded.");
    spreadsheet = response;

    if ($(".rewards-list li.act").length) showInformation();
    observeMutations(document, ".rewards-slider", true, (mut, obs) => {
        observeMutations($(".rewards-slider").get(0), ".rewards-list li.act", false, showInformation, {attributes: true});
    }, {childList: true, subtree: true});
}, error => {
    dkklog.fatal(error);
});

const API = new TornAPI(api => {
    api.sendRequest("user", null, "ammo").then(response => {
        apiAmmo = response.ammo;
    });
});

function loadSpreadsheet() {
    return new Promise((resolve, reject) => {
        GM_xmlhttpRequest({
            url: "https://script.google.com/macros/s/AKfycbwkO8mbiG1vfjsC03sAMutRNXGVsfPgQZ6PxM-20i0D8p95Lo5M/exec",
            method:"GET",
            onload: response => {
                try {
                    resolve(JSON.parse(response.responseText));
                } catch (error) {
                    reject(error.toString() + ": " + response.responseText);
                }
            },
            ontimeout: response => reject("Request timed out: " + response.responseText),
            onerror: response => reject("Received an error: " + response.responseText),
            onabort: response => reject("Request aborted: " + response.responseText)
        });
    });
}

function showInformation() {
    let $item = $(".rewards-list li.act");
    let item = JSON.parse($item.attr("data-ammo-info"));

    let type = item.basicType;

    if (type == "Upgrade") {
        let { name, points } = item;
        let mod = spreadsheet.filter(mod => mod.name == name)[0];

        let special = false;
        let { min, max } = mod[special ? "special" : "price"];
        let diff = max - min;
        let currentDif = max - points;
        let dif = (currentDif / diff) * 100;

        let priceMin = mod.price.min;
        let priceMax = mod.price.max;
        let specialMin = mod.special.min;
        let specialMax = mod.special.max;

        $(".mod-description > li:eq(1)").after(`<li><span>Price Range:</span> <span class="bold">${priceMin} - ${priceMax}${special ? ` @ ${dif.format(2)}%` : ""}</span></li>`)
        $(".mod-description > li:eq(2)").after(`<li><span>Special Offer Range:</span> <span class="bold">${specialMin} - ${specialMax}${special ? ` @ ${dif.format(2)}%` : ""}</span></li>`);
    } else if (type == "Ammo") {
        let { name, ammoType } = item;
        let size, owned;

        if (name.includes(ammoType)) {
            size = name.substring(0, name.indexOf(ammoType)).trim();

            if (apiAmmo) {
                owned = apiAmmo.filter(ammo => ammo.size === size && ammo.type === ammoType);
                owned = owned.length ? owned[0].quantity : 0;
            } else owned = "api not loaded";

            $(".ammo-description").append(`<li><span>Owned:</span> <span class="bold">${owned}</span></li>`)
            dkklog.info("size", size, owned);
        } else {
            dkklog.error("unsupported ammo type", name, item);
        }
    } else if (type == "Item") {
        let { points, amount } = item;

        if ($(".show-item-info .info-wrap").length) show();
        else observeMutations($(".show-item-info").get(0), ".show-item-info .info-wrap", true, show);

        function show() {
            let value = $(".show-item-info .graphs-stock:eq(0) .desc").text().replaceAll("$", "").replaceAll(",", "");

            let valueCredits = (value * amount) / points;
            $(".show-item-info .info-cont:eq(1) > li:eq(3)").html(`<div class='title'>Money / Credit:</div><div class='desc'>$${valueCredits.format()}</div><div class='clear'></div>`);
        }
    } else {
        dkklog.debug("other", item);
    }
}