diff --git a/plugins/beers.go b/plugins/beers.go index 0550e7e..b4e524e 100644 --- a/plugins/beers.go +++ b/plugins/beers.go @@ -3,7 +3,6 @@ package plugins import ( - "database/sql" "encoding/json" "errors" "fmt" @@ -17,6 +16,7 @@ import ( "github.com/jmoiron/sqlx" "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. @@ -26,14 +26,6 @@ type BeersPlugin struct { db *sqlx.DB } -type userBeers struct { - id int64 - nick string - count int - lastDrunk time.Time - saved bool -} - type untappdUser struct { id int64 untappdUser string @@ -75,58 +67,6 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin { 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. // 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. @@ -160,7 +100,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool { return true } if parts[1] == "+=" { - p.setBeers(nick, p.getBeers(nick)+count) + p.addBeers(nick, count) p.randomReply(channel) } else if parts[1] == "=" { 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 return true } else if parts[0] == "beers--" { - p.setBeers(nick, p.getBeers(nick)-1) + p.addBeers(nick, -1) p.Bot.SendAction(channel, "flushes") return true } else if parts[0] == "beers++" { - p.addBeers(nick) + p.addBeers(nick, 1) p.randomReply(channel) return true } else if parts[0] == "bourbon++" { - p.addBeers(nick) - p.addBeers(nick) + p.addBeers(nick, 2) p.randomReply(channel) return true } else if parts[0] == "puke" { @@ -204,7 +143,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool { } if message.Command && parts[0] == "imbibe" { - p.addBeers(nick) + p.addBeers(nick, 1) p.randomReply(channel) return true } @@ -292,22 +231,30 @@ func (p *BeersPlugin) Help(channel string, parts []string) { 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) { ub := getUserBeers(p.db, user) - ub.count = amount - ub.lastDrunk = time.Now() - if err := ub.Save(p.db); err != nil { + err := ub.Update(amount) + if err != nil { log.Println("Error saving beers: ", err) } } -func (p *BeersPlugin) addBeers(user string) { - p.setBeers(user, p.getBeers(user)+1) +func (p *BeersPlugin) addBeers(user string, delta int) { + 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 { ub := getUserBeers(p.db, nick) - return ub.count + return ub.Count } 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", user.chanNick, user.untappdUser, checkin.User.User_name) - p.addBeers(user.chanNick) + p.addBeers(user.chanNick, 1) drunken := p.getBeers(user.chanNick) msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d", diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index f1be7dd..a804ef4 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -28,6 +28,7 @@ type Item struct { Count int } +// GetItems returns all counters for a subject func GetItems(db *sqlx.DB, nick string) ([]Item, error) { var items []Item 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 } +// GetItem returns a specific counter for a subject func GetItem(db *sqlx.DB, nick, itemName string) (Item, error) { var item Item item.DB = db @@ -60,6 +62,7 @@ func GetItem(db *sqlx.DB, nick, itemName string) (Item, error) { return item, nil } +// Create saves a counter func (i *Item) Create() error { res, err := i.Exec(`insert into counter (nick, item, count) values (?, ?, ?);`, i.Nick, i.Item, i.Count) @@ -69,19 +72,29 @@ func (i *Item) Create() error { return err } -func (i *Item) Update(delta int) error { - i.Count += delta +// UpdateDelta sets a value +// 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 { return i.Delete() } if i.ID == -1 { 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) 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 { _, err := i.Exec(`delete from counter where id = ?`, i.ID) i.ID = -1 @@ -235,7 +248,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool { return false } 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, item.Count, item.Item)) return true @@ -247,7 +260,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool { // Item ain't there, I guess return false } - item.Update(-1) + item.UpdateDelta(-1) p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, item.Item)) return true