From 8d652f3fbec069479ddd1174246dee68cfb4e42b Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 14 Apr 2015 16:32:57 -0400 Subject: [PATCH] Detect user dynamically --- bot.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/bot.js b/bot.js index 7faf76d..d9345cc 100644 --- a/bot.js +++ b/bot.js @@ -1,4 +1,3 @@ -var botUsers = ['atomaka','greggnic']; var botLoop; var botGlobalBossTimer; var botPaused = true; @@ -14,6 +13,7 @@ var botRaidTarget = null; var botLastRandom = 0; var botLastRaid = 0; var botSleeping = false; +var botUser = null; function mainLoop() { if($('#popup').is(':visible')) { @@ -127,8 +127,34 @@ function mainLoop() { } } +botDetectUser(); 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() { var bestValueWorker; var bestValue; @@ -183,8 +209,11 @@ function chatIsPM(message) { } function chatHasName(message) { + if(botUser == null) { + return false; + } 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() { @@ -307,12 +336,10 @@ function botToggle() { if(botPaused) { console.log('Playing bot'); botLoop = setInterval(mainLoop, 1000); - chatMonitor(); botPaused = false; } else { console.log('Pausing bot'); clearInterval(botLoop); - $('#chatbox, #groupchatbox').unbind('DOMSubtreeModified'); botPaused = true; } }