Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created the troll launcher trigger #30

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
30 changes: 15 additions & 15 deletions decisions.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main
import (
"math/rand"
"strings"
)
func choose(nick, msg string) string {
choices := strings.Split(msg, "or") // declaring the choices array and splitting the msg content into parts
choice := choices[rand.Intn(len(choices)-1)] // using rand to select a random choice
return nick + ": The Powers that Be have chosen: " + choice // returning the choice
}
package main

import (
"math/rand"
"strings"
)

func choose(nick, msg string) string {

choices := strings.Split(msg, "or") // declaring the choices array and splitting the msg content into parts

choice := choices[rand.Intn(len(choices)-1)] // using rand to select a random choice

return nick + ": The Powers that Be have chosen: " + choice // returning the choice
}
2 changes: 2 additions & 0 deletions reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func reactions(message model.Message) (string, bool) {
reactionContent = ipinfo(message.Content)
case "!choose":
reactionContent = choose(message.From, message.Content)
case "!troll":
reactionContent = troll(message.From, message.Content)
}

return reactionContent, reactionContent != ""
Expand Down
55 changes: 55 additions & 0 deletions trollLauncher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"math/rand"
"strings"
)

func troll(nick, msg string) string {
const TROLL_USAGE = "Usage: !troll <nick>"

msg = strings.Trim(msg, "!troll ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not strings.Split(msg, " ") and then check length to get the target for the troll launcher?

if len(msg) > 1 || len(msg) < 1 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is equivalent to len(msg) != 1

return TROLL_USAGE
}

target := string(msg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend against casting this here. If you want to treat a slice of length 1 as a string, just make it a string when you create it initially.

numTrolls, dmg, dmgType := launchTrolls()

switch dmg {
case "":
return "The troll launcher has malfunctioned."
case "miss":
return "Wha?! The trolls missed! That, like, never happens!"
}

return nick + " fires " + numTrolls + " at " + target + ", dealing " + dmg + " points of " + dmgType + " damage!"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many concatenations here. Why not use a format string to make this simpler to read?

}

func launchTrolls() (numTrolls, dmg, dmgType string) {
damage_type := [13]string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a slice, not an array of fixed length.

Suggested change
damage_type := [13]string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison",
damage_type := []string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison",

"psychic", "necrotic", "radiant", "lightning", "thunder", "force"}

trolls := rand.Intn(10)
if trolls == 0 {
return string(trolls), "", ""
}

dmg = trollDamage(trolls)

return string(trolls), dmg, damage_type[rand.Intn(12)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Treating a []string with len 1 like a string. Fix the type at initialization so you can use it properly.

}
Comment on lines +33 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding a zero case to trollDamage? Doing so would allow you to do something like

Suggested change
trolls := rand.Intn(10)
if trolls == 0 {
return string(trolls), "", ""
}
dmg = trollDamage(trolls)
return string(trolls), dmg, damage_type[rand.Intn(12)]
}
return string(trolls), trollDamage(rand.Intn[10]), damage_type[rand.Intn(12)]
}


func trollDamage(trolls int) string {
i := 0
trollDmg := 0
for i < trolls {
trollDmg += rand.Intn(20)
}

if trollDmg == 0 {
return "miss"
}

return string(trollDmg)
}
Comment on lines +43 to +55
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this function a little less surprising. We expect we are dealing with integers, so let's stay dealing with integers and only cast if we need to

Suggested change
func trollDamage(trolls int) string {
i := 0
trollDmg := 0
for i < trolls {
trollDmg += rand.Intn(20)
}
if trollDmg == 0 {
return "miss"
}
return string(trollDmg)
}
func trollDamage(trolls int) int {
dmg := 0
for i, _ range trolls {
dmg += rand.Intn(20)
}
return dmg
}