Module: DndTreasureCalc

Defined in:
lib/dnd_treasure_calc/dice.rb,
lib/dnd_treasure_calc.rb,
lib/dnd_treasure_calc/coins.rb,
lib/dnd_treasure_calc/version.rb

Overview

coins.rb

Defined Under Namespace

Classes: Coins, Dice

Constant Summary collapse

D_100 =
Dice.new(1, 100).freeze
COINS =
%i(copper silver electrum gold platinum).freeze
INDIVIDUAL_TABLES =
YAML.load_file('assets/individual_tables.yml')
HORDE_TABLES =
YAML.load_file('assets/horde_tables.yml')
VERSION =
"0.1"

Class Method Summary collapse

Class Method Details

.get_index(challenge_rating) ⇒ Object



50
51
52
53
54
# File 'lib/dnd_treasure_calc.rb', line 50

def DndTreasureCalc.get_index(challenge_rating)
  idx = challenge_rating / 5
  idx = 3 if idx > 3
  idx
end

.horde(challenge_rating) ⇒ Object

Calculate horde treasure at challenge rating provided

Raises:

  • (RuntimeError)


36
37
38
# File 'lib/dnd_treasure_calc.rb', line 36

def DndTreasureCalc.horde(challenge_rating)
  raise RuntimeError, 'Not yet implemented.'
end

.horde_table_for(challenge_rating) ⇒ Object



45
46
47
48
# File 'lib/dnd_treasure_calc.rb', line 45

def DndTreasureCalc.horde_table_for(challenge_rating)
  idx = get_index(challenge_rating)
  HORDE_TABLES[idx]
end

.individual(challenge_rating) ⇒ Object

Calculate individual treasure at challenge rating provided

Raises:

  • (RuntimeError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dnd_treasure_calc.rb', line 15

def DndTreasureCalc.individual(challenge_rating)
  table = individual_table_for(challenge_rating)
  roll = D_100.roll
  table.each do |threshold, dice|
    if roll <= threshold
      coins = []

      dice.each do |d|
        die = Dice.new(d[:rolls], d[:faces])
        amount = die.roll * d[:multiplier]
        coins << Coins.new(amount, d[:type])
      end

      return coins
    end
  end

  raise RuntimeError, "#{roll} did not meet any of the thresholds!"
end

.individual_table_for(challenge_rating) ⇒ Object



40
41
42
43
# File 'lib/dnd_treasure_calc.rb', line 40

def DndTreasureCalc.individual_table_for(challenge_rating)
  idx = get_index(challenge_rating)
  INDIVIDUAL_TABLES[idx]
end