Class: Sorare::Rewards::FlatPicker
- Inherits:
-
Object
- Object
- Sorare::Rewards::FlatPicker
- Defined in:
- lib/sorare/rewards/flat_picker.rb
Overview
FlatPicker allows to randomly pick an element in a flat array
Direct Known Subclasses
Defined Under Namespace
Classes: NoSupplyError
Instance Attribute Summary collapse
-
#counter ⇒ Object
Returns the value of attribute counter.
-
#elements ⇒ Object
Returns the value of attribute elements.
Instance Method Summary collapse
- #add(slug, count) ⇒ Object
-
#initialize(seed, salt) ⇒ FlatPicker
constructor
A new instance of FlatPicker.
- #pick ⇒ Object
- #pick! ⇒ Object
- #picked_count(element) ⇒ Object
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
#counter ⇒ Object
Returns the value of attribute counter.
9 10 11 |
# File 'lib/sorare/rewards/flat_picker.rb', line 9 def counter @counter end |
#elements ⇒ Object
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 |
#pick ⇒ Object
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 |