diff --git a/package.json b/package.json index ba7d6a4490..ce8c64f1c0 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "dependencies": { "derby": "git://github.com/codeparty/derby#master", "racer": "git://github.com/lefnire/racer#master", - "racer-db-mongo": "git://github.com/codeparty/racer-db-mongo#master", + "racer-db-mongo": "git://github.com/lefnire/racer-db-mongo#mongo-update", "derby-ui-boot": "git://github.com/codeparty/derby-ui-boot#master", "derby-auth": "git://github.com/lefnire/derby-auth#master", "connect-mongo": "0.2.0", diff --git a/src/app/schema.coffee b/src/app/schema.coffee index 480a337d26..95299a04fb 100644 --- a/src/app/schema.coffee +++ b/src/app/schema.coffee @@ -1,6 +1,7 @@ content = require './content' moment = require 'moment' _ = require 'underscore' +lodash = require 'lodash' derby = require 'derby' module.exports.newUserObject = -> @@ -65,22 +66,17 @@ module.exports.BatchUpdate = BatchUpdate = (model) -> userObj = undefined updates = {} { - queue: (path, val) -> - commit = model._commit - model._commit = (txn) -> - txn.isPrivate = true - commit.apply(this, arguments) - user.set(path, val) - model._commit = commit - updates[path] = val + queue: (path, val) -> updates[path] = val - getUser: -> userObj ?= user.get() + getUser: -> + userObj ?= lodash.cloneDeep(user.get()) # whaaa??? modifying userObj modifies the value of user.get() at that path? ### Handles updating the user model. If this is an en-mass operation (eg, server cron), pass the user object as {update}. otherwise, null means commit the changes immediately ### updateAndQueue: (path, val) -> + @queue path, val # Special function for setting object properties by string dot-notation. See http://stackoverflow.com/a/6394168/362790 arr = path.split('.') arr.reduce (curr, next, index) -> @@ -88,8 +84,15 @@ module.exports.BatchUpdate = BatchUpdate = (model) -> curr[next] = val curr[next] , @getUser() - @queue path, val commit: -> + commit = model._commit + model._commit = (txn) -> + txn.isPrivate = true + commit.apply(model, arguments) + _.each updates, (val, path) -> + if path == 'stats.hp' then debugger + user.set(path, val) + model._commit = commit user.set "update__", updates # some hackery in our own branched racer-db-mongo, see findAndModify } diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index d9a7611e9b..c294639b30 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -99,7 +99,6 @@ score = (taskId, direction, times, batch) -> times ?= 1 commit = !batch? - console.log {commit:commit} batch ?= new schema.BatchUpdate(model) userObj = batch.getUser() @@ -135,9 +134,11 @@ score = (taskId, direction, times, batch) -> adjustvalue = if (taskObj.up==false or taskObj.down==false) then false else true calculateDelta(adjustvalue) # Add habit value to habit-history (if different) - historyEntry = { date: +new Date(), value: value } if taskObj.value != value + historyEntry = { date: +new Date, value: value } if (delta > 0) then addPoints() else subtractPoints() - model.push "_user.#{taskPath}.history", historyEntry + taskObj.history ?= [] + taskObj.history.push historyEntry + batch.updateAndQueue "#{taskPath}.history", taskObj.history if taskObj.value != value when 'daily' calculateDelta()