mirror of https://github.com/velour/catbase.git
dice: refactor
This commit is contained in:
parent
2b47654302
commit
55a206760f
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
package dice
|
package dice
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/velour/catbase/bot"
|
|
||||||
"github.com/velour/catbase/bot/msg"
|
|
||||||
)
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"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.
|
// 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
|
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 {
|
func New(b bot.Bot) *DicePlugin {
|
||||||
dp := &DicePlugin{
|
dp := &DicePlugin{
|
||||||
Bot: b,
|
Bot: b,
|
||||||
}
|
}
|
||||||
b.Register(dp, bot.Message, dp.message)
|
b.RegisterRegexCmd(dp, bot.Message, rollRegex, dp.rollCmd)
|
||||||
b.Register(dp, bot.Help, dp.help)
|
b.Register(dp, bot.Help, dp.help)
|
||||||
return dp
|
return dp
|
||||||
}
|
}
|
||||||
|
@ -32,28 +32,18 @@ func rollDie(sides int) int {
|
||||||
return rand.Intn(sides) + 1
|
return rand.Intn(sides) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message responds to the bot hook on recieving messages.
|
var rollRegex = regexp.MustCompile(`^(?P<number>\d+)d(?P<sides>\d+)$`)
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
channel := message.Channel
|
func (p *DicePlugin) rollCmd(r bot.Request) bool {
|
||||||
nDice := 0
|
nDice, _ := strconv.Atoi(r.Values["number"])
|
||||||
sides := 0
|
sides, _ := strconv.Atoi(r.Values["sides"])
|
||||||
|
|
||||||
if n, err := fmt.Sscanf(message.Body, "%dd%d", &nDice, &sides); n != 2 || err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if sides < 2 || nDice < 1 || nDice > 20 {
|
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
|
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++ {
|
for i := 0; i < nDice; i++ {
|
||||||
rolls = fmt.Sprintf("%s %d", rolls, rollDie(sides))
|
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
|
return true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Help responds to help requests. Every plugin must implement a help function.
|
// Help responds to help requests. Every plugin must implement a help function.
|
||||||
|
|
|
@ -3,26 +3,33 @@
|
||||||
package dice
|
package dice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/velour/catbase/plugins/cli"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/velour/catbase/plugins/cli"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
"github.com/velour/catbase/bot/user"
|
"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, "!")
|
isCmd := strings.HasPrefix(payload, "!")
|
||||||
if isCmd {
|
if isCmd {
|
||||||
payload = payload[1:]
|
payload = payload[1:]
|
||||||
}
|
}
|
||||||
return &cli.CliPlugin{}, bot.Message, msg.Message{
|
values := bot.ParseValues(rollRegex, payload)
|
||||||
User: &user.User{Name: "tester"},
|
return bot.Request{
|
||||||
Channel: "test",
|
Conn: &cli.CliPlugin{},
|
||||||
Body: payload,
|
Kind: bot.Message,
|
||||||
Command: isCmd,
|
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()
|
mb := bot.NewMockBot()
|
||||||
c := New(mb)
|
c := New(mb)
|
||||||
assert.NotNil(t, c)
|
assert.NotNil(t, c)
|
||||||
res := c.message(makeMessage("!1d6"))
|
res := c.rollCmd(makeMessage("1d6"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
assert.Contains(t, mb.Messages[0], "tester, you rolled:")
|
assert.Contains(t, mb.Messages[0], "tester, you rolled:")
|
||||||
|
@ -40,44 +47,17 @@ func TestDice(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
c := New(mb)
|
c := New(mb)
|
||||||
assert.NotNil(t, c)
|
assert.NotNil(t, c)
|
||||||
res := c.message(makeMessage("!5d6"))
|
res := c.rollCmd(makeMessage("5d6"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
assert.Contains(t, mb.Messages[0], "tester, you rolled:")
|
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) {
|
func TestLotsOfDice(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
c := New(mb)
|
c := New(mb)
|
||||||
assert.NotNil(t, c)
|
assert.NotNil(t, c)
|
||||||
res := c.message(makeMessage("!100d100"))
|
res := c.rollCmd(makeMessage("100d100"))
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.Contains(t, mb.Messages[0], "You're a dick.")
|
assert.Contains(t, mb.Messages[0], "You're a dick.")
|
||||||
|
|
Loading…
Reference in New Issue