Premium Exchange - Buy Resources

Automatically buy resources up to a predefined amount of resources

目前為 2018-11-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Premium Exchange - Buy Resources
// @description  Automatically buy resources up to a predefined amount of resources
// @author       FunnyPocketBook
// @version      2.0.3.1
// @include      https://*/game.php*screen=market*
// @namespace    https://greasyfork.org/users/151096
// ==/UserScript==
const incoming = "Incoming"; // Portuguese: const incoming = "Entrada";
const timeout = 9000;
let topUp, price, stack;
let start = false;

createInput();

function createInput() {
    "use strict";
    const userInputParent = _ID("premium_exchange_form"); // Parent element

    // Create input for setting how much res should be bought
    const divScript = document.createElement("div");
    divScript.setAttribute("id", "divScript");
    userInputParent.parentNode.insertBefore(divScript, userInputParent);
    _ID("divScript").innerHTML = "<p>Top up warehouse to: <input id='topUpInput'> " +
        "<button id='topUpOk' class='btn'>OK</button><span id='topUpText'></span></p><p>Buy when price above: <input id='priceInput'> " +
        "<button id='priceOk' class='btn'>OK</button><span id='priceText'></span></p>" +
        "<p>Buy max this much at once: <input id='stackInput'> <button id='stackOk' class='btn'>OK</button><span id='stackText'></span></p>" +
        "<p>Buy resources:</p><p><input type=\"checkbox\" name=\"wood\" id=\"woodCheck\"> Wood <input type=\"checkbox\" " +
        "name=\"stone\" id=\"stoneCheck\"> Stone <input type=\"checkbox\" name=\"iron\" id=\"ironCheck\"> Iron</p>" +
        "<p><button id='start' class='btn'></button></p>";
    if (!start) {
        _ID("start").innerHTML = "Start";
    } else {
        _ID("start").innerHTML = "Stop";
    }
    if (localStorage.topUp) {
        _ID("topUpInput").value = localStorage.topUp;
        topUp = localStorage.topUp;
    }
    if (localStorage.price) {
        _ID("priceInput").value = localStorage.price;
        price = localStorage.price;
    }
    if (localStorage.stack) {
        _ID("stackInput").value = localStorage.stack;
        stack = localStorage.stack;
    }
}

_ID("topUpOk").addEventListener("click", function() {
    topUp = _ID("topUpInput").value;
    localStorage.topUp = topUp;
    _ID("topUpText").innerHTML = "Top up to " + topUp;
});
_ID("priceOk").addEventListener("click", function() {
    price = _ID("priceInput").value;
    localStorage.price = price;
    _ID("priceText").innerHTML = "Buy when price above " + price;
});
_ID("stackOk").addEventListener("click", function() {
    stack = _ID("stackInput").value;
    localStorage.stack = stack;
    _ID("stackText").innerHTML = "Buy only " + stack + " resources at once";
});
_ID("start").addEventListener("click", function() {
    if (start) {
        start = false;
        _ID("start").innerHTML = "Start";
    } else {
        start = true;
        _ID("start").innerHTML = "Stop";
        buyRes();
    }
});

_ID("topUpInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#topUpOk"));
_ID("priceInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#priceOk"));
_ID("stackInput").addEventListener("keydown", clickOnKeyPress.bind(this, 13, "#stackOk"));

let warehouse = game_data.village.res[6];
let wood = game_data.village.wood;
let stone = game_data.village.stone;
let iron = game_data.village.iron;
let woodPrice = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
let stonePrice = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
let ironPrice = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
let woodInc = 0;
let stoneInc = 0;
let ironInc = 0;
const buyWoodInput = __("#premium_exchange_buy_wood > div:nth-child(1) > input");
const buyStoneInput = __("#premium_exchange_buy_stone > div:nth-child(1) > input");
const buyIronInput = __("#premium_exchange_buy_iron > div:nth-child(1) > input");

if (start) {
    buyRes();
}
const interval = setInterval(function() {
    if (start && (!document.querySelector("#fader") || document.querySelector("#fader").style.display === "none")) {
        buyRes();
    }
}, timeout);

