Class: RubyWarrior::Profile
- Inherits:
-
Object
- Object
- RubyWarrior::Profile
- Defined in:
- lib/ruby_warrior/profile.rb
Instance Attribute Summary collapse
-
#abilities ⇒ Object
Returns the value of attribute abilities.
-
#average_grade ⇒ Object
Returns the value of attribute average_grade.
-
#current_epic_grades ⇒ Object
Returns the value of attribute current_epic_grades.
-
#current_epic_score ⇒ Object
Returns the value of attribute current_epic_score.
-
#epic_score ⇒ Object
Returns the value of attribute epic_score.
-
#last_level_number ⇒ Object
Returns the value of attribute last_level_number.
-
#level_number ⇒ Object
Returns the value of attribute level_number.
-
#player_path ⇒ Object
Returns the value of attribute player_path.
-
#score ⇒ Object
Returns the value of attribute score.
-
#tower_path ⇒ Object
Returns the value of attribute tower_path.
-
#warrior_name ⇒ Object
Returns the value of attribute warrior_name.
Class Method Summary collapse
Instance Method Summary collapse
- #add_abilities(*abilities) ⇒ Object
- #calculate_average_grade ⇒ Object
- #current_level ⇒ Object
- #directory_name ⇒ Object
- #enable_epic_mode ⇒ Object
- #enable_normal_mode ⇒ Object
- #encode ⇒ Object
- #epic? ⇒ Boolean
- #epic_score_with_grade ⇒ Object
-
#initialize ⇒ Profile
constructor
A new instance of Profile.
- #level_after_epic? ⇒ Boolean
- #next_level ⇒ Object
- #save ⇒ Object
- #to_s ⇒ Object
- #tower ⇒ Object
- #update_epic_score ⇒ Object
Constructor Details
#initialize ⇒ Profile
Returns a new instance of Profile.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_warrior/profile.rb', line 7 def initialize @tower_path = nil @warrior_name = nil @score = 0 @current_epic_score = 0 @current_epic_grades = {} @epic_score = 0 @average_grade = nil @abilities = [] @level_number = 0 @last_level_number = nil end |
Instance Attribute Details
#abilities ⇒ Object
Returns the value of attribute abilities.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def abilities @abilities end |
#average_grade ⇒ Object
Returns the value of attribute average_grade.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def average_grade @average_grade end |
#current_epic_grades ⇒ Object
Returns the value of attribute current_epic_grades.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def current_epic_grades @current_epic_grades end |
#current_epic_score ⇒ Object
Returns the value of attribute current_epic_score.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def current_epic_score @current_epic_score end |
#epic_score ⇒ Object
Returns the value of attribute epic_score.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def epic_score @epic_score end |
#last_level_number ⇒ Object
Returns the value of attribute last_level_number.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def last_level_number @last_level_number end |
#level_number ⇒ Object
Returns the value of attribute level_number.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def level_number @level_number end |
#player_path ⇒ Object
Returns the value of attribute player_path.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def player_path @player_path end |
#score ⇒ Object
Returns the value of attribute score.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def score @score end |
#tower_path ⇒ Object
Returns the value of attribute tower_path.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def tower_path @tower_path end |
#warrior_name ⇒ Object
Returns the value of attribute warrior_name.
5 6 7 |
# File 'lib/ruby_warrior/profile.rb', line 5 def warrior_name @warrior_name end |
Class Method Details
.decode(str) ⇒ Object
30 31 32 |
# File 'lib/ruby_warrior/profile.rb', line 30 def self.decode(str) Marshal.load(Base64.decode64(str)) end |
.load(path) ⇒ Object
34 35 36 37 38 |
# File 'lib/ruby_warrior/profile.rb', line 34 def self.load(path) player = decode(File.read(path)) player.player_path = File.dirname(path) player end |
Instance Method Details
#add_abilities(*abilities) ⇒ Object
76 77 78 79 |
# File 'lib/ruby_warrior/profile.rb', line 76 def add_abilities(*abilities) @abilities += abilities @abilities.uniq! end |
#calculate_average_grade ⇒ Object
113 114 115 116 117 118 |
# File 'lib/ruby_warrior/profile.rb', line 113 def calculate_average_grade if @current_epic_grades.size > 0 sum = @current_epic_grades.values.inject(0) { |sum, value| sum + value } sum / @current_epic_grades.size end end |
#current_level ⇒ Object
68 69 70 |
# File 'lib/ruby_warrior/profile.rb', line 68 def current_level Level.new(self, level_number) end |
#directory_name ⇒ Object
44 45 46 |
# File 'lib/ruby_warrior/profile.rb', line 44 def directory_name [warrior_name.downcase.gsub(/[^a-z0-9]+/, '-'), tower.name].join('-') end |
#enable_epic_mode ⇒ Object
81 82 83 84 85 86 |
# File 'lib/ruby_warrior/profile.rb', line 81 def enable_epic_mode @epic = true @epic_score ||= 0 @current_epic_score ||= 0 @last_level_number ||= @level_number end |
#enable_normal_mode ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/ruby_warrior/profile.rb', line 88 def enable_normal_mode @epic = false @epic_score = 0 @current_epic_score = 0 @current_epic_grades = {} @average_grade = nil @level_number = @last_level_number @last_level_number = nil end |
#encode ⇒ Object
20 21 22 |
# File 'lib/ruby_warrior/profile.rb', line 20 def encode Base64.encode64(Marshal.dump(self)) end |
#epic? ⇒ Boolean
98 99 100 |
# File 'lib/ruby_warrior/profile.rb', line 98 def epic? @epic end |
#epic_score_with_grade ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/ruby_warrior/profile.rb', line 56 def epic_score_with_grade if average_grade "#{epic_score} (#{Level.grade_letter(average_grade)})" else epic_score end end |
#level_after_epic? ⇒ Boolean
102 103 104 |
# File 'lib/ruby_warrior/profile.rb', line 102 def level_after_epic? Level.new(self, last_level_number+1).exists? if last_level_number end |
#next_level ⇒ Object
72 73 74 |
# File 'lib/ruby_warrior/profile.rb', line 72 def next_level Level.new(self, level_number+1) end |
#save ⇒ Object
24 25 26 27 28 |
# File 'lib/ruby_warrior/profile.rb', line 24 def save update_epic_score @level_number = 0 if epic? File.open(player_path + '/.profile', 'w') { |f| f.write(encode) } end |
#to_s ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/ruby_warrior/profile.rb', line 48 def to_s if epic? [warrior_name, tower.name, "first score #{score}", "epic score #{epic_score_with_grade}"].join(' - ') else [warrior_name, tower.name, "level #{level_number}", "score #{score}"].join(' - ') end end |
#tower ⇒ Object
64 65 66 |
# File 'lib/ruby_warrior/profile.rb', line 64 def tower Tower.new(File.basename @tower_path) end |
#update_epic_score ⇒ Object
106 107 108 109 110 111 |
# File 'lib/ruby_warrior/profile.rb', line 106 def update_epic_score if @current_epic_score > @epic_score @epic_score = @current_epic_score @average_grade = calculate_average_grade end end |