We write a bot, or "The Tale of a Good Bot"

While you sleep the enemy is swinging! (c) folk wisdom

Sooner or later everyone wants to sleep. How to tear yourself away from the monitor if you are all in a battle? And here also resources can be dug between fights. And the most annoying thing is that you give all your best to 120% but always, at any level, there is a nerd of a person who outperforms you in terms of parameters, quality and quantity of clothes, equipment, a faithful pet. What to do? How to be? Write a bot!

Bot (bot, abbreviated from robot) is a special program that automatically and / or according to a given schedule performs any actions through the same interfaces as a regular user. This is what Wikipedia says. And this is about 20% of the iceberg, which can be called a real Bot.

Consider the simplest option. Online game with automatic combat. It is also possible to search for resources between battles. In our arsenal there is a LAMP server and several bottles of beer.

In fact, we need to do a few simple steps. Enter the game and attack the enemy.
There are several options for implementation. Let's consider one of them - сURL.

Proceed:
1) Enter the game.
First, carefully study the login form. Usually there, besides login, password, checkbox “remember”, there are some more hidden fields. For example, "action".
Next, run the script:
//…
$mail = ‘vasyapupkin@url.mail.com’; // логин
$password = ‘11111’; // пароль
//ссылка на игру 
$ch = curl_init("http://game.url/");
//вернуть полученную страницы
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
//передать параметр браузера
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
// передать куки
 curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile.txt');
//сохранить куки в файл
 curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookiefile.txt');
//передать в POST логин, пароль и необходимые переменные
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, 'action=login&email='.$mail.'&password='.$pass);
 $str = curl_exec($ch);
 curl_close($ch);
//…

We transferred our data to the game server. If everything went well, then the server authorized us, returned the cookies we needed for further work, and our script saved them to the cookiefile.txt file.

2) Choosing an adversary
Walking through the locations of the game, we can parse pages and choose who to attack. You can get HTML pages as follows:
//...
$ch = curl_init("http://game.url/location/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookiefile.txt'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookiefile.txt');
curl_setopt($ch, CURLOPT_REFERER, 'http://game.url/location/');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'action= find'); 
$str = curl_exec($ch);
curl_close($ch);
//…

Using the cookiefile.txt file obtained from the previous example, we transfer information about our bot to the server, and the server joyfully gives us the page with the choice of the enemy. On it we select the ID (nickname, any other parameter necessary for the attack) of the enemy.

3) Attack
Having chosen the enemy, we carefully study the form for the attack. Select all hidden fields. Substitute the enemy ID in the script. We do everything as in the case of the choice of the enemy, only by substituting the values ​​necessary for the attack into the POST variables. Attack and win!

So, we have a primitive bot that you can run on a schedule for the whole night and wake up as a millionaire.

Some tips for creating “artificial intelligence”:
- The bot should look like a person. It does not have to be accurate like a watch. Random to help you. Take different breaks between fights. Participate in a gaming atmosphere. Give presents. Get resources. Walk around the locations.

- There may be several bots. They can log in under one account at different times and do different actions.

- Thoroughly think over the algorithm for the bot to move around the game. The algorithm should include all possible situations, such as leaving a battle, waiting for an event, gathering information, etc. For example, you can’t go to the mine during a battle.

- Remember that the Administrators of the game are also not shaky, and for a long time everyone knows this. Keep track of which IP and how often you log into the game. How a character behaves when you and a bot control him.

Good game!

Useful links:
PHP cURL: http://php.net/manual/en/book.curl.php
Regular expressions for parsing a page on Wikipedia

Also popular now: