diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb index 2906c73101..00e18bff75 100644 --- a/app/controllers/habits_controller.rb +++ b/app/controllers/habits_controller.rb @@ -7,7 +7,7 @@ class HabitsController < ApplicationController def index @habits = current_user.habits.where(:habit_type => Habit::ALWAYS) @daily = current_user.habits.where(:habit_type => Habit::DAILY) - @score = current_user.habits.sum('score') + @score = current_user.habits.sum('score').to_i respond_to do |format| format.html # index.html.erb @@ -81,18 +81,12 @@ class HabitsController < ApplicationController def vote @habit = current_user.habits.find(params[:id]) @habit.vote(params[:vote]) - @score = current_user.habits.sum('score') + @score = current_user.habits.sum('score').to_i respond_to do |format| - if @habit.save - # format.html { redirect_to @habit, notice: 'Habit was successfully updated.' } - # format.json { head :no_content } - format.js - else - # format.html { render action: "edit" } - # format.json { render json: @habit.errors, status: :unprocessable_entity } - format.js - end + # format.html { render action: "edit" } + # format.json { render json: @habit.errors, status: :unprocessable_entity } + format.js end end diff --git a/app/models/habit.rb b/app/models/habit.rb index 6ced909a3e..2c2852f81f 100644 --- a/app/models/habit.rb +++ b/app/models/habit.rb @@ -17,12 +17,19 @@ class Habit < ActiveRecord::Base end def vote(direction) - next_vote = 1 #TODO return log or linear based on current score - next_vote *= -1 if(direction=='down') - self.score += next_vote + # 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 = ( direction=='up' ? 1 : -1 ) + if self.score < 0 + self.score += ( ( -0.1 * self.score + 1 ) * sign ) + else + self.score += ( ( 0.9 ** self.score ) * sign ) + end if(self.habit_type==Habit::DAILY) self.done = true if direction=='up' self.done = false if direction=='down' end + save end end diff --git a/app/views/habits/_habit.html.erb b/app/views/habits/_habit.html.erb index ca0725c343..4d3203957d 100644 --- a/app/views/habits/_habit.html.erb +++ b/app/views/habits/_habit.html.erb @@ -1,5 +1,4 @@