Cloud Server Installation

Assuming you already followed the step here, and already installed your cloud server.

Follow these steps to now install the API on your droplet. It should work no matter if you install the API on the same droplet than your game server, or on a different one, they both work.

Upload the code

You can find the API code inside a zip in the asset Install folder. You can unzip and upload the NodeJS code into any folder. In my demo I created this folder and uploaded all the JS code there.

/server/api

Note: If you previously installed NodeJS locally, a node_modules folder will have been created in your files. You don't need to upload this folder since it can be quite big and we will reinstall it on the server anyway.

Install MongoDB

To access the command line, from WinSCP, click on Commands->Open in Putty.

Follow the instruction to install MongoDB

https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-20-04

If you get an error about libssl, check this to resolve it:

https://askubuntu.com/questions/1403619/mongodb-install-fails-on-ubuntu-22-04-depends-on-libssl1-1-but-it-is-not-insta

Start Mongo with

sudo systemctl start mongod

And make sure it is working correctly

sudo systemctl status mongod

Install NodeJS and NPM

Run this to install NodeJS and NPM

sudo apt install nodejs
sudo apt install npm

Install forever globally to run NodeJS as a service

npm install forever -g

Install node modules required for the API app (make sure you uploaded the nodejs code to that folder first).

cd /server/api
npm install

In WinSCP, open the config.js file to edit a few things

Change jwt_secret to any random value

Use a different port for your API and for your Game Server, normally i use the default http ports for the api (80 for http and 443 for https).

In command line, try to run the app to see if it is working

cd /server/api
node server.js

If there are no error message, this means the app is working! Use ctrl-c to quit the app.

Enable Port on Firewall

If you enabled UFW in previous steps, you will also need to allow port 80 and 443

ufw allow http
ufw allow https

Run NodeJS as a service

You can use forever to run NodeJS as a service so it stays on when you close the command line window

forever start --uid tcgapi -a server.js

When you make any changes, or reupload the app, you can restart with

forever restart tcgapi

You can also check if it is running properly

forever list

Unity Test and Create Admin User

Go in Unity, in Resources/NetworkData, change the api_url to the IP of you server, and set api_https to false (since SSL cert is not installed yet). Also change auth_type to API.

In newest versions of Unity (2022+), HTTP is blocked by default and only HTTPS is allowed, but since our API doesn't have a SSL certificate yet, we need to test with HTTP. To allow HTTP request, go in Edit -> Project Settings -> Player Settings -> Other Settings -> Allow Download over HTTP.

While your NodeJS app is running, in the Login Menu scene. Click on Register and create a new user. The very first user that you Register will be granted Admin role, then, all following users will be regular users.

If you wish to create another admin user later, you will need to first register it, and then go in the Scenes/Tools/ChangePermission scene to update it's permission level, by login in with the first admin user.

Build Game Server with new settings

After your NetworkData settings are set correctly, you will need to rebuild your Game Server,

Create a new user for the server in the login scene. And use the ChangePermission scene to set it's permission to 5.

Go into the Server scene, and click on the Server object, in the list of components you will see a field for username and password, add the credentials of the server user you created. This will allow your server to give rewards and also record all match results in the DB.

You can now build your game server and reupload/restart it.

Note: Important to never include the Server scene in your client build, or you will expose this server password if people decide to decompile the client. This is why I included the field for password in the Server scene and not in NetworkData like the rest of settings. So that the client build does not have this data anywhere.

Upload Data to API

You will need to sync your data (cards, packs and more...) with the API so that some features work properly, such as pack opening, level rewards and starter decks selection.

To do this, go in the Scenes/Tools and open the scene CardUploader. Run it from unity and login with an admin user. It will start uploading automatically all your data to your API database.

Whenever you add or edit cards, packs, decks or rewards, you should do this step again if you want your API to be up to date.

Last updated