Merge branch 'master' into preen-users
Conflicts: src/app/index.coffee src/app/scoring.coffee
This commit is contained in:
+2
-22
@@ -94,34 +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 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.setNull 'lastCron', +new Date #set cron immediately
|
||||
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)
|
||||
|
||||
@@ -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 }
|
||||
|
||||
+19
-7
@@ -179,10 +179,13 @@ 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) ->
|
||||
cron = ->
|
||||
today = +new Date
|
||||
daysPassed = helpers.daysBetween(today, userObj.lastCron)
|
||||
daysPassed = helpers.daysBetween(today, user.get('lastCron'))
|
||||
if daysPassed > 0
|
||||
user.set 'lastCron', today
|
||||
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) ->
|
||||
@@ -205,14 +208,15 @@ cron = (userObj) ->
|
||||
daysFailed++
|
||||
score id, 'down', daysFailed, userObj
|
||||
|
||||
value = userObj.tasks[taskObj.id].value #get updated value
|
||||
value = taskObj.value #get updated value
|
||||
if type == 'daily'
|
||||
userObj.tasks[taskObj.id].history ?= []
|
||||
userObj.tasks[taskObj.id].history.push { date: today, value: value }
|
||||
userObj.tasks[taskObj.id].completed = false
|
||||
taskObj.history ?= []
|
||||
taskObj.history.push { date: today, value: value }
|
||||
taskObj.completed = false
|
||||
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 ?= []
|
||||
@@ -224,7 +228,15 @@ cron = (userObj) ->
|
||||
lvl++
|
||||
expTally += (lvl*100)/5
|
||||
userObj.history.exp.push { date: today, value: expTally }
|
||||
userObj.lastCron = today # reset cron
|
||||
|
||||
# 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
|
||||
|
||||
Reference in New Issue
Block a user