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
- 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
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.
For demonstration purposes, add a html file (index.html) to a folder named public. The file structure looks like this.
Then, start the server… (use Ctrl+C to stop the server)
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
Step 4: Client Message Handling
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.
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