User Tools

Site Tools


uab-bot

Cargar jQuery:

(function() {
    if(typeof jQuery=='undefined') {
        var headTag = document.getElementsByTagName("head")[0];
        var jqTag = document.createElement('script');
        jqTag.type = 'text/javascript';
        jqTag.src = 'http://code.jquery.com/jquery-2.2.1.min.js';
        headTag.appendChild(jqTag);
    }
}());

Cargar Bot:

(function(window) {
    var Bot = {
        prefered: {
            1: [40, 23, 14, 10, 8],
            2: [65, 57],
            3: []
        },
        inCity: {
            1: [],
            2: [],
            3: [],
        },
        aiLoaded: false,
        minHpBeforeHeal: 500,
        contenido: null,
        menu: null,
        eventsLoaded: false,
        opponent: null,
        battleType: 1,
        stopped: true,
        delay: 0,
        setDelay: function(d) {
            this.delay = d;
        },
        setMinHp: function(hp) {
            this.minHpBeforeHeal = hp;
        },
        setPrefered: function(prefered, type) {
            type = type || this.battleType;
            this.prefered[type] = prefered;
        },
        vs: function(type) {
            this.battleType = type;
        },
        stop: function() {
            this.stopped = true;
            Bot.menu.contents().find('#panel button:last-child').text('Start');
        },
        loadElements: function() {
            this.contenido = $('frame[name=contenido]');
            this.menu = $('frame[name=menu]');
        },
        loadEvents: function() {
            $(this.menu).on('load', function() {
                Bot.loadPanel();
                if (Bot.stopped) {
                    return false;
                }
                if (Bot.needHeal()) {
                    Bot.heal();
                }
            });
            $(this.contenido).on('load', function() {
                if (Bot.stopped) {
                    return false;
                }
                var $src = $(this).attr('src');
                var $listUrl = '/uab/index.php?accion=batalla&batalla=' + Bot.battleType;
                if (Bot.battleType == 1) {
                    $listUrl += '&letra=@';
                }
                if ($src == $listUrl) {
                    Bot.checkInCity(function() {
                        $.each(Bot.prefered[Bot.battleType], function(i, op) {
                            if ($.inArray(op, Bot.inCity[Bot.battleType]) > -1) {
                                Bot.fight(op);
                                return false;
                            }
                        });
                    });
                } else if ($src == '/uab/index.php?accion=batalla&batalla=' + Bot.battleType + '&rival=' + Bot.opponent + '&confirma=1') {
                    Bot.hit();
                } else if ($src == '/uab/index.php?accion=medico') {
                    Bot.start();
                }
            });
            this.eventsLoaded = true;
        },
        checkInCity: function(callback) {
            if (this.battleType == 1) {
                Bot.inCity[Bot.battleType] = [];
                $select = Bot.contenido.contents().find('select');
                $('option', $select).each(function() {
                    Bot.inCity[Bot.battleType].push(parseInt($(this).val()));
                });
            } else {
                if (!Bot.aiLoaded) {
                    var $tds = Bot.contenido.contents().find('body > center > center > table > tbody > tr:nth-child(2) > td > center > font > table > tbody > tr > td');
                    $.each($tds, function(i, el) {
                        var $parts = $(el).find('a').attr('href').split('?')[1].split('&');
                        $.each($parts, function(i, el) {
                            var $split = el.split('=');
                            if ($split[0] == 'rival') {
                                Bot.inCity[Bot.battleType].push(parseInt($split[1]));
                            }
                        });
                    });
                    Bot.aiLoaded = true;
                    if (Bot.inCity[Bot.battleType].length == 0) {
                        Bot.stop();
                    }
                }
            }
            callback();
        },
        hit: function() {
            $input = Bot.contenido.contents().find('input[name=reply]');
            if ($input.length > 0) {
                $input.click();
            } else {
                Bot.opponent = null;
                setTimeout(function() {
                    Bot.start();
                }, (Bot.delay * 1000));
            }
        },
        fight: function(id) {
            if (this.stopped) {
                return false;
            }
            Bot.opponent = id;
            $(this.contenido).attr('src', '/uab/index.php?accion=batalla&batalla=' + Bot.battleType + '&rival=' + Bot.opponent + '&confirma=1');
        },
        needHeal: function() {
            $hp = $('frame[name=menu]').contents().find('body > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(2) > table > tbody > tr > td > font > table:nth-child(11) > tbody > tr:nth-child(1) > td:nth-child(1) > font > i').text();
            return ($hp.split('/')[0] < Bot.minHpBeforeHeal);
        },
        heal: function() {
            $(this.contenido).attr('src', '/uab/index.php?accion=medico');
        },
        loadPanel: function() {
            var $panel, $br, $hpLabel, $preferedLabel, $vsLabel, $vsSelect, $delayLabel;
            $panel = $('<div/>').attr('id', 'panel').css({
                width: '75%',
                margin: '10px auto 0 0',
                minHeight: '100px',
                padding: '10px'
            });
            $br = $('<br/>');
            $vsLabel = $('<label/>').text('Vs: ');
            $vsSelect = $('<select/>').change(function() {
                var bt = $(this).val();
                Bot.stop();
                Bot.vs(bt);
                Bot.menu.attr('src', '/uab/index.php?accion=menu');
            });
            var $vs1 = $('<option/>').val(1).text('Personas');
            if (Bot.battleType == 1) {
                $vs1.attr('selected', 'selected');
            }
            $vs1.appendTo($vsSelect);
            var $vs2 = $('<option/>').val(2).text('AI');
            if (Bot.battleType == 2) {
                $vs2.attr('selected', 'selected');
            }
            $vs2.appendTo($vsSelect);
            $vs3 = $('<option/>').val(3).text('Lideres');
            if (Bot.battleType == 3) {
                $vs3.attr('selected', 'selected');
            }
            $vs3.appendTo($vsSelect);
            $vsSelect.appendTo($vsLabel);
            $vsLabel.appendTo($panel);
            $br.clone().appendTo($panel);
            $hpLabel = $('<label/>').text('Min HP: ');
            $('<input/>').attr({
                type: 'text',
                size: 4,
                id: 'hp'
            }).val(Bot.minHpBeforeHeal).appendTo($hpLabel);
            $hpLabel.appendTo($panel);
            $br.clone().appendTo($panel);
            $delayLabel = $('<label/>').text('Delay: ');
            $('<input/>').attr({
                type: 'text',
                size: 2
            }).val(Bot.delay).appendTo($delayLabel);
            $delayLabel.appendTo($panel);
            $br.clone().appendTo($panel);
            var $preferedLabel = $('<label/>').text('Prefered: ');
            $('<input/>').attr({
                type: 'text',
                size: 10,
                id: 'prefered'
            }).val(Bot.prefered[Bot.battleType].toString()).appendTo($preferedLabel);
            $preferedLabel.appendTo($panel);
            $br.clone().appendTo($panel);
            $('<button/>').text('Save').css({
                margin: '10px 5px 0 0'
            }).click(function() {
                var $button = $(this);
                Bot.setMinHp(parseInt($hpLabel.find('input').val()));
                Bot.setDelay(parseInt($delayLabel.find('input').val()));
                var p = [];
                $.each($preferedLabel.find('input').val().split(','), function(i, e) {
                    p.push(parseInt(e));
                });
                Bot.setPrefered(p);
                $button.text('Saved...').attr('disabled', 'disabled');
                setTimeout(function() {
                    $button.removeAttr('disabled');
                    $button.text('Save');
                }, 1000);
            }).appendTo($panel);
            $('<button/>').text(Bot.stopped ? 'Start' : 'Stop').click(function() {
                if (Bot.stopped) {
                    Bot.start();
                    $(this).text('Stop');
                } else {
                    Bot.stop();
                    $(this).text('Start');
                }
            }).appendTo($panel);
            $panel.appendTo(Bot.menu.contents().find('body'));
        },
        start: function() {
            this.stopped = false;
            if (Bot.contenido.attr('src') == '/uab/index.php?accion=batalla&batalla=' + Bot.battleType + '&rival=' + Bot.opponent + '&confirma=1') {
                Bot.hit();
                return true;
            }
            if ((this.battleType == 2 || this.battleType == 3) && this.aiLoaded) {
                $.each(Bot.prefered[Bot.battleType], function(i, op) {
                    if ($.inArray(op, Bot.inCity[Bot.battleType]) > -1) {
                        Bot.fight(op);
                        return false;
                    }
                });
            } else {
                var $listUrl = '/uab/index.php?accion=batalla&batalla=' + Bot.battleType;
                if (Bot.battleType == 1) {
                    $listUrl += '&letra=@';
                }
                $(this.contenido).attr('src', $listUrl);
            }
        }
    };
    Bot.loadElements();
    Bot.loadEvents();
    Bot.loadPanel();
    window.Bot = Bot;
}(window));
uab-bot.txt · Last modified: 2016/06/07 16:37 by osiux