goals: fix default channel reporting

This commit is contained in:
Chris Sexton 2021-06-17 13:59:29 -04:00 committed by Chris Sexton
parent 9eb801f570
commit 825a8d267f
4 changed files with 37 additions and 33 deletions

View File

@ -103,18 +103,18 @@ func (p *BeersPlugin) register() {
switch op { switch op {
case "=": case "=":
if count == 0 { if count == 0 {
p.puke(r.Conn, nick, id, r.Msg.Channel) p.puke(r)
} else { } else {
p.setBeers(nick, id, count) p.setBeers(&r, nick, id, count)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
} }
return true return true
case "+=": case "+=":
p.addBeers(nick, id, count) p.addBeers(&r, nick, id, count)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
case "-=": case "-=":
p.addBeers(nick, id, -count) p.addBeers(&r, nick, id, -count)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
} }
@ -127,9 +127,9 @@ func (p *BeersPlugin) register() {
nick := r.Msg.User.Name nick := r.Msg.User.Name
id := r.Msg.User.ID id := r.Msg.User.ID
if op == "++" { if op == "++" {
p.addBeers(nick, id, 1) p.addBeers(&r, nick, id, 1)
} else { } else {
p.addBeers(nick, id, -1) p.addBeers(&r, nick, id, -1)
} }
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
@ -152,14 +152,14 @@ func (p *BeersPlugin) register() {
{Kind: bot.Message, IsCmd: true, {Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^puke$`), Regex: regexp.MustCompile(`(?i)^puke$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
p.puke(r.Conn, r.Msg.User.Name, r.Msg.User.ID, r.Msg.Channel) p.puke(r)
return true return true
}}, }},
{Kind: bot.Message, IsCmd: true, {Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^` + Regex: regexp.MustCompile(`(?i)^` +
strings.Join(p.c.GetArray("beers.imbibewords", []string{"imbibe", "quaff"}), "|") + `$`), strings.Join(p.c.GetArray("beers.imbibewords", []string{"imbibe", "quaff"}), "|") + `$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
p.addBeers(r.Msg.User.Name, r.Msg.User.ID, 1) p.addBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 1)
p.randomReply(r.Conn, r.Msg.Channel) p.randomReply(r.Conn, r.Msg.Channel)
return true return true
}}, }},
@ -245,17 +245,17 @@ func getUserBeers(db *sqlx.DB, user, id string) counter.Item {
return booze return booze
} }
func (p *BeersPlugin) setBeers(user, id string, amount int) { func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
ub := getUserBeers(p.db, user, id) ub := getUserBeers(p.db, user, id)
err := ub.Update(amount) err := ub.Update(r, amount)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Error saving beers") log.Error().Err(err).Msgf("Error saving beers")
} }
} }
func (p *BeersPlugin) addBeers(user, id string, delta int) { func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) {
ub := getUserBeers(p.db, user, id) ub := getUserBeers(p.db, user, id)
err := ub.UpdateDelta(delta) err := ub.UpdateDelta(r, delta)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Error saving beers") log.Error().Err(err).Msgf("Error saving beers")
} }
@ -279,10 +279,10 @@ func (p *BeersPlugin) reportCount(c bot.Connector, nick, id, channel string, him
p.b.Send(c, bot.Message, channel, msg) p.b.Send(c, bot.Message, channel, msg)
} }
func (p *BeersPlugin) puke(c bot.Connector, user, id string, channel string) { func (p *BeersPlugin) puke(r bot.Request) {
p.setBeers(user, id, 0) p.setBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 0)
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user) msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", r.Msg.User.Name)
p.b.Send(c, bot.Message, channel, msg) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
} }
func (p *BeersPlugin) doIKnow(nick, id string) bool { func (p *BeersPlugin) doIKnow(nick, id string) bool {
@ -474,7 +474,7 @@ func (p *BeersPlugin) sendCheckin(c bot.Connector, channel string, user untappdU
} }
// Don't add beers till after a photo has been detected (or failed once) // Don't add beers till after a photo has been detected (or failed once)
p.addBeers(user.chanNick, "", 1) p.addBeers(nil, user.chanNick, "", 1)
drunken := p.getBeers(user.chanNick, "") drunken := p.getBeers(user.chanNick, "")
msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d", msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d",

View File

@ -65,7 +65,7 @@ func TestCounter(t *testing.T) {
t.Log(err) t.Log(err)
t.Fatal() t.Fatal()
} }
err = i.Update(5) err = i.Update(nil, 5)
assert.Nil(t, err) assert.Nil(t, err)
} }

View File

