bb/bot/rps.go

83 lines
1.6 KiB
Go
Raw Normal View History

2022-09-09 15:23:03 +00:00
package bot
import (
"fmt"
"strings"
2022-09-14 13:34:51 +00:00
"git.kill0.net/chill9/beepboop/lib"
2022-09-09 15:23:03 +00:00
"github.com/bwmarrin/discordgo"
)
const (
Rock = iota
Paper
Scissors
)
var (
2022-09-14 13:34:51 +00:00
rpsVictoryMap map[int][]int = map[int][]int{
Rock: {Scissors},
Paper: {Rock},
Scissors: {Paper},
2022-09-09 15:23:03 +00:00
}
rpsChoiceMap map[string]int = map[string]int{
"rock": Rock,
"paper": Paper,
"scissors": Scissors,
}
rpsEmojiMap map[int]string = map[int]string{
Rock: "🪨️",
Paper: "📝",
Scissors: "✂️",
}
)
func (b *Bot) RpsCommand() CommandFunc {
return func(args []string, m *discordgo.MessageCreate) error {
var (
2022-09-14 13:34:51 +00:00
bc, pc int
be, pe string
c string
2022-09-09 15:23:03 +00:00
)
if len(args) != 1 {
2022-09-14 13:34:51 +00:00
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
2022-09-09 15:23:03 +00:00
return nil
}
c = strings.ToLower(args[0])
2022-09-14 13:34:51 +00:00
pc, ok := rpsChoiceMap[c] // player's choice
2022-09-09 15:23:03 +00:00
if !ok {
2022-09-14 13:34:51 +00:00
b.Session.ChannelMessageSend(
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
)
2022-09-09 15:23:03 +00:00
}
2022-09-14 13:34:51 +00:00
bc = lib.MapRand(rpsChoiceMap) // bot's choice
pe = rpsEmojiMap[pc] // player's emoji
be = rpsEmojiMap[bc] // bot's emoji
2022-09-09 15:23:03 +00:00
2022-09-14 13:34:51 +00:00
if bc == pc {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: draw", be, pe,
))
2022-09-09 15:23:03 +00:00
return nil
2022-09-14 13:34:51 +00:00
} else if lib.Contains(rpsVictoryMap[bc], pc) {
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, bc),
))
2022-09-09 15:23:03 +00:00
return nil
}
2022-09-14 13:34:51 +00:00
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
))
2022-09-09 15:23:03 +00:00
return nil
}
}