class Gacha::Box
Public Class Methods
new()
click to toggle source
# File lib/gacha/box.rb, line 3 def initialize() self.clear @consumable = false end
Public Instance Methods
add(item)
click to toggle source
# File lib/gacha/box.rb, line 22 def add(item) @items.push(item) @total_rate += item.rate end
clear()
click to toggle source
# File lib/gacha/box.rb, line 38 def clear @items = [] @total_rate = 0 end
consumable?()
click to toggle source
# File lib/gacha/box.rb, line 55 def consumable? @consumable end
count()
click to toggle source
# File lib/gacha/box.rb, line 47 def count @items.count end
draw()
click to toggle source
# File lib/gacha/box.rb, line 12 def draw position = self.random_position return nil if position == nil item = self.find(position) if item != nil and @consumable self.remove(item) end return item end
empty?()
click to toggle source
# File lib/gacha/box.rb, line 43 def empty? @items.empty? and @total_rate == 0 end
remove(item)
click to toggle source
# File lib/gacha/box.rb, line 27 def remove(item) @items.delete_if do |x| if x == item @total_rate -= item.rate true else false end end end
set_consumable(consumable)
click to toggle source
# File lib/gacha/box.rb, line 51 def set_consumable(consumable) @consumable = consumable end
to_s()
click to toggle source
# File lib/gacha/box.rb, line 8 def to_s "Gacha::Box #{@items.join(", ")}" end
Protected Instance Methods
find(position)
click to toggle source
# File lib/gacha/box.rb, line 68 def find(position) current = 0 @items.each do |item| current += item.rate if position < current return item end end return nil end
random_position()
click to toggle source
# File lib/gacha/box.rb, line 60 def random_position if @total_rate < 1 nil else rand(@total_rate) end end