Merge pull request #100 from velour/counter_alias

Counter alias
This commit is contained in:
Chris Sexton 2018-01-19 14:22:08 -05:00 committed by GitHub
commit 02f3587f52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 19 deletions

View File

@ -22,6 +22,8 @@ import (
// 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.
const itemName = ":beer:"
type BeersPlugin struct { type BeersPlugin struct {
Bot bot.Bot Bot bot.Bot
db *sqlx.DB db *sqlx.DB
@ -117,18 +119,6 @@ func (p *BeersPlugin) Message(message msg.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--" {
p.addBeers(nick, -1)
p.Bot.SendAction(channel, "flushes")
return true
} else if parts[0] == "beers++" {
p.addBeers(nick, 1)
p.randomReply(channel)
return true
} else if parts[0] == "bourbon++" {
p.addBeers(nick, 2)
p.randomReply(channel)
return true
} else if parts[0] == "puke" { } else if parts[0] == "puke" {
p.puke(nick, channel) p.puke(nick, channel)
return true return true
@ -224,7 +214,7 @@ func (p *BeersPlugin) Help(channel string, parts []string) {
} }
func getUserBeers(db *sqlx.DB, user string) counter.Item { func getUserBeers(db *sqlx.DB, user string) counter.Item {
booze, _ := counter.GetItem(db, user, "booze") booze, _ := counter.GetItem(db, user, itemName)
return booze return booze
} }

View File

@ -31,6 +31,14 @@ type Item struct {
Count int Count int
} }
type alias struct {
*sqlx.DB
ID int64
Item string
PointsTo string `db:"points_to"`
}
// GetItems returns all counters for a subject // 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
@ -59,6 +67,7 @@ func LeaderAll(db *sqlx.DB) ([]Item, error) {
} }
func Leader(db *sqlx.DB, itemName string) ([]Item, error) { func Leader(db *sqlx.DB, itemName string) ([]Item, error) {
itemName = strings.ToLower(itemName)
s := `select * from counter where item=? order by count desc` s := `select * from counter where item=? order by count desc`
var items []Item var items []Item
err := db.Select(&items, s, itemName) err := db.Select(&items, s, itemName)
@ -71,11 +80,38 @@ func Leader(db *sqlx.DB, itemName string) ([]Item, error) {
return items, nil return items, nil
} }
func MkAlias(db *sqlx.DB, item, pointsTo string) (*alias, error) {
item = strings.ToLower(item)
pointsTo = strings.ToLower(pointsTo)
res, err := db.Exec(`insert into counter_alias (item, points_to) values (?, ?)`,
item, pointsTo)
if err != nil {
_, err := db.Exec(`update counter_alias set points_to=? where item=?`, pointsTo, item)
if err != nil {
return nil, err
}
var a alias
if err := db.Get(&a, `select * from counter_alias where item=?`, item); err != nil {
return nil, err
}
return &a, nil
}
id, _ := res.LastInsertId()
return &alias{db, id, item, pointsTo}, nil
}
// GetItem returns a specific counter for a subject // 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
log.Printf("GetItem db: %#v", db) var a alias
if err := db.Get(&a, `select * from counter_alias where item=?`, itemName); err == nil {
itemName = a.PointsTo
} else {
log.Println(err, a)
}
err := db.Get(&item, `select * from counter where nick = ? and item= ?`, err := db.Get(&item, `select * from counter where nick = ? and item= ?`,
nick, itemName) nick, itemName)
switch err { switch err {
@ -132,7 +168,6 @@ func (i *Item) Delete() error {
// NewCounterPlugin creates a new CounterPlugin with the Plugin interface // NewCounterPlugin creates a new CounterPlugin with the Plugin interface
func New(bot bot.Bot) *CounterPlugin { func New(bot bot.Bot) *CounterPlugin {
if bot.DBVersion() == 1 {
if _, err := bot.DB().Exec(`create table if not exists counter ( if _, err := bot.DB().Exec(`create table if not exists counter (
id integer primary key, id integer primary key,
nick string, nick string,
@ -141,6 +176,12 @@ func New(bot bot.Bot) *CounterPlugin {
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if _, err := bot.DB().Exec(`create table if not exists counter_alias (
id integer PRIMARY KEY AUTOINCREMENT,
item string NOT NULL UNIQUE,
points_to string NOT NULL
);`); err != nil {
log.Fatal(err)
} }
return &CounterPlugin{ return &CounterPlugin{
Bot: bot, Bot: bot,
@ -162,7 +203,15 @@ func (p *CounterPlugin) Message(message msg.Message) bool {
return false return false
} }
if parts[0] == "leaderboard" { if len(parts) == 3 && strings.ToLower(parts[0]) == "mkalias" {
if _, err := MkAlias(p.DB, parts[1], parts[2]); err != nil {
log.Println(err)
return false
}
p.Bot.SendMessage(channel, fmt.Sprintf("Created alias %s -> %s",
parts[1], parts[2]))
return true
} else if strings.ToLower(parts[0]) == "leaderboard" {
var cmd func() ([]Item, error) var cmd func() ([]Item, error)
itNameTxt := "" itNameTxt := ""