Add equation to voting (more points for harder habits, less for easier habit)

This commit is contained in:
Tyler Renelle 2012-02-02 00:48:20 -05:00
parent f131c2bc40
commit a9e46e1ca4
4 changed files with 27 additions and 20 deletions

View file

@ -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

View file

@ -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

View file

@ -1,5 +1,4 @@
<li id="habit_<%= habit.id %>" class="ui-state-default">
<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
<%
s = habit.score
@ -13,19 +12,17 @@
name = "<span class='#{score}'>#{h(habit.name)}</span>"
%>
<% if habit.habit_type==Habit::ALWAYS %>
<%= link_to("√", { :action => "vote", :id => habit.id, :vote => 'up' }, :remote=>true) if habit.up %>
<%= link_to("X", { :action => "vote", :id => habit.id, :vote => 'down' }, :remote=>true) if habit.down %>
<%= raw name %> (<%= habit.score %>)
<%= raw name %> (<%= habit.score.to_i %>)
<% elsif habit.habit_type==Habit::DAILY %>
<% if habit.done %>
<%= link_to("[X]", { :action => "vote", :id => habit.id, :vote => 'down' }, :remote=>true) %>
<strike><%= raw name %></strike> (<%= habit.score %>)
<strike><%= raw name %></strike> (<%= habit.score.to_i %>)
<% else %>
<%= link_to("[ ]", { :action => "vote", :id => habit.id, :vote => 'up' }, :remote=>true) %>
<%= raw name %> (<%= habit.score %>)
<%= raw name %> (<%= habit.score.to_i %>)
<% end %>
<% end %>
<%= link_to 'Edit', edit_habit_path(habit) %>

View file

@ -0,0 +1,9 @@
class ChangeHabitsScoreToFloat < ActiveRecord::Migration
def up
change_column :habits, :score, :float
end
def down
change_column :habits, :score, :integer
end
end