Dominion (A Node.js Project)

A few years ago, my friends and I got really into the card game Dominion. It’s a deck building game, but what made this game more enjoyable was the variation. In each round, you randomly select 10 cards that will be available for purchase, and the goal is to try to figure out different strategies for how those cards can interact with each other. The game is available on Amazon and is playable online via the official Dominion Online site.

As a programming exercise, we attempted to create a similar game using Node.js. This post is to help me document the process we took to set up this project.

Step 1: Install Node.js

Node.js is a runtime, or a program, that executes Javascript code on a server, thus allowing users to program server-side code using Javascript. For a good description of Node.js, check out this link.

  • Download and install Node.js. The installer installs the Node.js and npm, a package manager for Javascript that allows you to quickly install libraries and other components.
  • Next, two libraries will be installed to help set up a server. express is used to easily instantiate the server, and socket.io is used for communication between server and client. To install, enter the following lines in a terminal
$ npm install express
$ npm install socket.io

Step 2: Set up Server Code

Create a .js file for your server code (e.g. server.js). The following code sets up an initial server that runs on port 8081.

var express = require("express");
var app     = express();
var server  = require("http").createServer(app)
var io      = require("socket.io").listen(server);
var path    = require("path");

app.use(express.static(path.join(__dirname, "public"))); // Setting static folder to 'public' (relative to server.js)
server.listen(process.env.PORT || 8081);

For demonstration purposes, add a html file (index.html) to a folder named public. The file structure looks like this.

  • my_dir/server.js
  • my_dir/public/index.html
<!doctype html>  
<html>  
<body>
    <h1>Hello World!</h1>    
</body>
</html>  

Then, start the server… (use Ctrl+C to stop the server)

$ node server.js

and access your file from your web browser with the following URL: http://127.0.0.1:8081

Step 3: Server Message Handling

Then, this code is used to set up message handling on the server side

io.sockets.on("connection", function(socket) {
    // send a message to the client
    io.sockets.emit("<message_name>", <message_data>);

    // handler for message from client
    socket.on("<message_name>", function(<message_data>) {
        // do stuff here
    }

    // add more handlers...
}

Step 4: Client Message Handling

<script src="/socket.io/socket.io.js"></script>

Add that to your client html page. This is served from your node server, which is available after installing socket.io.

In a corresponding client.js file, the following code can be used to handle client messaging.

var socket = io.connect();

// send a message to the server
socket.emit("<message_name>", <message_data>);

// handler for message from server
socket.on("<message_name>", function(<message_data>) {
}

Step 5: Serve Page

  • Log in to your router
  • Find the Port Forwarding Options
  • Add a new Port Forwarding Rule… HOST DEVICE, HTTP (Web - Port 80), Forwarding to Port 8081
  • Find the Router’s IP Address (usually somewhere on the main router page)
  • Give out :80 to others

References

Node.js