function buyRes() {
    getRes();
    if (wood + woodInc < topUp || stone + stoneInc < topUp || iron + ironInc < topUp) {
        if (woodPrice > price && wood + woodInc < topUp && __("#woodCheck").checked) {
            // Buy wood
            let buyWoodAmnt = topUp - wood - woodInc;
            if (buyWoodAmnt <= 0) {
                buyWoodAmnt = woodPrice - 2;
            }
            if(buyWoodAmnt > stack) {
                buyWoodAmnt = stack;
            }
            buyStoneInput.value = "";
            buyIronInput.value = "";
            buyWoodInput.value = buyWoodAmnt;
            clickBuy();
        } else if (stonePrice > price && stone + stoneInc < topUp && __("#stoneCheck").checked) {
            // Buy stone
            let buyStoneAmnt = topUp - stone - stoneInc;
            if (buyStoneAmnt <= 0) {
                buyStoneAmnt = stonePrice - 2;
            }
            if(buyStoneAmnt > stack) {
                buyStoneAmnt = stack;
            }
            buyWoodInput.value = "";
            buyIronInput.value = "";
            buyStoneInput.value = buyStoneAmnt;
            clickBuy();
        } else if (ironPrice > price && iron + ironInc < topUp && __("#ironCheck").checked) {
            // Buy iron
            let buyIronAmnt = topUp - iron - ironInc;
            if (buyIronAmnt <= 0) {
                buyIronAmnt = ironPrice - 2;
            }
            if(buyIronAmnt > stack) {
                buyIronAmnt = stack;
            }
            buyWoodInput.value = "";
            buyStoneInput.value = "";
            buyIronInput.value = buyIronAmnt;
            clickBuy();
        }
    }
}

function clickBuy() {
    __("#premium_exchange_form > input").click();
    setTimeout(function() {
        __("#premium_exchange > div > div > div.confirmation-buttons > button.btn.evt-confirm-btn.btn-confirm-yes").click();
    }, 1000);
}

function _ID(selector) {
    return document.getElementById(selector);
}

function __(selector) {
    return document.querySelector(selector);
}

function getRes() {
    let parentInc;
    warehouse = game_data.village.res[6];
    wood = game_data.village.wood;
    stone = game_data.village.stone;
    iron = game_data.village.iron;
    woodPrice = parseInt(__("#premium_exchange_rate_wood > div:nth-child(1)").innerText);
    stonePrice = parseInt(__("#premium_exchange_rate_stone > div:nth-child(1)").innerText);
    ironPrice = parseInt(__("#premium_exchange_rate_iron > div:nth-child(1)").innerText);
    try {
        if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
            parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(1)");
        }
    } catch(e) {}
    try {
        if (__("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)").innerHTML.split(" ")[0].replace(":", "") === incoming) {
            parentInc = __("#market_status_bar > table:nth-child(2) > tbody > tr > th:nth-child(2)");
        }
    } catch(e) {}

    try {
        woodInc = setZeroIfNaN(parseInt(parentInc.querySelector(".wood").parentElement.innerText.replace(".", "")));
    } catch (e) {}
    try {
        stoneInc = setZeroIfNaN(parseInt(parentInc.querySelector(".stone").parentElement.innerText.replace(".", "")));
    } catch (e) {}
    try {
        ironInc = setZeroIfNaN(parseInt(parentInc.querySelector(".iron").parentElement.innerText.replace(".", "")));
    } catch (e) {}
}

function clickOnKeyPress(key, selector) {
    "use strict";
    if (event.defaultPrevented) {
        return; // Should do nothing if the default action has been cancelled
    }
    let handled = false;
    if (event.key === key) {
        document.querySelector(selector).click();
        handled = true;
    } else if (event.keyIdentifier === key) {
        document.querySelector(selector).click();
        handled = true;
    } else if (event.keyCode === key) {
        document.querySelector(selector).click();
        handled = true;
    }
    if (handled) {
        event.preventDefault();
    }
}

function setZeroIfNaN(x) {
    "use strict";
    if ((typeof x === 'number') && (x % 1 === 0)) {
        return x;
    } else {
        return 0;
    };
}