From 0efa148f63e52d6ca3a8cfd8c0d7ed0a12b8de44 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 24 Jan 2019 21:54:16 -0700 Subject: [PATCH] Better incrementUserStat() --- core/stat_log.js | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/core/stat_log.js b/core/stat_log.js index f03319d0..af88ff57 100644 --- a/core/stat_log.js +++ b/core/stat_log.js @@ -147,29 +147,34 @@ class StatLog { incrementUserStat(user, statName, incrementBy, cb) { incrementBy = incrementBy || 1; - let newValue = parseInt(user.properties[statName]); - if(newValue) { - if(!_.isNumber(newValue) && cb) { - return cb(new Error(`Value for ${statName} is not a number!`)); - } - newValue += incrementBy; - } else { - newValue = incrementBy; - } + const oldValue = user.getPropertyAsNumber(statName) || 0; + const newValue = oldValue + incrementBy; - this.setUserStatWithOptions(user, statName, newValue, { noEvent : true }, err => { - if(!err) { - const Events = require('./events.js'); // we need to late load currently - Events.emit( - Events.getSystemEvents().UserStatIncrement, - { user, statName, statIncrementBy: incrementBy, statValue : newValue } - ); - } + this.setUserStatWithOptions( + user, + statName, + newValue, + { noEvent : true }, + 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) { - return cb(err); + if(cb) { + return cb(err); + } } - }); + ); } // the time "now" in the ISO format we use and love :)