From 6e2035eef647ac71a895d5fb33d03aeaf5bac24e Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 4 Feb 2013 02:36:11 -0500 Subject: [PATCH] a super-hack to trick derby into showing notifications. bug fix on refresh disappearing habits `model.get(path, {getRef:false})` --- src/app/schema.coffee | 7 ++++--- src/app/scoring.coffee | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/app/schema.coffee b/src/app/schema.coffee index 4c52c28a38..8e5000a8ba 100644 --- a/src/app/schema.coffee +++ b/src/app/schema.coffee @@ -83,7 +83,7 @@ module.exports.BatchUpdate = BatchUpdate = (model) -> # Additionally, for some reason after getting the user object, changing properies manually (userObj.stats.hp = 50) # seems to actually run user.set('stats.hp',50) which we don't want to do - so we deepClone here #_.each Object.keys(userSchema), (key) -> obj[key] = lodash.cloneDeep user.get(key) - obj = user.get() + obj = model.get('users.'+user.get('id'), {getRef:false}) ### Handles updating the user model. If this is an en-mass operation (eg, server cron), changes are queued @@ -98,9 +98,10 @@ module.exports.BatchUpdate = BatchUpdate = (model) -> Hack to get around dom bindings being lost if parent objects are replaced whole-sale eg, user.set('stats', {hp:50, exp:10...}) will break dom bindings, but user.set('stats.hp',50) is ok ### - setStats: -> + setStats: (stats) -> + stats ?= obj.stats that = @ - _.each Object.keys(obj.stats), (key) -> that.set "stats.#{key}", obj.stats[key] + _.each Object.keys(stats), (key) -> that.set "stats.#{key}", stats[key] # queue: (path, val) -> # # Special function for setting object properties by string dot-notation. See http://stackoverflow.com/a/6394168/362790 diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index 372798fd0d..a3efeae6cc 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -108,6 +108,7 @@ score = (taskId, direction, times, batch, cron) -> {money, hp, exp, lvl} = obj.stats + taskPath = "tasks.#{taskId}" taskObj = obj.tasks[taskId] {type, value} = taskObj @@ -141,7 +142,10 @@ score = (taskId, direction, times, batch, cron) -> historyEntry = { date: +new Date, value: value } if (delta > 0) then addPoints() else subtractPoints() taskObj.history ?= [] - taskObj.history.push historyEntry if taskObj.value != value + if taskObj.value != value + taskObj.history.push historyEntry + batch.set "#{taskPath}.history", taskObj.history + when 'daily' calculateDelta() @@ -150,6 +154,8 @@ score = (taskId, direction, times, batch, cron) -> taskObj.history ?= [] taskObj.history.push { date: +new Date, value: value } taskObj.completed = false + batch.set "#{taskPath}.history", taskObj.history + batchSet "#{taskObj}.completed", false else addPoints() # obviously for delta>0, but also a trick to undo accidental checkboxes @@ -170,9 +176,14 @@ score = (taskId, direction, times, batch, cron) -> money = 0 taskObj.value = value - batch.set "tasks.#{taskId}", taskObj + batch.set "#{taskPath}.value", taskObj.value + origStats = _.clone obj.stats updateStats {hp: hp, exp: exp, money: money}, batch if commit + # newStats / origStats is a glorious hack to trick Derby into seeing the change in model.on(*) + newStats = _.clone batch.obj().stats + _.each Object.keys(origStats), (key) -> obj.stats[key] = origStats[key] + batch.setStats(newStats) batch.setStats() batch.commit() return delta