lib/dicechucker.rb in dicechucker-0.6.0 vs lib/dicechucker.rb in dicechucker-0.8.0

- old
+ new

@@ -1,4 +1,45 @@ module Dicechucker - autoload :Dice, File.expand_path('../dicechucker/dice.rb', __FILE__) - autoload :GameLogic, File.expand_path('../dicechucker/game_logic.rb', __FILE__) + 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