When we last spoke, I had finished a console implementation of the game. The ultimate goal is to be able to play the game with other people across the Internet. However, the next incremental step was to enable gameplay via the browser in a single session. Luckily, this was handily done using Reagent and Figwheel.
Quick sidebar: I decided to house the entire effort in a single git repository with two separate projects: one clojure project that holds the game engine (as well as the console game) and another reagent project for the web UI.
I started the web project simply enough. From my repository's root directory (/cartagena), I typed the following:
lein new figwheel cartagena-web -- --reagent
This magical incantation created an immediately runnable web project. To prove it, I changed to the newly created directory and launched the site.
cd cartagena-web lein figwheel
Opening a browser to http://localhost:3449 proved that something great was happening. All that's left to do is fill in the blanks with the game board, player display, pieces, cards, colors, and other bits of logic...
OK, maybe that's more work than simply filling in blanks. :-)
Luckily, the server code was done (in principle). Because I had left some code in the server to bootstrap a game for me, I didn't immediately have to implement a bunch of stuff to gather player information. Instead, I could focus on creating routes that would call the server as necessary (such as when a game starts, when a player takes and action, etc).
I set up a ring server to handle requests coming from the web client. Note that everything is a POST from the client (although I had a default GET in there for sanity's sake).
The results of all of the routes are the game states that the game engine provides, wrapped in transit for good measure. This actually *almost worked* out of the box. The most recent versions of all of the libraries required some poking and prodding to get interoperability working, but in the end turned out to be something that I could figure out without too much violence/swearing.
At this point, I've got transit moving from the server to the client. It was time to start building the web interface. The figwheel template had some scaffolding that allowed me to build the first parts of the UI without thinking too hard about them.
Ha! Fooled you! You thought I didn't think about it very much. As it turns out, I *did* think about how the board should be laid out. I went so far as to figure out what the (x, y) coordinates of each space should be, as well as how the space should be designated (with images representing the space's icon) and where the player pieces would be. I wound up with something like the following:
The space in the upper left is the jail, the lower right is the ship, and below would be the player area. Board spaces themselves would have a picture as well as three squares where pirates would sit. Pretty nifty, huh? NOW all that's left to do is fill in the blanks, right?
Yeah... not quite, still. I wanted the board images to be dynamic (because the board that's returned from the server is randomized), so I started with just the static pieces.
Notice a couple of things. First of all, I've got a little helper called to-scale. I found that bumping the resolution up or down was something I wanted to be able to do easily, and that function handles scaling. Secondly, even though React (which is what is underneath Reagent) is supposed to handle svg images gracefully, it does not. My first pass at this left me pretty discouraged, until Tim came to the rescue yet again and tipped me off to the :dangerouslySetInnerHTML bit.
Another quick aside: for a really great and far more in-depth view of how quickly you can get a game up and running in a browser using Reagent and Figwheel, have a look at Tim's Tetris implementation and write up.
The result of this bit of code looks something like:
Why a vector, you might ask? As it turns out, the board pieces are returned from the server as a vector. Iterating through it while using its index to find the space's x and y was really convenient. And if you haven't noticed by now, I'm ALL about convenience...
Adding the code for a normal space display item and then adding the code to display normal spaces:
For the code above, realize that the @app_state atom is populated by a POST to get the game state (either via New Game or another action). Hence, the :board is the vector of spaces and their states returned from the server.
All that's left to do is add the call to normal-spaces to the main display function, and you wind up with a display like:
That's a lot to take in all at once, so let's pause here for reflection. If you haven't read Tim's Tetris post, you definitely should. Next time, we'll cover getting some pirates on the board, figuring out the current player display, and actually playing the game.
Or if you're in too big a hurry, head over to github to have a look at the final product. :-)