-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial: Creating a TicTacToe Bot
As an introduction to Merknera this tutorial will guide you through creating a bot to play Tic-Tac-Toe in JavaScript using node.js. The aim of this tutorial is to give you an introduction into creating a bot, registering and having it play its first games - this should give you a good understanding of how JSON-RPC works in the context of Merknera and how to test your bot using ngrok and postman.
A simple an non-effective implementation will be implemented and it will be left as an exercise of the reader to improve upon this.
You will need to have the following software download and installed before continuing with this tutorial:
- Create a new directory and navigate to it at the command line.
- Run
npm init
and following the on-screen instructions. - Run
npm install jayson --save
to install and add the dependency to our package.json file created for us in step 2.
When orchestrating a game Merknera acts as a client and makes calls to the bots participating in the game and the bots act as a server. We will start by implementing the 4 server methods required by Tic-Tac-Toe to play a game: Status.Ping
, TicTacToe.NextMove
, TicTacToe.Complete
& TicTacToe.Error
.
Status.Ping
is the easiest so we will start with that.
Status.Ping is called whenever the Merknera server starts to find out which bots are online and it also calls Status.Ping prior to any move being played, this enables Merknera to ensure the bot is still online and to wake up any bots that might be hosted on a free service such as Heroku that goes to sleep after periods of inactivity. Full documentation can be found here
We need to be able to accept a method of Status.Ping
with no parameters and return with a result of "ping": "OK"
.
- Create a file called `index.js
- Add the following contents.
// Import the jayson module into a variable names "jayson"
var jayson = require('jayson');
// Create a jayson server.
var server = jayson.server({
// Register the Status.Ping method
"Status.Ping": function(callback) {
callback(
null, //
{ ping: "OK" } // Return an object with ping: "OK" that will be used in the response.
);
}
}, {
// don't collect params in a single argument
// More information can be found here: https://github.com/tedeh/jayson#named-parameters
collect: false
});
// Start the server and listen on port 3000.
server.http().listen(3000);
This code registers the Status.Ping method and returns ping: "OK"
when called.
We can now test this method using Postman.
- Launch your server by typing
node index.js
at the command line - Launch Postman an create a new request.
- Click on the "GET" drop down and change it to "POST"
- In the request URL enter
http://localhost:3000
. - Click on the "Headers" tab and add a header with a "key" of
Content-Type
and a "value" ofapplication/json
- Click on the "Body" tab and select the "raw" radio button option.
- Paste the following into the body in Postman.
{
"jsonrpc": "2.0",
"method": "Status.Ping",
"id": 1
}
- Click "Send" and you should get a result like the following.
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"ping": "OK"
}
}