Class: BCDice::DiceTable::D66LeftRangeTable

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

Overview

左側(十の位)のみ Range を用いる D66 表

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from D66Table

#choice, #roll

Constructor Details

#initialize(name, sort_type, items) ⇒ D66LeftRangeTable

Returns a new instance of D66LeftRangeTable.

Parameters:

  • name (String)

    表の名前

  • sort_type (Symbol)

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

  • items (Array<(Range, Array<String>)>)

    表の項目の配列



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bcdice/dice_table/d66_left_range_table.rb', line 33

def initialize(name, sort_type, items)
  expanded_items = {}
  items.each do |item|
    range, right_items = item

    range.each do |left_value|
      right_items.each_with_index do |right_item, right_value|
        key = left_value * 10 + (right_value + 1)
        expanded_items[key] = right_item
      end
    end
  end

  super(name, sort_type, expanded_items)
end

Class Method Details

.conv_string_range(x) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
# File 'lib/bcdice/dice_table/d66_left_range_table.rb', line 18

def conv_string_range(x)
  if x.include?("..")
    return Range.new(*x.split("..", 2).map { |n| Integer(n) })
  end

  raise(
    ArgumentError,
    "invalid format"
  )
end

.from_i18n(key, locale) ⇒ Object



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

def from_i18n(key, locale)
  table = I18n.t(key, locale: locale)
  converted_items = table[:items].map do |item|
    [conv_string_range(item[0]), item[1]]
  end
  new(table[:name], table[:d66_sort_type], converted_items)
end