-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Conversation
FINALLY THE BUILD PASSED |
func troll(nick, msg string) string { | ||
const TROLL_USAGE = "Usage: !troll <nick>" | ||
|
||
msg = strings.Trim(msg, "!troll ") |
There was a problem hiding this comment.
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?
const TROLL_USAGE = "Usage: !troll <nick>" | ||
|
||
msg = strings.Trim(msg, "!troll ") | ||
if len(msg) > 1 || len(msg) < 1 { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
return "Wha?! The trolls missed! That, like, never happens!" | ||
} | ||
|
||
return nick + " fires " + numTrolls + " at " + target + ", dealing " + dmg + " points of " + dmgType + " damage!" |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
damage_type := [13]string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison", | |
damage_type := []string{"bludgeoning", "piercing", "slashing", "cold", "fire", "acid", "poison", |
trolls := rand.Intn(10) | ||
if trolls == 0 { | ||
return string(trolls), "", "" | ||
} | ||
|
||
dmg = trollDamage(trolls) | ||
|
||
return string(trolls), dmg, damage_type[rand.Intn(12)] | ||
} |
There was a problem hiding this comment.
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
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)] | |
} |
|
||
dmg = trollDamage(trolls) | ||
|
||
return string(trolls), dmg, damage_type[rand.Intn(12)] |
There was a problem hiding this comment.
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.
func trollDamage(trolls int) string { | ||
i := 0 | ||
trollDmg := 0 | ||
for i < trolls { | ||
trollDmg += rand.Intn(20) | ||
} | ||
|
||
if trollDmg == 0 { | ||
return "miss" | ||
} | ||
|
||
return string(trollDmg) | ||
} |
There was a problem hiding this comment.
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
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 | |
} |
No description provided.