Module: MinerMover::Ore

Defined in:
lib/miner_mover.rb

Overview

ore is handled in blocks of 1M

Constant Summary collapse

BLOCK =

between fib(30) and fib(31)

1_000_000
WORD_LENGTH =

bytes

4
WORD_MAX =

up to 4.3B ore

256 ** WORD_LENGTH
HEX_UNPACK =

4 bytes is 8 hex digits

"H#{WORD_LENGTH * 2}"

Class Method Summary collapse

Class Method Details

.block(ore, size = BLOCK) ⇒ Object

raw ore in, blocks out



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

def self.block(ore, size = BLOCK)
  ore.to_f / size
end

.decode(word) ⇒ Object

return an integer from the first 4 bytes



58
59
60
61
# File 'lib/miner_mover.rb', line 58

def self.decode(word)
  raise "unexpected size: #{word.bytesize}" unless word.bytesize == 4
  word.unpack('N').first
end

.display(ore) ⇒ Object

entirely used for display purposes



88
89
90
# File 'lib/miner_mover.rb', line 88

def self.display(ore)
  format("%s ore", self.units(ore))
end

.encode(ore) ⇒ Object

return 4 bytes, unsigned 32 bit integer, network order



52
53
54
55
# File 'lib/miner_mover.rb', line 52

def self.encode(ore)
  raise "WORD_MAX overflow: #{self.block(ore)}M ore" if ore > WORD_MAX
  [ore].pack('N')
end

.hex(word) ⇒ Object

return “0x01020304” for “x01x02x03x04”



64
65
66
67
# File 'lib/miner_mover.rb', line 64

def self.hex(word)
  raise "unexpected size: #{word.bytesize}" unless word.bytesize == 4
  "0x" + word.unpack(HEX_UNPACK).first
end

.units(ore) ⇒ Object

mostly used for display purposes



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/miner_mover.rb', line 75

def self.units(ore)
  if ore % BLOCK == 0 or ore > BLOCK * 100
    format("%iM", self.block(ore).round)
  elsif ore > BLOCK
    format("%.2fM", self.block(ore))
  elsif ore > 10_000
    format("%iK", self.block(ore, 1_000).round)
  else
    format("%i", ore)
  end
end