class CwCardUtils::CurveCalculator
def calculate_curve
def calculate_curve @deck.each do |card| cmc = card.cmc # If CMC is nil, and it is a land, skip it, otherwise add it to the curve in the # zero cost bucket. (Handles X to cast cards.) if cmc.nil? || cmc == 0 if card.type.include?("Land") next else bucket = 0 end else bucket = cmc.ceil end @raw_curve[bucket] ||= 0 @raw_curve[bucket] += card.count end end
def collapsed_curve
def collapsed_curve return @collapsed_curve if @collapsed_curve.values.any? @collapsed_curve = { '0-1' => 0, '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6+' => 0, } curve.each do |cmc, count| case cmc when 0..1 then @collapsed_curve['0-1'] += count when 2 then @collapsed_curve['2'] += count when 3 then @collapsed_curve['3'] += count when 4 then @collapsed_curve['4'] += count when 5 then @collapsed_curve['5'] += count else @collapsed_curve['6+'] += count end end @collapsed_curve end
def collapsed_normalized_curve
def collapsed_normalized_curve normalize_collapsed_curve if @collapsed_normalized_curve.values.empty? @collapsed_normalized_curve end
def curve
def curve calculate_curve if @raw_curve.empty? @raw_curve end
def initialize(deck)
def initialize(deck) @deck = deck @deck_size = @deck.count_without_lands @raw_curve = {} @normalized_curve = {} @collapsed_normalized_curve = {} @collapsed_curve = {} end
def normalize_collapsed_curve
def normalize_collapsed_curve @collapsed_normalized_curve =collapsed_curve.sort_by { |cmc, _| cmc }.to_h.transform_values do |count| (count / @deck.count_without_lands.to_f).round(4) end end
def normalize_curve
def normalize_curve @normalized_curve = curve.sort_by { |cmc, _| cmc }.to_h.transform_values do |count| (count / @deck.count_without_lands.to_f).round(4) end end
def normalized_curve
def normalized_curve normalize_curve if @normalized_curve.values.empty? @normalized_curve end