@ -220,7 +220,7 @@ func (i *Item) Create() error {
// UpdateDelta sets a value // UpdateDelta sets a value
// This will create or delete the item if necessary // This will create or delete the item if necessary
func (i *Item) Update(value int) error { func (i *Item) Update(r *bot.Request, value int) error {
i.Count = value i.Count = value
if i.Count == 0 && i.ID != -1 { if i.Count == 0 && i.ID != -1 {
return i.Delete() return i.Delete()
@ -234,16 +234,16 @@ func (i *Item) Update(value int) error {
Msg("Updating item") Msg("Updating item")
_, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID) _, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID)
if err == nil { if err == nil {
sendUpdate(i.Nick, i.Item, i.Count) sendUpdate(r, i.Nick, i.Item, i.Count)
} }
return err return err
} }
// UpdateDelta changes a value according to some delta // UpdateDelta changes a value according to some delta
// This will create or delete the item if necessary // This will create or delete the item if necessary
func (i *Item) UpdateDelta(delta int) error { func (i *Item) UpdateDelta(r *bot.Request, delta int) error {
i.Count += delta i.Count += delta
return i.Update(i.Count) return i.Update(r, i.Count)
} }
// Delete removes a counter from the database // Delete removes a counter from the database
@ -587,7 +587,7 @@ func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
return false return false
} }
log.Debug().Msgf("About to update item: %#v", item) log.Debug().Msgf("About to update item: %#v", item)
item.UpdateDelta(1) item.UpdateDelta(&r, 1)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
@ -612,7 +612,7 @@ func (p *CounterPlugin) decrementCmd(r bot.Request) bool {
// Item ain't there, I guess // Item ain't there, I guess
return false return false
} }
item.UpdateDelta(-1) item.UpdateDelta(&r, -1)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
@ -636,7 +636,7 @@ func (p *CounterPlugin) addToCmd(r bot.Request) bool {
} }
n, _ := strconv.Atoi(r.Values["amount"]) n, _ := strconv.Atoi(r.Values["amount"])
log.Debug().Msgf("About to update item by %d: %#v", n, item) log.Debug().Msgf("About to update item by %d: %#v", n, item)
item.UpdateDelta(n) item.UpdateDelta(&r, n)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
@ -660,7 +660,7 @@ func (p *CounterPlugin) removeFromCmd(r bot.Request) bool {
} }
n, _ := strconv.Atoi(r.Values["amount"]) n, _ := strconv.Atoi(r.Values["amount"])
log.Debug().Msgf("About to update item by -%d: %#v", n, item) log.Debug().Msgf("About to update item by -%d: %#v", n, item)
item.UpdateDelta(-n) item.UpdateDelta(&r, -n)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count, item.Item)) item.Count, item.Item))
return true return true
@ -702,7 +702,7 @@ func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool {
if item.Count < 0 { if item.Count < 0 {
delta = -1 delta = -1
} }
item.UpdateDelta(delta) item.UpdateDelta(&r, delta)
p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s... %s has %d %s", p.Bot.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s... %s has %d %s",
strings.Join(everyDayImShuffling([]string{"bleep", "bloop", "blop"}), "-"), nick, item.Count, itemName)) strings.Join(everyDayImShuffling([]string{"bleep", "bloop", "blop"}), "-"), nick, item.Count, itemName))
return true return true
@ -764,9 +764,9 @@ func (p *CounterPlugin) handleCounterAPI(w http.ResponseWriter, r *http.Request)
return return
} }
if info.Action == "++" { if info.Action == "++" {
item.UpdateDelta(1) item.UpdateDelta(nil, 1)
} else if info.Action == "--" { } else if info.Action == "--" {
item.UpdateDelta(-1) item.UpdateDelta(nil, -1)
} else { } else {
w.WriteHeader(400) w.WriteHeader(400)
fmt.Fprint(w, "Invalid increment") fmt.Fprint(w, "Invalid increment")
@ -795,7 +795,7 @@ type Update struct {
Amount int Amount int
} }
type updateFunc func(Update) type updateFunc func(bot.Request, Update)
var updateFuncs = []updateFunc{} var updateFuncs = []updateFunc{}
@ -803,10 +803,14 @@ func RegisterUpdate(f updateFunc) {
log.Debug().Msgf("registering update func") log.Debug().Msgf("registering update func")
updateFuncs = append(updateFuncs, f) updateFuncs = append(updateFuncs, f)
} }
func sendUpdate(who, what string, amount int) {
func sendUpdate(r *bot.Request, who, what string, amount int) {
if r == nil {
return
}
log.Debug().Msgf("sending updates to %d places", len(updateFuncs)) log.Debug().Msgf("sending updates to %d places", len(updateFuncs))
for _, f := range updateFuncs { for _, f := range updateFuncs {
f(Update{who, what, amount}) f(*r, Update{who, what, amount})
} }
} }

View File

@ -306,7 +306,7 @@ func (g goal) Delete() error {
return err return err
} }
func (p *GoalsPlugin) update(u counter.Update) { func (p *GoalsPlugin) update(r bot.Request, u counter.Update) {
log.Debug().Msgf("entered update for %#v", u) log.Debug().Msgf("entered update for %#v", u)
gs, err := p.getGoal(u.Who, u.What) gs, err := p.getGoal(u.Who, u.What)
if err != nil { if err != nil {