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

@ -62,10 +62,11 @@ type ResponseHandler func(Request) bool
type CallbackMap map[string]map[Kind][]HandlerSpec type CallbackMap map[string]map[Kind][]HandlerSpec
type HandlerSpec struct { type HandlerSpec struct {
Kind Kind Kind Kind
IsCmd bool IsCmd bool
Regex *regexp.Regexp Regex *regexp.Regexp
Handler ResponseHandler HelpText string
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"
@ -18,9 +17,10 @@ import (
) )
type GoalsPlugin struct { 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) return true
who := message.User.Name }},
ch := message.Channel {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:]]+)?`),
if registerOther.MatchString(body) { HelpText: "Register with `%s` for other people",
c := parseCmd(registerOther, body) Handler: func(r bot.Request) bool {
amount, _ := strconv.Atoi(c["amount"]) amount, _ := strconv.Atoi(r.Values["amount"])
p.register(conn, ch, c["type"], c["what"], c["who"], amount) p.register(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Values["who"], amount)
} else if registerSelf.MatchString(body) { return true
c := parseCmd(registerSelf, body) }},
amount, _ := strconv.Atoi(c["amount"]) {Kind: bot.Message, IsCmd: true,
p.register(conn, ch, c["type"], c["what"], who, amount) Regex: regexp.MustCompile(`(?i)^deregister (?P<type>competition|goal) (?P<what>[[:punct:][:alnum:]]+)`),
} else if deRegisterOther.MatchString(body) { HelpText: "Deregister with `%s` for yourself",
c := parseCmd(deRegisterOther, body) Handler: func(r bot.Request) bool {
p.deregister(conn, ch, c["type"], c["what"], c["who"]) p.deregister(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Msg.User.Name)
} else if deRegisterSelf.MatchString(body) { return true
c := parseCmd(deRegisterSelf, body) }},
p.deregister(conn, ch, c["type"], c["what"], who) {Kind: bot.Message, IsCmd: true,
} else if checkOther.MatchString(body) { Regex: regexp.MustCompile(`(?i)^deregister (?P<type>competition|goal) for (?P<who>[[:punct:][:alnum:]]+) (?P<what>.*)`),
c := parseCmd(checkOther, body) HelpText: "Deregister with `%s` for other people",
p.check(conn, ch, c["type"], c["what"], c["who"]) Handler: func(r bot.Request) bool {
} else if checkSelf.MatchString(body) { p.deregister(r.Conn, r.Msg.Channel, r.Values["type"], r.Values["what"], r.Values["who"])
c := parseCmd(checkSelf, body) return true
p.check(conn, ch, c["type"], c["what"], who) }},
} else { {Kind: bot.Message, IsCmd: true,
return false 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
}},
} }
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
} }