
After reading the article about bots, I decided to share my modest experience of creating a bot using Java and using a network protocol (that is, no macros or implementations in AS).
Introduction
Somewhere half a year ago I met one quite popular VKontakte game - Zonk, the game itself is a variation on the
Zilch dice game, and is that you need to score 10,000 points faster than your opponent by throwing various combinations on your bones.
Training
Before starting to write a bot, I decided to study how the game communicates with the server and exchanges data with it. For this was used a fairly popular sniffer -
Wireshark .
After starting Wireshark, you need to run the game itself and play a couple of games. Then find messages between the browser and the server in Wireshark and open the entire history of negotiations.
')
As a result, it was revealed that the game communicates with the server on port 9337/9338 and exchanges with it using JSON requests. Authentication happens in the form of an exchange of XML messages containing your VKontakte information (as I understand it, this information is retrieved via the VKontakte API).
Bot writing
I decided to create the bot myself with the help of Java, since I was pretty familiar with the language itself, and also wanted to get experience writing applications using Swing.
Since I didn’t want to spend time parsing XML, the exchange of messages for authentication was rather rough and straightforward:
socket = new Socket(serverIP, port); out = new PrintWriter(socket.getOutputStream()); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out.print("<msg t='sys'><body action='verChk' r='0'><ver v='161' /></body></msg>" + END_CHAR); out.flush(); while (!ZonkBotApp.getMessage(in).contains("apiOK")) {} out.print("XMLMESSAGE" + END_CHAR);
To send messages using JSON, the
Jackson Java JSON-processor was used.
Then an instance of the bot was created, the constructor of which was passed Input and OutputStreams (opened through a socket):
public Bot(BufferedReader in, PrintWriter out) { inputStream = in; outputStream = out; }
Bot had only one method that creates a room and plays one match:
public void playGame(JTextArea outConsole, int gameType, int aiType)
The second component of the bot was his brain, which is a class with a public method:
public BotAction actionToDo(DiceThrow diceThrow, int aiType);
This method returned the next decision based on the current situation in the game, taking into account the goal, the number of shots left, the weight of the throw, etc.
The bot had a rather austere interface that allowed connecting to the server, making basic settings and launching the bot.
Further research
After writing the bot, I decided to tackle the mechanisms for tweaking it; for this, several variants of the bot’s brains aimed at a different game, aggressive, passive or reaching a certain threshold, were implemented. As I did not try, but I could not bring the bot to the points plus, the best thing I could achieve was a loss of 5% of points during 2000 games.
The original cats are in a very poor condition (the bot was written a year ago), but if there is a desire, I will try to put them in order and put it on github.