Develop your Battlesnake using a MuleSoft API & DataWeave with this starter project
Tags: mulesoft, dataweave, api, programming
Published: 2023-02-13 21:55:29

This guide assumes you have a basic understanding on MuleSoft and DataWeave. As well as basic knowledge on git and GitHub repos.

This is intended as a short guide. For detailed instructions please see [this article](https://www.prostdev.com/post/how-to-develop-a-battlesnake-using-a-mulesoft-api-and-the-dataweave-language).

## Step 1: Create your own repo

1. Go to the [official list of Starter Projects](https://docs.battlesnake.com/starter-projects) in the Battlesnake docs.

2. Scroll down until you see the **Mule and DataWeave Battlesnake Quickstart** project and click on it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zo69a2qra7w28wb44bb.png)

You can use that repo to either fork or use as template for your own repo.

## Step 2: Modify the basic setup

Once you forked/created from the template your own repo, you need to modify the code to make your own snake.

1. Go to `src/main/resources/application.yaml` and modify the properties to match your own settings. [See the docs](https://docs.battlesnake.com/guides/customizations) for more information.

2. Go to `src/main/resources/dw/move-snake.dwl` to find the DataWeave code you will use to create the snake logic.

```dataweave
%dw 2.0
output application/json

var body = payload.you.body
var board = payload.board
var head = body[0] // First body part is always head
var neck = body[1] // Second body part is always neck

var moves = ["up", "down", "left", "right"]

// Step 0: Find my neck location so I don't eat myself
var myNeckLocation = neck match {
	case neck if neck.x < head.x -> "left" //my neck is on the left of my head
	case neck if neck.x > head.x -> "right" //my neck is on the right of my head
	case neck if neck.y < head.y -> "down" //my neck is below my head
	case neck if neck.y > head.y -> "up"	//my neck is above my head
	else -> ''
}

// TODO: Step 1 - Don't hit walls.
// Use information from `board` and `head` to not move beyond the game board.

// TODO: Step 2 - Don't hit yourself.
// Use information from `body` to avoid moves that would collide with yourself.

// TODO: Step 3 - Don't collide with others.
// Use information from `payload` to prevent your Battlesnake from colliding with others.

// TODO: Step 4 - Find food.
// Use information in `payload` to seek out and find food.
// food = board.food


// Find safe moves by eliminating neck location and any other locations computed in above steps
var safeMoves = moves - myNeckLocation // - remove other dangerous locations

// Next random move from safe moves
var nextMove = safeMoves[randomInt(sizeOf(safeMoves))]

---
{
	move: nextMove,
	shout: "Moving $(nextMove)"
}
```

> The starter project gives you a basic script where your snake won't move towards its own neck, but you still have to create the code to avoid hitting walls, avoid hitting your own body, find food, etc.

## Step 3: Deploy your Mule API

To try out your snake, you have to expose the API in a public URL that can be accessed by Battlesnake. You can deploy the API directly to CloudHub, or do it locally with tools like `ngrok` for quicker testing.

### Deploy to CloudHub

You can deploy your API directly from Anypoint Studio to your Anypoint Platform account. You can create a free Anypoint Platform trial account [here](https://anypoint.mulesoft.com/login/signup).

> **Tip**:
>
> If your Anypoint Platform account expires, you can create a new account using the same details as before (name, email, phone, etc.) and just changing your username.

Once you deploy your Mule app, extract the URL. We will use this to connect the snake in the next step. 

> Example: `http://amartinez-mule-battlesnake.us-e2.cloudhub.io`

### Deploy locally with ngrok

Deployments to CloudHub can take a lot of time. If you want to be agile and deploy locally to reflect your changes in less time, you can use a tool like `ngrok` to get a public URL to your local app.

1. Install [ngrok](https://ngrok.com/).
2. Run your Mule application locally.
3. Run `ngrok http 8081`.
4. Retrieve the public URL. 

> Example: `https://c0cb-2001-1970-5425-e000-00-7e86.ngrok.io`

## Step 4: Create your Battlesnake

1. Create an account at [play.battlesnake.com](https://play.battlesnake.com/).
2. Go to the [Battlesnakes](https://play.battlesnake.com/account/battlesnakes) tab.
3. Click on **New Battlesnake**.
4. Select your snake's name and choose the engine region closest to you.
5. In URL, paste the URL you generated with CloudHub/ngrok and add `/api` to the end of the URL. For example, `http://...cloudhub.io/api`
6. In tags, add `MuleSoft` and `DataWeave`.
7. Finish adding the rest of the settings and click **Save**.

Your Battlesnake should now be created.

## Step 5: Create a new game

Now you can start playing with your battlesnake!

1. Go to the [Create Game](https://play.battlesnake.com/account/games/create) tab.
2. Leave the default options selected and add your new battlesnake to the game by clicking **Add to Game** next to your snake.
3. Click on **Start Game**.

Voila!

## Additional resources

I have been streaming some Battlesnake live streams and publishing them in [this YouTube playlist](https://www.youtube.com/playlist?list=PLb61lESgk6hi60IazebMZ7pmZBZeIcEKt) if you want to take a look.

You can also follow me on [Twitch](https://twitch.tv/devalexmartinez) to see me coding live!