Skip to content

Commit 4f2a62f

Browse files
committed
slight refactor
1 parent b5548c7 commit 4f2a62f

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

lib/miner_mover/worker.rb

+38-30
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,36 @@ def varied n
7777
class Miner < Worker
7878
attr_accessor :depth, :partial_reward
7979

80-
def initialize(depth: 10,
81-
partial_reward: true,
80+
def initialize(depth: 5,
81+
partial_reward: false,
8282
variance: 0,
8383
logging: false,
8484
debugging: false,
8585
timer: nil)
8686
@partial_reward = partial_reward
8787
@depth = depth
88-
super(variance: variance, logging: logging,
89-
debugging: debugging, timer: timer)
88+
super(variance: variance, timer: timer,
89+
logging: logging, debugging: debugging)
9090
end
9191

9292
def state
9393
super.merge(depth: @depth, partial_reward: @partial_reward)
9494
end
9595

96+
# return an array of integers representing ore mined at each depth
97+
def mine_ores(depth = @depth)
98+
# every new depth is a new mining operation
99+
Array.new(depth) { |d|
100+
# mine ore by calculating fibonacci for that depth
101+
mined = MinerMover.fib(self.varied(d).round)
102+
@partial_reward ? rand(1 + mined) : mined
103+
}
104+
end
105+
106+
# wrap the above method with logging, timing, and summing
96107
def mine_ore(depth = @depth)
97108
log format("MINE Depth %i", depth)
98-
ores, elapsed = Timer.elapsed {
99-
# every new depth is a new mining operation
100-
Array.new(depth) { |d|
101-
# mine ore by calculating fibonacci for that depth
102-
mined = MinerMover.fib self.varied(d).round
103-
@partial_reward ? rand(1 + mined) : mined
104-
}
105-
}
109+
ores, elapsed = Timer.elapsed { self.mine_ores(depth) }
106110
total = ores.sum
107111
log format("MIND %s %s (%.2f s)",
108112
Ore.display(total), ores.inspect, elapsed)
@@ -124,8 +128,8 @@ def initialize(batch_size: 10,
124128
@rate = rate.to_f * Ore::BLOCK
125129
@work_type = work_type
126130
@batch, @batches, @ore_moved = 0, 0, 0
127-
super(variance: variance, logging: logging,
128-
debugging: debugging, timer: timer)
131+
super(variance: variance, timer: timer,
132+
logging: logging, debugging: debugging)
129133
end
130134

131135
def state
@@ -145,33 +149,37 @@ def status
145149
].join(' | ')
146150
end
147151

148-
def load_ore(amt)
149-
@batch += amt
152+
# accept some ore for moving; just accumulate unless we have a full batch
153+
def load_ore amount
154+
log format("LOAD %s | %s", Ore.display(amount), self.status)
155+
@batch += amount
150156
move_batch if @batch >= @batch_size
151-
log format("LOAD %s", self.status)
157+
log format("LDED %s | %s", Ore.display(amount), self.status)
152158
@batch
153159
end
154160

155-
def move(duration)
156-
MinerMover.work(duration, @work_type)
157-
end
158-
161+
# return the amount moved
159162
def move_batch
160163
raise "unexpected batch: #{@batch}" if @batch <= 0
161-
amt = [@batch, @batch_size].min
162-
duration = self.varied(amt / @rate)
164+
amount = [@batch, @batch_size].min
163165

164-
log format("MOVE %s (%.2f s)", Ore.display(amt), duration)
165-
_, elapsed = Timer.elapsed { self.move(duration) }
166-
log format("MOVD %s (%.2f s)", Ore.display(amt), elapsed)
166+
self.move amount
167167

168168
# accounting
169-
@ore_moved += amt
170-
@batch -= amt
169+
@ore_moved += amount
170+
@batch -= amount
171171
@batches += 1
172172

173-
# what moved
174-
amt
173+
amount
174+
end
175+
176+
# perform the work of moving the amount of ore
177+
def move amount
178+
duration = self.varied(amount / @rate)
179+
log format("MOVE %s (%.2f s)", Ore.display(amount), duration)
180+
_, elapsed = Timer.elapsed { MinerMover.work(duration, @work_type) }
181+
log format("MOVD %s (%.2f s)", Ore.display(amount), elapsed)
182+
self
175183
end
176184
end
177185
end

0 commit comments

Comments
 (0)