From f59d5a9d775a02f42368011680ef56c3e768b23d Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Thu, 26 Oct 2023 23:28:33 +0300 Subject: [PATCH 01/15] Update roll command, added joinNumbers making it so you can do `!roll 2d6+10` and get `Bot: user rolled 12 from 2d6+10: (1, 1) + 10` --- commands.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/commands.go b/commands.go index de1bf53..e355f95 100644 --- a/commands.go +++ b/commands.go @@ -526,6 +526,16 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) { } } +func joinNumbers(numbers []string) string { + if len(numbers) > 5 { + numbers = numbers[:5] + numbers = append(numbers, "...") + } + + result := strings.Join(numbers, ", ") + return result +} + // !roll sides [count] - roll dice func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { if !strings.HasPrefix(m.Message, "!roll") { @@ -538,10 +548,16 @@ func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { } args := parts[1:] + var modifier int64 = 0 // parse XdY if strings.Contains(parts[1], "d") { parts := strings.Split(parts[1], "d") + if strings.Contains(parts[1], "+") { + subparts := strings.Split(parts[1], "+") + modifier, _ = strconv.ParseInt(subparts[1], 10, 64) + parts[1] = subparts[0] + } args = []string{parts[1], parts[0]} } @@ -563,9 +579,20 @@ func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { } var sum int64 + var tracker []string + for i := uint64(0); i < count; i++ { - sum += rand.Int63n(int64(sides)) + 1 + var value = rand.Int63n(int64(sides)) + 1 + sum += value + if i <= 5 { + tracker = append(tracker, fmt.Sprintf("%d", value)) + } + } + + sum += modifier + if modifier > 0 { + b.sendMessageDedupe(fmt.Sprintf("%s rolled %d in %s: (%s) + %d", m.Sender.Nick, sum, parts[1], joinNumbers(tracker), modifier), s) + } else { + b.sendMessageDedupe(fmt.Sprintf("%s rolled %d in %s: (%s)", m.Sender.Nick, sum, parts[1], joinNumbers(tracker)), s) } - - b.sendMessageDedupe(fmt.Sprintf("%s rolled %d", m.Sender.Nick, sum), s) } From bc12408f0b92a02b1dd471d078801be0da32dbe0 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Thu, 26 Oct 2023 23:59:20 +0300 Subject: [PATCH 02/15] Update commands.go --- commands.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/commands.go b/commands.go index e355f95..20a3b99 100644 --- a/commands.go +++ b/commands.go @@ -590,9 +590,5 @@ func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { } sum += modifier - if modifier > 0 { - b.sendMessageDedupe(fmt.Sprintf("%s rolled %d in %s: (%s) + %d", m.Sender.Nick, sum, parts[1], joinNumbers(tracker), modifier), s) - } else { - b.sendMessageDedupe(fmt.Sprintf("%s rolled %d in %s: (%s)", m.Sender.Nick, sum, parts[1], joinNumbers(tracker)), s) - } + b.sendMessageDedupe(fmt.Sprintf("%s rolled %d", m.Sender.Nick, sum), s) } From ee514855f27ad8efd50d589a265a8380fddd4ea9 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:01:53 +0200 Subject: [PATCH 03/15] Update commands_test.go --- commands_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/commands_test.go b/commands_test.go index fe75b8d..be90d1a 100644 --- a/commands_test.go +++ b/commands_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "testing" ) @@ -20,3 +21,29 @@ func TestParseModifiers(t *testing.T) { } // fmt.Println(err.Error()) } + +func TestCompute(t *testing.T) { + var testInputs [9]string + + testInputs[0] = "!roll 2d2+100 foo biz baz" + testInputs[1] = "!roll 2d2 + 100" + testInputs[2] = "!roll 2d2 +100" + testInputs[3] = "!roll 2d2+ 100" + testInputs[4] = "!roll 2d2-100" + testInputs[5] = "!roll 2d2 - 100" + testInputs[6] = "!roll 2d2 -100" + testInputs[7] = "!roll 2d2- 100" + testInputs[8] = "!roll 2d2- 100 foo biz baz" + testInputs[8] = "!roll 23904823904823904823490d20 +1" + + for _, input := range testInputs { + result, err := Compute(input) + errorMessage := fmt.Sprintf("%v", err) + if err != nil { + if errorMessage != "Sides or count too large" { + t.Error(fmt.Sprintf("Error: %v\n %d", err, result)) + } + } + } + +} From bf980c0c8ab884cf7cbf1b75142a2ed898b9a4c0 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:02:02 +0200 Subject: [PATCH 04/15] Update commands.go --- commands.go | 82 ++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 48 deletions(-) diff --git a/commands.go b/commands.go index 20a3b99..8f9a06e 100644 --- a/commands.go +++ b/commands.go @@ -526,69 +526,55 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) { } } -func joinNumbers(numbers []string) string { - if len(numbers) > 5 { - numbers = numbers[:5] - numbers = append(numbers, "...") - } +func Compute(input string) (int, error) { - result := strings.Join(numbers, ", ") - return result -} + // Define a regular expression to extract dice rolling information + regexPattern := `^!roll\s+(\d+)d(\d+)\s*([+\-]\s*\d+)?(.*?)$` + regex := regexp.MustCompile(regexPattern) -// !roll sides [count] - roll dice -func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { - if !strings.HasPrefix(m.Message, "!roll") { - return - } + // Match the regular expression against the input string + matches := regex.FindStringSubmatch(input) - parts := strings.Split(m.Message, " ") - if len(parts) < 2 { - return + if matches == nil { + return 0, fmt.Errorf("Invalid input format: %s", input) } - args := parts[1:] - var modifier int64 = 0 + // Extract matched values + numDice, _ := strconv.Atoi(matches[1]) + numSides, _ := strconv.Atoi(matches[2]) + modifierStr := matches[3] - // parse XdY - if strings.Contains(parts[1], "d") { - parts := strings.Split(parts[1], "d") - if strings.Contains(parts[1], "+") { - subparts := strings.Split(parts[1], "+") - modifier, _ = strconv.ParseInt(subparts[1], 10, 64) - parts[1] = subparts[0] - } - args = []string{parts[1], parts[0]} + if math.MaxInt64/numDice <= numSides || numDice > 100 { + return 0, fmt.Errorf("Sides or count too large") } - sides, _ := strconv.ParseUint(args[0], 10, 64) - if sides < 2 { - return + // Roll the dice + result := 0 + for i := 0; i < numDice; i++ { + result += rand.Intn(numSides) + 1 } - count := uint64(1) - if len(args) > 1 { - c, _ := strconv.ParseUint(args[1], 10, 64) - if c != 0 { - count = c - } + // Apply the modifier if present + if modifierStr != "" { + modifier, _ := strconv.Atoi(modifierStr) + result += modifier } - if math.MaxInt64/count <= sides || count > 100 { + return result, nil +} + +// !roll sides [count] - roll dice +func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { + if !strings.HasPrefix(m.Message, "!roll") { return } - var sum int64 - var tracker []string - - for i := uint64(0); i < count; i++ { - var value = rand.Int63n(int64(sides)) + 1 - sum += value - if i <= 5 { - tracker = append(tracker, fmt.Sprintf("%d", value)) - } + parts := strings.Split(m.Message, " ") + if len(parts) < 2 { + return } - - sum += modifier + + var sum, _ = Compute(m.Message) + b.sendMessageDedupe(fmt.Sprintf("%s rolled %d", m.Sender.Nick, sum), s) } From 327dd40f6b187cafac4b1ee4128acbe8d33703f8 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:56:16 +0200 Subject: [PATCH 05/15] Update commands.go --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 8f9a06e..a7c83c3 100644 --- a/commands.go +++ b/commands.go @@ -526,7 +526,7 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) { } } -func Compute(input string) (int, error) { +func compute(input string) (int, error) { // Define a regular expression to extract dice rolling information regexPattern := `^!roll\s+(\d+)d(\d+)\s*([+\-]\s*\d+)?(.*?)$` From bddf04a111d33a0c87f28e4d5202516d2c95eff7 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:00:44 +0200 Subject: [PATCH 06/15] Update commands_test.go --- commands_test.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/commands_test.go b/commands_test.go index be90d1a..6666086 100644 --- a/commands_test.go +++ b/commands_test.go @@ -23,18 +23,7 @@ func TestParseModifiers(t *testing.T) { } func TestCompute(t *testing.T) { - var testInputs [9]string - - testInputs[0] = "!roll 2d2+100 foo biz baz" - testInputs[1] = "!roll 2d2 + 100" - testInputs[2] = "!roll 2d2 +100" - testInputs[3] = "!roll 2d2+ 100" - testInputs[4] = "!roll 2d2-100" - testInputs[5] = "!roll 2d2 - 100" - testInputs[6] = "!roll 2d2 -100" - testInputs[7] = "!roll 2d2- 100" - testInputs[8] = "!roll 2d2- 100 foo biz baz" - testInputs[8] = "!roll 23904823904823904823490d20 +1" + testInputs := [9]string{"!roll 2d2+100 foo biz baz", "!roll 2d2 + 100", "!roll 2d2 +100", "!roll 2d2+ 100", "!roll 2d2-100", "!roll 2d2 - 100", "!roll 2d2 -100", "!roll 2d2- 100", "!roll 2d2- 100 foo biz baz", "!roll 23904823904823904823490d20 +1"} for _, input := range testInputs { result, err := Compute(input) From 6d43ddbe3aa19daa4cec2d9159f9eb445d6517fb Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:02:53 +0200 Subject: [PATCH 07/15] Update commands_test.go --- commands_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands_test.go b/commands_test.go index 6666086..49af5f3 100644 --- a/commands_test.go +++ b/commands_test.go @@ -26,7 +26,7 @@ func TestCompute(t *testing.T) { testInputs := [9]string{"!roll 2d2+100 foo biz baz", "!roll 2d2 + 100", "!roll 2d2 +100", "!roll 2d2+ 100", "!roll 2d2-100", "!roll 2d2 - 100", "!roll 2d2 -100", "!roll 2d2- 100", "!roll 2d2- 100 foo biz baz", "!roll 23904823904823904823490d20 +1"} for _, input := range testInputs { - result, err := Compute(input) + result, err := compute(input) errorMessage := fmt.Sprintf("%v", err) if err != nil { if errorMessage != "Sides or count too large" { From 88a40d03925f32b105b359bd4ac225285feff860 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:03:24 +0200 Subject: [PATCH 08/15] Update commands.go --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index a7c83c3..a2eda68 100644 --- a/commands.go +++ b/commands.go @@ -574,7 +574,7 @@ func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { return } - var sum, _ = Compute(m.Message) + var sum, _ = compute(m.Message) b.sendMessageDedupe(fmt.Sprintf("%s rolled %d", m.Sender.Nick, sum), s) } From 2215959c3ce9e0c387d993b11028cc087ecde94a Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:06:49 +0200 Subject: [PATCH 09/15] Update commands_test.go --- commands_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/commands_test.go b/commands_test.go index 49af5f3..a7515a2 100644 --- a/commands_test.go +++ b/commands_test.go @@ -23,7 +23,17 @@ func TestParseModifiers(t *testing.T) { } func TestCompute(t *testing.T) { - testInputs := [9]string{"!roll 2d2+100 foo biz baz", "!roll 2d2 + 100", "!roll 2d2 +100", "!roll 2d2+ 100", "!roll 2d2-100", "!roll 2d2 - 100", "!roll 2d2 -100", "!roll 2d2- 100", "!roll 2d2- 100 foo biz baz", "!roll 23904823904823904823490d20 +1"} + testInputs := [10]string{ + "!roll 2d2+100 foo biz baz", + "!roll 2d2 + 100", + "!roll 2d2 +100", + "!roll 2d2+ 100", + "!roll 2d2-100", + "!roll 2d2 - 100", + "!roll 2d2 -100", + "!roll 2d2- 100", + "!roll 2d2- 100 foo biz baz", + "!roll 23904823904823904823490d20 +1"} for _, input := range testInputs { result, err := compute(input) From b770927cccfad97d9cefd29f75a3f50705c04002 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:08:31 +0200 Subject: [PATCH 10/15] Update commands_test.go --- commands_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/commands_test.go b/commands_test.go index a7515a2..b9818cf 100644 --- a/commands_test.go +++ b/commands_test.go @@ -23,7 +23,7 @@ func TestParseModifiers(t *testing.T) { } func TestCompute(t *testing.T) { - testInputs := [10]string{ + testInputs := [11]string{ "!roll 2d2+100 foo biz baz", "!roll 2d2 + 100", "!roll 2d2 +100", @@ -33,7 +33,8 @@ func TestCompute(t *testing.T) { "!roll 2d2 -100", "!roll 2d2- 100", "!roll 2d2- 100 foo biz baz", - "!roll 23904823904823904823490d20 +1"} + "!roll 23904823904823904823490d20 +1", + "!roll 2d20"} for _, input := range testInputs { result, err := compute(input) From 984d1190ce0f4139fe9d94acad0a41489504e353 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 7 Nov 2023 20:26:52 +0200 Subject: [PATCH 11/15] review feedback --- commands.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index a2eda68..6190933 100644 --- a/commands.go +++ b/commands.go @@ -526,10 +526,10 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) { } } -func compute(input string) (int, error) { +func computeRoll(input string) (int, error) { // Define a regular expression to extract dice rolling information - regexPattern := `^!roll\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 @@ -543,9 +543,15 @@ func compute(input string) (int, error) { numDice, _ := strconv.Atoi(matches[1]) numSides, _ := strconv.Atoi(matches[2]) modifierStr := matches[3] + checkMod := modifierStr != "" - if math.MaxInt64/numDice <= numSides || numDice > 100 { - return 0, fmt.Errorf("Sides or count too large") + var modifier int + if checkMod { + modifier, _ = strconv.Atoi(modifierStr) + } + + if math.MaxInt64/numDice <= numSides || numDice > 100 || modifier > math.MaxInt64 { + return 0, fmt.Errorf("Sides, count or modifier too large") } // Roll the dice @@ -555,8 +561,7 @@ func compute(input string) (int, error) { } // Apply the modifier if present - if modifierStr != "" { - modifier, _ := strconv.Atoi(modifierStr) + if checkMod { result += modifier } @@ -574,7 +579,11 @@ func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { return } - var sum, _ = compute(m.Message) + var sum, err = computeRoll(m.Message) + + if err != nil { + return + } b.sendMessageDedupe(fmt.Sprintf("%s rolled %d", m.Sender.Nick, sum), s) } From 509571359ccc7ec4cf3e4292041c768e08800fb3 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 7 Nov 2023 20:27:30 +0200 Subject: [PATCH 12/15] Update commands_test.go --- commands_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands_test.go b/commands_test.go index b9818cf..2834e7b 100644 --- a/commands_test.go +++ b/commands_test.go @@ -22,7 +22,7 @@ func TestParseModifiers(t *testing.T) { // fmt.Println(err.Error()) } -func TestCompute(t *testing.T) { +func TestComputeRoll(t *testing.T) { testInputs := [11]string{ "!roll 2d2+100 foo biz baz", "!roll 2d2 + 100", From 5b5761f9164ecf351b64ea86c7ec9c5bf8564c93 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 7 Nov 2023 20:58:49 +0200 Subject: [PATCH 13/15] Update commands.go --- commands.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 6190933..a30e49b 100644 --- a/commands.go +++ b/commands.go @@ -550,7 +550,13 @@ func computeRoll(input string) (int, error) { modifier, _ = strconv.Atoi(modifierStr) } - if math.MaxInt64/numDice <= numSides || numDice > 100 || modifier > math.MaxInt64 { + if numSides <= 0 || numDice <= 0 || modifier > math.MaxInt64 { + return 0, fmt.Errorf("Sides, count or modifier too large") + } + + if math.MaxInt64/numSides < numDice || + (modifier > 0 && math.MaxInt64-numSides*numDice < modifier) || + (modifier < 0 && math.MinInt64-numSides*numDice > modifier) { return 0, fmt.Errorf("Sides, count or modifier too large") } From 0862af5b0057fc522a0237683b12deef9a65e311 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 7 Nov 2023 20:59:07 +0200 Subject: [PATCH 14/15] Update commands_test.go --- commands_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands_test.go b/commands_test.go index 2834e7b..f11d18e 100644 --- a/commands_test.go +++ b/commands_test.go @@ -37,10 +37,10 @@ func TestComputeRoll(t *testing.T) { "!roll 2d20"} for _, input := range testInputs { - result, err := compute(input) + result, err := computeRoll(input) errorMessage := fmt.Sprintf("%v", err) if err != nil { - if errorMessage != "Sides or count too large" { + if errorMessage != "Sides, count or modifier too large" { t.Error(fmt.Sprintf("Error: %v\n %d", err, result)) } } From f3b4ede10edc7ba4a8c270413b68d1abdae04078 Mon Sep 17 00:00:00 2001 From: real-salad <149112256+real-salad@users.noreply.github.com> Date: Tue, 7 Nov 2023 21:01:02 +0200 Subject: [PATCH 15/15] Update commands.go