/*
* @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);