IDK, experiment is over anyway

This commit is contained in:
Chris Sexton 2021-12-29 08:25:42 -05:00
parent 4e16c8f464
commit 27c782b7bc
6 changed files with 27 additions and 30 deletions

View File

@ -67,9 +67,7 @@ func ParseValues(r *regexp.Regexp, body string) RegexValues {
func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.Message, args ...interface{}) bool { func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.Message, args ...interface{}) bool {
t := reflect.TypeOf(plugin).String() t := reflect.TypeOf(plugin).String()
for _, spec := range b.callbacks[t][evt] { for _, spec := range b.callbacks[t][evt] {
log.Debug().Msgf("Trying callback %v with regex %v for msg %v", t, spec.Regex.String(), message.Body)
if spec.Regex.MatchString(message.Body) { if spec.Regex.MatchString(message.Body) {
log.Debug().Msgf("Running callback")
req := Request{ req := Request{
Conn: conn, Conn: conn,
Kind: evt, Kind: evt,

View File

@ -217,7 +217,6 @@ func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
} }
func getUserBeers(store *bh.Store, user, id, itemName string) *counter.Item { func getUserBeers(store *bh.Store, user, id, itemName string) *counter.Item {
// TODO: really ought to have an ID here
booze, _ := counter.GetUserItem(store, user, id, itemName) booze, _ := counter.GetUserItem(store, user, id, itemName)
return booze return booze
} }

View File

@ -36,7 +36,7 @@ type Item struct {
UserID string UserID string
} }
type Alias struct { type CounterAlias struct {
store *bh.Store store *bh.Store
created bool created bool
@ -60,18 +60,18 @@ func GetAllItems(store *bh.Store) ([]Item, error) {
} }
// GetItems returns all counters for a subject // GetItems returns all counters for a subject
func GetItems(store *bh.Store, nick, id string) ([]Item, error) { func GetItems(store *bh.Store, nick, id string) ([]*Item, error) {
var items []Item var items []*Item
var err error var err error
q := &bh.Query{}
if id != "" { if id != "" {
q = bh.Where("UserID").Eq(id) if err = store.Find(&items, bh.Where("UserID").Eq(id)); err != nil {
} else {
q = bh.Where("Nick").Eq(nick)
}
if err = store.Find(&items, q); err != nil {
return nil, err return nil, err
} }
} else {
if err = store.Find(&items, bh.Where("Nick").Eq(nick)); err != nil {
return nil, err
}
}
// Don't forget to embed the db into all of that shiz // Don't forget to embed the db into all of that shiz
for i := range items { for i := range items {
items[i].store = store items[i].store = store
@ -110,17 +110,17 @@ func Leader(store *bh.Store, itemName string) ([]Item, error) {
} }
func RmAlias(store *bh.Store, aliasName string) error { func RmAlias(store *bh.Store, aliasName string) error {
a := &Alias{} a := &CounterAlias{}
if err := store.FindOne(a, bh.Where("Item").Eq(aliasName)); err != nil { if err := store.FindOne(a, bh.Where("Item").Eq(aliasName)); err != nil {
return err return err
} }
return store.Delete(a.ID, Alias{}) return store.Delete(a.ID, CounterAlias{})
} }
func MkAlias(store *bh.Store, aliasName, pointsTo string) (*Alias, error) { func MkAlias(store *bh.Store, aliasName, pointsTo string) (*CounterAlias, error) {
aliasName = strings.ToLower(aliasName) aliasName = strings.ToLower(aliasName)
pointsTo = strings.ToLower(pointsTo) pointsTo = strings.ToLower(pointsTo)
alias := &Alias{ alias := &CounterAlias{
store: store, store: store,
Item: aliasName, Item: aliasName,
PointsTo: pointsTo, PointsTo: pointsTo,
@ -134,7 +134,7 @@ func MkAlias(store *bh.Store, aliasName, pointsTo string) (*Alias, error) {
func GetItem(store *bh.Store, itemName string) ([]Item, error) { func GetItem(store *bh.Store, itemName string) ([]Item, error) {
itemName = trimUnicode(itemName) itemName = trimUnicode(itemName)
var items []Item var items []Item
var a Alias var a CounterAlias
if err := store.FindOne(&a, bh.Where("Item").Eq(itemName)); err == nil { if err := store.FindOne(&a, bh.Where("Item").Eq(itemName)); err == nil {
itemName = a.PointsTo itemName = a.PointsTo
} else { } else {
@ -162,7 +162,7 @@ func GetUserItem(store *bh.Store, nick, id, itemName string) (*Item, error) {
item.Nick = nick item.Nick = nick
item.Item = itemName item.Item = itemName
item.UserID = id item.UserID = id
var a Alias var a CounterAlias
err := store.FindOne(&a, bh.Where("Item").Eq(itemName)) err := store.FindOne(&a, bh.Where("Item").Eq(itemName))
if err == nil { if err == nil {
log.Debug().Msgf("itemName now is %s", a.PointsTo) log.Debug().Msgf("itemName now is %s", a.PointsTo)

View File

@ -36,20 +36,20 @@ type Factoid struct {
Count int Count int
} }
type Alias struct { type FactAlias struct {
Fact string Fact string
Next string Next string
} }
func (a Alias) Key() string { func (a FactAlias) Key() string {
return a.Fact + a.Next return a.Fact + a.Next
} }
func (a *Alias) resolve(store *bh.Store) (*Factoid, error) { func (a *FactAlias) resolve(store *bh.Store) (*Factoid, error) {
// perform db query to fill the To field // perform db query to fill the To field.
// todo: remove this query // todo: remove this query
//q := `select fact, next from factoid_alias where fact=?` //q := `select fact, next from factoid_alias where fact=?`
var next Alias var next FactAlias
err := store.FindOne(&next, bh.Where("Fact").Eq(a.Next)) err := store.FindOne(&next, bh.Where("Fact").Eq(a.Next))
if err != nil { if err != nil {
// we hit the end of the chain, get a factoid named Next // we hit the end of the chain, get a factoid named Next
@ -66,7 +66,7 @@ func (a *Alias) resolve(store *bh.Store) (*Factoid, error) {
func findAlias(store *bh.Store, fact string) (bool, *Factoid) { func findAlias(store *bh.Store, fact string) (bool, *Factoid) {
// todo: remove this query // todo: remove this query
//q := `select * from factoid_alias where fact=?` //q := `select * from factoid_alias where fact=?`
var a Alias var a FactAlias
err := store.FindOne(&a, bh.Where("Fact").Eq(fact)) err := store.FindOne(&a, bh.Where("Fact").Eq(fact))
if err != nil { if err != nil {
return false, nil return false, nil
@ -75,9 +75,9 @@ func findAlias(store *bh.Store, fact string) (bool, *Factoid) {
return err == nil, f return err == nil, f
} }
func (a *Alias) save(store *bh.Store) error { func (a *FactAlias) save(store *bh.Store) error {
//q := `select * from factoid_alias where fact=?` //q := `select * from factoid_alias where fact=?`
var offender Alias var offender FactAlias
err := store.FindOne(&offender, bh.Where("Fact").Eq(a.Next)) err := store.FindOne(&offender, bh.Where("Fact").Eq(a.Next))
if err == nil { if err == nil {
return fmt.Errorf("DANGER: an opposite alias already exists") return fmt.Errorf("DANGER: an opposite alias already exists")
@ -95,8 +95,8 @@ func (a *Alias) save(store *bh.Store) error {
return nil return nil
} }
func aliasFromStrings(from, to string) *Alias { func aliasFromStrings(from, to string) *FactAlias {
return &Alias{from, to} return &FactAlias{from, to}
} }
func (f *Factoid) Save(store *bh.Store) error { func (f *Factoid) Save(store *bh.Store) error {

View File

@ -30,7 +30,7 @@ func migrateCounter(db *sqlx.DB, store *bh.Store) error {
} }
} }
aliases := []counter2.Alias{} aliases := []counter2.CounterAlias{}
log.Printf("Migrating %T", aliases) log.Printf("Migrating %T", aliases)
if err := db.Select(&aliases, `select * from counter_alias`); err != nil { if err := db.Select(&aliases, `select * from counter_alias`); err != nil {
return err return err

View File

@ -36,7 +36,7 @@ func migrateFacts(db *sqlx.DB, store *bh.Store) error {
log.Printf("Migrated %d facts", len(allFacts)) log.Printf("Migrated %d facts", len(allFacts))
q = `select * from factoid_alias` q = `select * from factoid_alias`
allAliases := []fact2.Alias{} allAliases := []fact2.FactAlias{}
if err := db.Select(&allAliases, q); err != nil { if err := db.Select(&allAliases, q); err != nil {
return err return err