Class: BCDice::DiceTable::D66Table

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

Overview

D66を振って出目を昇順/降順にして表を参照する

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sort_type, items) ⇒ D66Table

Returns a new instance of D66Table.

Parameters:

  • name (String)

    表の名前

  • sort_type (Symbol)

    出目入れ替えの方式 BCDice::D66SortType

  • items (Hash)

    表の項目 Key は数値



20
21
22
23
24
# File 'lib/bcdice/dice_table/d66_table.rb', line 20

def initialize(name, sort_type, items)
  @name = name
  @sort_type = sort_type
  @items = items.freeze
end

Class Method Details

.from_i18n(key, locale) ⇒ D66Table

Parameters:

  • key (String)
  • locale (Symbol)

Returns:



10
11
12
13
14
15
# File 'lib/bcdice/dice_table/d66_table.rb', line 10

def self.from_i18n(key, locale)
  table = I18n.t(key, locale: locale)
  sort_type = D66SortType.const_get(table[:d66_sort_type])

  new(table[:name], sort_type, table[:items])
end

Instance Method Details

#choice(key) ⇒ Object



45
46
47
# File 'lib/bcdice/dice_table/d66_table.rb', line 45

def choice(key)
  RollResult.new(@name, key, @items[key])
end

#roll(randomizer) ⇒ String

表を振る

Parameters:

  • randomizer (#roll_barabara)

    ランダマイザ

Returns:

  • (String)

    結果



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bcdice/dice_table/d66_table.rb', line 29

def roll(randomizer)
  dice = randomizer.roll_barabara(2, 6)

  case @sort_type
  when D66SortType::ASC
    dice.sort!
  when D66SortType::DESC
    dice.sort!.reverse!
  end

  key = dice[0] * 10 + dice[1]
  chosen = @items[key]
  chosen = chosen.roll(randomizer) if chosen.respond_to?(:roll)
  RollResult.new(@name, key, chosen)
end