56 lines
1.1 KiB
Ruby
56 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "dice"
|
|
|
|
class DiceRoller
|
|
attr_reader :dice
|
|
attr_reader :result
|
|
|
|
def initialize(roll_command, stat: nil)
|
|
@roll_command = roll_command
|
|
@stat = stat&.value
|
|
@dice = []
|
|
end
|
|
|
|
def roll
|
|
@roll_command = @roll_command.sub("self", @stat.to_s) if @roll_command.include?("self")
|
|
Dice.roll(@roll_command)
|
|
end
|
|
|
|
def valid?
|
|
return if @roll_command.blank?
|
|
|
|
# No repeated math operators
|
|
return false if @roll_command.match?(/[+\-*\/]{2,}/)
|
|
|
|
# No leading or trailing math operators
|
|
return false if @roll_command.match?(/\A[+\-*\/]/) || @roll_command.match?(/[+\-*\/]\z/)
|
|
|
|
@roll_command.match?(
|
|
/
|
|
\A(
|
|
(\d*d\d*) |
|
|
([+\-*\/]) |
|
|
(\d+) |
|
|
(self)
|
|
)*\z/xi,
|
|
)
|
|
end
|
|
|
|
private
|
|
|
|
def roll_dice(command)
|
|
parts = command.downcase.split("d").compact_blank
|
|
die_type = parts.last
|
|
dice_number = parts.length > 1 ? parts.first.to_i : 1
|
|
|
|
result = 0
|
|
dice_number.times do
|
|
roll = rand(1..die_type.to_i)
|
|
result += roll
|
|
dice << [ die_type, result ]
|
|
end
|
|
result
|
|
end
|
|
end
|