Class: MinerMover::Miner

Inherits:
Worker
  • Object
show all
Defined in:
lib/miner_mover/worker.rb

Instance Attribute Summary collapse

Attributes inherited from Worker

#logging, #timer, #variance

Instance Method Summary collapse

Methods inherited from Worker

#id, #log, #to_s, #varied

Constructor Details

#initialize(depth: 10, partial_reward: true, variance: 0, logging: false, timer: nil) ⇒ Miner

Returns a new instance of Miner.



71
72
73
74
75
76
77
78
79
# File 'lib/miner_mover/worker.rb', line 71

def initialize(depth: 10,
               partial_reward: true,
               variance: 0,
               logging: false,
               timer: nil)
  @partial_reward = partial_reward
  @depth = depth
  super(variance: variance, logging: logging, timer: timer)
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



69
70
71
# File 'lib/miner_mover/worker.rb', line 69

def depth
  @depth
end

#partial_rewardObject

Returns the value of attribute partial_reward.



69
70
71
# File 'lib/miner_mover/worker.rb', line 69

def partial_reward
  @partial_reward
end

Instance Method Details

#mine_ore(depth = @depth) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/miner_mover/worker.rb', line 85

def mine_ore(depth = @depth)
  log format("MINE Depth %i", depth)
  ores, elapsed = CompSci::Timer.elapsed {
    # every new depth is a new mining operation
    Array.new(depth) { |d|
      # mine ore by calculating fibonacci for that depth
      mined = CompSci::Fibonacci.classic(self.varied(d).round)
      @partial_reward ? rand(1 + mined) : mined
    }
  }
  total = ores.sum
  log format("MIND %s %s (%.2f s)",
             Ore.display(total), ores.inspect, elapsed)
  total
end

#stateObject



81
82
83
# File 'lib/miner_mover/worker.rb', line 81

def state
  super.merge(depth: @depth, partial_reward: @partial_reward)
end