TweetShootingBot

    @@ -22,23 +22,26 @@ this.screen = document.querySelector('.tpLayer'); this.x = this.screen.clientWidth / 2; this.y = this.screen.clientHeight / 3 * 2; + var rect = document.querySelector('canvas').getBoundingClientRect(); + this.offset_x = rect.left + window.scrollX; + this.offset_y = rect.top + window.scrollY; } Controller.prototype = { move: function(x, y) { this.x = x; this.y = y; - this.dispatch(MouseUtil.mousemove(this.x - 12, this.y - 12)); // -12 ??? + this.dispatch(MouseUtil.mousemove(this.x + this.offset_x, this.y + this.offset_y)); }, moveRelative: function(x, y) { this.move(this.x+x, this.y+y); - this.dispatch(MouseUtil.mousemove(this.x, this.y)); + this.dispatch(MouseUtil.mousemove(this.x + this.offset_x, this.y + this.offset_y)); }, startShot: function() { - this.dispatch(MouseUtil.mousedown(this.x, this.y)); + this.dispatch(MouseUtil.mousedown(this.x + this.offset_x, this.y + this.offset_y)); }, stopShot: function() { - this.dispach(MouseUtil.mouseup(this.x, this.y)); + this.dispach(MouseUtil.mouseup(this.x + this.offset_x, this.y + this.offset_y)); }, dispatch: function(event) { this.screen.dispatchEvent(event); @@ -55,9 +58,10 @@ Bot.prototype = { do: function() { + var self = this; var nears = this.bullets.filter(function(bullet) { - return bullet.enemy && bullet.distance < 80 && bullet.y + 18 < self.controller.y && bullet.y > self.enemy.y; + return bullet.enemy && bullet.distance < 80 && bullet.y + 9 < self.controller.y && bullet.y > self.enemy.y; }).sort(function(a, b) { return a.distance - b.distance }); var speed = 20.0; @@ -66,6 +70,7 @@ this.safeCycle++; } else { this.safeCycle = 0; + } if (nears.length > 1 && self.controller.y > self.enemy.y) { var center = { x: (nears[0].x + nears[1].x) / 2, y: (nears[0].y + nears[1].y) / 2 }; @@ -100,8 +105,8 @@ notifyText: function(text, x, y) { var myTweet = document.querySelector('#myInfo marquee').textContent; this.bullets.push({ text: text, - x: x, - y: y, + x: x + 9, + y: y + 9, distance: Math.sqrt(Math.pow(this.controller.x - x, 2) + Math.pow(this.controller.y - y, 2)), enemy: myTweet.indexOf(text) == -1 });
  • /*
     * @title TweetShootingBot
     * @description TweetShootingBot
     * @include http://tweetshooting.appspot.com/
     * @license MIT License
     * @require
     */
    
    var MouseUtil = { };
    ['mousemove', 'mousedown', 'mouseup', 'click'].forEach(function (method) {
        MouseUtil[method] = function(x, y) {
            if (!x) x = 0;
            if (!y) y = 0;
            var event = document.createEvent("MouseEvents");
            event.initMouseEvent(method, true, false, window,
                                 0, x, y, x, y, false, false, true, false, 0, null );
            return event;
        }
    });
    
    var Controller = function() {
        this.screen = document.querySelector('.tpLayer');
        this.x = this.screen.clientWidth / 2;
        this.y = this.screen.clientHeight / 3 * 2;
        var rect = document.querySelector('canvas').getBoundingClientRect();
        this.offset_x = rect.left + window.scrollX;
        this.offset_y = rect.top + window.scrollY;
    }
    
    Controller.prototype = {
        move: function(x, y) {
            this.x = x;
            this.y = y;
            this.dispatch(MouseUtil.mousemove(this.x + this.offset_x, this.y + this.offset_y));
        },
        moveRelative: function(x, y) {
            this.move(this.x+x, this.y+y);
            this.dispatch(MouseUtil.mousemove(this.x + this.offset_x, this.y + this.offset_y));
        },
        startShot: function() {
            this.dispatch(MouseUtil.mousedown(this.x + this.offset_x, this.y + this.offset_y));
        },
        stopShot: function() {
            this.dispach(MouseUtil.mouseup(this.x + this.offset_x, this.y + this.offset_y));
        },
        dispatch: function(event) {
            this.screen.dispatchEvent(event);
        }
    };
    
    var Bot = function(player) {
        this.controller = new Controller();
        var self = this;
        setInterval(function() {
            self.do();
        }, 50);
    }
    
    Bot.prototype = {
        do: function() {
    
            var self = this;
            var nears = this.bullets.filter(function(bullet) {
                return bullet.enemy && bullet.distance < 80 && bullet.y + 9 < self.controller.y && bullet.y > self.enemy.y;
            }).sort(function(a, b) { return a.distance - b.distance });
    
            var speed = 20.0;
    
            if (nears.length == 0) {
                this.safeCycle++;
            } else {
                this.safeCycle = 0;
    
            }
            if (nears.length > 1 && self.controller.y > self.enemy.y) {
                var center = { x: (nears[0].x + nears[1].x) / 2, y: (nears[0].y + nears[1].y) / 2 };
                var dx = center.x - self.controller.x;
                var dy = center.y - self.controller.y;
    
                var gate = Math.sqrt(Math.pow(nears[0].x - nears[1].x, 2) + Math.pow(nears[0].y - nears[1].y, 2));
    
                if (gate > 40) {
                    self.controller.moveRelative(dx * 1.5, dy * 1.5);
                    console.log('go', nears[0].text + nears[1].text, gate);
                } else {
                    self.controller.moveRelative(dx, speed);
                    console.log('back', nears[0].text + nears[1].text, gate);
                }
            } else if (this.safeCycle > 3) {
                console.log('follow');
                var dx = self.enemy.x - self.controller.x + 48 + Math.sin(self.cycle / 10) * 20;
                var dy = self.enemy.y - self.controller.y + 200 + Math.sin(self.cycle / 20) * 80;
                if (dx > speed) dx = speed;
                if (dx < -speed) dx = -speed;
                if (dy > speed) dy = speed;
                if (dy < -speed) dy = -speed;
                self.controller.moveRelative(dx, dy);
            }
            this.cycle++;
            if (this.cycle %20 == 0) {
                this.controller.startShot();
            }
            this.bullets = [];
        },
        notifyText: function(text, x, y) {
            var myTweet = document.querySelector('#myInfo marquee').textContent;
            this.bullets.push({ text: text,
                                x: x + 9,
                                y: y + 9,
                                distance: Math.sqrt(Math.pow(this.controller.x - x, 2) + Math.pow(this.controller.y - y, 2)),
                                enemy: myTweet.indexOf(text) == -1
                              });
        },
        notifyImage: function(image, x, y, w, h) {
            if (w == 64) {
                // enemy
                this.enemy = { x: x, y: y };
            }
        },
        notifyNode: function(node) {
        },
        cycle: 0,
        bullets: [],
        enemy: { x: 0, y: 0 },
        safeCycle: 0
    };
    
    var bot = new Bot();
    
    document.body.addEventListener('DOMNodeInserted',function(ev){
        bot.notifyNode(ev.target);
    }, false);
    
    var _drawImage = document.querySelector('canvas').getContext('2d').drawImage;
    document.querySelector('canvas').getContext('2d').drawImage = function(image, x, y, w, h) {
        _drawImage.apply(this, arguments)
        bot.notifyImage(image, x, y, w, h);
    };
    
    var _fillText = document.querySelector('canvas').getContext('2d').fillText;
    document.querySelector('canvas').getContext('2d').fillText = function(chr, x, y) {
        _fillText.apply(this, arguments);
        bot.notifyText(chr, x, y);
    };
    
    setInterval(function() {
        var halfScreen = document.querySelector('.halfScreen');
        if (halfScreen.style.display == 'none') return;
    
        ['.startButton', '.postButton'].forEach(function(selector) {
            var button = document.querySelector(selector);
            if (button && button.style.display == 'block') {
                button.dispatchEvent(MouseUtil.click());
            }
        });
    }, 1000);
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2011/01/12 19:05:37 - 2011-01-12
  2. 2011/01/12 15:15:54 - 2011-01-12
  3. 2011/01/11 14:41:14 - 2011-01-11
  4. 2011/01/11 14:40:17 - 2011-01-11
  5. 2011/01/11 14:33:22 - 2011-01-11
  6. 2011/01/11 14:30:23 - 2011-01-11
  7. 2011/01/11 14:29:23 - 2011-01-11
  8. 2011/01/11 14:21:21 - 2011-01-11
  9. 2011/01/11 14:20:47 - 2011-01-11
  10. 2011/01/11 14:19:16 - 2011-01-11
  11. 2011/01/11 14:16:45 - 2011-01-11