Class: Sorare::Rewards::Cards::PickForDivisionRarityAndQuality

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

Overview

PickForDivisionRarityAndQuality picks the rewards for a given division, rarity, and quality Receive a picker A game week data And a reward allocations

15

Constant Summary collapse

MISSING_CARDS =
'There are not enough cards to fulfill the requirements for %<league>s %<rarity>s'

Instance Method Summary collapse

Instance Method Details

#callObject



23
24
25
26
27
28
29
# File 'lib/sorare/rewards/interactors/cards/pick_for_division_rarity_and_quality.rb', line 23

def call
  context.cards_picked = []

  pick!
  check_length! unless force
  reorder!
end

#check_length!Object



48
49
50
51
52
# File 'lib/sorare/rewards/interactors/cards/pick_for_division_rarity_and_quality.rb', line 48

def check_length!
  return if cards_picked.length == allocations

  context.fail!(error: format(MISSING_CARDS, { league: league, rarity: rarity }))
end

#pick!Object



31
32
33
# File 'lib/sorare/rewards/interactors/cards/pick_for_division_rarity_and_quality.rb', line 31

def pick!
  pick_if_available! while cards_picked.length != allocations && picker.length.positive?
end

#pick_if_available!Object



35
36
37
38
39
40
# File 'lib/sorare/rewards/interactors/cards/pick_for_division_rarity_and_quality.rb', line 35

def pick_if_available!
  player = picker.pick
  return if picker.picked_count(player) > game_week.player(player).supply_for(league, rarity).available

  cards_picked.push(player)
end

#reorder!Object



42
43
44
45
46
# File 'lib/sorare/rewards/interactors/cards/pick_for_division_rarity_and_quality.rb', line 42

def reorder!
  cards_picked.sort! do |a, b|
    game_week.player(a).rank(league, rarity) <=> game_week.player(b).rank(league, rarity)
  end
end