Skip to content

Commit

Permalink
Bugfixes and New Feature
Browse files Browse the repository at this point in the history
Fixed bugs with invalid dice notation input.  Using `dice:new('3d4+')`
or `dice:new('3d4^1')` before this bugfix should have caused an error,
but did not due to a loophole in the pattern matching that was used.
This has been changed and it will now cause an error when invalid
notation is used.

Fixed bug involving `dice.__concat` metamethod.  When using the concat
operator, if a dice instance `dice.sets < 1` then it would accidentally
overwrite the notation and exclude the sets.  This resulted in the dice
instance being defaulted to `dice.sets = 1`.

Added new method `dice:getNotation()` this returns the raw notation of
all the dice variables. Using this method if your dice instance has any
of the following faces, num_dice, or sets as a negative it will reveal
it in the string.  `Ex. '(-5d-7)x-2'`  The same dice instance used with
the `dice:__tostring()` metamethod will default those negative values to
1.  `Ex. using the same dice instance '1d1'`
  • Loading branch information
timothymtorres committed Jul 7, 2017
1 parent d08c4a0 commit 1eac691
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions dice.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ function dice:new(dice_notation, minimum)
-- If dice_notation is a number, we must convert it into the proper dice string format
if type(dice_notation) == 'number' then dice_notation = '1d'..dice_notation end

local dice_pattern = '[(]?[-]?%d+[d][-]?%d+[+-]?[+-]?%d*[%^]?[+-]?[+-]?%d*[)]?[x]?[-]?%d*'
assert(dice_notation == match(dice_notation, dice_pattern), "Dice string incorrectly formatted.")
local dice_pattern = '[(]?[-]?%d+[d][-]?%d+[+-]?[+-]?%d*[%^]?[+-]?[+-]?%d*[)]?[x]?[-]?%d*'
assert(dice_notation == match(dice_notation, dice_pattern), "Dice string incorrectly formatted.")

local bonus_notation = match(dice_notation, '[d][-]?%d+([+-]?[+-]%d+)')
if bonus_notation then assert(bonus_notation == match(bonus_notation, '[+-]+%d+'), "Dice string bonus portion incorrectly formatted.") end

local reroll_notation = match(dice_notation, '[%^][+-]?[+-]?%d*')
if reroll_notation then assert(reroll_notation == match(reroll_notation, '[%^][+-]+%d+'), "Dice string reroll portion incorrectly formatted.") end

local merged_notation = dice_notation .. (minimum and '['..minimum..']' or '')

Expand Down Expand Up @@ -81,6 +87,10 @@ function dice.setClassMin(min) dice.class_minimum = min end
--- Resets the cache for dice instances
function dice.resetCache() dice._cache = {} end

--- The raw notation including all negatives
-- @treturn string
function dice:getNotation() return self.notation end

--- Number of total dice
-- @treturn int
function dice:getNum() return self.num end
Expand Down Expand Up @@ -224,8 +234,8 @@ function dice:__concat(pluralism_notation)

local new_dice_notation

if sets > 1 then new_dice_notation = '('..num_dice..'d'..dice_faces..bonus..rerolls..')x'..sets
else new_dice_notation = num_dice..'d'..dice_faces..bonus..rerolls
if sets == 1 then new_dice_notation = num_dice..'d'..dice_faces..bonus..rerolls
else new_dice_notation = '('..num_dice..'d'..dice_faces..bonus..rerolls..')x'..sets
end

return dice:new(new_dice_notation, minimum)
Expand Down

0 comments on commit 1eac691

Please sign in to comment.