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