Class: PayoutSystem::PodiumSplit

Inherits:
Base
  • Object
show all
Defined in:
lib/payout_system/podium_split.rb

Instance Attribute Summary

Attributes inherited from Base

#entries, #logger, #options, #order, #pot

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from PayoutSystem::Base

Instance Method Details

#default_ratiosObject



9
10
11
# File 'lib/payout_system/podium_split.rb', line 9

def default_ratios
  [0.1, 0.2, 0.7]
end

#ratiosObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/payout_system/podium_split.rb', line 13

def ratios
  temps = default_ratios

  results = []
  sorted_entry_groups.each do |points, entries|
    if entries.size == 1
      results << temps.pop
    else
      # pop number of times eq to entries size or pot size, whichever is smaller
      common_value = (0...([entries.size,temps.size].min)).to_a.inject(0) do |value, i|
        value += temps.pop
        value
      end

      each_value = (common_value.to_f/entries.size)

      entries.each { |entry| results << each_value }
    end
    break if temps.empty?
  end

  # if any of the pot split is left, distribute it among other entries
  if temps.any? && results.any?
    total = temps.inject(0){|s,a| s += a}
    each_split = (total.to_f / results.size)
    results.map!{|value| (value + each_split)}
  end

  results
end

#runObject



5
6
7
# File 'lib/payout_system/podium_split.rb', line 5

def run
  ratios.to_enum.with_index.map{|s,i| [sorted_entries[i], (pot*s).round]}
end