2017-09-29 04:58:21 +00:00
|
|
|
// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
// Package inventory contains the plugin that allows the bot to pad messages
|
|
|
|
// See the bucket RFC inventory: http://wiki.xkcd.com/irc/Bucket_inventory
|
|
|
|
package inventory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2017-09-29 04:58:21 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InventoryPlugin struct {
|
|
|
|
*sqlx.DB
|
|
|
|
bot bot.Bot
|
|
|
|
config *config.Config
|
|
|
|
r1, r2, r3, r4, r5 *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new InventoryPlugin with the Plugin interface
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *InventoryPlugin {
|
|
|
|
config := b.Config()
|
2019-01-22 00:16:57 +00:00
|
|
|
nick := config.Get("nick", "bot")
|
2017-09-29 04:58:21 +00:00
|
|
|
r1, err := regexp.Compile("take this (.+)")
|
|
|
|
checkerr(err)
|
|
|
|
r2, err := regexp.Compile("have a (.+)")
|
|
|
|
checkerr(err)
|
2019-01-20 20:21:26 +00:00
|
|
|
r3, err := regexp.Compile(fmt.Sprintf("puts (.+) in %s([^a-zA-Z].*)?", nick))
|
2017-09-29 04:58:21 +00:00
|
|
|
checkerr(err)
|
2019-01-20 20:21:26 +00:00
|
|
|
r4, err := regexp.Compile(fmt.Sprintf("gives %s (.+)", nick))
|
2017-09-29 04:58:21 +00:00
|
|
|
checkerr(err)
|
2019-01-20 20:21:26 +00:00
|
|
|
r5, err := regexp.Compile(fmt.Sprintf("gives (.+) to %s([^a-zA-Z].*)?", nick))
|
2017-09-29 04:58:21 +00:00
|
|
|
checkerr(err)
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
p := &InventoryPlugin{
|
|
|
|
DB: b.DB(),
|
|
|
|
bot: b,
|
2017-09-29 04:58:21 +00:00
|
|
|
config: config,
|
|
|
|
r1: r1, r2: r2, r3: r3, r4: r4, r5: r5,
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
b.RegisterFilter("$item", p.itemFilter)
|
|
|
|
b.RegisterFilter("$giveitem", p.giveItemFilter)
|
2017-09-29 04:58:21 +00:00
|
|
|
|
|
|
|
_, err = p.DB.Exec(`create table if not exists inventory (
|
|
|
|
item string primary key
|
|
|
|
);`)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err)
|
2017-09-29 04:58:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(p, bot.Message, p.message)
|
|
|
|
|
|
|
|
return p
|
2017-09-29 04:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) giveItemFilter(input string) string {
|
|
|
|
for strings.Contains(input, "$giveitem") {
|
|
|
|
item := p.random()
|
|
|
|
input = strings.Replace(input, "$giveitem", item, 1)
|
|
|
|
p.remove(item)
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) itemFilter(input string) string {
|
|
|
|
for strings.Contains(input, "$item") {
|
|
|
|
input = strings.Replace(input, "$item", p.random(), 1)
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func (p *InventoryPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2017-09-29 04:58:21 +00:00
|
|
|
m := message.Body
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("inventory trying to read %+v", message)
|
2017-09-29 04:58:21 +00:00
|
|
|
if message.Command {
|
|
|
|
if strings.ToLower(m) == "inventory" {
|
|
|
|
items := p.getAll()
|
|
|
|
say := "I'm not holding anything"
|
|
|
|
if len(items) > 0 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("I think I have more than 0 items: %+v, len(items)=%d", items, len(items))
|
2017-09-29 04:58:21 +00:00
|
|
|
say = fmt.Sprintf("I'm currently holding %s", strings.Join(items, ", "))
|
|
|
|
}
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Message, message.Channel, say)
|
2017-09-29 04:58:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// <Randall> Bucket[:,] take this (.+)
|
|
|
|
// <Randall> Bucket[:,] have a (.+)
|
|
|
|
if matches := p.r1.FindStringSubmatch(m); len(matches) > 0 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("Found item to add: %s", matches[1])
|
2017-09-29 04:58:21 +00:00
|
|
|
return p.addItem(message, matches[1])
|
|
|
|
}
|
|
|
|
if matches := p.r2.FindStringSubmatch(m); len(matches) > 0 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("Found item to add: %s", matches[1])
|
2017-09-29 04:58:21 +00:00
|
|
|
return p.addItem(message, matches[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if message.Action {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msg("Inventory found an action")
|
2017-09-29 04:58:21 +00:00
|
|
|
// * Randall puts (.+) in Bucket([^a-zA-Z].*)?
|
|
|
|
// * Randall gives Bucket (.+)
|
|
|
|
// * Randall gives (.+) to Bucket([^a-zA-Z].*)?
|
|
|
|
|
|
|
|
if matches := p.r3.FindStringSubmatch(m); len(matches) > 0 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("Found item to add: %s", matches[1])
|
2017-09-29 04:58:21 +00:00
|
|
|
return p.addItem(message, matches[1])
|
|
|
|
}
|
|
|
|
if matches := p.r4.FindStringSubmatch(m); len(matches) > 0 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("Found item to add: %s", matches[1])
|
2017-09-29 04:58:21 +00:00
|
|
|
return p.addItem(message, matches[1])
|
|
|
|
}
|
|
|
|
if matches := p.r5.FindStringSubmatch(m); len(matches) > 0 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Debug().Msgf("Found item to add: %s", matches[1])
|
2017-09-29 04:58:21 +00:00
|
|
|
return p.addItem(message, matches[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) removeRandom() string {
|
|
|
|
var name string
|
|
|
|
err := p.QueryRow(`select item from inventory order by random() limit 1`).Scan(
|
|
|
|
&name,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msgf("Error finding random entry")
|
2017-09-29 04:58:21 +00:00
|
|
|
return "IAMERROR"
|
|
|
|
}
|
|
|
|
_, err = p.Exec(`delete from inventory where item=?`, name)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msgf("Error finding random entry")
|
2017-09-29 04:58:21 +00:00
|
|
|
return "IAMERROR"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) count() int {
|
|
|
|
var output int
|
|
|
|
err := p.QueryRow(`select count(*) as count from inventory`).Scan(&output)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error checking for item")
|
2017-09-29 04:58:21 +00:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) random() string {
|
|
|
|
var name string
|
|
|
|
err := p.QueryRow(`select item from inventory order by random() limit 1`).Scan(
|
|
|
|
&name,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error finding random entry")
|
2017-09-29 04:58:21 +00:00
|
|
|
return "IAMERROR"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) getAll() []string {
|
|
|
|
rows, err := p.Queryx(`select item from inventory`)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error getting all items")
|
2017-09-29 04:58:21 +00:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
output := []string{}
|
|
|
|
for rows.Next() {
|
|
|
|
var item string
|
|
|
|
rows.Scan(&item)
|
|
|
|
output = append(output, item)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) exists(i string) bool {
|
|
|
|
var output int
|
|
|
|
err := p.QueryRow(`select count(*) as count from inventory where item=?`, i).Scan(&output)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error checking for item")
|
2017-09-29 04:58:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return output > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) remove(i string) {
|
|
|
|
_, err := p.Exec(`delete from inventory where item=?`, i)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Msg("Error inserting new inventory item")
|
2017-09-29 04:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *InventoryPlugin) addItem(m msg.Message, i string) bool {
|
|
|
|
if p.exists(i) {
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Message, m.Channel, fmt.Sprintf("I already have %s.", i))
|
2017-09-29 04:58:21 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
var removed string
|
2019-01-22 00:16:57 +00:00
|
|
|
max := p.config.GetInt("inventory.max", 10)
|
2019-01-21 19:24:03 +00:00
|
|
|
if p.count() > max {
|
2017-09-29 04:58:21 +00:00
|
|
|
removed = p.removeRandom()
|
|
|
|
}
|
|
|
|
_, err := p.Exec(`INSERT INTO inventory (item) values (?)`, i)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error inserting new inventory item")
|
2017-09-29 04:58:21 +00:00
|
|
|
}
|
|
|
|
if removed != "" {
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Action, m.Channel, fmt.Sprintf("dropped %s and took %s from %s", removed, i, m.User.Name))
|
2017-09-29 04:58:21 +00:00
|
|
|
} else {
|
2019-02-05 15:54:13 +00:00
|
|
|
p.bot.Send(bot.Action, m.Channel, fmt.Sprintf("takes %s from %s", i, m.User.Name))
|
2017-09-29 04:58:21 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkerr(e error) {
|
|
|
|
if e != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(e)
|
2017-09-29 04:58:21 +00:00
|
|
|
}
|
|
|
|
}
|