MrDastan / Hools AutoBot Plugin

// ==UserScript==
// @name         Hools AutoBot Plugin
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Plugin for Hools.
// @author       MrDastan (skype: Dastan_Danik)
// @match        http://hools.mgates.ru/*
// @match        http://hools.mobi/*
// @match        http://hools.mg.wapos.ru/*
// @match        http://hools.play.tegos.ru/*
// @match        http://hools.games.nasimke.ru/*
// @match        http://hools.my.mgates.spaces.ru/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_addStyle
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
/* jshint -W097 */
'use strict';

GM_addStyle ( "#setting-button-container { padding-bottom: 5px; text-align: center; height: 24px; margin: 1px } "
             +".settings-button { display: inline-block; margin: 2px; border: 1px solid; background-color: #585858 }"
             + ".settings-button:hover { background-color: #A8A8A8 }" );

var me = {};

me.getLevel = parseInt($('.h_level a').text().split('(')[0]);
me.getMoney = $('.h_money a').text();
me.getMoney2 = $('.h_money2').text();
me.getStuff = $('.h_stuff').text();
me.getEnergy = parseInt($('.h_energy a').text().split('/')[0]);
me.getHealth = parseInt($('.h_health a').text().split('/')[0]);
me.getAggression = parseInt($('.h_aggression a').text().split('/')[0]);
me.getAngry = parseInt($('.h_angry a').text().split('/')[0]);

var Settings = function(profileName){
    var settings = GM_getValue('hools_' + profileName, {});

    this.getValue = function (key, defaultValue) {

        if (settings[key] !== undefined) {
            return settings[key];
        }
        else {
            return defaultValue;
        }
    };

    this.setValue = function (key, value) {
        settings[key] = value;
        this.save();
    };

    this.save = function(){
        GM_setValue('hools_' + profileName, settings);
    };
};

