mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 03:52:14 +00:00
pass model by reference to scoring functions
This commit is contained in:
parent
e4b8742320
commit
d20fccea75
4 changed files with 223 additions and 224 deletions
|
|
@ -45,13 +45,12 @@ get '/', (page, model, params, next) ->
|
|||
|
||||
ready (model) ->
|
||||
user = model.at('_user')
|
||||
score = new scoring.Scoring(model)
|
||||
|
||||
#set cron immediately
|
||||
lastCron = user.get('lastCron')
|
||||
user.set('lastCron', +new Date) if (!lastCron? or lastCron == 'new')
|
||||
|
||||
score.cron()
|
||||
scoring.cron(model)
|
||||
|
||||
character.app(exports, model)
|
||||
tasks.app(exports, model)
|
||||
|
|
|
|||
|
|
@ -7,15 +7,15 @@ character = require './character'
|
|||
items = require './items'
|
||||
algos = require './algos'
|
||||
|
||||
module.exports.Scoring = (model) ->
|
||||
MODIFIER = algos.MODIFIER # each new level, armor, weapon add 2% modifier (this mechanism will change)
|
||||
MODIFIER = algos.MODIFIER # each new level, armor, weapon add 2% modifier (this mechanism will change)
|
||||
|
||||
# {taskId} task you want to score
|
||||
# {direction} 'up' or 'down'
|
||||
# {times} # times to call score on this task (1 unless cron, usually)
|
||||
# {update} if we're running updates en-mass (eg, cron on server) pass in userObj
|
||||
score = (model, taskId, direction, times, batch, cron) ->
|
||||
user = model.at '_user'
|
||||
|
||||
# {taskId} task you want to score
|
||||
# {direction} 'up' or 'down'
|
||||
# {times} # times to call score on this task (1 unless cron, usually)
|
||||
# {update} if we're running updates en-mass (eg, cron on server) pass in userObj
|
||||
score = (taskId, direction, times, batch, cron) ->
|
||||
commit = false
|
||||
unless batch?
|
||||
commit = true
|
||||
|
|
@ -108,7 +108,7 @@ module.exports.Scoring = (model) ->
|
|||
taskObj.value = value
|
||||
batch.set "#{taskPath}.value", taskObj.value
|
||||
origStats = _.clone obj.stats
|
||||
updateStats {hp: hp, exp: exp, gp: gp}, batch
|
||||
updateStats model, {hp: hp, exp: exp, gp: gp}, batch
|
||||
if commit
|
||||
# newStats / origStats is a glorious hack to trick Derby into seeing the change in model.on(*)
|
||||
newStats = _.clone batch.obj().stats
|
||||
|
|
@ -118,12 +118,13 @@ module.exports.Scoring = (model) ->
|
|||
batch.commit()
|
||||
return delta
|
||||
|
||||
###
|
||||
###
|
||||
Updates user stats with new stats. Handles death, leveling up, etc
|
||||
{stats} new stats
|
||||
{update} if aggregated changes, pass in userObj as update. otherwise commits will be made immediately
|
||||
###
|
||||
updateStats = (newStats, batch) ->
|
||||
###
|
||||
updateStats = (model, newStats, batch) ->
|
||||
user = model.at '_user'
|
||||
obj = batch.obj()
|
||||
|
||||
# if user is dead, dont do anything
|
||||
|
|
@ -172,11 +173,12 @@ module.exports.Scoring = (model) ->
|
|||
gp = 0.0 if (!gp? or gp<0)
|
||||
obj.stats.gp = newStats.gp
|
||||
|
||||
###
|
||||
###
|
||||
At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
|
||||
For incomplete Dailys, deduct experience
|
||||
###
|
||||
cron = () ->
|
||||
###
|
||||
cron = (model) ->
|
||||
user = model.at '_user'
|
||||
today = +new Date
|
||||
daysPassed = helpers.daysBetween(today, user.get('lastCron'))
|
||||
if daysPassed > 0
|
||||
|
|
@ -203,7 +205,7 @@ module.exports.Scoring = (model) ->
|
|||
thatDay = moment().subtract('days', n+1)
|
||||
if repeat[helpers.dayMapping[thatDay.day()]]==true
|
||||
daysFailed++
|
||||
score id, 'down', daysFailed, batch, true
|
||||
score model, id, 'down', daysFailed, batch, true
|
||||
|
||||
if type == 'daily'
|
||||
taskObj.history ?= []
|
||||
|
|
@ -235,7 +237,7 @@ module.exports.Scoring = (model) ->
|
|||
setTimeout (-> user.set 'stats.hp', hpAfter), 1000 # animate hp loss
|
||||
|
||||
|
||||
return {
|
||||
module.exports = {
|
||||
score: score
|
||||
cron: cron
|
||||
|
||||
|
|
@ -243,4 +245,4 @@ module.exports.Scoring = (model) ->
|
|||
expModifier: algos.expModifier
|
||||
hpModifier: algos.hpModifier
|
||||
taskDeltaFormula: algos.taskDeltaFormula
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ module.exports.view = (view) ->
|
|||
|
||||
module.exports.app = (appExports, model) ->
|
||||
user = model.at('_user')
|
||||
score = new scoring.Scoring(model)
|
||||
|
||||
user.on 'set', 'tasks.*.completed', (i, completed, previous, isLocal, passed) ->
|
||||
return if passed? && passed.cron # Don't do this stuff on cron
|
||||
|
|
@ -37,7 +36,7 @@ module.exports.app = (appExports, model) ->
|
|||
|
||||
# Score the user based on todo task
|
||||
task = user.at("tasks.#{i}")
|
||||
score.score(i, direction())
|
||||
scoring.score(model, i, direction())
|
||||
|
||||
appExports.addTask = (e, el, next) ->
|
||||
type = $(el).attr('data-task-type')
|
||||
|
|
@ -82,7 +81,7 @@ module.exports.app = (appExports, model) ->
|
|||
return # Cancel. Don't delete, don't hurt user
|
||||
else
|
||||
task.set('type','habit') # hack to make sure it hits HP, instead of performing "undo checkbox"
|
||||
score.score(id, direction:'down')
|
||||
scoring.score(model, id, direction:'down')
|
||||
|
||||
# prevent accidently deleting long-standing tasks
|
||||
else
|
||||
|
|
@ -172,4 +171,4 @@ module.exports.app = (appExports, model) ->
|
|||
direction = 'up' if direction == 'true/'
|
||||
direction = 'down' if direction == 'false/'
|
||||
task = model.at $(el).parents('li')[0]
|
||||
score.score(task.get('id'), direction)
|
||||
scoring.score(model, task.get('id'), direction)
|
||||
|
|
|
|||
|
|
@ -48,8 +48,7 @@ router.post '/v1/users/:uid/tasks/:taskId/:direction', (req, res) ->
|
|||
down: true
|
||||
notes: "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task."
|
||||
|
||||
score = new scoring.Scoring(model)
|
||||
delta = score.score(taskId, direction)
|
||||
delta = scoring.score(model, taskId, direction)
|
||||
result = model.get ('_user.stats')
|
||||
result.delta = delta
|
||||
res.send(result)
|
||||
|
|
|
|||
Loading…
Reference in a new issue