Merge branch 'atomaka/bugfix/username-detection' into 'master'

Detect user dynamically

Detects the user by messaging the first random guest and detecting the user that sent that message.  Chat monitoring is always running once bot code is run as a consequence of this change; nbd.

See merge request !2
This commit is contained in:
Andrew Tomaka 2015-04-14 17:04:55 -04:00
commit d2c0b25476

35
bot.js
View file

@ -1,4 +1,3 @@
var botUsers = ['atomaka','greggnic'];
var botLoop; var botLoop;
var botGlobalBossTimer; var botGlobalBossTimer;
var botPaused = true; var botPaused = true;
@ -14,6 +13,7 @@ var botRaidTarget = null;
var botLastRandom = 0; var botLastRandom = 0;
var botLastRaid = 0; var botLastRaid = 0;
var botSleeping = false; var botSleeping = false;
var botUser = null;
function mainLoop() { function mainLoop() {
if($('#popup').is(':visible')) { if($('#popup').is(':visible')) {
@ -127,8 +127,34 @@ function mainLoop() {
} }
} }
botDetectUser();
botToggle(); botToggle();
function botDetectUser() {
var guests = $('#player-list').children().find('span.username-holder:contains("Guest_")');
var randomGuest = $(guests[Math.floor(Math.random() * guests.length)]).text();
$('#chatbox').bind('DOMSubtreeModified', function() {
var lastMessage = $(this).find('.chat-message').last();
if(lastMessage.find('.chat-pm').text() == 'PM -> ' + randomGuest) {
botUser = lastMessage.find('.username').text().trim();
console.log('Setting bot user to ' + botUser);
$('#chatbox').unbind('DOMSubtreeModified');
chatMonitor();
}
});
botSendChat('/pm ' + randomGuest + ' hello!');
}
function botSendChat(message) {
botFillIn('#chat_msg', message);
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;
$("#chat_msg").trigger(e);
}
function mostEfficientWorker() { function mostEfficientWorker() {
var bestValueWorker; var bestValueWorker;
var bestValue; var bestValue;
@ -183,8 +209,11 @@ function chatIsPM(message) {
} }
function chatHasName(message) { function chatHasName(message) {
if(botUser == null) {
return false;
}
var string = message.find('.message').text(); var string = message.find('.message').text();
return (new RegExp( '\\b' + botUsers.join('\\b|\\b') + '\\b')).test(string) === true; return (new RegExp('\\b' + botUser + '\\b')).test(string);
} }
function botTimestamp() { function botTimestamp() {
@ -307,12 +336,10 @@ function botToggle() {
if(botPaused) { if(botPaused) {
console.log('Playing bot'); console.log('Playing bot');
botLoop = setInterval(mainLoop, 1000); botLoop = setInterval(mainLoop, 1000);
chatMonitor();
botPaused = false; botPaused = false;
} else { } else {
console.log('Pausing bot'); console.log('Pausing bot');
clearInterval(botLoop); clearInterval(botLoop);
$('#chatbox, #groupchatbox').unbind('DOMSubtreeModified');
botPaused = true; botPaused = true;
} }
} }