batch: add convenience batch.set / batch.get for updating paths simultaneously

This commit is contained in:
Tyler Renelle 2013-05-29 10:54:16 +01:00
parent 1745ae6205
commit 4c684502f2
4 changed files with 16 additions and 21 deletions

View file

@ -33,16 +33,11 @@ module.exports.app = (appExports, model) ->
items.updateStore(model)
appExports.reset = (e, el) ->
misc.batchTxn model, (uObj, paths) ->
taskTypes = ['habit', 'daily', 'todo', 'reward']
uObj.tasks = {}; paths['tasks'] = true
taskTypes.forEach (type) -> uObj["#{type}Ids"] = []; paths["#{type}Ids"] = true
# Reset stats
[uObj.stats.hp, uObj.stats.lvl, uObj.stats.gp, uObj.stats.exp] = [50, 1, 0, 0]
# Reset items
[uObj.items.armor, uObj.items.weapon, uObj.items.head, uObj.items.shield] = [0, 0, 0, 0]
['stats.hp', 'stats.lvl', 'stats.gp', 'stats.exp', 'items.armor', 'items.weapon', 'items.head', 'items.shield'].forEach (path) ->
paths[path] = true
misc.batchTxn model, (uObj, paths, batch) ->
batch.set 'tasks', {}
['habit', 'daily', 'todo', 'reward'].forEach (type) -> batch.set("#{type}Ids", [])
_.each {hp:50, lvl:1, gp:0, exp:0}, (v,k) -> batch.set("stats.#{k}",v)
_.each {armor:0, weapon:0, head:0, shield:0}, (v,k) -> batch.set("items.#{k}",v)
items.updateStore(model)
browser.resetDom(model)
@ -61,12 +56,11 @@ module.exports.app = (appExports, model) ->
appExports.customizeArmorSet = (e, el) ->
user.set 'preferences.armorSet', $(el).attr('data-value')
appExports.restoreSave = (e, el) ->
misc.batchTxn model, (uObj, paths) ->
appExports.restoreSave = ->
misc.batchTxn model, (uObj, paths, batch) ->
$('#restore-form input').each ->
[path, val] = [$(this).attr('data-for'), parseInt($(this).val() || 1)]
helpers.dotSet(path, val, uObj); paths[path] = true
debugger
batch.set(path,val)
appExports.toggleHeader = (e, el) ->
user.set 'preferences.hideHeader', !user.get('preferences.hideHeader')

View file

@ -40,7 +40,7 @@ cleanupCorruptTasks = (model) ->
delete tasks[key]
true
misc.batchTxn model, (uObj, paths) ->
misc.batchTxn model, (uObj, paths, batch) ->
## Task List Cleanup
['habit','daily','todo','reward'].forEach (type) ->
@ -55,7 +55,7 @@ cleanupCorruptTasks = (model) ->
# There were indeed issues found, set the new list
if !_.isEqual(idList, preened)
uObj["#{type}Ids"] = preened; paths["#{type}Ids"] = true
batch.set("#{type}Ids", preened)
console.error uObj.id + "'s #{type}s were corrupt."
true

View file

@ -7,9 +7,12 @@ character = require('./character')
module.exports.batchTxn = batchTxn = (model, cb, options) ->
user = model.at("_user")
uObj = hydrate(user.get()) # see https://github.com/codeparty/racer/issues/116
batch =
set: (k,v) -> helpers.dotSet(k,v,uObj); paths[k] = true
get: (k) -> helpers.dotGet(k,uObj)
paths = {}
model._dontPersist = true
cb uObj, paths
cb uObj, paths, batch
_.each paths, (v,k) -> user.pass({cron:options?.cron}).set(k,helpers.dotGet(k, uObj));true
model._dontPersist = false
# some hackery in our own branched racer-db-mongo, see findAndModify of lefnire/racer-db-mongo#habitrpg index.js

View file

@ -34,12 +34,10 @@ module.exports.app = (appExports, model) ->
Buy Reroll Button
###
appExports.buyReroll = ->
misc.batchTxn model, (uObj, paths) ->
misc.batchTxn model, (uObj, paths, batch) ->
uObj.balance -= 1; paths['balance'] =1
_.each uObj.tasks, (task) ->
unless task.type is 'reward'
uObj.tasks[task.id].value = 0
paths["tasks.#{task.id}.value"] = 1
batch.set("tasks.#{task.id}.value", 0) unless task.type is 'reward'
true
$('#reroll-modal').modal('hide')