mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-31 23:11:15 +00:00
habitpg-shared: move viewHelpers to misc, add hydrate() function (see https://github.com/codeparty/racer/issues/116) to prevent data-loss on user.get()
This commit is contained in:
parent
f412a20203
commit
2230ce6688
3 changed files with 27 additions and 13 deletions
|
|
@ -17,7 +17,7 @@ i18n.localize app,
|
|||
urlScheme: false
|
||||
checkHeader: true
|
||||
|
||||
require('./viewHelpers').setup view
|
||||
require('./misc').viewHelpers view
|
||||
|
||||
_ = require('lodash')
|
||||
algos = require 'habitrpg-shared/script/algos'
|
||||
|
|
@ -135,11 +135,9 @@ ready (model) ->
|
|||
require('./unlock').app(exports, model)
|
||||
require('./filters').app(exports, model)
|
||||
|
||||
###
|
||||
Cron
|
||||
###
|
||||
if algos.shouldCron(user)
|
||||
uObj = _.cloneDeep user.get() # need to clone, else derby won't catch model.set()'s after obj property sets
|
||||
cron = ->
|
||||
#return setTimeout(cron, 1) if model._txnQueue.length > 0
|
||||
uObj = _.cloneDeep misc.hydrate(user.get()) # need to clone, else derby won't catch model.set()'s after obj property sets
|
||||
# habitrpg-shared/algos requires uObj.habits, uObj.dailys etc instead of uObj.tasks
|
||||
_.each ['habit','daily','todo','reward'], (type) ->
|
||||
uObj["#{type}s"] = _.where(uObj.tasks, {type:type}); true
|
||||
|
|
@ -149,4 +147,5 @@ ready (model) ->
|
|||
_.each paths, (v,k) -> user.pass({cron:true}).set(k,helpers.dotGet(k, uObj)); true
|
||||
if lostHp
|
||||
browser.resetDom(model)
|
||||
setTimeout (-> user.set('stats.hp', uObj.stats.hp)), 750
|
||||
setTimeout (-> user.set('stats.hp', uObj.stats.hp)), 750
|
||||
cron() if algos.shouldCron {lastCron: user.get('lastCron'), preferences: user.get('preferences')}
|
||||
|
|
@ -1,9 +1,18 @@
|
|||
_ = require 'underscore'
|
||||
_ = require 'lodash'
|
||||
algos = require 'habitrpg-shared/script/algos'
|
||||
items = require('habitrpg-shared/script/items').items
|
||||
helpers = require('habitrpg-shared/script/helpers')
|
||||
|
||||
module.exports.setup = (view) ->
|
||||
###
|
||||
Make sure model.get() returns all properties, see https://github.com/codeparty/racer/issues/116
|
||||
###
|
||||
module.exports.hydrate = hydrate = (spec, hydrated={}) ->
|
||||
if _.isPlainObject(spec)
|
||||
hydrated[k] = hydrate(v, hydrated[k]) for k,v of spec
|
||||
hydrated
|
||||
else spec
|
||||
|
||||
module.exports.viewHelpers = (view) ->
|
||||
|
||||
#misc
|
||||
view.fn "percent", (x, y) ->
|
||||
|
|
@ -23,6 +32,9 @@ module.exports.setup = (view) ->
|
|||
view.fn "or", -> _.reduce arguments, (cumm, curr) -> cumm || curr
|
||||
view.fn "truarr", (num) -> num-1
|
||||
view.fn 'count', (arr) -> arr?.length or 0
|
||||
view.fn 'int',
|
||||
get: (num) -> num
|
||||
set: (num) -> parseInt(num)
|
||||
|
||||
#iCal
|
||||
view.fn "encodeiCalLink", helpers.encodeiCalLink
|
||||
|
|
@ -2,6 +2,7 @@ algos = require 'habitrpg-shared/script/algos'
|
|||
helpers = require 'habitrpg-shared/script/helpers'
|
||||
_ = require 'lodash'
|
||||
moment = require 'moment'
|
||||
misc = require './misc'
|
||||
|
||||
|
||||
###
|
||||
|
|
@ -18,12 +19,14 @@ module.exports.app = (appExports, model) ->
|
|||
perform the updates while tracking paths, then all the values at those paths
|
||||
###
|
||||
score = (taskId, direction) ->
|
||||
uObj = _.cloneDeep user.get() # need to clone, else derby won't catch model.set()'s after obj property sets
|
||||
# return setTimeout( (-> score(taskId, direction)), 500) if model._txnQueue.length > 0
|
||||
|
||||
uObj = _.cloneDeep misc.hydrate(user.get()) # see https://github.com/codeparty/racer/issues/116
|
||||
tObj = uObj.tasks[taskId]
|
||||
|
||||
# Stuff for undo
|
||||
tObjBefore = _.cloneDeep tObj
|
||||
tObjBefore.completed = !tObjBefore.completed if tObj.type in ['daily', 'todo']
|
||||
tObjBefore.completed = !tObj.completed if tObj.type in ['daily', 'todo']
|
||||
setUndo uObj.stats, tObjBefore # set previous state for undo
|
||||
|
||||
paths = {}
|
||||
|
|
@ -134,9 +137,9 @@ module.exports.app = (appExports, model) ->
|
|||
Call scoring functions for habits & rewards (todos & dailies handled below)
|
||||
###
|
||||
appExports.score = (e, el) ->
|
||||
task = model.at $(el).parents('li')[0]
|
||||
id = $(el).parents('li').attr('data-id')
|
||||
direction = $(el).attr('data-direction')
|
||||
score(task.get('id'), direction)
|
||||
score(id, direction)
|
||||
|
||||
###
|
||||
This is how we handle appExports.score for todos & dailies. Due to Derby's special handling of `checked={:task.completd}`,
|
||||
|
|
|
|||
Loading…
Reference in a new issue