Class: Sorare::Rewards::FlatPicker

Inherits:
Object
  • Object
show all
Defined in:
lib/sorare/rewards/flat_picker.rb

Overview

FlatPicker allows to randomly pick an element in a flat array

Direct Known Subclasses

CardPicker

Defined Under Namespace

Classes: NoSupplyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seed, salt) ⇒ FlatPicker

Returns a new instance of FlatPicker.



15
16
17
18
19
20
# File 'lib/sorare/rewards/flat_picker.rb', line 15

def initialize(seed, salt)
  @random = Random.new(seed, salt)
  @elements = []
  @counter = Hash.new(0)
  @shuffled = false
end

Instance Attribute Details

#counterObject

Returns the value of attribute counter.



9
10
11
# File 'lib/sorare/rewards/flat_picker.rb', line 9

def counter
  @counter
end

#elementsObject

Returns the value of attribute elements.



9
10
11
# File 'lib/sorare/rewards/flat_picker.rb', line 9

def elements
  @elements
end

Instance Method Details

#add(slug, count) ⇒ Object



22
23
24
25
26
# File 'lib/sorare/rewards/flat_picker.rb', line 22

def add(slug, count)
  raise "Can't add an element to a started draw" if @shuffled

  @elements += Array.new(count, slug)
end

#pickObject



28
29
30
31
32
33
34
# File 'lib/sorare/rewards/flat_picker.rb', line 28

def pick
  shuffle! unless @shuffled

  @elements.shift.tap do |element|
    @counter[element] += 1 if element
  end
end

#pick!Object



36
37
38
39
40
# File 'lib/sorare/rewards/flat_picker.rb', line 36

def pick!
  pick.tap do |picked|
    raise NoSupplyError unless picked
  end
end

#picked_count(element) ⇒ Object



42
43
44
# File 'lib/sorare/rewards/flat_picker.rb', line 42

def picked_count(element)
  @counter[element]
end