Trimps tools

Trimps tools (visual)

目前為 2017-03-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Trimps tools
// @namespace    trimps.github.io
// @version      1.052
// @description  Trimps tools (visual)
// @author       Anton
// @match        https://trimps.github.io
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';
    console.log(GM_info);
    var tAutoBuy, tStyleFix, tPassiveWatcher, $ = jQuery, isStarted = false,
        version = typeof GM_Info == 'function' ? GM_Info().script.version : 
            (typeof GM_Info == 'object' ? GM_Info.script.version : '?');

    var _isPortal = function() {
        return ($('#portalWrapper:visible').length > 0);
    }

    var _getBarWidthPercent = function(barSelector) {
        return jQuery(barSelector).width() / jQuery(barSelector).parent().width() * 100;
    }

    var _log = function(mes, type) {
        if (typeof type === 'undefined') type = "Story";
        message("BOT: " + mes, type);
    }

    var _start = function() {
        clearInterval(tPassiveWatcher);
        
        tAutoBuy = setInterval(function() {
            if (_isPortal()) return;
            
            var cnt = _autoUpgrade() + _autoBuy() + _autoJobs();
            if (cnt > 0) {
                tooltip('hide');
            }
        }, 1000);
        $('#botStart').text('Bot stop');
    }

    var _buyStorage = function() {
        var cnt = 0;
        var food = _getBarWidthPercent('#foodBar');
        var wood = _getBarWidthPercent('#woodBar');
        var metal = _getBarWidthPercent('#metalBar');
        if (food > 95) {
            $('#Barn').trigger('click');
            cnt++;
        }
        if (wood > 95) {
            $('#Shed').trigger('click');
            cnt++;
        }
        if (metal > 95) {
            $('#Forge').trigger('click');
            cnt++;
        }
        if (cnt > 0) {
            tooltip('hide');
        }
    }

    var _passiveWatcher = function() {
        if (_isPortal()) return;

        _buyStorage();
        
        if (game.global.buildingsQueue.length > 0 && game.global.autoCraftModifier < 1) {
            setGather('buildings');
            return;
        }

        if (getPsString('food', true) < 1 && game.resources.food.owned < 30) {
            $('#foodCollectBtn').trigger('click');
            return;
        }
        
        if (getPsString('wood', true) < 1 && game.resources.wood.owned < 30) {
            $('#woodCollectBtn').trigger('click');
            return;
        }
        
        if (game.buildings.Trap.owned < 1 && game.resources.food.owned >= 10 && game.resources.wood.owned >= 10) {
            buyBuilding('Trap');
            return;
        }
        
        if (game.resources.trimps.owned < game.resources.trimps.realMax() && game.global.breedTime > 5) {
            setGather('trimps');
            return;
        }
        
        if (getPsString('science', true) < 2 && game.resources.science.owned < 100) {
            $('#scienceCollectBtn').trigger('click');
            return;
        }

        if (getPsString('metal', true) < 2 && game.resources.metal.owned < 100) {
            $('#metalCollectBtn').trigger('click');
            return;
        }
    }

    var _stop = function() {
        clearInterval(tAutoBuy);
        $('#botStart').text('Bot stop');
        
        tPassiveWatcher = setInterval(_passiveWatcher, 1000);
        _log('Passive watcher started version ' + version);
    }
    
    var _onStartButton = function() {
        if (isStarted) {
            _stop();
            _log('Stop.');
            isStarted = false;
        } else {
            _start();
            _log('Started!');
            isStarted = true;
        }
    }

    var _styleUpdate = function() {
        // remove counts
        $('head').append('<style type="text/css">.queueItem,.btn{padding:0}.thingColorCanNotAfford.upgradeThing{background-color:#530053;}</style>');
        // remove tabs
        $('#buyTabs').hide();
        filterTabs('all');
        // remove captions
        $('#buildingsTitleDiv,#upgradesTitleDiv,#equipmentTitleDiv').hide();
        // fix height
        $('#topRow,#queueContainer').css('margin-bottom', '0');
        $('#jobsTitleDiv').css('padding', '0').css('font-size', 'smaller');
        $('#buyHere').css('margin', '0').css('padding', '0').css('overflow-x', 'hidden');
        $('#queueContainer').css('height', '70px');
        $('#numTabs').css('margin', '0');
        $('#buyContainer').css('height', 'calc(99vh - 20vw - 96px)');
        // add button
        $('#settingsTable tr').append('<td class="btn btn-info" id="botStart">Bot start</td>');
        $('#botStart').click(_onStartButton);
    }

    var _autoUpgrade = function() {
        var $item = $('#upgradesHere').find('.thingColorCanAfford').first();
        if ($item.length > 0) {
            $item.trigger( "click" );
            _log('Upgrading ' + $item.attr('id'));
            return 1;
        } else {
            return 0;
        }
    }

    var _autoBuy = function() {
        _buyStorage();
        
        var nursery = '';
        if (game.buildings.Nursery.owned >= game.buildings.Tribute.owned) {
            nursery = ':not([id=Nursery])';
        }
        var notStorage = ':not([id=Barn]):not([id=Shed]):not([id=Forge])';
        var $item = $('#buildingsHere').find('.thingColorCanAfford:not([id=Wormhole])' + nursery + notStorage).last();
        if ($item.length > 0 && $item.attr('id') != 'Trap') {
            $item.trigger("click");
            _log('Building ' + $item.attr('id'));
            return 1;
        } else {
            return 0;
        }
    }

    var _buyJobs = function($obj, unemployed, objName) {
        if ($obj.length > 0) {
            var cnt = 1;
            if (unemployed > 61*10) {numTab(4);cnt=100;}
            else if (unemployed > 61*2.5) {numTab(3);cnt=25;}
            else if (unemployed > 61) {numTab(2);cnt=10;}
            $obj.trigger("click");
            if (unemployed > 61) numTab(1); // +1
            _log('New ' + objName + (cnt > 1 ? " x" + cnt : ''), 'Combat');
            return cnt;
        } else {
            return 0;
        }
    }

    var _autoJobs = function() {
        var cnt = 0;
        var $trainer = $('#jobsHere').find('.thingColorCanAfford[id=Trainer]');
        if ($trainer.length > 0) {
            $trainer.trigger("click");
            _log('New trainer');
            cnt++;
        }
        var $explorer = $('#jobsHere').find('.thingColorCanAfford[id=Explorer]');
        if ($explorer.length > 0) {
            $explorer.trigger("click");
            _log('New explorer');
            cnt++;
        }
        
        var farmer = parseInt($('#jobsHere').find('#FarmerOwned').text());
        var lumber = parseInt($('#jobsHere').find('#LumberjackOwned').text());
        var miner = parseInt($('#jobsHere').find('#MinerOwned').text());
        var science = parseInt($('#jobsHere').find('#ScientistOwned').text());
        var jobsTotal = farmer + lumber + miner + science;
        
        var unemployed = parseInt($('#jobsTitleUnemployed').text());
        
        if (farmer < (jobsTotal / 3.05)) {
            var $farmer = $('#jobsHere').find('.thingColorCanAfford[id=Farmer]');
            cnt += _buyJobs($farmer, unemployed, 'farmer');
        } else if (lumber < (jobsTotal / 3.05)) {
            var $lumber = $('#jobsHere').find('.thingColorCanAfford[id=Lumberjack]');
            cnt += _buyJobs($lumber, unemployed, 'lumberjack');
        } else if (miner < (jobsTotal / 3.05)) {
            var $miner = $('#jobsHere').find('.thingColorCanAfford[id=Miner]');
            cnt += _buyJobs($miner, unemployed, 'miner');
        } else if (science < (jobsTotal / 61)) {
            var $science = $('#jobsHere').find('.thingColorCanAfford[id=Scientist]');
            cnt += _buyJobs($science, unemployed, 'scientist');
        }
        
        if (unemployed > 0 && cnt === 0) {
            $farmer = $('#jobsHere').find('.thingColorCanAfford[id=Farmer]');
            cnt += _buyJobs($farmer, unemployed, 'farmer');
        }

        return cnt;
    }

    var _styleFix = function() {
        $('.buyBox').find('.thing').find('br').remove();
        $('.buyBox').find('.thing').find('.thingOwned').css('margin-left','4px');
        $('#upgradesHere').find('.alert.badge').text('');
    }

    setTimeout(function() {
        tStyleFix = setInterval(_styleFix, 2000);
        
        _styleUpdate();
        _styleFix();
        
        _stop(); // start passive watcher
        
    }, 1000);

})();