From 1eac691beb254b3baa13d9357491ce7d3e6e3970 Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 6 Jul 2017 21:07:21 -0500 Subject: [PATCH] Bugfixes and New Feature 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'` --- dice.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/dice.lua b/dice.lua index 1055667..70f7c4c 100644 --- a/dice.lua +++ b/dice.lua @@ -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 '') @@ -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 @@ -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)