diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb index 00e18bff75..16273b51c1 100644 --- a/app/controllers/habits_controller.rb +++ b/app/controllers/habits_controller.rb @@ -80,6 +80,11 @@ class HabitsController < ApplicationController def vote @habit = current_user.habits.find(params[:id]) + + # habit.vote() saves money to the user, this hack prevents having to + # reload current_user to catch that change + @habit.user=current_user + @habit.vote(params[:vote]) @score = current_user.habits.sum('score').to_i diff --git a/app/helpers/habits_helper.rb b/app/helpers/habits_helper.rb index 373fb18648..0b05c7eaec 100644 --- a/app/helpers/habits_helper.rb +++ b/app/helpers/habits_helper.rb @@ -26,4 +26,12 @@ module HabitsHelper return link_to(text, { :action => "vote", :id => habit.id, :vote => dir }, :class=>style, :remote=>true) end + def user_gold + current_user.money.to_i + end + + def user_silver + number_with_precision(current_user.money, :precision=>1).split('.')[1] + end + end diff --git a/app/models/habit.rb b/app/models/habit.rb index 2c2852f81f..f552d3c91f 100644 --- a/app/models/habit.rb +++ b/app/models/habit.rb @@ -21,15 +21,27 @@ class Habit < ActiveRecord::Base # 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 ) + value = 0 if self.score < 0 - self.score += ( ( -0.1 * self.score + 1 ) * sign ) + value = ( ( -0.1 * self.score + 1 ) * sign ) else - self.score += ( ( 0.9 ** self.score ) * sign ) + value = ( ( 0.9 ** self.score ) * sign ) end + + self.score += value + + # also add money (we never take away money) + if direction=='up' + self.user.money += value + self.user.save + end + + # up/down -voting as checkbox & assigning as done, 2 birds one stone 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/_money.erb b/app/views/habits/_money.erb new file mode 100644 index 0000000000..7c73b908f7 --- /dev/null +++ b/app/views/habits/_money.erb @@ -0,0 +1,4 @@ +
+ <%= user_gold %><%= image_tag('coin_single_gold.png') %> + <%= user_silver %><%= image_tag('coin_single_silver.png') %> +
\ No newline at end of file diff --git a/app/views/habits/_progressbar.erb b/app/views/habits/_progressbar.erb index 50de5da38f..2d60d87a3e 100644 --- a/app/views/habits/_progressbar.erb +++ b/app/views/habits/_progressbar.erb @@ -1,4 +1,4 @@ -
<%= score %>/100
+
(Level 1)   <%= score %>/100