Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 2nd, 2013  |  syntax: Ruby  |  size: 3.67 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. TARGET_TOTAL = { :common => 200, :rare => 140, :epic => 100, :legendary => 30 }
  2. CARDS_BY_RARITY = { :common => 100, :rare => 70, :epic => 50, :legendary => 30 }
  3. RARITY_CHANCE = { :legendary => 0.005, :epic => 0.025, :rare => 0.1, :common => 1 }
  4. RARITY_PRIORITY_CHECK = [ :legendary, :epic, :rare, :common ]
  5. DISENCHANTED_ARCANE_DUST = { :common => 2, :rare => 10, :epic => 100, :legendary => 400 }
  6. CRAFT_ARCANE_DUST = { :legendary => 1600, :epic => 400, :rare => 100, :common => 10 }
  7. CRAFT_RARITY_PRIORITY = [ :legendary, :epic, :rare, :common ]
  8. CARDS_PER_PACK = 5
  9.  
  10. class Card
  11.   attr_accessor :rarity, :number
  12.   def initialize(rarity, number)
  13.     @rarity = rarity
  14.     @number = number
  15.   end
  16. end
  17.  
  18. class PackGenerator
  19.  
  20.   def initialize
  21.     @collection = { :common => [], :rare => [], :epic => [], :legendary => [] }
  22.     @arcane_dust = 0
  23.     @packs_open = 0
  24.     @disenchanted = 0
  25.   end
  26.  
  27.   def check_packs_needed
  28.     while !is_collection_complete?
  29.       add_collection(random_pack)
  30.       @packs_open += 1
  31.       #puts info
  32.     end
  33.    
  34.     return @packs_open
  35.   end
  36.  
  37.   def add_collection(pack)
  38.     pack.each do |card|
  39.       unless is_card_number_already_in_collection?(card.rarity, card.number)
  40.         @collection[card.rarity] << card
  41.       else
  42.         disenchant_card(card)
  43.       end
  44.     end
  45.   end
  46.  
  47.   def disenchant_card(card)
  48.     @arcane_dust += DISENCHANTED_ARCANE_DUST[card.rarity]
  49.     @disenchanted += 1
  50.  
  51.     CRAFT_RARITY_PRIORITY.each do |rarity|
  52.       if @collection[rarity].size != TARGET_TOTAL[rarity]
  53.         craft_card(rarity) if CRAFT_ARCANE_DUST[rarity] <= @arcane_dust
  54.         break
  55.       end
  56.     end
  57.   end
  58.  
  59.   def craft_card(rarity)
  60.     (1..CARDS_BY_RARITY[rarity]).each do |i|
  61.       unless is_card_number_already_in_collection?(rarity, i)
  62.         @arcane_dust -= CRAFT_ARCANE_DUST[rarity]
  63.         @collection[rarity] << Card.new(rarity, i)
  64.         return
  65.       end
  66.     end
  67.   end
  68.  
  69.   def is_card_number_already_in_collection?(rarity, i)
  70.     target_total = rarity == :legendary ? 1 : 2
  71.     return @collection[rarity].select { |card| card.number == i }.size == target_total  
  72.   end
  73.  
  74.   def random_pack
  75.     @last_pack = []
  76.     (1..5).each do |i|
  77.       @last_pack << random_rarity_card((i == 5 && !@last_pack.any? { |card| card.rarity != :common }))
  78.       #@last_pack << random_rarity_card(i == 1)
  79.     end
  80.     return @last_pack
  81.   end
  82.  
  83.   def random_rarity_card(force_rare)
  84.     r = rand # get rarity
  85.  
  86.     card = nil
  87.     RARITY_PRIORITY_CHECK.each do |rarity|
  88.       chance = force_rare && rarity == :rare ? 1 : RARITY_CHANCE[rarity]
  89.       if r < chance
  90.         card = Card.new(rarity, random_card_from_rarity(CARDS_BY_RARITY[rarity]))
  91.         break
  92.       end
  93.     end
  94.  
  95.     return card
  96.   end
  97.  
  98.   def random_card_from_rarity(max)
  99.     return (rand * (max - 1) + 1).round
  100.   end
  101.  
  102.   def is_collection_complete?
  103.     @collection.each do |rarity, list|
  104.       return false if list.size != TARGET_TOTAL[rarity]
  105.     end
  106.     return true
  107.   end
  108.  
  109.   def info
  110.     msg = ""
  111.     msg << "#{@packs_open}\t"
  112.     msg << "#{@last_pack.collect { |c| c.rarity }.inspect}\t"
  113.     msg << "#{@collection[:common].size}\t"
  114.     msg << "#{@collection[:rare].size}\t"
  115.     msg << "#{@collection[:epic].size}\t"
  116.     msg << "#{@collection[:legendary].size}\t"
  117.     msg << "#{@disenchanted}\t"
  118.     msg << "#{@arcane_dust}"
  119.     return msg
  120.   end
  121. end
  122.  
  123. #STDOUT.reopen(File.open('generator.log', 'w+'))
  124.  
  125. results = []
  126. (1..500).each do |i|
  127.   p = PackGenerator.new
  128.   results << p.check_packs_needed
  129.   puts "[#{i}] Packs needed: #{results[i-1]}."
  130. end
  131. puts "Average: #{results.inject{|sum,x| sum + x} / results.size}"
  132. puts "Max: #{results.max}"
  133. puts "Min: #{results.min}"