Erlang Serpents_ 1

Disclaimer

It took me too long to write this blog post and the main reason is that it encompasses many many different topics. This will be a blog post about

- Hackathons

- TDD

- Game development

- Data Type abstraction in functional languages

Erlang

bitstring syntax

RESTful APIs

multiple tools like cowboy, mixer, trails, swagger, lasse, etc.

History

A long time ago, when I was a little kid, I started programming in QBasic and, as many of you might know, QBasic came with two little games in it. One of them was Nibbles

Fast-forward 25 years… and you’ll find me at Erlang Solutions trying to come up with the subject for our next hackathon with the help of Roberto Romero and Hernán Rivas Acosta. We wanted a multiplayer game that could be watched on a single screen. Of course we came up with serpents!

Inakathons

Every year at Inaka, a former arm of Erlang Solutions, we all take one day off from our projects and we spend it all together celebrating what we love the most: programming. Last year we ran a “build your app in just one day” competition with awesome results. This year we decided to come up with a multi-player game where the organizers set up the server and the teams developed the clients. The code that I will be showing through the rest of this blog post, it all comes from the game server that Hernán, Roberto and myself developed.

Goals

The main goal of serpents was of course to be used for the hackathon in 2015. But I had a hidden agenda for it as well: Since this project was going to be open-sourced on github, I wanted to use it as a case to show how we work. I wanted to be able to get people on IRC, erlang-questions, reddit, stackoverflow, etc. that asks questions like “hey! how do you build a web-server in Erlang?” and tell them “look here. This is how you do it!”. With that in mind, we faced this project as if it was a client project and, turns out, our clients (our fellow devs) were in fact one of the toughest clients we have ever faced.

Architecture

So, how did we build a multiplayer server for a serpent’s game? We started by dividing it into 3 main components:

a web page that displayed the progress of the games.
a web server that served the webpage and provided a RESTful API for it to be updated in real-time.
a game server that provided the means for clients to connect to it.
For the web page we used plain HTML and some Javascript (you can tell how that is not really interesting to me at least :P). What is interesting is the real-time part. We implemented that one (as we usually do) over SSE. During a game, the website looked like this:

From the server perspective, we implemented the RESTful API using cowboy on trails and we documented it with swagger. It ended up looking like this:

On the other side, we decided to give client devs 2 options. They could just use the same RESTful API and SSE connection to communicate with the server, or they could use our brand new HDP protocol.

HDP is a blazingly fast protocol that runs on UDP. It was developed by Hernán and documented in kafka style for all to use. It’s specialized for serpents, but it can be replicated in other projects with ease.

Internal Architecture

Internally, as you can see on github, the code is divided in multiple folders and multiple modules that we could test individually.

One one side, we had the basic building blocks of the game: the rules (in spts_core) implemented as a gen_fsm per game and its entities (represented as Data Types or Models, each one with its own module and opaque type). You can see our approach in spts_serpents:

To report events from the core to either the SSE or the HDP clients, we used `gen_event` but, to ease our lives we implemented a simple `gen_event` handler that, in turn, required only one function to be implemented in your client `:notify(pid(), event())`. We called it `[spts_gen_event_handler](https://github.com/inaka/serpents/blob/master/src/spts_gen_event_handler.erl)`. We used that same handler to write tests for the core without depending on any type of client.
For HDP, we wrote an asynchronous UDP listener which starts a `gen_server` for each client that *connects* (there are no actual connections in UDP). The simplicity of that comes from the fact that we abstracted all the protocol parsing/validation logic to its own module: `spts_hdp`. That module in itself is a great example of the power of Erlang’s [bit syntax](http://erlang.org/documentation/doc-5.6/doc/programming_examples/bit_syntax.html). It showcases list-comprehensions, combined with binary-comprehensions and a lot of amazing binary pattern-match expressions. My favourite:
```%% @doc Parses a list of serpent diffs.
%% Each one includes the serpent id and the
%% length of its body, followed by each of
%% the body cells.
parse_diff_serpents(0, Next, Acc) ->
{lists:reverse(Acc), Next};
parse_diff_serpents(
N,
<< SerpentId:?UINT
, BodyLength:?USHORT
, Body1:BodyLength/binary
, Body2:BodyLength/binary
, Next/binary
>>,
Acc) ->
Body = <<Body1/binary, Body2/binary>>,
Serpent =
#{ id => SerpentId
, body =>
[ {Row, Col}
|| <<Row:?UCHAR, Col:?UCHAR>> <= Body]
},
parse_diff_serpents(N-1, Next, [Serpent|Acc]).```
```
#Development Methodology
And, to create all this, we used our usual approach: **TDD**. We worked with `common_test` for that and you can see the somewhat long list of test suites we implemented [here](https://github.com/inaka/serpents/tree/master/test). We've written many blog posts about TDD, code-coverage and even meta-testing so I'll not go over it again here. Just keep in mind that if you need examples of those things, you have plenty around here.
#The Clients
And if you are asking yourself *are there any client examples out there?* Yes, they are [here](https://github.com/inaka/serpents/tree/master/src/clients). I've developed all of them myself and you can create your own either starting from scratch or using [spts_cli](https://github.com/inaka/serpents/blob/master/src/spts_cli.erl).
#The Outcome
<div style="text-align:center"><iframe width="560" height="315" src="https://www.youtube.com/embed/9LBQeK5CwQY" frameborder="0" allowfullscreen></iframe></div><br/>
#Conclusions
We ended up complying with both of my goals. On the one hand, I can say we had an extraordinary Inakathon with lots of people happily playing while coding. On the other hand, now we have a great example of how we build stuff at Inaka to show the world. And you can enjoy it, too! just do…
```$ git clone https://github.com/inaka/serpents.git
$ cd serpents
$ make
$ _rel/serpents/bin/serpents start```

And, of course, as I did with Nibbles when I was just a kid, the funniest part starts when you start modifying the code to make the serpents fatter, thinner, randomly drunk after eating a poisoned fruit…

Oh, man! I have to implement that! Bye all, 9-year-old me has something important to do…