Module: Dicechucker

Defined in:
lib/dicechucker.rb,
lib/dicechucker/dice.rb,
lib/dicechucker/diesheet.rb

Defined Under Namespace

Classes: Dice, DiceDropHigh, DiceDropLow, DiceDropper, DiceExplode, DieSingle, Diesheet, NotationError

Constant Summary collapse

PATTERN =
/^(?:(?<dice>\d+)d)?(?<size>\d+)(?<logic>[eEhHlL])?(?<mod>[+\-]\d+)?$/

Class Method Summary collapse

Class Method Details

.make_dice(dice, size, logic, mod) ⇒ Object



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

def self.make_dice(dice, size, logic, mod)
  case logic.upcase
  when 'L'
    return DiceDropLow.new(dice, size, mod)
  when 'H'
    return DiceDropHigh.new(dice, size, mod)
  when 'E'
    return DiceExplode.new(dice, size, mod)
  end
  if dice == 1 and mod == 0
    return DieSingle.new(dice, size, mod)
  end
  return Dice.new(dice, size, mod)
end

.parse(raw, reportstyle = :total_only) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dicechucker.rb', line 15

def self.parse(raw, reportstyle = :total_only)
  if (match = raw.match(PATTERN))
    dice = Integer(match[:dice]) rescue 1
    size = Integer(match[:size]) 
    mod = Integer(match[:mod]) rescue 0
    logic = String(match[:logic]) rescue nil
    dieset = make_dice(dice, size, logic, mod)
    dieset.roll
    dieset
  else
    raise NotationError, "Invalid die notation, #{raw}"
  end
end