Class: RubyWarrior::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProfile

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

#abilitiesObject

Returns the value of attribute abilities.



5
6
7
# File 'lib/ruby_warrior/profile.rb', line 5

def abilities
  @abilities
end

#average_gradeObject

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_gradesObject

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_scoreObject

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_scoreObject

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_numberObject

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_numberObject

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_pathObject

Returns the value of attribute player_path.



5
6
7
# File 'lib/ruby_warrior/profile.rb', line 5

def player_path
  @player_path
end

#scoreObject

Returns the value of attribute score.



5
6
7
# File 'lib/ruby_warrior/profile.rb', line 5

def score
  @score
end

#tower_pathObject

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_nameObject

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_gradeObject



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_levelObject



68
69
70
# File 'lib/ruby_warrior/profile.rb', line 68

def current_level
  Level.new(self, level_number)
end

#directory_nameObject



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_modeObject



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_modeObject



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

#encodeObject



20
21
22
# File 'lib/ruby_warrior/profile.rb', line 20

def encode
  Base64.encode64(Marshal.dump(self))
end

#epic?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ruby_warrior/profile.rb', line 98

def epic?
  @epic
end

#epic_score_with_gradeObject



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

Returns:

  • (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_levelObject



72
73
74
# File 'lib/ruby_warrior/profile.rb', line 72

def next_level
  Level.new(self, level_number+1)
end

#saveObject



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_sObject



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

#towerObject



64
65
66
# File 'lib/ruby_warrior/profile.rb', line 64

def tower
  Tower.new(File.basename @tower_path)
end

#update_epic_scoreObject



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