window.userStats updating on vote, changes exp bar (but not updating

to server)
This commit is contained in:
Tyler Renelle
2012-02-10 23:04:30 -05:00
parent 952cc206c5
commit b4c755f295
7 changed files with 46 additions and 39 deletions
@@ -23,14 +23,12 @@ class HabitTracker.Models.Habit extends Backbone.Model
isRemainingTodo: ->
@get("habit_type")==3 and !@get("done")
# TODO before_save on user, calculate delta on score & experience there, not in vote
# TODO retrieve this formula from server so don't have to have it in two locations (Habit.clear requires it server-side)
vote: (direction) ->
# For negative values, use a line: something like y=-.1x+1
# For positibe values, taper off with inverse log: y=.9^x
# Would love to use inverse log for the whole thing, but after 13 fails it hits infinity
sign = if (direction == "up") then 1 else -1
score = this.get("score")
score = @get("score")
delta = 0
if score < 0
delta = (( -0.1 * score + 1 ) * sign)
@@ -40,30 +38,14 @@ class HabitTracker.Models.Habit extends Backbone.Model
score += delta
# up/down -voting as checkbox & assigning as done, 2 birds one stone
done = this.get("done")
if this.get("habit_type")!=1
done = @get("done")
if !@isHabit()
done = true if direction=="up"
done = false if direction=="down"
this.save({ score: score, done: done })
@save({ score: score, done: done })
window.userStats.updateStats(this, delta)
class HabitTracker.Collections.HabitsCollection extends Backbone.Collection
model: HabitTracker.Models.Habit
url: '/habits'
#TODO
# standard: () ->
# @filter (habit)->
# habit.get('habit_type')==1
#
# daily: () ->
# @filter (habit) ->
# habit.get('habit_type')==2
#
# todosDone: () ->
# @filter (habit) ->
# (habit.get "done") and (habit.get('habit_type')==3)
#
# todosRemaining: () ->
# @without.apply(this, this.done()) and (habit.get('habit_type')==3)
@@ -0,0 +1,25 @@
# Basically a singleton global variable, not being synced with the server
# Reason is habit.update() will calculate user stats on server
class HabitTracker.Models.User extends Backbone.Model
updateStats: (habit, delta) ->
# set exp
@set({exp: @get('exp')+delta})
# can't go below 0 exp
if @get('exp') < 0 then @set({exp: 0})
# level up & carry-over exp
if @get('exp') > @tnl()
@set({ exp: @get('exp') - @tnl() })
@set({ lvl: @get('lvl') + 1 })
# also add money. Only take away money if it was a mistake (aka, a checkbox)
if delta>0 or !habit.isHabit()
@set({money: @get('money')+delta})
@trigger('updatedStats')
tnl: () ->
# http://tibia.wikia.com/wiki/Formula
50 * Math.pow(@get('lvl'), 2) - 150 * @get('lvl') + 200