Proper reward processing & updating user-stats on vote save

This commit is contained in:
Tyler Renelle
2012-02-11 02:26:09 -05:00
parent d39dae354b
commit e2429632a1
9 changed files with 83 additions and 57 deletions
@@ -11,17 +11,23 @@ class HabitTracker.Models.Habit extends Backbone.Model
done: false
position: 0
isHabit: ->
isHabit: =>
@get("habit_type")==1
isDaily: ->
isDaily: =>
@get("habit_type")==2
isTodo: =>
@get('habit_type')==3
isDoneTodo: ->
@get("habit_type")==3 and @get("done")
isDoneTodo: =>
@isTodo() and @get("done")
isRemainingTodo: ->
@get("habit_type")==3 and !@get("done")
isRemainingTodo: =>
@isTodo() and !@get("done")
isReward: =>
@get('habit_type')==4
vote: (direction) ->
# For negative values, use a line: something like y=-.1x+1
@@ -34,18 +40,20 @@ class HabitTracker.Models.Habit extends Backbone.Model
delta = (( -0.1 * score + 1 ) * sign)
else
delta = (( Math.pow(0.9, score) ) * sign)
score += delta
score += delta unless @isReward
# up/down -voting as checkbox & assigning as done, 2 birds one stone
done = @get("done")
if !@isHabit()
done = true if direction=="up"
done = false if direction=="down"
@save({ score: score, done: done })
@set({ score: score, done: done })
window.userStats.updateStats(this, delta)
#send all the update information, as well as tack on userStats which will save to Users
@save({ user_stats: window.userStats })
class HabitTracker.Collections.HabitsCollection extends Backbone.Collection
model: HabitTracker.Models.Habit
url: '/habits'
@@ -15,8 +15,11 @@ class HabitTracker.Models.User extends Backbone.Model
@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()
if delta>0 or (habit.isDaily() or habit.isTodo())
@set({money: @get('money')+delta})
# if buying an item, deduct cost
if habit.isReward()
@set({money: @get('money')-habit.get('score')})
@trigger('updatedStats')
@@ -0,0 +1,7 @@
<!-- TODO buy link -->
<a class="buy-link" href=#><img src="assets/dollar.png" /></a>
<%= name %>
($<%= score %>)
<div class='edit'>
<a href="#/<%= id %>/edit">Edit</a>
</div>
@@ -8,15 +8,15 @@ class HabitTracker.Views.Habits.HabitView extends Backbone.View
"click .vote-up" : "voteUp"
"click .vote-down" : "voteDown"
destroy: () ->
destroy: () =>
@model.destroy()
this.remove()
return false
voteUp: ->
voteUp: =>
@model.vote("up")
voteDown: ->
voteDown: =>
@model.vote("down")
tagName: "li"
@@ -11,9 +11,10 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View
# TODO create a view & template, bind to existing element
updateStats: () =>
stats = window.userStats
@$('#tnl').html( "(Level #{stats.get('lvl')})&nbsp;&nbsp;&nbsp;#{Math.round(stats.get('exp'))} / #{stats.tnl()}" )
@$( "#progressbar" ).progressbar value: stats.get('exp')/stats.tnl() * 100
$('#tnl').html( "(Level #{stats.get('lvl')})&nbsp;&nbsp;&nbsp;#{Math.round(stats.get('exp'))} / #{stats.tnl()}" )
$( "#progressbar" ).progressbar value: stats.get('exp')/stats.tnl() * 100
# TODO for some reason this has to be @$, but above has to be $
money = stats.get('money').toFixed(1).split('.')
@$('#money').html("#{money[0]} <img src='assets/coin_single_gold.png'/> #{money[1]} <img src='assets/coin_single_silver.png'/>")
@@ -26,6 +27,9 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View
if habit.isDaily() then @$("#habits-daily").append(view.render().el)
if habit.isDoneTodo() then @$("#habits-todos-done").append(view.render().el)
if habit.isRemainingTodo() then @$("#habits-todos-remaining").append(view.render().el)
if habit.isReward()
view = new HabitTracker.Views.Habits.RewardView({model : habit})
@$("#rewards").append(view.render().el)
render: =>
$(@el).html(@template(habits: @options.habits.toJSON() ))
@@ -0,0 +1,32 @@
HabitTracker.Views.Habits ||= {}
class HabitTracker.Views.Habits.RewardView extends Backbone.View
template: JST["backbone/templates/habits/reward"]
events:
"click .destroy" : "destroy"
"click .buy-link" : "voteDown"
destroy: () ->
@model.destroy()
this.remove()
return false
voteDown: =>
if @model.get('score') > window.userStats.get('money')
$('#money').effect("pulsate", 100);
else
@model.vote("down")
tagName: "li"
className: "reward"
render: ->
$(@el).attr('id', "habit_#{@model.get('id')}")
$(@el).html(@template(@model.toJSON() ))
@$(".comment").qtip content:
text: (api) ->
$(this).next().html()
return this