Custom Code and Data
Here is a quick explanation of the different javascript code files and where to go to change things.
users.model.js
Contain the schema variables for the database.
If you want to add new values, you need to add them here inside the schema at the start of the file. For more information, check mongoose doc: https://mongoosejs.com/docs/guide.html
users.controller.js
Contains the code logic that happens when a request is called. Usually those functions will have a request and response as parameter and will call
return res.status(204).send(data);
To return the status code and json data to the client.
users.routes.js
contain all the requests definitions and comment on parameters they should have in the body or in
app.get("/users", [
AuthTool.isValidJWT,
AuthTool.isPermissionLevel(USER),
UsersController.list,
]);
This functions defines the https://yourapi.com/users
request. And will return the list of all users.
Example of adding variables to the code
Let's say we want to add a description variable and a gem variable.
In the model.js file, add this inside the schema
desc: {type: String, default: ""},
gem: {type: Number, default: 0},
The description is just some text the user can add to their account, since they are allowed to change it themselves, we will add it to the edit user function (where we change the avatar). The gems variable should not be edited by the users because it represent a currency they own, so only the server should be able to give it to users. For this reason we will add it to the rewards request.
users.controller.js, editUserAccount function
var avatar = req.body.avatar;
var desc= req.body.desc;
if(avatar && typeof avatar !== "string")
return res.status(400).json({error: "Invalid avatar"});
if(desc&& typeof desc!== "string")
return res.status(400).json({error: "Invalid description"});
//Change Avatar
if(avatar)
userData.avatar = avatar;
//Change Description
if(desc)
userData.desc = desc ;
users.controller.js, gainReward function
var coin = req.body.coin || 0;
var xp = req.body.xp || 0;
var gem = req.body.gem || 0;
if(coin && typeof coin !== "number")
return res.status(400).json({error: "Invalid parameters"});
if(xp && typeof xp !== "number")
return res.status(400).json({error: "Invalid parameters"});
if(gem && typeof gem !== "number")
return res.status(400).json({error: "Invalid parameters"});
user.coin += coin;
user.xp += xp;
user.gem += gem;
In Unity, inside ApiMsg.cs you will need to edit the class EditUserRequest and GainRewardRequest to add the new variables.
Last updated