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

Extend coin flip syntax to support multiple flips #217

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions dice.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,58 @@ func validateDice(input string, dice []string) (Roll, error) {
return wellFormedDice, nil
}

func flipCoin() string {
coin := []string{"Heads", "Tails"}
side := coin[rand.Intn(len(coin))]
func flipCoin(input string) string {
coinCount, err := parseCoinCount(input)
if err != nil {
return err.Error()
}

flips := make([]int, coinCount)
for i := 0; i < coinCount; i++ {
flips[i] = rand.Intn(2)
}
return formatFlips(flips, coinCount)
}

func parseCoinCount(input string) (int, error) {
coinMaximum := 50

coins := coinRegex.FindStringSubmatch(input)
if coins[1] == "" {
// no number specified - implicit one flip
return 1, nil
}

numCoins, err := strconv.Atoi(coins[1])
if err != nil {
return 0, errors.New("malformed coin toss")
}

return fmt.Sprintf("%s.", side)
if numCoins > coinMaximum {
return 0, errors.New("malformed coin toss (max count is 50)")
}
if numCoins < 1 {
return 0, errors.New("malformed coin toss (min count is 1)")
}
return numCoins, nil
}

func formatFlips(flips []int, count int) string {
prefix := ""
coinNames := []string{"Heads", "Tails"}
joiner := ", "
if count > 5 {
prefix = fmt.Sprintf("%d coins: ", count)
coinNames = []string{"H", "T"}
joiner = ""
}

formatted := make([]string, len(flips))
for i := 0; i < len(formatted); i++ {
formatted[i] = coinNames[flips[i]]
}

return fmt.Sprintf("%s%s.", prefix, strings.Join(formatted, joiner))
}


2 changes: 1 addition & 1 deletion dice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestFlipCoin(t *testing.T) {
for i := 1; i < 10; i++ {
result := flipCoin()
result := flipCoin("coin")
Copy link
Owner

Choose a reason for hiding this comment

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

Can we write some tests for the new functionality?
Many coins, too many coins, negative number of coins etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some tests.

validOutput := regexp.MustCompile(`(?:Heads|Tails).`)
if !(validOutput.MatchString(result)) {
t.Errorf(`FAIL: Expected heads or tails in output, but result was \"%s\"`, result)
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func printHelp() string {
ret = append(ret, "!uncard/vanguard/plane/scheme <cardname> to bring up normally filtered out cards")
ret = append(ret, "!url <mtr/ipg/cr/jar> to bring up the links to policy documents")
ret = append(ret, "!roll <X> to roll X-sided die; !roll <XdY> to roll X Y-sided dice")
ret = append(ret, "!coin to flip a coin (heads/tails)")
ret = append(ret, "!coin to flip a coin (heads/tails); !coin <X> to flip X coins")
ret = append(ret, "https://github.com/Fryyyyy/Fryatog/issues for bugs & feature requests")
return strings.Join(ret, " · ")
}
Expand Down Expand Up @@ -434,9 +434,9 @@ func handleCommand(params *fryatogParams, c chan string) {
c <- rollDice(message)
return

case message == "coin":
case coinRegex.MatchString(message):
log.Debug("Coin flip")
c <- flipCoin()
c <- flipCoin(message)
return

case ruleRegexp.MatchString(message),
Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
wordStartingWithBang = regexp.MustCompile(`\s+! *\S+`)

diceRegex = regexp.MustCompile(`^(?:roll )?\s*(.*?)d(\d+)([+-]\d+)?`)
coinRegex = regexp.MustCompile(`^coin(?:\s+(\d+))?`)

cardMetadataRegex = regexp.MustCompile(`(?i)^(?:rulings?|reminder|flavou?r) `)

Expand Down
Loading