Class: BCDice::DiceTable::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/dice_table/table.rb

Overview

表を表すクラス

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, items) ⇒ Table

Returns a new instance of Table.

Parameters:

  • name (String)

    表の名前

  • type (String)

    項目を選ぶときのダイスロールの方法 ‘1D6’など

  • items (Array<String>)

    表の項目の配列



18
19
20
21
22
23
24
25
26
27
28
29
# 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

Class Method Details

.from_i18n(key, locale) ⇒ Table

Parameters:

  • key (String)
  • locale (Symbol)

Returns:



10
11
12
13
# 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

Instance Method Details

#choice(value) ⇒ Object



39
40
41
42
# 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) ⇒ String

表を振る

Parameters:

  • bcdice (BCDice)

    ランダマイザ

Returns:

  • (String)

    結果



34
35
36
37
# File 'lib/bcdice/dice_table/table.rb', line 34

def roll(bcdice)
  value = bcdice.roll_sum(@times, @sides)
  return choice(value)
end