Class: Sorare::Rewards::Allocations::ComputeForQuality

Inherits:
Object
  • Object
show all
Includes:
Interactor
Defined in:
lib/sorare/rewards/interactors/allocations/compute_for_quality.rb

Overview

ComputeForQuality computes the reward allocations for a given quality supply between the divisions of a league

Instance Method Summary collapse

Instance Method Details

#allocate!Object



29
30
31
32
33
34
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 29

def allocate!
  allocate_cards
  allocate_pct if has?('pct')
  allocate_extra if has?('pct')
  allocate_loop if has?('cards_per_loop')
end

#allocate_cardsObject



40
41
42
43
44
45
46
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 40

def allocate_cards
  allocate_divisions do |division_config, division|
    count = [remaining_supply, division_config['cards'] || 0].min

    allocated[division] += count
  end
end

#allocate_divisions(&block) ⇒ Object



36
37
38
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 36

def allocate_divisions(&block)
  config.each_with_index(&block)
end

#allocate_extraObject



64
65
66
67
68
69
70
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 64

def allocate_extra
  allocate_divisions do |division_config, division|
    count = pick_extra_supply((supply * (division_config['pct'] || 0)).modulo(1))

    allocated[division] += count
  end
end

#allocate_loopObject



56
57
58
59
60
61
62
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 56

def allocate_loop
  while remaining_supply.positive?
    allocate_divisions do |division_config, division|
      allocated[division] += [division_config['cards_per_loop'] || 0, remaining_supply].min
    end
  end
end

#allocate_pctObject



48
49
50
51
52
53
54
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 48

def allocate_pct
  allocate_divisions do |division_config, division|
    count = [remaining_supply, (supply * (division_config['pct'] || 0)).floor].min

    allocated[division] += count
  end
end

#allocationsObject



23
24
25
26
27
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 23

def allocations
  config.length.times.to_h do |division|
    [Sorare::Rewards.configuration.transform_division.call(division + 1), allocated[division]]
  end
end

#callObject



15
16
17
18
19
20
21
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 15

def call
  check_config!

  context.allocated = Array.new(config.length, 0)
  allocate!
  context.quality_allocations = allocations
end

#check_config!Object



88
89
90
91
92
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 88

def check_config!
  return if config

  context.fail!(error: 'Invalid config')
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 84

def has?(key)
  !config.first[key].nil?
end

#pick_extra_supply(probability) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 76

def pick_extra_supply(probability)
  reward_probability = randomizer.rand
  return 0 unless remaining_supply.positive?
  return 0 unless reward_probability < probability

  1
end

#remaining_supplyObject



72
73
74
# File 'lib/sorare/rewards/interactors/allocations/compute_for_quality.rb', line 72

def remaining_supply
  supply - allocated.sum
end