diff --git a/app/assets/javascripts/backbone/models/habit.js.coffee b/app/assets/javascripts/backbone/models/habit.js.coffee index 26923570e5..0ba6b46415 100644 --- a/app/assets/javascripts/backbone/models/habit.js.coffee +++ b/app/assets/javascripts/backbone/models/habit.js.coffee @@ -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) \ No newline at end of file diff --git a/app/assets/javascripts/backbone/models/user.js.coffee b/app/assets/javascripts/backbone/models/user.js.coffee new file mode 100644 index 0000000000..aa5d4053ed --- /dev/null +++ b/app/assets/javascripts/backbone/models/user.js.coffee @@ -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 diff --git a/app/assets/javascripts/backbone/routers/habits_router.js.coffee b/app/assets/javascripts/backbone/routers/habits_router.js.coffee index 2aab891573..9a6831e583 100644 --- a/app/assets/javascripts/backbone/routers/habits_router.js.coffee +++ b/app/assets/javascripts/backbone/routers/habits_router.js.coffee @@ -2,6 +2,7 @@ class HabitTracker.Routers.HabitsRouter extends Backbone.Router initialize: (options) -> @habits = new HabitTracker.Collections.HabitsCollection() @habits.reset options.habits + window.userStats = new HabitTracker.Models.User(options.user) routes: "/new" : "newHabit" diff --git a/app/assets/javascripts/backbone/views/habits/index_view.js.coffee b/app/assets/javascripts/backbone/views/habits/index_view.js.coffee index 72a00a0c80..56f160204e 100644 --- a/app/assets/javascripts/backbone/views/habits/index_view.js.coffee +++ b/app/assets/javascripts/backbone/views/habits/index_view.js.coffee @@ -6,6 +6,14 @@ class HabitTracker.Views.Habits.IndexView extends Backbone.View initialize: () -> @options.habits.bind('reset', @addAll) @options.habits.bind('change', @render, this) #TODO this ruins tabs, revisit + window.userStats.bind('updatedStats', @updateStats, this) + + updateStats: () => + # TODO create a view & template, bind to existing element + stats = window.userStats + #.to_i.to_s + $('#tnl').html( "(Level #{stats.get('lvl')})   #{Math.round(stats.get('exp'))} / #{stats.tnl()}" ) + $( "#progressbar" ).progressbar value: stats.get('exp')/stats.tnl() * 100 addAll: () => @options.habits.each(@addOne) diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb index 6be44c489d..ec95ac8ef2 100644 --- a/app/controllers/habits_controller.rb +++ b/app/controllers/habits_controller.rb @@ -4,26 +4,13 @@ class HabitsController < ApplicationController # GET /habits.json def index - # TODO @rewards = current_user.rewards - @habits = current_user.habits + @user = current_user respond_to do |format| format.html # index.html.erb - format.json { render json: @habits } + format.json { render json: @user } end end - # GET /habits/completed - # GET /habits/completed.json - def completed - @one_time = current_user.habits.where(:habit_type => Habit::ONE_TIME).where(:done=>true) - - respond_to do |format| - format.html # completed.html.erb - format.json { render json: @one_time } - end - end - - # GET /habits/new # GET /habits/new.json def new diff --git a/app/models/user.rb b/app/models/user.rb index 8a6695d5ff..c74cfaed29 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,4 +25,8 @@ class User < ActiveRecord::Base # http://tibia.wikia.com/wiki/Formula 50*self.lvl**2 - 150*self.lvl + 200 end + + def as_json(options={}) + super(:only => [:email, :lvl, :exp] ) + end end diff --git a/app/views/habits/index.html.erb b/app/views/habits/index.html.erb index de48a2725d..344e834a8b 100644 --- a/app/views/habits/index.html.erb +++ b/app/views/habits/index.html.erb @@ -3,7 +3,7 @@ \ No newline at end of file