mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-08-01 11:40:25 +00:00
another attempt at cron set optimizations
This commit is contained in:
parent
d7e28ea07f
commit
0512aacbbf
3 changed files with 21 additions and 28 deletions
|
|
@ -94,33 +94,14 @@ resetDom = (model) ->
|
|||
window.DERBY.app.dom.clear()
|
||||
view.render(model)
|
||||
|
||||
cron = (model) ->
|
||||
user = model.at('_user')
|
||||
|
||||
# This is an expensive function, only call it on cron
|
||||
lastCron = user.get('lastCron')
|
||||
return unless !lastCron or (helpers.daysBetween(new Date(), lastCron) > 0)
|
||||
|
||||
userObj = user.get()
|
||||
|
||||
# hp-shimmy so we can animate the hp-loss
|
||||
before = {hp:userObj.stats.hp, lastCron:userObj.lastCron}
|
||||
scoring.cron(userObj)
|
||||
after = {hp:userObj.stats.hp, lastCron:userObj.lastCron}
|
||||
userObj.stats.hp = before.hp
|
||||
|
||||
model.set "users.#{userObj.id}", userObj
|
||||
resetDom(model)
|
||||
setTimeout (-> user.set 'stats.hp', after.hp), 1000 # animate hp loss
|
||||
|
||||
ready (model) ->
|
||||
user = model.at('_user')
|
||||
user.set('lastCron', +new Date) if user.get('lastCron')=='new' #set cron immediately
|
||||
|
||||
# Setup model in scoring functions
|
||||
scoring.setModel(model)
|
||||
|
||||
# First things first. Preen the user object, check if dailies, etc
|
||||
cron(model)
|
||||
scoring.cron()
|
||||
|
||||
# Load all the jQuery, Growl, Tour, etc
|
||||
browser.loadJavaScripts(model)
|
||||
|
|
@ -228,7 +209,7 @@ ready (model) ->
|
|||
|
||||
matrix = [['Date', 'Score']]
|
||||
for obj in model.get(historyPath)
|
||||
date = new Date(obj.date)
|
||||
date = +new Date(obj.date)
|
||||
readableDate = moment(date).format('MM/DD')
|
||||
matrix.push [ readableDate, obj.value ]
|
||||
data = google.visualization.arrayToDataTable matrix
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ _ = require 'underscore'
|
|||
module.exports.userSchema = ->
|
||||
# deep clone, else further new users get duplicate objects
|
||||
newUser = require('lodash').cloneDeep
|
||||
lastCron: 'new' #this will be replaced with a date on first run
|
||||
balance: 2
|
||||
stats: { money: 0, exp: 0, lvl: 1, hp: 50 }
|
||||
items: { itemsEnabled: false, armor: 0, weapon: 0 }
|
||||
|
|
|
|||
|
|
@ -144,7 +144,7 @@ score = (taskId, direction, times, update) ->
|
|||
adjustvalue = if (taskObj.up==false or taskObj.down==false) then false else true
|
||||
calculateDelta(adjustvalue)
|
||||
# Add habit value to habit-history (if different)
|
||||
historyEntry = { date: new Date(), value: value } if taskObj.value != value
|
||||
historyEntry = { date: +new Date(), value: value } if taskObj.value != value
|
||||
if (delta > 0) then addPoints() else subtractPoints()
|
||||
model.push "_user.#{taskPath}.history", historyEntry
|
||||
|
||||
|
|
@ -179,11 +179,12 @@ score = (taskId, direction, times, update) ->
|
|||
At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
|
||||
For incomplete Dailys, deduct experience
|
||||
###
|
||||
cron = (userObj) ->
|
||||
today = new Date()
|
||||
userObj.lastCron ?= today
|
||||
daysPassed = helpers.daysBetween(today, userObj.lastCron)
|
||||
cron = ->
|
||||
today = +new Date
|
||||
daysPassed = helpers.daysBetween(today, user.get('lastCron'))
|
||||
if daysPassed > 0
|
||||
userObj = user.get()
|
||||
hpBefore = userObj.stats.hp #we'll use this later so we can animate hp loss
|
||||
# Tally each task
|
||||
todoTally = 0
|
||||
_.each userObj.tasks, (taskObj) ->
|
||||
|
|
@ -214,6 +215,7 @@ cron = (userObj) ->
|
|||
else
|
||||
absVal = if (completed) then Math.abs(value) else value
|
||||
todoTally += absVal
|
||||
user.set 'tasks.' + taskObj.id, taskObj
|
||||
|
||||
# Finished tallying
|
||||
userObj.history ?= {}; userObj.history.todos ?= []; userObj.history.exp ?= []
|
||||
|
|
@ -225,7 +227,16 @@ cron = (userObj) ->
|
|||
lvl++
|
||||
expTally += (lvl*100)/5
|
||||
userObj.history.exp.push { date: today, value: expTally }
|
||||
userObj.lastCron = today # reset cron
|
||||
user.set 'lastCron', today
|
||||
|
||||
# Set the new user specs, and animate HP loss
|
||||
[hpAfter, userObj.stats.hp] = [userObj.stats.hp, hpBefore]
|
||||
user.set 'stats', userObj.stats
|
||||
user.set 'history', userObj.history
|
||||
window.DERBY.app.dom.clear()
|
||||
window.DERBY.app.view.render(model)
|
||||
setTimeout (-> user.set 'stats.hp', hpAfter), 1000 # animate hp loss
|
||||
|
||||
|
||||
module.exports = {
|
||||
setModel: setModel
|
||||
|
|
|
|||
Loading…
Reference in a new issue