class BCDice::DiceTable::Table

表を表すクラス

Public Class Methods

from_i18n(key, locale) click to toggle source

@param key [String] @param locale [Symbol] @return [Table]

# File lib/bcdice/dice_table/table.rb, line 10
def self.from_i18n(key, locale)
  table = I18n.t(key, locale: locale)
  new(table[:name], table[:type], table[:items])
end
new(name, type, items) click to toggle source

@param [String] name 表の名前 @param [String] type 項目を選ぶときのダイスロールの方法 '1D6'など @param [Array<String>] items 表の項目の配列

# File lib/bcdice/dice_table/table.rb, line 18
def initialize(name, type, items)
  @name = name
  @items = items.freeze

  m = /(\d+)D(\d+)/i.match(type)
  unless m
    raise ArgumentError, "Unexpected table type: #{type}"
  end

  @times = m[1].to_i
  @sides = m[2].to_i
end

Public Instance Methods

choice(value) click to toggle source
# File lib/bcdice/dice_table/table.rb, line 39
def choice(value)
  index = value - @times
  return RollResult.new(@name, value, @items[index])
end
roll(bcdice) click to toggle source

表を振る @param [BCDice] bcdice ランダマイザ @return [String] 結果

# File lib/bcdice/dice_table/table.rb, line 34
def roll(bcdice)
  value = bcdice.roll_sum(@times, @sides)
  return choice(value)
end