Dev/firebaseBots

Firebase Bots
In order to quality assure a gaming system, having functional bots is often a requirement. This page shows how to utilize the Cubeia Firebase Bot server to quickly write bots that interact with a Firebase server.

Requirements
You need Java 1.6 or higher installed. You will also need Maven 3.0 or higher installed. Also, this assumes you have a passing knowledge of Firebase and specifically how Firebase communicates with clients.

Creating a New Project
The Firebase Bots are best created as a Maven project, via an archetype, like so:

mvn archetype:generate \ -DarchetypeGroupId=com.cubeia.firebase.bots \ -DarchetypeArtifactId=firebase-bots-archetype \ -DarchetypeVersion=1.8.0 \ -DarchetypeRepository=http://m2.cubeia.com/nexus/content/groups/public

When the above Maven command is run it will ask for group and artifact ID for your new bots and also "gameId" - this should correspond to the game you want to write the bots for. So if your Firebase game has a "gameId" of 666, the bots should be created with the same ID.

You will now have gotten a new project, with a Maven POM file which looks like this:

 4.0.0 test test-bot 1.0-SNAPSHOT com.cubeia.firebase.bots firebase-bots 1.8.0 				com.cubeia.firebase.bots firebase-bots-maven-plugin 1.8.0 					bot.html</botControlFile> <botMenuName>Bot</botMenuName>

When the project is created above, it will also setup basic classes and files for your new bots, these being:


 * BotImpl.java - This is an implementation of a single bot behavior. When you run, the bot server will instantiate as many of these as you require and handle the lobby and seating for you automatically.
 * bot.html - This is a control file used to configure your bots and to start them in a server.

You can now verify that the projects muilds and that you can start a but server by running the following command:

mvn package firebase-bots:run

This should package your project and start a bot server locally with your code.

Implementing a Bot
The Firebase Bot server will start any number of batches with any number of bots per batch. It will keep track of the lobby and seat bots when possible, the only actual code left to implement is how each bot acts on a given action. This is done via this method in BotImpl:

public void handleGamePacket(GameTransportPacket packet) { /*    * Handle the game data packet here... */ }

The transport packet above is a packet sent by the server to the specific bot. The bot should then act on the message optionally send something back to the server. This is done via the concrete bot representation the server has created for your logic, which you can get hold of like so:

Bot bot = getBot; Table table = getTable; GameTransportPacket action = new GameTransportPacket; action.pid = bot.getId; action.tableid = table.getId; action.gamedata = // set action as bytes here bot.sendPacket(action);

If your bot needs to act when it is seated, for example asking the table for more information about the current game, you can override the "handle seated" method that will be called when the bot is successfully seated, but don't forget to call "super" first:

@Override protected void handleSeated { super.handleSeated; /*    * This is where you add logic the bot should do     * when it is first seated, like scheduling the first * action to do... */ }

Bot Configuration
In order to start your bots, the bot server needs a bit of information. This is given in the "bot.html" file you can find under resources. There are two important hidden parameters in an HTML form on that page:


 * "gameId" - This is the game the bot connects to and is used to handle the lobby and seating
 * "aiclass" - This should point to your BotImpl class, fully qualified class name required

All other input elements will be matched against properties, either in the the "BasicIA" class, in the bot server or in your bot implementation. The bot server needs the following:


 * "url" - This should be the URL of your Firebase server to run against
 * "port" - The port on the Firebase server to use
 * "connectorType" - The bots can use binary sockets, Web Sockets or even Comet

On top of this there's a number of standard parameters used by the bot server to setup a particular batch of bots. To configure your own parameters:
 * 1) add a field to your implementation, including getters and setters; and
 * 2) add an input with the same "name" in the "bot.html" file. The example projects sets up one such property as an example, the "fixedRate" property.

Running and Starting Bots
The following command packages your bots, and then starts a bot server which you can use to start and stop bots:

mvn package firebase-bots:run

By default the bot server starts at port 8080 and will start bots from counting up from ID 0. You can configure this in the Maven POM file:

com.cubeia.firebase.bots</groupId> firebase-bots-maven-plugin</artifactId> 1.8.0        <botControlFile>bot.html</botControlFile> <botMenuName>Bot</botMenuName> <serverPort>8081</serverPort> <botStartId>100</botStartId>

Setting the start ID of your bots is useful if you need to run several bot server in parallel in order to make sure they don't clash.

Handle Bot Logins Server Side
The bots use a trivial login mechanism using the bot screen name as user name, and ID as password. You can customize the screen name by implementing a "BotGroupConfig" and pointing to it in a hidden parameter in your "bot.html"

<input type="hidden" name="groupconfigclass" value="<your config class name here" />

On the server side you need to recognize a bot when it tries to login, and accept the bot ID as a password.