Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 128 additions & 13 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.getUser = function(req, res) {
{
'facebookId': req.params.id
},
'_id username facebookId level health health_limit agility appearance strength spritename stamina skillInUse',
'_id username facebookId level health agility appearance strength spritename stamina skillInUse experience healthLimit',
function(err, user) {
if (err) {
return res.status(500).json({
Expand All @@ -28,18 +28,133 @@ exports.getUser = function(req, res) {
});
};

exports.updateUser = function(req, res) {
var query = { '_id': req.user._id };
User.findOneAndUpdate(query, req.body.newData, { upsert:true }, function(err, user) {
if (err) {
return res.status(500).json({
message: err.message
})
}
res.send({
status: "ok",
});
});
exports.updateUserSpritename = function(req, res) {
var query = { '_id': req.user._id };
if (!req.body.spritename) {
res.send({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a return here
Add a status code (400)

status: "error",
error: "Please input new spritename for update"
});
}
var updateAttributes = { "spritename":req.body.spritename }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check not empty and limit the input length?

User.findOneAndUpdate(query, updateAttributes, { upsert:true }, function(err, user) {
if (err) {
return res.status(500).json({
message: err.message
})
}
res.send({
status: "ok",
});
});
}

//(strength, stamina, agility, health, healthLimit)
exports.updateUserAttributes = function(req, res) {
var query = { '_id': req.user._id };
var updateAttributes = {}
if (req.body.strength) {
updateAttributes['strength'] = req.body.strength
}
if (req.body.stamina) {
updateAttributes['stamina'] = req.body.stamina
}
if (req.body.agility) {
updateAttributes['agility'] = req.body.agility
}
if (req.body.health) {
updateAttributes['health'] = req.body.health
}
if (req.body.healthLimit) {
updateAttributes['healthLimit'] = req.body.healthLimit
}
if (!updateAttributes.strength && !updateAttributes.stamina && !updateAttributes.agility && !updateAttributes.health && !updateAttributes.healthLimit) {
res.send({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return, status code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

condition should be || (reject as long as one input is not correct)
Also, specify the condition explicitly (for example, must be >= 0)

status: "error",
error: "Please input attributes for update"
});
}

User.findOneAndUpdate(query, updateAttributes, { upsert:true }, function(err, user) {
if (err) {
return res.status(500).json({
message: err.message
})
}
res.send({
status: "ok",
});
});
}

exports.updateUserSkills = function(req, res) {
var query = { '_id': req.user._id };
if (!req.body.skills) {
res.send({
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return and status code, same for all codes below

status: "error",
error: "Please input skills in used for update"
});
}
var updateAttributes = { "skillInUse":req.body.skills }
User.findOneAndUpdate(query, updateAttributes, { upsert:true }, function(err, user) {
if (err) {
return res.status(500).json({
message: err.message
})
}
res.send({
status: "ok",
});
});
}

exports.updateUserAppearance = function(req, res) {
var query = { '_id': req.user._id };
if (!req.body.appearance) {
res.send({
status: "error",
error: "Please input appearance for update"
});
}
var updateAttributes = { "appearance":req.body.appearance }
User.findOneAndUpdate(query, updateAttributes, { upsert:true }, function(err, user) {
if (err) {
return res.status(500).json({
message: err.message
})
}
res.send({
status: "ok",
});
});
}

exports.updateUserLevelExperience = function(req, res) {
var query = { '_id': req.user._id };
var updateAttributes = {}
if (req.body.level) {
updateAttributes['level'] = req.body.level
}
if (req.body.experience) {
updateAttributes['experience'] = req.body.experience
}
if (!updateAttributes.level && !updateAttributes.experience) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. condition should be || (no level or no experience)
  2. if level == 0, !level will be true. Write explicit condition

res.send({
status: "error",
error: "Please input level / experience for update"
});
}

User.findOneAndUpdate(query, updateAttributes, { upsert:true }, function(err, user) {
if (err) {
return res.status(500).json({
message: err.message
})
}
res.send({
status: "ok",
});
});
}

exports.getFriends = function(req, res) {
Expand Down
1 change: 1 addition & 0 deletions models/skill.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var mongoose = require('mongoose');
// Skill Schema
var SkillSchema = new mongoose.Schema({
name: String,
unlockLevel: Number,
strengthFactor: Number,
staminaFactor: Number,
agilityFactor: Number
Expand Down
4 changes: 2 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var UserSchema = new mongoose.Schema({
agility: Number,
health: Number,
healthLimit: Number,
experience: Number,
experience: { type: Number, min: 0 },
appearance: String,
level: Number,
level: { type: Number, min: 0 },
skillInUse: [String],
updatedAt: Date,
createdAt: Date,
Expand Down
16 changes: 10 additions & 6 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ router.get('/me', verifyToken, authCtrl.refreshToken, function(req, res) {
router.get('/users/friends', verifyToken, authCtrl.refreshToken, userCtrl.getFriends);
// Get user
router.get('/users/:id', verifyToken, authCtrl.refreshToken, userCtrl.getUser);
// Update user
router.post('/users', verifyToken, authCtrl.refreshToken, userCtrl.updateUser);
// Update user spritename
router.post('/users/spritename', verifyToken, authCtrl.refreshToken, userCtrl.updateUserSpritename);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept this method and its route for now. Not all users will upgrade to use Api v2.

// Update user mi attributes (strength, stamina, agility, health, healthLimit)
router.post('/users/attributes', verifyToken, authCtrl.refreshToken, userCtrl.updateUserAttributes);
// Update user skills is used in use
router.post('/users/skills', verifyToken, authCtrl.refreshToken, userCtrl.updateUserSkills);
// Update user appearance
router.post('/users/appearance', verifyToken, authCtrl.refreshToken, userCtrl.updateUserAppearance);
// Update user level and experience
router.post('/users/level_experience', verifyToken, authCtrl.refreshToken, userCtrl.updateUserLevelExperience);
// Add combat
router.post('/combats/create', verifyToken, authCtrl.refreshToken, combatCtrl.createCombat);
// Get combat
router.get('/combats/:id', verifyToken, authCtrl.refreshToken, combatCtrl.getCombat);
// Add combat
router.post('/skills/create', verifyToken, authCtrl.refreshToken, skillCtrl.createSkill);
// Add combat
router.get('/skills/:id', verifyToken, authCtrl.refreshToken, skillCtrl.getSkill);

module.exports = router;