Better incrementUserStat()

This commit is contained in:
Bryan Ashby 2019-01-24 21:54:16 -07:00
parent eea9e7b5e6
commit 0efa148f63
1 changed files with 25 additions and 20 deletions

View File

@ -147,29 +147,34 @@ class StatLog {
incrementUserStat(user, statName, incrementBy, cb) { incrementUserStat(user, statName, incrementBy, cb) {
incrementBy = incrementBy || 1; incrementBy = incrementBy || 1;
let newValue = parseInt(user.properties[statName]); const oldValue = user.getPropertyAsNumber(statName) || 0;
if(newValue) { const newValue = oldValue + incrementBy;
if(!_.isNumber(newValue) && cb) {
return cb(new Error(`Value for ${statName} is not a number!`));
}
newValue += incrementBy;
} else {
newValue = incrementBy;
}
this.setUserStatWithOptions(user, statName, newValue, { noEvent : true }, err => { this.setUserStatWithOptions(
if(!err) { user,
const Events = require('./events.js'); // we need to late load currently statName,
Events.emit( newValue,
Events.getSystemEvents().UserStatIncrement, { noEvent : true },
{ user, statName, statIncrementBy: incrementBy, statValue : newValue } err => {
); if(!err) {
} const Events = require('./events.js'); // we need to late load currently
Events.emit(
Events.getSystemEvents().UserStatIncrement,
{
user,
statName,
oldValue,
statIncrementBy : incrementBy,
statValue : newValue
}
);
}
if(cb) { if(cb) {
return cb(err); return cb(err);
}
} }
}); );
} }
// the time "now" in the ISO format we use and love :) // the time "now" in the ISO format we use and love :)