$(function() {

    var settings = new Settings( $('#i a')[0].innerText );

    if (settings.getValue("useRefresh", true))
    {
        setTimeout(function() {
            window.location.reload()
        },60000)
    }

    switch (window.location.pathname) {
        case '/user':
            /*Главная страница*/
            setTimeout(function() {

                settings.setValue('level', me.getLevel);

                if ($('#c .notice .center a').length !== 0 && settings.getValue("useHappyHour", true)) {
                    var jobLink = $('#c .notice .center a').first().attr('href');
                    if (jobLink) {
                        document.location = jobLink;
                    }
                } else if ($('.left .caption').text().indexOf('[') !== -1 && settings.getValue("useQuests", true)) {
                    document.location = '/user/quests';
                } else if (me.getHealth >= 90 && me.getAggression >= 15 && settings.getValue("useFight", true)) {
                    document.location = '/user/fight';
                } else if (me.getEnergy >= 20 && settings.getValue("useOutwork", true)) {
                    document.location = '/user/outwork';
                } else if (settings.getValue("useBattle", true) && $('#hdr .notice')[0].innerText.indexOf('Динамо') != -1) {
                    document.location = '/user/battle';
                } else if (settings.getValue("useSoccerGame", true)) {
                    document.location = '/user/soccer_game';
                } else {
                    document.location = '/user';
                }

            }, 10000);
            break;

        case '/user/outwork':
            /*Работа*/
        case '/user/outwork/index':
        case '/user/outwork/details':
            var moveLinkValid = $('.actions a').length;
            if (me.getEnergy >= 20 && moveLinkValid !== 0 && settings.getValue("useOutwork", true)) {
                var jobLink = $('.actions a').first().attr('href');
                document.location = jobLink;
            } else if (settings.getValue("useOutwork", true)) {
                document.location = '/user';
            }
            break;

        case '/user/soccer_game':
            /*Настольный футбол*/
            if ($('.info').length == 0 && settings.getValue("useSoccerGame", true)) {
                var gameLink = $('.actions a').first().attr('href');
                if (gameLink) {
                    document.location = gameLink;
                }
            } else if (settings.getValue("useSoccerGame", true)) {
                document.location = '/user';
            }
            break;

        case '/user/soccer_game/play':
            if (settings.getValue("useSoccerGame", true)) {
                var move = settings.getValue("lastMove", 1);
                settings.getValue("lastMove", move ^ 1);
                var moveLink = $('.items li a').eq(move).attr('href');
                if (moveLink) {
                    document.location = moveLink;
                }
                var resLink = $('.actions a').first().attr('href');
                if (resLink) {
                    GM_deleteValue("lastMove");
                    document.location = resLink;
                }
            }
            break;

        case '/user/fight':
            /*Район*/
            if ($(".notice").length === 1)
            {
                if (settings.getValue("useBattle", true) && $(".notice")[0].innerText.indexOf('месте'))
                {
                    document.location = '/user/battle';
                }
            }
            else if (me.getHealth >= 90 && me.getAggression >= 15 && settings.getValue("useFight", true) ) {
                var _one = $("h3")[0].outerText;
                var _oneHref = $(".img_action a")[1].href;
                var _tho = $("h3")[1].outerText;
                var _thoHref = $(".img_action a")[3].href;
                var _tre = $("h3")[2].outerText;
                var _treHref = $(".img_action a")[5].href;
                if ("Егор" == _one || "Фома" == _one || "Кузьминишна" == _one || "Бородач А.Р." == _one || "Копченый" == _one) {
                    document.location = _oneHref;
                } else if ("Егор" == _tho || "Фома" == _tho || "Кузьминишна" == _tho || "Бородач А.Р." == _tho || "Копченый" == _tho) {
                    document.location = _thoHref;
                } else if ("Егор" == _tre || "Фома" == _tre || "Кузьминишна" == _tre || "Бородач А.Р." == _tre || "Копченый" == _tre) {
                    document.location = _treHref;
                } else {
                    var _oneLevelPlayer = parseInt(/\[(\d+)\]/g.exec(_one)[1]);
                    var _thoLevelPlayer = parseInt(/\[(\d+)\]/g.exec(_one)[1]);
                    var _treLevelPlayer = parseInt(/\[(\d+)\]/g.exec(_one)[1]);
                    if (me.getLevel == _oneLevelPlayer) {
                        document.location = _oneHref;
                    } else if (me.getLevel == _thoLevelPlayer) {
                        document.location = _thoHref;
                    } else if (me.getLevel == _treLevelPlayer) {
                        document.location = _treHref;
                    } else {
                        document.location = '/user/fight';
                    }
                }
            } else if (settings.getValue("useFight", true)) {
                document.location = '/user';
            }
            break;

        case '/user/fight/result':
            if (settings.getValue("useFight", true)) {
                document.location = '/user/fight';
            }
            break;

        case '/user/quests':
            /*Задания*/
            if ($('.actions').length != 0 && settings.getValue("useQuests", true)) {
                var moveLink = $('.actions a')[4];
                var moveLink2 = $('.actions a')[2];
                if (moveLink) {
                    document.location = moveLink.href;
                } else if (moveLink2) {
                    document.location = moveLink2.href;
                }
            } else if (settings.getValue("useQuests", true)) {
                document.location = '/user';
            }
            break;

        case '/user/quests/details':
            if (settings.getValue("useQuests", true)) {
                document.location = '/user/quests';
            }
            break;

        case '/user/battle':
            /*бм*/
            if ($('.actions a').length == 0) {
                setTimeout(function() {
                    window.location.reload()
                },10000)
            } else if ($('.actions a').length == 1) {
                settings.setValue("useBattle", false);
                document.location = '/user';
            } else if($('.actions a').length == 2) {
                if(settings.getValue("useBattle", true))
                {
                    if (me.getAngry === 100) {
                        document.location = $('.actions a')[0].href;
                    } else {
                        document.location = '/user/battle';
                    }
                }
            }
            break;

        case '/user/battle/battle_space':
            if (settings.getValue("usebattle", true)) {
                document.location = $('.item_button a')[1].href;
            }
            break;

        case '/user/stadium/tickets':
            /*Касса*/
            if (settings.getValue("useTickets", true)) {
                console.log($('h3')[0].textContent);
            }
            break;

        case '/user/workshop/category':
            /*Пром зона*/
            setTimeout(function() {
                if ($('.actions a').length != 0 && settings.getValue("useWorkshop", true)) {
                    document.location = $('.actions a')[0].href;
                } else {
                    location.reload(true);
                }
            }, 10000);
            break;

        case '/user/shop/category':
            /*Барыга*/
            var host = 'http://hools.mgates.ru/';
            var host1 = 'http://hools.mobi/';
            var host2 = 'http://hools.mg.wapos.ru/';
            var host3 = 'http://hools.play.tegos.ru/';
            var host4 = 'http://hools.games.nasimke.ru/';
            var host5 = 'http://hools.my.mgates.spaces.ru/';
            var linkWeapon = 'user/shop/category?id=weapon';
            var linkWear = 'user/shop/category?id=wear';

            if (location.href == host + linkWeapon || host1 + linkWeapon || host2 + linkWeapon || host3 + linkWeapon || host4 + linkWeapon || host5 + linkWeapon) {
                setTimeout(function() {
                    if (settings.getValue("useWeapon", true)) {
                        if (me.getLevel === 8 && settings.getValue("useShop", true)) {
                            document.location = $('.items:last a')[1].href;
                            settings.setValue("useWeapon", !settings.getValue("useWeapon", true));
                        } else {
                            location.reload(true);
                        }
                    }
                }, 10000);
            }
            if (location.href == host + linkWear || host1 + linkWear || host2 + linkWear || host3 + linkWear || host4 + linkWear || host5 + linkWear) {
                setTimeout(function() {
                    if (settings.getValue("useWear", true)) {
                        if (me.getLevel === 8 && settings.getValue("useShop", true)) {
                            document.location = $('.items:last a')[1].href;
                            settings.setValue("useWeapon", !settings.getValue("useWeapon", true));
                        } else {
                            location.reload(true);
                        }
                    }
                }, 10000);
            }
            break;

    }

    var ui = (function() {
        //init
        var container = $('<div>')
        .attr('id', 'setting-button-container')
        $('#hdr_i').after(container);
        //end:init

        var api = {};

        api.appendBoolButton = function(buttonData) {
            var button = $('<div>');

            var updateButtonStatus = function() {
                if(settings.getValue(buttonData.key, true)) {
                    button.css('border-color', 'blue');
                } else {
                    button.css('border-color', 'red');
                }
            };

            button.addClass('settings-button')
                .html($('<img>')
                      .attr('src', '/images/' + buttonData.icon + '.gif')
                      .attr('title', buttonData.title)
                      .css('width','20px')
                      .css('height','20px'))
                .click(function() {
                settings.setValue(buttonData.key, !settings.getValue(buttonData.key, true));
                updateButtonStatus();
            });
            updateButtonStatus();


            container.append(button)
        };

        return api;
    })();

    ui.appendBoolButton({title: 'Обновление',        key: 'useRefresh',    icon: 'buttons/refresh'});
    ui.appendBoolButton({title: 'Драки',             key: 'useFight',      icon: 'icons/i_fight'});
    ui.appendBoolButton({title: 'Работа',            key: 'useOutwork',    icon: 'icons/i_outwork'});
    ui.appendBoolButton({title: 'Настольный футбол', key: 'useSoccerGame', icon: 'icons/i_soccer_game'});
    ui.appendBoolButton({title: 'Счасливый час',     key: 'useHappyHour',  icon: 'bonus_box'});
    ui.appendBoolButton({title: 'Квесты',            key: 'useQuests',     icon: 'icons/i_quests'});
    ui.appendBoolButton({title: 'БМ',                key: 'useBattle',     icon: 'icons/i_match'});
    ui.appendBoolButton({title: 'Касса',             key: 'useTickets',    icon: 'icons/i_tickets'});
    ui.appendBoolButton({title: 'Пром зона',         key: 'useWorkshop',   icon: 'icons/i_workshop'});
    ui.appendBoolButton({title: 'Барыга',            key: 'useShop',       icon: 'icons/i_huckster'});
    ui.appendBoolButton({title: 'Оружие',            key: 'useWeapon',     icon: 'icons/i_sc_weapon'});
    ui.appendBoolButton({title: 'Одежда',            key: 'useWear',       icon: 'icons/i_sc_wear'});

});