goals: refactor

This commit is contained in:
Chris Sexton 2021-02-07 13:29:02 -05:00 committed by Chris Sexton
parent c802b52a3b
commit ca97a07a4d
2 changed files with 60 additions and 50 deletions

View File

@ -65,6 +65,7 @@ type HandlerSpec struct {
Kind Kind Kind Kind
IsCmd bool IsCmd bool
Regex *regexp.Regexp Regex *regexp.Regexp
HelpText string
Handler ResponseHandler Handler ResponseHandler
} }
type HandlerTable []HandlerSpec type HandlerTable []HandlerSpec

View File

@ -5,7 +5,6 @@ import (
"regexp" "regexp"
"sort" "sort"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -21,6 +20,7 @@ type GoalsPlugin struct {
b bot.Bot b bot.Bot
cfg *config.Config cfg *config.Config
db *sqlx.DB db *sqlx.DB
handlers bot.HandlerTable
} }
func New(b bot.Bot) *GoalsPlugin { func New(b bot.Bot) *GoalsPlugin {
@ -30,7 +30,7 @@ func New(b bot.Bot) *GoalsPlugin {
db: b.DB(), db: b.DB(),
} }
p.mkDB() p.mkDB()
b.Register(p, bot.Message, p.message) p.registerCmds()
b.Register(p, bot.Help, p.help) b.Register(p, bot.Help, p.help)
counter.RegisterUpdate(p.update) counter.RegisterUpdate(p.update)
return p return p
@ -51,42 +51,54 @@ func (p *GoalsPlugin) mkDB() {
} }
} }
var registerSelf = regexp.MustCompile(`(?i)^register (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+)\s?(?P<amount>[[:digit:]]+)?`) func (p *GoalsPlugin) registerCmds() {
var registerOther = regexp.MustCompile(`(?i)^register (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>[[:punct:][:alnum:]]+)\s?(?P<amount>[[:digit:]]+)?`) p.handlers = bot.HandlerTable{
var deRegisterSelf = regexp.MustCompile(`(?i)^deregister (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+)`) {Kind: bot.Message, IsCmd: true,
var deRegisterOther = regexp.MustCompile(`(?i)^deregister (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>.*)`) Regex: regexp.MustCompile(`(?i)^register (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+) (?P<amount>[[:digit:]]+)?`),
var checkSelf = regexp.MustCompile(`(?i)^check (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+)`) HelpText: "Register with `%s` for yourself",
var checkOther = regexp.MustCompile(`(?i)^check (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>[[:punct:][:alnum:]]+)`) Handler: func(r bot.Request) bool {
amount, _ := strconv.Atoi(r.Values["amount"])
func (p *GoalsPlugin) message(conn bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { p.register(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Msg.User.Name, amount)
body := strings.TrimSpace(message.Body)
who := message.User.Name
ch := message.Channel
if registerOther.MatchString(body) {
c := parseCmd(registerOther, body)
amount, _ := strconv.Atoi(c["amount"])
p.register(conn, ch, c["type"], c["what"], c["who"], amount)
} else if registerSelf.MatchString(body) {
c := parseCmd(registerSelf, body)
amount, _ := strconv.Atoi(c["amount"])
p.register(conn, ch, c["type"], c["what"], who, amount)
} else if deRegisterOther.MatchString(body) {
c := parseCmd(deRegisterOther, body)
p.deregister(conn, ch, c["type"], c["what"], c["who"])
} else if deRegisterSelf.MatchString(body) {
c := parseCmd(deRegisterSelf, body)
p.deregister(conn, ch, c["type"], c["what"], who)
} else if checkOther.MatchString(body) {
c := parseCmd(checkOther, body)
p.check(conn, ch, c["type"], c["what"], c["who"])
} else if checkSelf.MatchString(body) {
c := parseCmd(checkSelf, body)
p.check(conn, ch, c["type"], c["what"], who)
} else {
return false
}
return true return true
}},
{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^register (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>[[:punct:][:alnum:]]+) (?P<amount>[[:digit:]]+)?`),
HelpText: "Register with `%s` for other people",
Handler: func(r bot.Request) bool {
amount, _ := strconv.Atoi(r.Values["amount"])
p.register(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Values["who"], amount)
return true
}},
{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^deregister (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+)`),
HelpText: "Deregister with `%s` for yourself",
Handler: func(r bot.Request) bool {
p.deregister(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Msg.User.Name)
return true
}},
{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^deregister (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>.*)`),
HelpText: "Deregister with `%s` for other people",
Handler: func(r bot.Request) bool {
p.deregister(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Values["who"])
return true
}},
{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^check (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+)`),
HelpText: "Check with `%s` for yourself",
Handler: func(r bot.Request) bool {
p.check(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Msg.User.Name)
return true
}},
{Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^check (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>[[:punct:][:alnum:]]+)`),
HelpText: "Check with `%s` for other people",
Handler: func(r bot.Request) bool {
p.check(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Values["who"])
return true
}},
}
p.b.RegisterTable(p, p.handlers)
} }
func (p *GoalsPlugin) register(c bot.Connector, ch, kind, what, who string, howMuch int) { func (p *GoalsPlugin) register(c bot.Connector, ch, kind, what, who string, howMuch int) {
@ -201,12 +213,9 @@ func (p *GoalsPlugin) checkGoal(c bot.Connector, ch, what, who string) {
func (p *GoalsPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *GoalsPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
ch := message.Channel ch := message.Channel
msg := "Goals can set goals and competition for your counters." msg := "Goals can set goals and competition for your counters."
msg += fmt.Sprintf("\nRegister with `%s` for yourself", registerSelf) for _, cmd := range p.handlers {
msg += fmt.Sprintf("\nRegister with `%s` for other people", registerOther) msg += fmt.Sprintf("\n"+cmd.HelpText, cmd.Regex)
msg += fmt.Sprintf("\nDeregister with `%s` for yourself", deRegisterSelf) }
msg += fmt.Sprintf("\nDeregister with `%s` for other people", deRegisterOther)
msg += fmt.Sprintf("\nCheck with `%s` for yourself", checkSelf)
msg += fmt.Sprintf("\nCheck with `%s` for other people", checkOther)
p.b.Send(c, bot.Message, ch, msg) p.b.Send(c, bot.Message, ch, msg)
return true return true
} }