From 12372522e9c984c1b81b70e54157d679758d41d9 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 14 Sep 2022 08:35:21 -0500 Subject: [PATCH] Add rock, paper, scissors, lizard, spock --- bot/bot.go | 5 ++++ bot/rps.go | 2 ++ bot/rpsls.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 bot/rpsls.go diff --git a/bot/bot.go b/bot/bot.go index 073424a..0ef1540 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -60,6 +60,11 @@ func (b *Bot) RegisterCommands() { Func: b.RpsCommand(), NArgs: 1, }) + AddCommand(&Command{ + Name: "rpsls", + Func: b.RpslsCommand(), + NArgs: 1, + }) AddCommand(&Command{ Name: "time", Func: b.TimeCommand(), diff --git a/bot/rps.go b/bot/rps.go index ffce5af..2524ce7 100644 --- a/bot/rps.go +++ b/bot/rps.go @@ -12,6 +12,8 @@ const ( Rock = iota Paper Scissors + Lizard + Spock ) var ( diff --git a/bot/rpsls.go b/bot/rpsls.go new file mode 100644 index 0000000..5c3bc09 --- /dev/null +++ b/bot/rpsls.go @@ -0,0 +1,82 @@ +package bot + +import ( + "fmt" + "strings" + + "git.kill0.net/chill9/beepboop/lib" + "github.com/bwmarrin/discordgo" +) + +var ( + rpslsVictoryMap map[int][]int = map[int][]int{ + Rock: {Scissors, Lizard}, + Paper: {Rock, Spock}, + Scissors: {Paper, Lizard}, + Lizard: {Paper, Spock}, + Spock: {Scissors, Rock}, + } + + rpslsChoiceMap map[string]int = map[string]int{ + "rock": Rock, + "paper": Paper, + "scissors": Scissors, + "lizard": Lizard, + "spock": Spock, + } + + rpslsEmojiMap map[int]string = map[int]string{ + Rock: "🪨️", + Paper: "📝", + Scissors: "✂️", + Lizard: "🦎", + Spock: "🖖", + } +) + +func (b *Bot) RpslsCommand() CommandFunc { + return func(args []string, m *discordgo.MessageCreate) error { + var ( + bc, pc int + be, pe string + c string + ) + + if len(args) != 1 { + b.Session.ChannelMessageSend( + m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock )`", + ) + return nil + } + + c = strings.ToLower(args[0]) + + pc, ok := rpslsChoiceMap[c] // player's choice + if !ok { + b.Session.ChannelMessageSend( + m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`", + ) + } + + bc = lib.MapRand(rpslsChoiceMap) // bot's choice + pe = rpslsEmojiMap[pc] // player's emoji + be = rpslsEmojiMap[bc] // bot's emoji + + if bc == pc { + b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf( + "%s v %s: draw", be, pe, + )) + return nil + } else if lib.Contains(rpslsVictoryMap[bc], pc) { + b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf( + "%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, bc), + )) + return nil + } + + b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf( + "%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, pc), + )) + return nil + } +}