mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 03:52:14 +00:00
Leveling up!!
This commit is contained in:
parent
98c29e0459
commit
e99b3ee755
4 changed files with 32 additions and 2 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
<% if current_user %>
|
<% if current_user %>
|
||||||
<div id="progressbar" style="width:30%;"><span id="tnl">(Level 1) <%= score %>/100</span></div>
|
<div id="progressbar" style="width:30%;">
|
||||||
|
<span id="tnl">(Level <%= current_user.lvl %>) <%= 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>
|
||||||
|
|
|
||||||
11
db/migrate/20120206170948_add_lvl_experience_to_user.rb
Normal file
11
db/migrate/20120206170948_add_lvl_experience_to_user.rb
Normal 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
|
||||||
Loading…
Reference in a new issue