diff --git a/src/app/character.coffee b/src/app/character.coffee index 08a309e66d..24f0cf477e 100644 --- a/src/app/character.coffee +++ b/src/app/character.coffee @@ -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') diff --git a/src/app/index.coffee b/src/app/index.coffee index b8d4a8f71e..e77c8f0bed 100644 --- a/src/app/index.coffee +++ b/src/app/index.coffee @@ -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 diff --git a/src/app/misc.coffee b/src/app/misc.coffee index 85e71fe11f..369946bed5 100644 --- a/src/app/misc.coffee +++ b/src/app/misc.coffee @@ -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 diff --git a/src/server/private.coffee b/src/server/private.coffee index 93038a620a..ab72c8206d 100644 --- a/src/server/private.coffee +++ b/src/server/private.coffee @@ -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')