Refactor RPS/RPSLS
This commit is contained in:
24
bot/rps.go
24
bot/rps.go
@ -1,20 +1,14 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"git.kill0.net/chill9/beepboop/lib/rps"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) RpsCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
bc, pc, be, pe string
|
||||
)
|
||||
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||
@ -22,23 +16,21 @@ func (b *Bot) RpsCommand() CommandFunc {
|
||||
return nil
|
||||
}
|
||||
|
||||
pc = strings.ToLower(args[0])
|
||||
pc := strings.ToLower(args[0]) // player's choice
|
||||
|
||||
_, ok := rps.EmojiMapRps[pc] // player's choice
|
||||
if !ok {
|
||||
g := rps.NewGame(rps.RulesRps, rps.EmojiMapRps)
|
||||
|
||||
bc := g.Rand() // bot's choice
|
||||
|
||||
s, err := g.Play(bc, pc)
|
||||
if _, ok := err.(rps.InvalidChoiceError); ok {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
bc = lib.MapRandKey(rps.EmojiMapRps) // bot's choice
|
||||
pe = rps.EmojiMapRps[pc] // player's emoji
|
||||
be = rps.EmojiMapRps[bc] // bot's emoji
|
||||
b.Session.ChannelMessageSend(m.ChannelID, s)
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"%s v %s: %s", pe, be, rps.Play(rps.RulesRps, bc, pc),
|
||||
))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
28
bot/rpsls.go
28
bot/rpsls.go
@ -1,44 +1,36 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"git.kill0.net/chill9/beepboop/lib/rps"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) RpslsCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
bc, pc, be, pe string
|
||||
)
|
||||
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors | lizard | spock)`",
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
pc = strings.ToLower(args[0])
|
||||
pc := strings.ToLower(args[0]) // player's choice
|
||||
|
||||
_, ok := rps.EmojiMapRpsls[pc] // player's choice
|
||||
if !ok {
|
||||
g := rps.NewGame(rps.RulesRpsls, rps.EmojiMapRpsls)
|
||||
|
||||
bc := g.Rand() // bot's choice
|
||||
|
||||
s, err := g.Play(bc, pc)
|
||||
if _, ok := err.(rps.InvalidChoiceError); ok {
|
||||
b.Session.ChannelMessageSend(
|
||||
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
|
||||
m.ChannelID, "help: `!rps (rock | paper | scissors | lizard | spock)`",
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
bc = lib.MapRandKey(rps.EmojiMapRpsls) // bot's choice
|
||||
pe = rps.EmojiMapRpsls[pc] // player's emoji
|
||||
be = rps.EmojiMapRpsls[bc] // bot's emoji
|
||||
b.Session.ChannelMessageSend(m.ChannelID, s)
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"%s v %s: %s", pe, be, rps.Play(rps.RulesRpsls, bc, pc),
|
||||
))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user