Class: Natas

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

Overview

OverTheWire wargame Natas

Constant Summary collapse

MAXLEVEL =
34

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ Natas

Returns a new instance of Natas.



16
17
18
19
20
21
22
# File 'lib/natas.rb', line 16

def initialize(shell)
  @shell = shell
  @level = nil
  @levels = []
  ObjectSpace.each_object(Class).select { |c| c < NatasLevelBase }.each { |c| @levels << c.new(@shell) }
  @levels.sort! { |a, b| a.level <=> b.level }
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



14
15
16
# File 'lib/natas.rb', line 14

def level
  @level
end

#levelsObject

Returns the value of attribute levels.



14
15
16
# File 'lib/natas.rb', line 14

def levels
  @levels
end

Instance Method Details

#execObject

Raises:

  • (StandardError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/natas.rb', line 24

def exec
  level = @levels.detect { |l| l.level == @level }

  raise StandardError, "Level #{@level} not implemented" if level.nil?
  raise StandardError, "Level #{@level} has no password" if level.password.nil?

  password = level.exec
  level = @levels.detect { |l| l.level == @level + 1 }
  return if level.nil?

  level.password = password
end

#to_yamlObject



37
38
39
40
41
42
43
# File 'lib/natas.rb', line 37

def to_yaml
  data = {}
  @levels.each do |level|
    data[level.level] = level.password
  end
  YAML.dump(data)
end