This repository contains the source code for a 'snake' (webserver) compatible with the ## 2018 ## version of the battlesnake server. The web server is written in ## Haskell ##, and users write their snake AI in Haskell also.
The contents of this README will guide the reader through the process of getting their snake live on Heroku, ready to connect to a battlesnake server.
The first step is to fork the repository. This will create a clone of this repository where the reader is allowed to commit changes (their snake AI).
Then clone the repo to get the local files:
git clone https://github.com/<YOUR GITHUB USERNAME>/starter-snake-haskell.git
The only requirement is a 1.7.1 version or newer of the Haskell build tool Stack. Stack will download the required components as necessary (GHC, Cabal, packages)
Note that the version of Stack in the Ubuntu repositories is outdated (1.5)
and therefore needs to be upgraded with stack upgrade
if it was installed
through sudo apt install haskell-stack
.
Another option is to use the generic Linux installer described in the official documentation:
curl -sSL https://get.haskellstack.org/ | sh
# or
wget -qO- https://get.haskellstack.org/ | sh
Verify that Stack was installed correctly by running:
stack --version
The code can be compiled by running (in the project directory):
stack build
Running stack install
will then copy an executable into ~/.local/bin
which
can be run to start your snake. stack exec starter-snake-haskell
accomplishes the same thing. You should get an output similar to:
Setting phasers to stun... (port 8080) (ctrl-c to quit)
From here, your snake will be listening for POST requests on
http://localhost:<port>
, <port>
being 8080 in this case.
After installing, and while developing, you can recompile and run the snake by running the following:
stack build && stack exec starter-snake-haskell-exe
The name, looks, and other properties of the snake can be configured in
src/Snake.hs
. The logic controlling the movement of the snake resides
in src/Move.hs
, along with some helper functions for extracting data
from the received JSON objects.
These steps assume the reader already has an account on Heroku.
Next the reader should create a new application on Heroku. This is done in two simple steps. The first one is to click the 'New' button on the Heroku landing page (assuming the reader is logged in).
Next, a unique name for the new application needs to be chosen, and the reader needs to specify their location.
Now the new app needs to be configured such that it knows how to build the reader snake and how to deploy it (expose the snake API).
First the correct GitHub repository needs to be tracked. This is done by connecting the Heroku account to the readers GitHub account.
After the GitHub account is connected it is possible to search through the users repositories and select the one containing the snake AI. Search for the name of the repository.
In order to tell Heroku how to build and deploy the project, two components are needed. The Procfile
in the repository tells Heroku the name of the file to deploy. This should be the same filename as described in the .cabal-file of the project.
The second component is a Buildpack, which contains instructions of how to build the project. We reach this configuration by first selecting the settings bar at the top of the page.
![image broken](https://github.com/Rewbert/starter-snake-haskell/blob/master/image/select-settings.png "Select the settings tab)
Once at the settings page, scroll down to the Buildpack part. Click the Add Buildpack
button.
Adding the proper Buildpack for a stack project is as simple as putting this link in the field that emerges: ## https://github.com/mfine/heroku-buildpack-stack ## (credits for the buildpack goes to Joe Nelson et al). Click ok and the reader should see something like this.
The app should now be ready for deployment. Commit and push your snake AI code to the repository, and on the deployment page simply click deploy at the bottom.
The first time the reader deploys his or her snake, stack will download all required dependencies. This can take up to 10 minutes (but only the first time). When all is done, it should look like this.
Now, your Heroku app is located at https://yourappname.herokuapp.com, which in this example is https://haskell-nope-rope.herokuapp.com.