Class: MinerMover::Mover

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

Instance Attribute Summary collapse

Attributes inherited from Worker

#debugging, #logging, #timer, #variance

Instance Method Summary collapse

Methods inherited from Worker

#debug, #id, #log, #to_s, #varied

Constructor Details

#initialize(batch_size: 10, rate: 2, work_type: :cpu, variance: 0, logging: false, debugging: false, timer: nil) ⇒ Mover

Returns a new instance of Mover.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/miner_mover/worker.rb', line 120

def initialize(batch_size: 10,
               rate: 2,  # 2M ore per sec
               work_type: :cpu,
               variance: 0,
               logging: false,
               debugging: false,
               timer: nil)
  @batch_size = batch_size * Ore::BLOCK
  @rate = rate.to_f * Ore::BLOCK
  @work_type = work_type
  @batch, @batches, @ore_moved = 0, 0, 0
  super(variance: variance, timer: timer,
        logging: logging, debugging: debugging)
end

Instance Attribute Details

#batchObject (readonly)

Returns the value of attribute batch.



118
119
120
# File 'lib/miner_mover/worker.rb', line 118

def batch
  @batch
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



118
119
120
# File 'lib/miner_mover/worker.rb', line 118

def batch_size
  @batch_size
end

#batchesObject (readonly)

Returns the value of attribute batches.



118
119
120
# File 'lib/miner_mover/worker.rb', line 118

def batches
  @batches
end

#ore_movedObject (readonly)

Returns the value of attribute ore_moved.



118
119
120
# File 'lib/miner_mover/worker.rb', line 118

def ore_moved
  @ore_moved
end

#rateObject (readonly)

Returns the value of attribute rate.



118
119
120
# File 'lib/miner_mover/worker.rb', line 118

def rate
  @rate
end

#work_typeObject (readonly)

Returns the value of attribute work_type.



118
119
120
# File 'lib/miner_mover/worker.rb', line 118

def work_type
  @work_type
end

Instance Method Details

#load_ore(amount) ⇒ Object

accept some ore for moving; just accumulate unless we have a full batch



153
154
155
156
157
158
159
# File 'lib/miner_mover/worker.rb', line 153

def load_ore amount
  log format("LOAD %s | %s", Ore.display(amount), self.status)
  @batch += amount
  move_batch if @batch >= @batch_size
  log format("LDED %s | %s", Ore.display(amount), self.status)
  @batch
end

#move(amount) ⇒ Object

perform the work of moving the amount of ore



177
178
179
180
181
182
183
# File 'lib/miner_mover/worker.rb', line 177

def move amount
  duration = self.varied(amount / @rate)
  log format("MOVE %s (%.2f s)", Ore.display(amount), duration)
  _, elapsed = Timer.elapsed { MinerMover.work(duration, @work_type) }
  log format("MOVD %s (%.2f s)", Ore.display(amount), elapsed)
  self
end

#move_batchObject

return the amount moved



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/miner_mover/worker.rb', line 162

def move_batch
  raise "unexpected batch: #{@batch}" if @batch <= 0
  amount = [@batch, @batch_size].min

  self.move amount

  # accounting
  @ore_moved += amount
  @batch -= amount
  @batches += 1

  amount
end

#stateObject



135
136
137
138
139
140
141
# File 'lib/miner_mover/worker.rb', line 135

def state
  super.merge(work_type: @work_type,
              batch_size: @batch_size,
              batch: @batch,
              batches: @batches,
              ore_moved: @ore_moved)
end

#statusObject



143
144
145
146
147
148
149
150
# File 'lib/miner_mover/worker.rb', line 143

def status
  [format("Batch %s / %s %i%%",
          Ore.units(@batch),
          Ore.units(@batch_size),
          @batch.to_f * 100 / @batch_size),
   format("Moved %ix (%s)", @batches, Ore.units(@ore_moved)),
  ].join(' | ')
end