Greasy Fork

GOTA_Extender_Auxiliary

Auxiliary functions for the page and the extender.

目前为 2014-10-10 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/5618/20682/GOTA_Extender_Auxiliary.js

// --> Message handling
function log(message, type) {
    if (extender_debugMode && console && console.log
        && typeof (console.log) == "function") {
        if (!type) type = "page";

        var prefix = type.toString().toUpperCase() + " <" + new Date().toLocaleTimeString() + "> ";
        console.log(prefix + message);
    }
}

function error(message, type) {
    if (extender_debugMode && console && console.error
        && typeof (console.error) == "function") {
        if (!type) type = "page";

        var prefix = type.toString().toUpperCase() + " - ERROR <" + new Date().toLocaleTimeString() + "> ";
        console.error(prefix + message);
    }
}

function warn(message, type) {
    if (extender_debugMode && console && console.warn
        && typeof (console.warn) == "function") {
        if (!type) type = "page";

        var prefix = type.toString().toUpperCase() + " - WARNING <" + new Date().toLocaleTimeString() + "> ";
        console.warn(prefix + message);
    }
}
// <-- Message handling

var extender = {
    command: function (name, args) {
        var cmd = { name: name, args: args };
        $("textarea#observable").attr("command", JSON.stringify(cmd));
    },
    lastcommand: function () {
        return $("textarea#observable").attr("command");
    },
    option: function (option, val) {
        if (typeof option == "string") {
            this.command("option", [option, val]);
        } else {
            this.error("Please specify name and value as parameters.");
        }
    }
};

var productionQueue = [];
function attemptProduction() {
    if (!productionQueue || productionQueue.length == 0) {
        log('Attempted production, but queue was missing or empty. Exiting...');
        return;
    }

    for (var i = 0; i < userContext.buildingsData.length; i++) {
        var b = userContext.buildingsData[i];

        if (buildingProducing(b)) {
            log("Building " + b.symbol + " is busy.");
            continue;
        }

        if (buildingFinished(b)) {
            log("Building " + b.symbol + " finished production.");
            doFinishProduction(b.item_id, attemptProduction);
            return;
        }

        var element = getElement(b.symbol);
        if (element) {
            executeElement(element, attemptProduction);
            return;
        }
    }
};

function getElement(buildingSymbol) {
    if (!productionQueue || productionQueue.length == 0) {
        log('Attempted to extract item from queue, but the production queue was missing or empty. Exiting...');
        return null;
    }

    var element;

    for (var i = 0; i < productionQueue.length; i++) {

        if (productionQueue[i].activeBuildingPanel == buildingSymbol) {
            element = productionQueue[i];
            break;
        }
    }

    if (!element) {
        log('No elements enqueued for building ' + buildingSymbol + '. Array size: ' + productionQueue.length);
        return null;
    }

    return element;
};

function executeElement(element, callback) {

    var index = productionQueue.indexOf(element);
    log('Production of element ' + element.name + ' : ' + element.type + ' with index ' + index + ' initiated.');

    if (element.type == "item") {
        userContext.recipeData = element.recipeData;
        userContext.activeBuildingPanel = element.activeBuildingPanel;

        doProduction(element.outputSymbol, element.recipeCategory, null, null, element.recipeName, callback);
        productionQueue.splice(index, 1);

        log('Production details: ' + element.name + ' at ' + element.activeBuildingPanel + ', ' + element.outputSymbol + ', ' + element.recipeCategory + ', ' + element.recipeName + ';');
    } else {

        var buildingId = buildingBySymbol(element.activeBuildingPanel).id;

        applySelectedUpgrade({ building_id: buildingId, id: element.upgradeId, gold: 0, silver: 0 }, null, callback);
        productionQueue.splice(index, 1);

        log('Production details: ' + element.name + ' : ' + element.type + ' at ' + element.activeBuildingPanel + ', ' + element.symbol + ';');

    }
};

function buildingFinished(b) {
    return b.producing_archetype_id && !b.build_remaining;
}

function buildingProducing(b) {
    return b.producing_archetype_id && b.build_remaining;
}

var bruteForceTimeout;
function bruteForce(enabled) {

    if (typeof enabled == "boolean" && !enabled) {
        bruteForceTimeout = clearTimeout(bruteForceTimeout);
        log("Bruting terminated.");
        return;
    }

    if (extender_bruteWounds && extender_bruteWounds == 0) {
        warn("Bruting is disabled. Please set number of max wounds from options to continue.");
        bruteForce(false);
    }

    var s = userContext.setSwornSword;

    if (!s) {
        error("Bruting failed, no sworn sword set.");
        bruteForce(false);
        return;
    }

    if (s.damage < extender_bruteWounds) {

        doAdventure("", s.modifier, false, function () {
            bruteForce(true);
        });

        return;
    }

    if (extender_bruteSwitchOff) {

        warn("Sworn sword recieved " + extender_bruteWounds + " wounds! Brute timer will self terminate.");
        bruteForce(false);
        return;

    }

    // Add a minute to the cooldown, and then multiply by wounds
    var interval = extender_bruteWounds * (s.damage_cooldown + 60);
    warn("Sworn sword recieved " + extender_bruteWounds + " wounds! " +
            "Brute timer will self adjust. Wait " + interval + " seconds.");

    bruteForceTimeout = setTimeout(function () {
        bruteForce(true);
    }, interval * 1000);
}