Leveling up!!

This commit is contained in:
Tyler Renelle 2012-02-06 12:44:07 -05:00
parent 98c29e0459
commit e99b3ee755
4 changed files with 32 additions and 2 deletions

View file

@ -29,6 +29,7 @@ class Habit < ActiveRecord::Base
end end
self.score += value self.score += value
self.user.exp += value
# also add money. Only take away money if it was a mistake (aka, a checkbox) # also add money. Only take away money if it was a mistake (aka, a checkbox)
if (direction=='up') || (direction=='down' && clear==false && self.habit_type!=Habit::ALWAYS) if (direction=='up') || (direction=='down' && clear==false && self.habit_type!=Habit::ALWAYS)

View file

@ -9,4 +9,20 @@ class User < ActiveRecord::Base
has_many :habits, :dependent => :destroy has_many :habits, :dependent => :destroy
has_many :rewards, :dependent => :destroy has_many :rewards, :dependent => :destroy
before_save :calculate_experience
def calculate_experience
self.exp = 0 if self.exp < 0
if (self.exp > self.tnl)
self.exp -= self.tnl # carry over
self.lvl += 1
end
end
#TODO figure this out. Google "RPG level up formula" or something
def tnl
# http://tibia.wikia.com/wiki/Formula
50*self.lvl**2 - 150*self.lvl + 200
end
end end

View file

@ -1,9 +1,11 @@
<% if current_user %> <% if current_user %>
<div id="progressbar" style="width:30%;"><span id="tnl">(Level 1)&nbsp;&nbsp;&nbsp;<%= score %>/100</span></div> <div id="progressbar" style="width:30%;">
<span id="tnl">(Level <%= current_user.lvl %>)&nbsp;&nbsp;&nbsp;<%= current_user.exp.to_i.to_s + "/" + current_user.tnl.to_i.to_s %></span>
</div>
<script> <script>
$(function() { $(function() {
$( "#progressbar" ).progressbar({ $( "#progressbar" ).progressbar({
value: <%= score %> value: <%= (current_user.exp/current_user.tnl) * 100 %>
}); });
}); });
</script> </script>

View file

@ -0,0 +1,11 @@
class AddLvlExperienceToUser < ActiveRecord::Migration
def up
add_column :users, :exp, :float, :default=>0.0
add_column :users, :lvl, :integer, :default=>1
end
def down
remove_column :users, :exp
remove_column :users, :lvl
end
end