Merge pull request #1984 from SyntaxPickax/patch-2

Fixed challenge prize logic to avoid double paying
This commit is contained in:
Tyler Renelle 2013-12-28 21:09:48 -08:00
commit 8ed891db83

View file

@ -106,20 +106,22 @@ api.create = function(req, res){
if (+req.body.prize > 0) { if (+req.body.prize > 0) {
waterfall.push(function(cb){ waterfall.push(function(cb){
var groupBalance = ((group.balance && group.leader==user._id) ? group.balance : 0); var groupBalance = ((group.balance && group.leader==user._id) ? group.balance : 0);
if (req.body.prize > (user.balance*4 + groupBalance*4)) var prizeCost = req.body.prize/4; // I really should have stored user.balance as gems rather than dollars... stupid...
return cb("Challenge.prize > (your gems + group balance). Purchase more gems or lower prize amount.s") if (prizeCost > user.balance + groupBalance)
return cb("You can't afford this prize. Purchase more gems or lower the prize amount.")
var net = req.body.prize/4; // I really should have stored user.balance as gems rather than dollars... stupid... if (groupBalance >= prizeCost) {
// Group pays for all of prize
// user is group leader, and group has balance. Subtract from that first, then take the rest from user group.balance -= prizeCost;
if (groupBalance > 0) { } else if (groupBalance > 0) {
group.balance -= net; // User pays remainder of prize cost after group
if (group.balance < 0) { var remainder = prizeCost - group.balance;
net = Math.abs(group.balance); group.balance = 0;
group.balance = 0; user.balance -= remainder;
} } else {
} // User pays for all of prize
user.balance -= net; user.balance -= prizeCost;
}
cb(null) cb(null)
}); });
} }
@ -344,4 +346,4 @@ api.unlink = function(req, res, next) {
if (err) return res.json(500,{err:err}); if (err) return res.json(500,{err:err});
res.send(200); res.send(200);
}); });
} }