Sha256: c6ae52620ba39b5590ac5722aef3dada16460a13803b1be4ff57773c3a816d30

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module Dicechucker
  autoload :Diesheet,  File.expand_path('../dicechucker/diesheet.rb', __FILE__)
  autoload :Dice,         File.expand_path('../dicechucker/dice.rb', __FILE__)
  autoload :DieSingle,    File.expand_path('../dicechucker/dice.rb', __FILE__)
  autoload :DiceDropHigh, File.expand_path('../dicechucker/dice.rb', __FILE__)
  autoload :DiceDropLow,  File.expand_path('../dicechucker/dice.rb', __FILE__)
  autoload :DiceExplode,  File.expand_path('../dicechucker/dice.rb', __FILE__)


  class NotationError < ArgumentError
  end
  
  PATTERN = /^(?:(?<dice>\d+)d)?(?<size>\d+)(?<logic>[eEhHlL])?(?<mod>[+\-]\d+)?$/
  
  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
  
  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
  

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dicechucker-0.8.0 lib/dicechucker.rb