├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── docker-compose.yml
├── game_map.json
├── game_map2.json
├── images
└── app_preview_image.png
├── marketplace.json
├── package-lock.json
├── package.json
├── public
├── index.html
├── sounds
│ ├── door
│ ├── flag
│ ├── key
│ └── wall
├── sprites
│ ├── door.png
│ ├── flag.png
│ ├── key.png
│ ├── lockeddoor.png
│ ├── original
│ │ ├── door.png
│ │ ├── flag.png
│ │ ├── key.png
│ │ ├── lockeddoor.png
│ │ ├── player.png
│ │ └── wall.png
│ ├── player.png
│ └── wall.png
└── static
│ ├── css
│ └── game.css
│ └── js
│ └── game.js
├── redis_kaboom_game.gif
├── redis_rpg_map.jpg
├── screenshots
├── screenshot1.png
└── screenshot2.png
└── src
├── apis
└── apis.http
├── data_loader.js
└── server.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules/
3 | *.tmp
4 | *.swp
5 | *.bak
6 | redisdata/
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM node:14
2 | WORKDIR /app/
3 | COPY package.json ./
4 | RUN npm install
5 | COPY . .
6 | EXPOSE 8080
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This program is free software: you can redistribute it and/or modify
2 | it under the terms of the GNU General Public License as published by
3 | the Free Software Foundation, either version 3 of the License, or
4 | (at your option) any later version.
5 |
6 | This program is distributed in the hope that it will be useful,
7 | but WITHOUT ANY WARRANTY; without even the implied warranty of
8 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 | GNU General Public License for more details.
10 |
11 | You should have received a copy of the GNU General Public License
12 | along with this program. If not, see .
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Redis Kaboom RPG Game
2 |
3 | This is an RPG maze type game built with [Kaboom.js](https://kaboomjs.com/), [Node.js](https://nodejs.org/) and [Redis](https://redis.io). It makes use of the [Redis JSON](https://redisjson.io) module from [Redis Inc](https://redis.com).
4 |
5 | 
6 |
7 | [Watch the video on YouTube!](https://www.youtube.com/watch?v=cowIZWASJNs)
8 |
9 | Since making the video and GIF above, we changed out the sprite images as part of our Hacktoberfest initiative. If you're looking for the originals that you see in the video, they're in this repo in `public/sprites/original`.
10 |
11 | ## Setup
12 |
13 | To run this game, you'll need [Docker](https://www.docker.com/) (or a local Redis instance, version 5 or higher with Redis JSON installed) and [Node.js](https://nodejs.org/) (use the current LTS version). First, clone the repo and install the dependencies:
14 |
15 | ```bash
16 | $ git clone https://github.com/redis-developer/redis-kaboom-rpg.git
17 | $ cd redis-kaboom-rpg
18 | $ npm install
19 | ```
20 |
21 | ### Docker setup
22 |
23 | With Docker - you need to have Docker installed and there are no other requirements. You can use Docker to get a Redis instance with Redis JSON:
24 |
25 | ```bash
26 | $ docker-compose up -d
27 | ⠿ Network redis-kaboom-rpg_default Created
28 | ⠿ Container redis_kaboom Started
29 | ⠿ Container node_kaboom Started
30 | $
31 | ```
32 |
33 | Redis creates a folder named `redisdata` (inside the `redis-kaboom-rpg` folder that you cloned the GitHub repo to) and writes its append-only file there. This ensures that your data is persisted periodically and will still be there if you stop and restart the Docker container.
34 |
35 | Note that when using Docker, there is no need to load the game data as this is done for you. Once the containers are running you should be able to start a game simply by pointing the browser at http://localhost:8080/.
36 |
37 | ### Stopping Redis (Docker)
38 |
39 | If you started Redis using `docker-compose`, stop it as follows when you are done playing the game:
40 |
41 | ```bash
42 | $ docker-compose down
43 | Container node_kaboom Removed
44 | Container redis_kaboom Removed
45 | Network redis-kaboom-rpg_default Removed
46 | $
47 | ```
48 |
49 | ### Redis Setup (without Docker)
50 |
51 | Without Docker - you will need Redis 5 or higher, Redis JSON and Node.js (current LTS version recommended)
52 |
53 | This game uses Redis as a data store. The code assumes that Redis is running on localhost port 6379. You can configure an alternative Redis host and port by setting the `REDIS_HOST` and `REDIS_PORT` environment variables. If your Redis instance requires a password, supply that in the `REDIS_PASSWORD` environment variable. You'll need to have Redis JSON installed.
54 |
55 | ### Loading the Game Data
56 |
57 | Next, load the game map into Redis. This stores the map data from the `game_map.json` file in Redis, using Redis JSON:
58 |
59 | ```bash
60 | $ npm run load
61 |
62 | > redis-kaboom-rpg@1.0.0 load
63 | > node src/data_loader.js
64 |
65 | Data loaded!
66 | $
67 | ```
68 |
69 | You only need to do this once. Verify that the data loaded by ensuring that the key `kaboom:rooms` exists in Redis and is a Redis JSON document:
70 |
71 | ```bash
72 | 127.0.0.1:6379> type kaboom:rooms
73 | ReJSON-RL
74 | ```
75 |
76 | ### Starting the Server
77 |
78 | To start the game server:
79 |
80 | ```bash
81 | $ npm run dev
82 | ```
83 |
84 | Once the server is running, point your browser at `http://localhost:8080`.
85 |
86 | This starts the server using [nodemon](https://www.npmjs.com/package/nodemon), so saving changes to the source code files restarts the server for you automatically.
87 |
88 | If the server logs an error similar to this one, then Redis isn't running on the expected / configured host / port:
89 |
90 | ```
91 | [ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
92 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
93 | ```
94 |
95 | ### Stopping the Server
96 |
97 | To stop the Node.js server, press Ctrl-C.
98 |
99 |
100 | ### Playing the Game
101 |
102 | Press space to start, then use the arrow keys to move your character. Red doors are locked until you have found the appropriate number of keys. Touch a red door to find out how many keys are required, or pass through it if you have enough keys. Green doors are unlocked and don't require keys.
103 |
104 | Find all 3 keys and unlock the door in the room you started in (room 0) to escape. Touching a flag teleports you to a random other room.
105 |
106 | At the end of the game, you'll see how long you took to complete the challenge, and how many times you moved between rooms.
107 |
108 | ## How it Works
109 |
110 | Let's take a look at how the different components of the game architecture fit together.
111 |
112 | ### Project Structure
113 |
114 | The project consists of a Node.js back end that has API routes for some of the game logic and a static server for the front end.
115 |
116 | The back end code lives in the `src` folder, along with the data loader code, used to load the game room map into Redis. It uses the [Express framework](https://expressjs.com/) both to serve the front end HTML / JavaScript / CSS and image files, and also to implement a small API for starting and tracking game events. Redis connectivity is handled using the [ioredis client](https://www.npmjs.com/package/ioredis).
117 |
118 | The front end is written in JavaScript using [Kaboom.js](https://kaboomjs.com/) as the game engine, and the [Bulma CSS framework](https://bulma.io/) for some basic layout. It lives in the `public` folder.
119 |
120 | ### Working with Kaboom.js
121 |
122 | [Kaboom.js](https://kaboomjs.com/) describes itself as "...a JavaScript library that helps you make games fast and fun!". It renders games as a set of scenes in a HTML `