A simple multi-player online game using node.js - Part I
Tags: javascript, node, webdev
Published: 2021-02-26 20:59:16

This is the first part of a 4 part series story where I describe what it took me to build a simple multiplayer online game.

# Intro
Once upon a time I got a job interview task of coding the "snake" game, the result was a nice one ([github](https://github.com/ValYouW/snake), [codepen](http://codepen.io/ValYouW/full/RNGjdz)).

Then I thought, what would it be to convert it into an online multi-player game, using node.js and WebSockets? let's see...

## Why "simple"? Who controls the game?
I started to think about how I wanted to implement the game, and the first question was "who controls the game"? do we put all the game management and business logic in the client, and use the server just as a hub for broadcasting messages? or do we let the server control the game and the client just draw what the server says.

Set "cheating" aside, implementing everything in the client was much more simple, as I already had a working snake game... but... that isn't really an <b>online</b> game, so I decided to let the server be the king.

But, if the server controls the game, it means the snake (on the client) can't move until the server told it so, and if the player wants to change the snake direction she has to wait for the server's response before it will happen, this could lead to a very laggy game...

After reading about the concepts of "client-side predictions" and "server reconciliation" ([here](http://www.gabrielgambetta.com/fast_paced_multiplayer.html)) I decided to start with a "simple" game, meaning a game that would <b>work perfectly over LAN, and will be OKish over WAN</b>, I believe there is still a lot to learn by doing so, and adding advanced concepts could be done later.

So... you can play the game on [Heroku](http://snakematch.herokuapp.com/), the lag is obviously apparent, but not that bad...

# Terminology
The SnakeMatch is a regular "snake" game where player1 competes with player2 for Pellets, each match is for 60 seconds and the player who collected more pellets wins. Of course that the regular Snake rules apply (a snake can't collide with itslef and can't hit the wall).

<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-Zo7AeC4SDXI/VWcdIpiy2bI/AAAAAAAAAtc/OED1W1iVOOE/s1600/snakematch.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://4.bp.blogspot.com/-Zo7AeC4SDXI/VWcdIpiy2bI/AAAAAAAAAtc/OED1W1iVOOE/s400/snakematch.jpg" width="398" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>

The game is built from the following "game objects":
1. Board - This is where everything happens, we divide the board into a grid, this will help us to make sure that all elements on the board are aligned. We index the cells for simplicity, starting from zero, then we can convert each cell index to a canvas x/y and vice versa
1. Cell/Box - A fixed-sized rectangle, each element on the board must fit exactly into a cell
1. Snake - The snake is built from "parts", where the first part is called the "snake head", we will see later how it is different from the rest of the body. Each snake part has the size of a board cell.
1. Pellet - This is what the snake needs to eat in order to grow, it also has the size of a board cell.
1. Status Bar - Holds the scores and the time till the end of the game.

# High Level Design
As we said, this is a fairly simple game, the server is responsible for managing the game, and the client is responsible for rendering the game state and send commands to the server.

Below is a schema of a game flow:

<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-2SS4_QC4Cq4/VWhac9mH1CI/AAAAAAAAAts/nGoueby7Szo/s1600/SnakeMatchDiagram.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="572" src="https://2.bp.blogspot.com/-2SS4_QC4Cq4/VWhac9mH1CI/AAAAAAAAAts/nGoueby7Szo/s640/SnakeMatchDiagram.jpg" width="640" /></a></div>

And here is a diagram with the main classes in the client and server:

<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-1zxhGn0dMec/VWt5PObhU8I/AAAAAAAAAuA/9ruGx6DWU_0/s1600/SnakeMatchDesign.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://1.bp.blogspot.com/-1zxhGn0dMec/VWt5PObhU8I/AAAAAAAAAuA/9ruGx6DWU_0/s640/SnakeMatchDesign.jpg" width="568" /></a></div>

# The Protocol
The protocol determines how messages between the client and server will look like, my first thought was to simply use json, however, there are two issues with json that bothered me:
1. It is a wasteful protocol (compared to custom protocol)
1. Although parse/stringify are fast, when the server is under load, a custom protocol could do better.
For example, let's take a look at the following update message (remember we have 10 of those every second):
```js
var updMessage = {
    type: 5,                     // Message type
    timeToEnd: 53,               // Time to game end
    directions: [ '6', '4' ],    // The directions each snake is heading
    sizes: [ 6, 6 ],             // The snake sizes
    pellets: [ 34, 21, 67, 54 ], // The cell indices where we have pellets
    score: [ 6, 5 ]              // The players score
};
var encoded = JSON.stringify(updMessage); // encoded.length = 100
```
On the other hand, using a custom protocol, we would get the following string:
```js
var encoded = '5#53#6,4#6,6#34,21,67,54#6,5'; // encoded.length = 28
```
Performance wise, **`JSON.stringify` is 83% slower**, that is quite a difference, especially if we would want later to increase the update rate from 10/sec to something like 30/sec...

<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-f34FFqFdgwg/VWuFE1YF4aI/AAAAAAAAAuQ/f2C4w7Ekeww/s1600/jsonVScustom.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://2.bp.blogspot.com/-f34FFqFdgwg/VWuFE1YF4aI/AAAAAAAAAuQ/f2C4w7Ekeww/s400/jsonVScustom.jpg" width="400" /></a></div>

OK, enough talking, in part 2 we will dive into the code...