Fix #19: Merge beers and counters

This commit is contained in:
Chris Sexton 2016-03-19 14:27:02 -04:00
parent ae5d7dec2e
commit 6938efc430
2 changed files with 39 additions and 79 deletions

View File

@ -3,7 +3,6 @@
package plugins package plugins
import ( import (
"database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -17,6 +16,7 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/plugins/counter"
) )
// This is a skeleton plugin to serve as an example and quick copy/paste for new plugins. // This is a skeleton plugin to serve as an example and quick copy/paste for new plugins.
@ -26,14 +26,6 @@ type BeersPlugin struct {
db *sqlx.DB db *sqlx.DB
} }
type userBeers struct {
id int64
nick string
count int
lastDrunk time.Time
saved bool
}
type untappdUser struct { type untappdUser struct {
id int64 id int64
untappdUser string untappdUser string
@ -75,58 +67,6 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
return &p return &p
} }
func (u *userBeers) Save(db *sqlx.DB) error {
if !u.saved {
res, err := db.Exec(`insert into beers (
nick,
count,
lastDrunk
) values (?, ?, ?)`, u.nick, u.count, u.lastDrunk)
if err != nil {
return err
}
id, err := res.LastInsertId()
if err != nil {
return err
}
u.id = id
} else {
_, err := db.Exec(`update beers set
count = ?,
lastDrunk = ?
where id = ?;`, u.count, time.Now(), u.id)
if err != nil {
log.Println("Error updating beers: ", err)
return err
}
}
return nil
}
func getUserBeers(db *sqlx.DB, nick string) *userBeers {
ub := userBeers{saved: true}
err := db.QueryRow(`select id, nick, count, lastDrunk from beers
where nick = ?`, nick).Scan(
&ub.id,
&ub.nick,
&ub.count,
&ub.lastDrunk,
)
if err == sql.ErrNoRows {
log.Println("DIDN'T FIND THAT USER: ", err)
return &userBeers{
nick: nick,
count: 0,
}
}
if err != nil && err != sql.ErrNoRows {
log.Println("Error finding beers: ", err)
return nil
}
return &ub
}
// Message responds to the bot hook on recieving messages. // Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message. // 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. // Otherwise, the function returns false and the bot continues execution of other plugins.
@ -160,7 +100,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
return true return true
} }
if parts[1] == "+=" { if parts[1] == "+=" {
p.setBeers(nick, p.getBeers(nick)+count) p.addBeers(nick, count)
p.randomReply(channel) p.randomReply(channel)
} else if parts[1] == "=" { } else if parts[1] == "=" {
if count == 0 { if count == 0 {
@ -186,16 +126,15 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
// no matter what, if we're in here, then we've responded // no matter what, if we're in here, then we've responded
return true return true
} else if parts[0] == "beers--" { } else if parts[0] == "beers--" {
p.setBeers(nick, p.getBeers(nick)-1) p.addBeers(nick, -1)
p.Bot.SendAction(channel, "flushes") p.Bot.SendAction(channel, "flushes")
return true return true
} else if parts[0] == "beers++" { } else if parts[0] == "beers++" {
p.addBeers(nick) p.addBeers(nick, 1)
p.randomReply(channel) p.randomReply(channel)
return true return true
} else if parts[0] == "bourbon++" { } else if parts[0] == "bourbon++" {
p.addBeers(nick) p.addBeers(nick, 2)
p.addBeers(nick)
p.randomReply(channel) p.randomReply(channel)
return true return true
} else if parts[0] == "puke" { } else if parts[0] == "puke" {
@ -204,7 +143,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
} }
if message.Command && parts[0] == "imbibe" { if message.Command && parts[0] == "imbibe" {
p.addBeers(nick) p.addBeers(nick, 1)
p.randomReply(channel) p.randomReply(channel)
return true return true
} }
@ -292,22 +231,30 @@ func (p *BeersPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, msg) p.Bot.SendMessage(channel, msg)
} }
func getUserBeers(db *sqlx.DB, user string) counter.Item {
booze, _ := counter.GetItem(db, user, "booze")
return booze
}
func (p *BeersPlugin) setBeers(user string, amount int) { func (p *BeersPlugin) setBeers(user string, amount int) {
ub := getUserBeers(p.db, user) ub := getUserBeers(p.db, user)
ub.count = amount err := ub.Update(amount)
ub.lastDrunk = time.Now() if err != nil {
if err := ub.Save(p.db); err != nil {
log.Println("Error saving beers: ", err) log.Println("Error saving beers: ", err)
} }
} }
func (p *BeersPlugin) addBeers(user string) { func (p *BeersPlugin) addBeers(user string, delta int) {
p.setBeers(user, p.getBeers(user)+1) ub := getUserBeers(p.db, user)
err := ub.UpdateDelta(delta)
if err != nil {
log.Println("Error saving beers: ", err)
}
} }
func (p *BeersPlugin) getBeers(nick string) int { func (p *BeersPlugin) getBeers(nick string) int {
ub := getUserBeers(p.db, nick) ub := getUserBeers(p.db, nick)
return ub.count return ub.Count
} }
func (p *BeersPlugin) reportCount(nick, channel string, himself bool) { func (p *BeersPlugin) reportCount(nick, channel string, himself bool) {
@ -452,7 +399,7 @@ func (p *BeersPlugin) checkUntappd(channel string) {
} }
log.Printf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s", log.Printf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
user.chanNick, user.untappdUser, checkin.User.User_name) user.chanNick, user.untappdUser, checkin.User.User_name)
p.addBeers(user.chanNick) p.addBeers(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

@ -28,6 +28,7 @@ type Item struct {
Count int Count int
} }
// GetItems returns all counters for a subject
func GetItems(db *sqlx.DB, nick string) ([]Item, error) { func GetItems(db *sqlx.DB, nick string) ([]Item, error) {
var items []Item var items []Item
err := db.Select(&items, `select * from counter where nick = ?`, nick) err := db.Select(&items, `select * from counter where nick = ?`, nick)
@ -41,6 +42,7 @@ func GetItems(db *sqlx.DB, nick string) ([]Item, error) {
return items, nil return items, nil
} }
// GetItem returns a specific counter for a subject
func GetItem(db *sqlx.DB, nick, itemName string) (Item, error) { func GetItem(db *sqlx.DB, nick, itemName string) (Item, error) {
var item Item var item Item
item.DB = db item.DB = db
@ -60,6 +62,7 @@ func GetItem(db *sqlx.DB, nick, itemName string) (Item, error) {
return item, nil return item, nil
} }
// Create saves a counter
func (i *Item) Create() error { func (i *Item) Create() error {
res, err := i.Exec(`insert into counter (nick, item, count) values (?, ?, ?);`, res, err := i.Exec(`insert into counter (nick, item, count) values (?, ?, ?);`,
i.Nick, i.Item, i.Count) i.Nick, i.Item, i.Count)
@ -69,19 +72,29 @@ func (i *Item) Create() error {
return err return err
} }
func (i *Item) Update(delta int) error { // UpdateDelta sets a value
i.Count += delta // This will create or delete the item if necessary
func (i *Item) Update(value int) error {
i.Count = value
if i.Count == 0 && i.ID != -1 { if i.Count == 0 && i.ID != -1 {
return i.Delete() return i.Delete()
} }
if i.ID == -1 { if i.ID == -1 {
i.Create() i.Create()
} }
log.Printf("Updating item: %#v, delta: %d", i, delta) log.Printf("Updating item: %#v, value: %d", i, value)
_, 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)
return err return err
} }
// UpdateDelta changes a value according to some delta
// This will create or delete the item if necessary
func (i *Item) UpdateDelta(delta int) error {
i.Count += delta
return i.Update(i.Count)
}
// Delete removes a counter from the database
func (i *Item) Delete() error { func (i *Item) Delete() error {
_, err := i.Exec(`delete from counter where id = ?`, i.ID) _, err := i.Exec(`delete from counter where id = ?`, i.ID)
i.ID = -1 i.ID = -1
@ -235,7 +248,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool {
return false return false
} }
log.Printf("About to update item: %#v", item) log.Printf("About to update item: %#v", item)
item.Update(1) item.UpdateDelta(1)
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject,
item.Count, item.Item)) item.Count, item.Item))
return true return true
@ -247,7 +260,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool {
// Item ain't there, I guess // Item ain't there, I guess
return false return false
} }
item.Update(-1) item.UpdateDelta(-1)
p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject,
item.Count, item.Item)) item.Count, item.Item))
return true return true