mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 03:52:14 +00:00
don't use userObj, back to user.get() - we'll just intercept commit
This commit is contained in:
parent
4c5b8aacab
commit
7bfefb07dd
3 changed files with 41 additions and 54 deletions
|
|
@ -47,16 +47,9 @@ get '/', (page, model, next) ->
|
|||
model.ref '_user', user
|
||||
batch = new schema.BatchUpdate(model)
|
||||
batch.startTransaction()
|
||||
userObj = batch.userObj
|
||||
|
||||
unless userObj?
|
||||
#this should never happen, but it is. Looking into it
|
||||
console.error 'User object was null!'
|
||||
return page.redirect '/500.html'
|
||||
|
||||
|
||||
# Setup Item Store
|
||||
items = userObj.items
|
||||
items = user.get('items')
|
||||
_view.items =
|
||||
armor: content.items.armor[parseInt(items?.armor || 0) + 1]
|
||||
weapon: content.items.weapon[parseInt(items?.weapon || 0) + 1]
|
||||
|
|
@ -72,8 +65,8 @@ get '/', (page, model, next) ->
|
|||
setupModelFns(model)
|
||||
|
||||
# Subscribe to friends
|
||||
if !_.isEmpty(userObj.friends)
|
||||
model.subscribe model.query('users').friends(userObj.friends), (err, friends) ->
|
||||
if !_.isEmpty(user.get('friends'))
|
||||
model.subscribe model.query('users').friends(user.get('friends')), (err, friends) ->
|
||||
model.ref '_friends', friends
|
||||
|
||||
page.render()
|
||||
|
|
|
|||
|
|
@ -33,28 +33,28 @@ module.exports.newUserObject = ->
|
|||
return newUser
|
||||
|
||||
module.exports.updateUser = (batch) ->
|
||||
userObj = batch.userObj
|
||||
user = batch.user
|
||||
|
||||
batch.set('notifications.kickstarter', 'show') unless userObj.notifications?.kickstarter?
|
||||
batch.set('friends', []) unless !_.isEmpty(userObj.friends)
|
||||
batch.set('notifications.kickstarter', 'show') unless user.get('notifications.kickstarter')
|
||||
batch.set('friends', []) unless !_.isEmpty(user.get('friends'))
|
||||
|
||||
# Preferences, including API key
|
||||
# Some side-stepping to avoid unecessary set (one day, model.update... one day..)
|
||||
prefs = _.clone(userObj.preferences)
|
||||
prefs = _.defaults prefs, { gender: 'm', armorSet: 'v1', api_token: derby.uuid() }
|
||||
batch.set('preferences', prefs) unless _.isEqual(prefs, userObj.preferences)
|
||||
currentPrefs = _.clone user.get('preferences')
|
||||
mergedPrefs = _.defaults currentPrefs, { gender: 'm', armorSet: 'v1', api_token: derby.uuid() }
|
||||
batch.set('preferences', mergedPrefs)
|
||||
|
||||
## Task List Cleanup
|
||||
# FIXME temporary hack to fix lists (Need to figure out why these are happening)
|
||||
# FIXME consolidate these all under user.listIds so we can set them en-masse
|
||||
tasks = user.get('tasks')
|
||||
_.each ['habit','daily','todo','reward'], (type) ->
|
||||
path = "#{type}Ids"
|
||||
|
||||
# 1. remove duplicates
|
||||
# 2. restore missing zombie tasks back into list
|
||||
where = {type:type}
|
||||
taskIds = _.pluck( _.where(userObj.tasks, where), 'id')
|
||||
union = _.union userObj[path], taskIds
|
||||
taskIds = _.pluck( _.where(tasks, {type:type}), 'id')
|
||||
union = _.union user.get(path), taskIds
|
||||
|
||||
# 2. remove empty (grey) tasks
|
||||
preened = _.filter(union, (val) -> _.contains(taskIds, val))
|
||||
|
|
@ -64,13 +64,12 @@ module.exports.updateUser = (batch) ->
|
|||
|
||||
module.exports.BatchUpdate = BatchUpdate = (model) ->
|
||||
user = model.at("_user")
|
||||
userObj = user.get()
|
||||
origCommit = model._commit
|
||||
transactionInProgress = false
|
||||
updates = {}
|
||||
|
||||
{
|
||||
userObj: userObj
|
||||
user: user
|
||||
|
||||
startTransaction: ->
|
||||
# start a batch transaction - nothing between now and @commit() will be set immediately
|
||||
|
|
@ -80,7 +79,7 @@ module.exports.BatchUpdate = BatchUpdate = (model) ->
|
|||
# many cases, userObj.tasks.{taskId}.value is undefined - so we manually .get() each attribute here.
|
||||
# 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) -> userObj[key] = lodash.cloneDeep user.get(key)
|
||||
#_.each Object.keys(userSchema), (key) -> userObj[key] = lodash.cloneDeep user.get(key)
|
||||
model._commit = (txn) ->
|
||||
txn.dontPersist = true
|
||||
origCommit.apply(model, arguments)
|
||||
|
|
@ -91,22 +90,13 @@ module.exports.BatchUpdate = BatchUpdate = (model) ->
|
|||
If transaction not in progress, it just runs standard model.set()
|
||||
###
|
||||
set: (path, val) ->
|
||||
if transactionInProgress
|
||||
updates[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) ->
|
||||
if (arr.length - 1) == index then curr[next] = val
|
||||
return curr[next]
|
||||
, userObj
|
||||
else
|
||||
updates[path] = val if transactionInProgress
|
||||
user.set(path, val)
|
||||
|
||||
commit: ->
|
||||
_.each updates, (val, path) ->
|
||||
user.set(path, val)
|
||||
model._commit = origCommit
|
||||
# some hackery in our own branched racer-db-mongo, see findAndModify of lefnire/racer-db-mongo#habitrpg index.js
|
||||
user.set "update__", updates
|
||||
transactionInProgress = false
|
||||
updates = {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,10 +59,11 @@ taskDeltaFormula = (currentValue, direction) ->
|
|||
{update} if aggregated changes, pass in userObj as update. otherwise commits will be made immediately
|
||||
###
|
||||
updateStats = (newStats, batch) ->
|
||||
userObj = batch.userObj
|
||||
user = batch.user
|
||||
stats = batch.user.get('stats')
|
||||
|
||||
# if user is dead, dont do anything
|
||||
return if userObj.stats.lvl == 0
|
||||
return if stats.lvl == 0
|
||||
|
||||
if newStats.hp?
|
||||
# Game Over
|
||||
|
|
@ -78,16 +79,17 @@ updateStats = (newStats, batch) ->
|
|||
tnl = user.get '_tnl'
|
||||
if newStats.exp >= tnl
|
||||
newStats.exp -= tnl
|
||||
batch.set 'stats.lvl', userObj.stats.lvl + 1
|
||||
batch.set 'stats.lvl', stats.lvl + 1
|
||||
batch.set 'stats.hp', 50
|
||||
newStats.lvl = userObj.stats.lvl
|
||||
if !userObj.items?.itemsEnabled and newStats.lvl >= 2
|
||||
newStats.lvl = stats.lvl
|
||||
if !user.get('items.itemsEnabled') and newStats.lvl >= 2
|
||||
batch.set 'items.itemsEnabled', true #bit of trouble using userSet here
|
||||
if !userObj.flags?.partyEnabled and newStats.lvl >= 3
|
||||
if !user.get('flags.partyEnabled') and newStats.lvl >= 3
|
||||
batch.set 'flags.partyEnabled', true
|
||||
batch.set 'stats.exp', newStats.exp
|
||||
|
||||
if newStats.money?
|
||||
#FIXME what was I doing here? I can't remember, money isn't defined
|
||||
money = 0.0 if (!money? or money<0)
|
||||
batch.set 'stats.money', newStats.money
|
||||
|
||||
|
|
@ -100,15 +102,15 @@ score = (taskId, direction, times, batch, cron) ->
|
|||
|
||||
commit = false
|
||||
unless batch?
|
||||
console.log("HI")
|
||||
commit = true
|
||||
batch = new schema.BatchUpdate(model)
|
||||
batch.startTransaction()
|
||||
userObj = batch.userObj
|
||||
|
||||
{money, hp, exp, lvl} = userObj.stats
|
||||
{money, hp, exp, lvl} = user.get('stats')
|
||||
|
||||
taskPath = "tasks.#{taskId}"
|
||||
taskObj = userObj.tasks[taskId]
|
||||
taskObj = user.get(taskPath)
|
||||
{type, value} = taskObj
|
||||
|
||||
delta = 0
|
||||
|
|
@ -182,11 +184,11 @@ cron = (resetDom_cb) ->
|
|||
batch = new schema.BatchUpdate(model)
|
||||
batch.startTransaction()
|
||||
batch.set 'lastCron', today
|
||||
userObj = batch.userObj
|
||||
hpBefore = userObj.stats.hp #we'll use this later so we can animate hp loss
|
||||
user = batch.user
|
||||
hpBefore = user.get('stats.hp') #we'll use this later so we can animate hp loss
|
||||
# Tally each task
|
||||
todoTally = 0
|
||||
_.each userObj.tasks, (taskObj) ->
|
||||
_.each user.get('tasks'), (taskObj) ->
|
||||
#FIXME remove broken tasks
|
||||
if taskObj.id? # a task had a null id during cron, this should not be happening
|
||||
{id, type, completed, repeat} = taskObj
|
||||
|
|
@ -217,20 +219,22 @@ cron = (resetDom_cb) ->
|
|||
batch.set('tasks.' + taskObj.id, taskObj)
|
||||
|
||||
# Finished tallying
|
||||
userObj.history ?= {}; userObj.history.todos ?= []; userObj.history.exp ?= []
|
||||
userObj.history.todos.push { date: today, value: todoTally }
|
||||
history = user.get('history') || {}
|
||||
history.todos ?= []; history.exp ?= []
|
||||
history.todos.push { date: today, value: todoTally }
|
||||
# tally experience
|
||||
expTally = userObj.stats.exp
|
||||
expTally = user.get('stats.exp')
|
||||
lvl = 0 #iterator
|
||||
while lvl < (userObj.stats.lvl-1)
|
||||
while lvl < (user.get('stats.lvl')-1)
|
||||
lvl++
|
||||
expTally += (lvl*100)/5
|
||||
userObj.history.exp.push { date: today, value: expTally }
|
||||
history.exp.push { date: today, value: expTally }
|
||||
|
||||
# Set the new user specs, and animate HP loss
|
||||
[hpAfter, userObj.stats.hp] = [userObj.stats.hp, hpBefore]
|
||||
batch.set('stats', userObj.stats)
|
||||
batch.set('history', userObj.history)
|
||||
stats = user.get('stats')
|
||||
[hpAfter, stats.hp] = [stats.hp, hpBefore]
|
||||
batch.set('stats', stats)
|
||||
batch.set('history', history)
|
||||
batch.commit()
|
||||
resetDom_cb(model)
|
||||
setTimeout (-> user.set 'stats.hp', hpAfter), 1000 # animate hp loss
|
||||
|
|
|
|||
Loading…
Reference in a new issue