Skip to content

Instantly share code, notes, and snippets.

@cvrebert
Created February 17, 2014 10:40
Show Gist options
  • Save cvrebert/9048378 to your computer and use it in GitHub Desktop.
Save cvrebert/9048378 to your computer and use it in GitHub Desktop.
Automatic participant for TwitchPlaysPokemon
// This is super-hacky since it's 2am.
function press(button) {
$("#chat_text_input").val(button);
$('#chat_speak').click();
}
var silence = false;
var xPos = 0;
var yPos = 0;
var aaa = 0;
var bbb = 0;
var myself = 'YOUR_TWITCH_USERNAME_HERE';
var origOnEvent = TMI._flash._conn._onEvent;
TMI._flash._conn._onEvent = function (eventName, info) {
if (eventName === 'debug' || eventName === 'user_color' || eventName === 'emote_sets' || eventName === 'special_user') {
// slight speedup
return;
}
if (eventName === 'channel_message') {
var msg = info.message.trim().toLowerCase();
if (info.sender !== myself) {
switch (msg) {
case "left":
xPos -= 1;
return;
case "right":
yPos += 1;
return;
case "up":
yPos += 1;
return;
case "down":
yPos -= 1;
return;
case 'a':
aaa += 1;
return;
case 'b':
bbb += 1;
return;
case 'start':
return;
case 'select':
return;
default:
if (silence) {
return;
}
}
}
}
origOnEvent.call(this, eventName, info);
};
function fakeChat(msg) {
TMI._flash._conn._onEvent('channel_message', {
history: false,
is_action: false,
sender: myself,
recipient: "#twitchplayspokemon",
message: msg,
timestamp: Date.now()
});
}
function logIt() {
fakeChat("x: " + xPos + ", y: " + yPos + ", a: " + aaa + ", b: " + bbb);
}
function stratergy() {
var absX = Math.abs(xPos);
var absY = Math.abs(yPos);
var doMove = Math.random() < 0.85;// favor movement over A/B
var choice = "";
if (doMove) {
// attempt to counteract movement
if (absX > absY) {
choice = (xPos > 0 ? 'left' : 'right');
}
else {
choice = (yPos > 0 ? 'down' : 'up');
}
}
else {
// go with the flow on A vs. B
choice = (aaa > bbb ? 'a' : 'b');
}
logIt();
press(choice);
// reset stats
aaa = bbb = 0;
xPos = yPos = 0;
var extra = Math.random() * 10;// jitter to simulate humans
window.setTimeout(stratergy, 1000*(30.1 + extra));
}
stratergy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment