📜 ⬆️ ⬇️

Writing a bot for an online JavaScript game using AOP


If you like me and love online games, but do not like to spend on them much time, welcome under the cat. We will not discuss bots as it is good or not, but just analyze how you can make a bot for a specific online game. It will not stupidly click on the button by timeout, but will respond to events in the browser. We will do this with the help of Aspect-Oriented Programming (hereinafter AOP). For example, I chose the favorite game Pernatsk.

1. Cooking ingredients


We will need:


Important! The game should work in the browser, not in the client. And not on Flash, but on HTML + JavaScript.
At the output, we should have an extension for Chrome, which will play instead of us.

2. Making an extension


I will not describe in detail how this extension is made. On Habré about this already written, for example, here .
I will give only the codes we need files.
In manifest.json

{ "name": "BOT for pernatsk", "version": "0.1", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://pernatsk.ru/*" ], "js": [ "background.js" ], "run_at": "document_end" } ], "permissions": [ "storage" ], "web_accessible_resources": [ "/injected.js" ] } 

In the line "matches": [" pernatsk.ru *"] you will need to specify the address of your game.
I use the background.js file for cases when I want to inject my JS code on the site. Actually background.js code:
')
 $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "ai_on();"); } ); 

Important! If you do not understand what we are doing in this single function, then it is too early for you to make a bot. Read the basics of javascript.
All our work will be done in the file injected.js. His code is so far:
 function ai_on(){ console.log("Hello world") } 

All these files are saved in the same bot folder.

3. The first launch of the bot


Go to Chomre in Settings - Tools - Extensions, click on "Download unpacked extension." You should have a tick on "Developer Mode". Specify the folder with our files.
Now let's go to our game. Turn on the developer console (we will have to do this often) - press F12 and see “Hello Wolrd”. The application has earned.
Now everything we write in the ai_on function will be processed after the page loads.

4. Add AOP


We need libraries for the bot to work. My favorite jQuery is already used in Pernatsk, so it won't be added.
Add AOP for Jquery plugin. For good, it was worth packing into the extension itself as a separate file, but I'm lazy. So just add the code bin / aop.pack.js in the first line of our injected.js.
Check that it works by changing ai_on
 function ai_on(){ bot = jQuery.aop.after( {target: window, method: '$'}, function(result){ console.log('jQuery detected!'); bot[0].unweave(); return result; }); } 

Check that the AOP is properly connected. The developer console will now have the line “jQuery detected!” The message will be only once, since I disable the advice after the first trigger.
Important! Read the AOP for Jquery documentation to understand jQuery.aop.after and bot [0] .unweave ().

5. Why we will use AOP


The essence of AOP can be very roughly expressed as follows: " After function_1, function_2 must be done. What this function_1 worries us very little. It worked, it means we must work it out. We’ll not go into function_1. " It is better to read the normal description for example here.
How to use it? We after any function can run ours. For example, in the browser, a function was executed that a monster appeared in sight. We launch the function to attack it, which we have already written ourselves.

6. We teach the bot the first team


In injected.js add the following code:
 var commands = { conessearch:function(){ if( window.location.pathname != "/location/conessearch") { window.location.pathname = "/location/conessearch"; return false; } var buttons = $('form').eq(0).find('button'); for (var index = 0; index < buttons.length; ++index) { if(buttons.eq(index).css('display') == "inline-block") { buttons.eq(index).mouseenter(); buttons.eq(index).click(); return true; } } return false; }, } 


At this command, our bot-bird will fly in Pernatsk for cones. The code is a bit tricky, as in Pernatsk there is a little protection from bots.
When you write your commands, I recommend first trying out their performance in the console, and only then transfer the code to the editor.
In order to test and test the work of our team, we will launch the commands.conessearch() code in the spacebar.

7. We are looking for an event to which the bot should respond.


There are two methods here: the first is analyzing the game code. Long: (
The second method is to use AOP, and after all the functions that worked to output their name to the log. Then select the desired.
Change ai_on ()
 function ai_on(){ bot = jQuery.aop.around( {target: window, method: ''}, function(invocation){ console.log(invocation.method); return invocation.proceed(); }); } 

We will have many, many functions. There will be $ from jQuery or standard setTimeout.
Working with this is not very convenient to change the code again.
 function ai_on(){ fnList = []; bot = jQuery.aop.around( {target: window, method: ''}, function(invocation){ var fnName = invocation.method.toString(); if(fnList.indexOf(fnName) == -1) { console.log(fnName); fnList.push(fnName); } }); } 

Now we have reflected only those functions that have not yet been displayed. Their full list is stored in fnList.
After a couple of minutes there will be such options for the hitching function ["clearInterval", "$", "setTimeout", "timerTick", "serverTimeUpdate", "getComputedStyle", "setInterval", "tutorialArr", "showQ", "showQc", "updateBirdData", "viz", "unviz", "weatherUpdate"]
Changing the target and the regular expression in method, we can choose the function that will suit us to cling to it. For example, I choose the weatherUpdate function. Now, every time the weather changes, our bird will fly behind the cones.

7. We teach the bot to respond to events


We again change the function code ai_on ()
 function ai_on(){ bot = jQuery.aop.after( {target: window, method: 'weatherUpdate'}, commands.conessearch()); } function ai_off(){ bot[0].unweave(); } 

The ai_off function is needed to turn off the bot through the console.

8. Directions for further development


I hope this material was useful for you. What else can you do?

The code doesn’t really do anything, so I don’t post it.

Source: https://habr.com/ru/post/228607/


All Articles