Skip to content

Commit

Permalink
fix roll
Browse files Browse the repository at this point in the history
  • Loading branch information
slugalisk committed Nov 20, 2023
1 parent 7ff4265 commit c74a1ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 7 additions & 6 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) {
func computeRoll(input string) (int, error) {

// Define a regular expression to extract dice rolling information
regexPattern := `^!rolls?\s+(\d+)d(\d+)\s*([+\-]\s*\d+)?(.*?)$`
regexPattern := `^!rolls?\s+(\d+)(?:d(\d+))?\s*([+\-]\s*\d+)?`
regex := regexp.MustCompile(regexPattern)

// Match the regular expression against the input string
Expand All @@ -542,14 +542,15 @@ func computeRoll(input string) (int, error) {
// Extract matched values
numDice, _ := strconv.Atoi(matches[1])
numSides, _ := strconv.Atoi(matches[2])
modifierStr := matches[3]
checkMod := modifierStr != ""

var modifier int
if checkMod {
modifier, _ = strconv.Atoi(modifierStr)
if matches[2] == "" {
numSides = numDice
numDice = 1
}

modifier, _ := strconv.Atoi(matches[3])
checkMod := modifier != 0

if numSides <= 0 || numDice <= 0 || modifier > math.MaxInt64 {
return 0, fmt.Errorf("Sides, count or modifier too large")
}
Expand Down
6 changes: 4 additions & 2 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestParseModifiers(t *testing.T) {
}

func TestComputeRoll(t *testing.T) {
testInputs := [11]string{
testInputs := []string{
"!roll 2d2+100 foo biz baz",
"!roll 2d2 + 100",
"!roll 2d2 +100",
Expand All @@ -34,7 +34,9 @@ func TestComputeRoll(t *testing.T) {
"!roll 2d2- 100",
"!roll 2d2- 100 foo biz baz",
"!roll 23904823904823904823490d20 +1",
"!roll 2d20"}
"!roll 2d20",
"!roll 20",
"!roll 20+10"}

for _, input := range testInputs {
result, err := computeRoll(input)
Expand Down

0 comments on commit c74a1ad

Please sign in to comment.