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
# File 'lib/ruby_warrior/profile.rb', line 7

def initialize
  @tower_path = nil
  @warrior_name = nil
  @score = 0
  @current_epic_score = 0
  @epic_score = 0
  @abilities = []
  @level_number = 0
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

#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

#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



27
28
29
# File 'lib/ruby_warrior/profile.rb', line 27

def self.decode(str)
  Marshal.load(Base64.decode64(str))
end

.load(path) ⇒ Object



31
32
33
34
35
# File 'lib/ruby_warrior/profile.rb', line 31

def self.load(path)
  player = decode(File.read(path))
  player.player_path = File.dirname(path)
  player
end

Instance Method Details

#add_abilities(*abilities) ⇒ Object



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

def add_abilities(*abilities)
  @abilities += abilities
  @abilities.uniq!
end

#current_levelObject



57
58
59
# File 'lib/ruby_warrior/profile.rb', line 57

def current_level
  Level.new(self, level_number)
end

#directory_nameObject



41
42
43
# File 'lib/ruby_warrior/profile.rb', line 41

def directory_name
  [warrior_name.downcase.gsub(/[^a-z0-9]+/, '-'), tower.name].join('-')
end

#enable_epic_modeObject



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

def enable_epic_mode
  @epic = true
  @epic_score ||= 0
  @current_epic_score ||= 0
end

#encodeObject



17
18
19
# File 'lib/ruby_warrior/profile.rb', line 17

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

#epic?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ruby_warrior/profile.rb', line 76

def epic?
  @epic
end

#next_levelObject



61
62
63
# File 'lib/ruby_warrior/profile.rb', line 61

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

#saveObject



21
22
23
24
25
# File 'lib/ruby_warrior/profile.rb', line 21

def save
  update_epic_score
  @level_number = 0 if epic?
  File.open(player_path + '/.profile', 'w') { |f| f.write(encode) }
end

#to_sObject



45
46
47
48
49
50
51
# File 'lib/ruby_warrior/profile.rb', line 45

def to_s
  if epic?
    [warrior_name, tower.name, "first score #{score}", "epic score #{epic_score}"].join(' - ')
  else
    [warrior_name, tower.name, "level #{level_number}", "score #{score}"].join(' - ')
  end
end

#towerObject



53
54
55
# File 'lib/ruby_warrior/profile.rb', line 53

def tower
  Tower.new(@tower_path)
end

#update_epic_scoreObject



80
81
82
83
84
# File 'lib/ruby_warrior/profile.rb', line 80

def update_epic_score
  if @current_epic_score > @epic_score
    @epic_score = @current_epic_score
  end
end