beers: change default beer item

This commit is contained in:
Chris Sexton 2021-08-21 08:42:27 -04:00 committed by Chris Sexton
parent 02837bf136
commit ba0f38b342
2 changed files with 19 additions and 16 deletions

View File

@ -33,10 +33,10 @@ 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:"
var cachedImages = map[string][]byte{} var cachedImages = map[string][]byte{}
const DEFAULT_ITEM = "🍺"
type BeersPlugin struct { type BeersPlugin struct {
b bot.Bot b bot.Bot
c *config.Config c *config.Config
@ -240,14 +240,15 @@ func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
return true return true
} }
func getUserBeers(db *sqlx.DB, user, id string) counter.Item { func getUserBeers(db *sqlx.DB, user, id, itemName string) counter.Item {
// TODO: really ought to have an ID here // TODO: really ought to have an ID here
booze, _ := counter.GetUserItem(db, user, id, itemName) booze, _ := counter.GetUserItem(db, user, id, itemName)
return booze return booze
} }
func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) { func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
ub := getUserBeers(p.db, user, id) itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName)
err := ub.Update(r, amount) err := ub.Update(r, amount)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Error saving beers") log.Error().Err(err).Msgf("Error saving beers")
@ -255,15 +256,17 @@ func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
} }
func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) { func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) {
ub := getUserBeers(p.db, user, id) itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName)
err := ub.UpdateDelta(r, delta) err := ub.UpdateDelta(r, delta)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("Error saving beers") log.Error().Err(err).Msgf("Error saving beers")
} }
} }
func (p *BeersPlugin) getBeers(nick, id string) int { func (p *BeersPlugin) getBeers(user, id string) int {
ub := getUserBeers(p.db, nick, id) itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName)
return ub.Count return ub.Count
} }

View File

@ -53,8 +53,8 @@ func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) {
counter.New(mb) counter.New(mb)
mb.DB().MustExec(`delete from counter; delete from counter_alias;`) mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
b := New(mb) b := New(mb)
counter.MkAlias(mb.DB(), "beer", ":beer:") counter.MkAlias(mb.DB(), "beer", DEFAULT_ITEM)
counter.MkAlias(mb.DB(), "beers", ":beer:") counter.MkAlias(mb.DB(), "beers", DEFAULT_ITEM)
return b, mb return b, mb
} }
@ -75,7 +75,7 @@ func TestImbibe(t *testing.T) {
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
testMessage(b, "imbibe") testMessage(b, "imbibe")
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 2, it.Count) assert.Equal(t, 2, it.Count)
} }
@ -83,7 +83,7 @@ func TestEq(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
testMessage(b, "beers = 3") testMessage(b, "beers = 3")
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 3, it.Count) assert.Equal(t, 3, it.Count)
} }
@ -94,7 +94,7 @@ func TestEqZero(t *testing.T) {
testMessage(b, "beers = 0") testMessage(b, "beers = 0")
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
assert.Contains(t, mb.Messages[1], "reversal of fortune") assert.Contains(t, mb.Messages[1], "reversal of fortune")
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, it.Count) assert.Equal(t, 0, it.Count)
} }
@ -105,7 +105,7 @@ func TestBeersPlusEq(t *testing.T) {
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
testMessage(b, "beers += 5") testMessage(b, "beers += 5")
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 10, it.Count) assert.Equal(t, 10, it.Count)
} }
@ -113,11 +113,11 @@ func TestBeersPlusEq(t *testing.T) {
func TestPuke(t *testing.T) { func TestPuke(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
testMessage(b, "beers += 5") testMessage(b, "beers += 5")
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 5, it.Count) assert.Equal(t, 5, it.Count)
testMessage(b, "puke") testMessage(b, "puke")
it, err = counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err = counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 0, it.Count) assert.Equal(t, 0, it.Count)
} }
@ -125,7 +125,7 @@ func TestPuke(t *testing.T) {
func TestBeersReport(t *testing.T) { func TestBeersReport(t *testing.T) {
b, mb := makeBeersPlugin(t) b, mb := makeBeersPlugin(t)
testMessage(b, "beers += 5") testMessage(b, "beers += 5")
it, err := counter.GetUserItem(mb.DB(), "tester", "id", itemName) it, err := counter.GetUserItem(mb.DB(), "tester", "id", DEFAULT_ITEM)
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 5, it.Count) assert.Equal(t, 5, it.Count)
testMessage(b, "beers") testMessage(b, "beers")