Adventure Capitalist is an idle business sim-game. The basic idea is to purchase a business, win capital from that business, upgrade the business and then purchase more businesses.
This repo includes game core and server implementation of adventure capitalist
game.
This project is built using Node.js, Socket.io and Typescript.
The project is using components-based
architecture in order to provide maintainability and simplicity. At the moment it has 3 main components:
game-core
- includes all the game logicserver
- includes handling of the game events and broadcasting them to a user.database
- includes a very simple imitation of a database. Basically it takes the data from a.json
config due to the simplicity of the requirements. But nevertheless, it can be changed in the future.
This repo is using commitizen to follow conventional commits. To be able to commit into this repo, you should run following commands:
npm install -g commitizen
npm install
In order to create a new commit you should type in your console:
git cz
You'll be prompted to fill out required commit fields.
Note: this repo is using
hasky
hooks, so you won't be able to push commits which don't follow the conventional commits.
Minimum required version of node is 11.15.0
.
In order to run the project, you need to rename the .env.example
file to .env
and set you environmental variables.
When your .env
file is set up, you need to install dependencies with the following commands in your console:
npm i
Now to run the project in dev or prod mode, run the following command:
npm run start:dev
or
npm run build
npm start
This project is using eslint
for linting.
npm run lint
To run the tests, simply run:
npm test
Note. Not all files are covered with tests, and some of them have quite poor test implementation due to lack of time. It should be refactored in the future.
The server is running the game even when a user is away, so in order to use this feature, the client should send Authorization: Auth ${clientId}
header when initializing the socket on client.
It's not quite secure, but due to lack of time, it was the fastest option available. It should be changed in the future for a better implementation.
When you're all setup, you can use this project as a back-end socket server for you application.
You just need to connect it to your front-end, example:
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io('http://localhost');
</script>
This server exposes two kinds of events:
GAME
- events related to the game flowSERVER
- events related to the server itself
This server emits the following socket events(all type definition you can find in src/server/typings/server-response.typings.ts
file):
Event name | Payload | Description |
---|---|---|
GAME:get-user-info | User's info | Returns a basic user info |
GAME:get-business-info | Business info by id | Returns a business info by id |
GAME:get-business-list | List of all businesses | Returns a list of all businesses |
GAME:error | Game error | Returns a game error |
SERVER: authorization | ClientId | Returns a clientId if a user is not authorized |
Also, the server listens to the following socket events(all type definitions for the client payload you can find in src/server/typings/client-payload.typings.ts
file):
Event name | Payload | Description |
---|---|---|
GAME:get-user-info | - | Returns basic user info |
GAME:get-business-info | - | Returns business info by id |
GAME:buy-business | { businessId: number } | Buys a business |
GAME:upgrade-business | { businessId: number } | Upgrade a business |
GAME:gain-capital | { businessId: number } | Starts gaining the capital from a business |
GAME:hire-manager | { businessId: number } | Hires a manager for a business |
Note. After one of
GAME:buy-business
,GAME:upgrade-business
,GAME:gain-capital
orGAME:hire-manager
events - the server will emitGAME:get-business-info
event in order to update the state on client.
In this implementation of the game server, we are saving all user instances in the server instance memory. This approach has one very big disadvantage: every time we deploy on the server - the users will lose their progress. This needs to be fixed using DB and allowing to init player instance with initial business data. It wasn't implemented due to lack of time.