TweetShootingBot
by
hitode909
2011-01-12 [2011/01/12 19:05:37]
TweetShootingBot
@@ -3,7 +3,7 @@
* @description TweetShootingBot
* @include http://tweetshooting.appspot.com/
* @license MIT License
- * @require
+ * @require
*/
var MouseUtil = { };
@@ -28,11 +28,10 @@
move: function(x, y) {
this.x = x;
this.y = y;
- this.dispatch(MouseUtil.mousemove(this.x, this.y));
+ this.dispatch(MouseUtil.mousemove(this.x - 12, this.y - 12)); // -12 ???
},
moveRelative: function(x, y) {
- this.x += x;
- this.y += y;
+ this.move(this.x+x, this.y+y);
this.dispatch(MouseUtil.mousemove(this.x, this.y));
},
startShot: function() {
@@ -48,28 +47,96 @@
var Bot = function(player) {
this.controller = new Controller();
- this.cycle = 0;
- this.run();
+ var self = this;
+ setInterval(function() {
+ self.do();
+ }, 50);
}
Bot.prototype = {
- run: function() {
- this.controller.startShot();
- var self = this;
- setInterval(function() {
- self.do();
- }, 50);
- },
do: function() {
- this.controller.move(Math.sin(this.cycle / 20) * (this.controller.screen.clientWidth * 0.5 - 24) + (this.controller.screen.clientWidth * 0.5 + 24), this.controller.screen.clientHeight * 0.9);
+ 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;
+ }).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,
+ y: y,
+ 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
};
-new Bot();
+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');
/*
* @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;
}
Controller.prototype = {
move: function(x, y) {
this.x = x;
this.y = y;
this.dispatch(MouseUtil.mousemove(this.x - 12, this.y - 12)); // -12 ???
},
moveRelative: function(x, y) {
this.move(this.x+x, this.y+y);
this.dispatch(MouseUtil.mousemove(this.x, this.y));
},
startShot: function() {
this.dispatch(MouseUtil.mousedown(this.x, this.y));
},
stopShot: function() {
this.dispach(MouseUtil.mouseup(this.x, this.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 + 18 < 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,
y: y,
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 です。