mirror of https://github.com/velour/catbase.git
impossible: refactor
This commit is contained in:
parent
ca97a07a4d
commit
353f289cae
|
@ -19,8 +19,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Impossible struct {
|
type Impossible struct {
|
||||||
b bot.Bot
|
b bot.Bot
|
||||||
c *config.Config
|
c *config.Config
|
||||||
|
handlers bot.HandlerTable
|
||||||
|
|
||||||
title string
|
title string
|
||||||
content []string
|
content []string
|
||||||
|
@ -39,7 +40,7 @@ func New(b bot.Bot) *Impossible {
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Register(i, bot.Help, i.help)
|
b.Register(i, bot.Help, i.help)
|
||||||
b.Register(i, bot.Message, i.message)
|
i.register()
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
@ -55,7 +56,7 @@ func newTesting(b bot.Bot) *Impossible {
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Register(i, bot.Help, i.help)
|
b.Register(i, bot.Help, i.help)
|
||||||
b.Register(i, bot.Message, i.message)
|
i.register()
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
@ -65,39 +66,61 @@ func (p *Impossible) help(c bot.Connector, kind bot.Kind, message msg.Message, a
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Impossible) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
func (p *Impossible) tryRefresh(r bot.Request) (sent bool) {
|
||||||
messaged := false
|
|
||||||
if p.updated.Before(time.Now()) {
|
if p.updated.Before(time.Now()) {
|
||||||
if p.title != "" {
|
if p.title != "" {
|
||||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("The last impossible wikipedia article was: \"%s\"", p.title))
|
||||||
messaged = true
|
sent = true
|
||||||
}
|
}
|
||||||
for !p.refreshImpossible() {
|
for !p.refreshImpossible() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.testing {
|
if p.testing {
|
||||||
p.b.Send(c, bot.Message, message.Channel, p.title)
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.title)
|
||||||
messaged = true
|
sent = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return sent
|
||||||
|
}
|
||||||
|
|
||||||
lowercase := strings.ToLower(message.Body)
|
func (p *Impossible) register() {
|
||||||
if lowercase == "hint" || lowercase == "clue" {
|
p.handlers = bot.HandlerTable{
|
||||||
messaged = true
|
{Kind: bot.Message, IsCmd: false,
|
||||||
p.b.Send(c, bot.Message, message.Channel, p.content[rand.Intn(len(p.content))])
|
Regex: regexp.MustCompile(`(?i)^hint|clue$`),
|
||||||
} else if strings.Contains(lowercase, strings.ToLower(p.title)) {
|
Handler: func(r bot.Request) bool {
|
||||||
messaged = true
|
if p.tryRefresh(r) {
|
||||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
return true
|
||||||
for !p.refreshImpossible() {
|
}
|
||||||
}
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, p.content[rand.Intn(len(p.content))])
|
||||||
} else if strings.Contains(lowercase, "i friggin give up") {
|
return true
|
||||||
messaged = true
|
}},
|
||||||
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("You're a failure the last impossible wikipedia article: \"%s\"", p.title))
|
{Kind: bot.Message, IsCmd: false,
|
||||||
for !p.refreshImpossible() {
|
Regex: regexp.MustCompile(`(?i)^i friggin give up.?$`),
|
||||||
}
|
Handler: func(r bot.Request) bool {
|
||||||
|
if p.tryRefresh(r) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
||||||
|
for !p.refreshImpossible() {
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}},
|
||||||
|
{Kind: bot.Message, IsCmd: false,
|
||||||
|
Regex: regexp.MustCompile(`.*`),
|
||||||
|
Handler: func(r bot.Request) bool {
|
||||||
|
if p.tryRefresh(r) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(strings.ToLower(r.Msg.Body), strings.ToLower(p.title)) {
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", p.title))
|
||||||
|
for !p.refreshImpossible() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}},
|
||||||
}
|
}
|
||||||
|
p.b.RegisterTable(p, p.handlers)
|
||||||
return messaged
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Impossible) refreshImpossible() bool {
|
func (p *Impossible) refreshImpossible() bool {
|
||||||
|
|
|
@ -2,10 +2,12 @@ package impossible
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/velour/catbase/plugins/cli"
|
"regexp"
|
||||||
"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"
|
||||||
|
@ -13,16 +15,22 @@ import (
|
||||||
"github.com/velour/catbase/plugins/counter"
|
"github.com/velour/catbase/plugins/counter"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
func makeMessage(payload string, r *regexp.Regexp) 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(r, 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,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,24 +42,32 @@ func makePlugin(t *testing.T) (*Impossible, *bot.MockBot) {
|
||||||
return p, mb
|
return p, mb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testMessage(p *Impossible, body string) {
|
||||||
|
for _, h := range p.handlers {
|
||||||
|
if h.Regex.MatchString(body) && h.Handler(makeMessage(body, h.Regex)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNothing(t *testing.T) {
|
func TestNothing(t *testing.T) {
|
||||||
p, mb := makePlugin(t)
|
p, mb := makePlugin(t)
|
||||||
p.message(makeMessage("hi"))
|
testMessage(p, "hi")
|
||||||
p.message(makeMessage("nothing"))
|
testMessage(p, "nothing")
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHint(t *testing.T) {
|
func TestHint(t *testing.T) {
|
||||||
p, mb := makePlugin(t)
|
p, mb := makePlugin(t)
|
||||||
p.message(makeMessage("hi"))
|
testMessage(p, "hi")
|
||||||
p.message(makeMessage("!hint"))
|
testMessage(p, "hint")
|
||||||
assert.Len(t, mb.Messages, 2)
|
assert.Len(t, mb.Messages, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCorrect(t *testing.T) {
|
func TestCorrect(t *testing.T) {
|
||||||
p, mb := makePlugin(t)
|
p, mb := makePlugin(t)
|
||||||
p.message(makeMessage("hi"))
|
testMessage(p, "hi")
|
||||||
p.message(makeMessage(mb.Messages[0]))
|
testMessage(p, mb.Messages[0])
|
||||||
|
|
||||||
congrats := fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", mb.Messages[0])
|
congrats := fmt.Sprintf("You guessed the last impossible wikipedia article: \"%s\"", mb.Messages[0])
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue