Class: BCDice::GameSystem::BeginningIdol::ChainTable

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/game_system/beginning_idol/chain_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, type, items) ⇒ ChainTable

Returns a new instance of ChainTable.

Parameters:

  • name (String)

    表の名前

  • type (String)

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

  • items (Array<Array<String, #roll>>)

    表の項目の配列



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bcdice/game_system/beginning_idol/chain_table.rb', line 10

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

Instance Method Details

#roll(randomizer) ⇒ String

表を振る

Parameters:

  • randomizer (#roll_sum)

    ランダマイザ

Returns:

  • (String)

    結果



26
27
28
29
30
31
32
33
# File 'lib/bcdice/game_system/beginning_idol/chain_table.rb', line 26

def roll(randomizer)
  value = randomizer.roll_sum(@times, @sides)
  index = value - @times
  chosen = @items[index]
  body = chosen.map { |item| item.respond_to?(:roll) ? item.roll(randomizer) : item }.join("\n")

  return "#{@name}(#{value}) > #{body}"
end