dice: refactor

This commit is contained in:
Chris Sexton 2021-02-01 15:57:34 -05:00 committed by Chris Sexton
parent 2b47654302
commit 55a206760f
2 changed files with 31 additions and 62 deletions

View File

@ -2,14 +2,14 @@
package dice
import (
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
import (
"fmt"
"math/rand"
"regexp"
"strconv"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
)
// This is a dice plugin to serve as an example and quick copy/paste for new plugins.
@ -18,12 +18,12 @@ type DicePlugin struct {
Bot bot.Bot
}
// NewDicePlugin creates a new DicePlugin with the Plugin interface
// New creates a new DicePlugin with the Plugin interface
func New(b bot.Bot) *DicePlugin {
dp := &DicePlugin{
Bot: b,
}
b.Register(dp, bot.Message, dp.message)
b.RegisterRegexCmd(dp, bot.Message, rollRegex, dp.rollCmd)
b.Register(dp, bot.Help, dp.help)
return dp
}
@ -32,28 +32,18 @@ func rollDie(sides int) int {
return rand.Intn(sides) + 1
}
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *DicePlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
if !message.Command {
return false
}
var rollRegex = regexp.MustCompile(`^(?P<number>\d+)d(?P<sides>\d+)$`)
channel := message.Channel
nDice := 0
sides := 0
if n, err := fmt.Sscanf(message.Body, "%dd%d", &nDice, &sides); n != 2 || err != nil {
return false
}
func (p *DicePlugin) rollCmd(r bot.Request) bool {
nDice, _ := strconv.Atoi(r.Values["number"])
sides, _ := strconv.Atoi(r.Values["sides"])
if sides < 2 || nDice < 1 || nDice > 20 {
p.Bot.Send(c, bot.Message, channel, "You're a dick.")
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You're a dick.")
return true
}
rolls := fmt.Sprintf("%s, you rolled: ", message.User.Name)
rolls := fmt.Sprintf("%s, you rolled: ", r.Msg.User.Name)
for i := 0; i < nDice; i++ {
rolls = fmt.Sprintf("%s %d", rolls, rollDie(sides))
@ -64,9 +54,8 @@ func (p *DicePlugin) message(c bot.Connector, kind bot.Kind, message msg.Message
}
}
p.Bot.Send(c, bot.Message, channel, rolls)
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, rolls)
return true
}
// Help responds to help requests. Every plugin must implement a help function.

View File

@ -3,26 +3,33 @@
package dice
import (
"github.com/velour/catbase/plugins/cli"
"strings"
"testing"
"github.com/velour/catbase/plugins/cli"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
)
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
func makeMessage(payload string) bot.Request {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
return &cli.CliPlugin{}, bot.Message, msg.Message{
values := bot.ParseValues(rollRegex, payload)
return bot.Request{
Conn: &cli.CliPlugin{},
Kind: bot.Message,
Values: values,
Msg: msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
},
}
}
@ -30,7 +37,7 @@ func TestDie(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("!1d6"))
res := c.rollCmd(makeMessage("1d6"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "tester, you rolled:")
@ -40,44 +47,17 @@ func TestDice(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("!5d6"))
res := c.rollCmd(makeMessage("5d6"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "tester, you rolled:")
}
func TestNotCommand(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("1d6"))
assert.False(t, res)
assert.Len(t, mb.Messages, 0)
}
func TestBadDice(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("!aued6"))
assert.False(t, res)
assert.Len(t, mb.Messages, 0)
}
func TestBadSides(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("!1daoeu"))
assert.False(t, res)
assert.Len(t, mb.Messages, 0)
}
func TestLotsOfDice(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.message(makeMessage("!100d100"))
res := c.rollCmd(makeMessage("100d100"))
assert.True(t, res)
assert.Len(t, mb.Messages, 1)
assert.Contains(t, mb.Messages[0], "You're a dick.")