From f6b7bcf4ffb8e1e01e255de0d985865367043c91 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 13 Jul 2020 11:34:53 -0400 Subject: [PATCH] goals: add remaining/ahead text --- plugins/goals/goals.go | 34 ++++++++++++++++- plugins/goals/goals_test.go | 75 +++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 plugins/goals/goals_test.go diff --git a/plugins/goals/goals.go b/plugins/goals/goals.go index beafb3c..1331b30 100644 --- a/plugins/goals/goals.go +++ b/plugins/goals/goals.go @@ -6,6 +6,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/jmoiron/sqlx" "github.com/rs/zerolog/log" @@ -182,14 +183,15 @@ func (p *GoalsPlugin) checkGoal(c bot.Connector, ch, what, who string) { } perc := float64(item.Count) / float64(g.Amount) * 100.0 + remaining := p.remainingText(item, g) if perc >= 100 { p.deregister(c, ch, g.Kind, g.What, g.Who) m := fmt.Sprintf("You made it! You have %.2f%% of %s and now it's done.", perc, what) p.b.Send(c, bot.Message, ch, m) } else { - m := fmt.Sprintf("You have %d out of %d for %s. You're %.2f%% of the way there!", - item.Count, g.Amount, what, perc) + m := fmt.Sprintf("You have %d out of %d for %s. You're %.2f%% of the way there! %s", + item.Count, g.Amount, what, perc, remaining) p.b.Send(c, bot.Message, ch, m) } @@ -307,3 +309,31 @@ func (p *GoalsPlugin) update(u counter.Update) { } } } + +var now = time.Now + +func (p *GoalsPlugin) calculateRemaining(i counter.Item, g *goal) int { + today := float64(now().YearDay()) + thisYear := time.Date(now().Year(), 0, 0, 0, 0, 0, 0, time.UTC) + nextYear := time.Date(now().Year()+1, 0, 0, 0, 0, 0, 0, time.UTC) + days := nextYear.Sub(thisYear).Hours() / 24.0 // hopefully either a leap year on not + + perc := today / days + shouldHave := float64(g.Amount) * perc + diff := int(shouldHave) - i.Count + + log.Printf("Today is the %f-th day with %f days in the year", today, days) + + return diff +} + +func (p *GoalsPlugin) remainingText(i counter.Item, g *goal) string { + remaining := p.calculateRemaining(i, g) + txt := "" + if remaining < 0 { + txt = fmt.Sprintf("You're ahead by %d!", -1*remaining) + } else if remaining > 0 { + txt = fmt.Sprintf("You need %d to get back on track.", remaining) + } + return txt +} diff --git a/plugins/goals/goals_test.go b/plugins/goals/goals_test.go new file mode 100644 index 0000000..9998557 --- /dev/null +++ b/plugins/goals/goals_test.go @@ -0,0 +1,75 @@ +package goals + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/plugins/counter" +) + +func TestRemainingSomeToGo(t *testing.T) { + mb := bot.NewMockBot() + + i := counter.Item{ + DB: mb.DB(), + ID: 0, + Nick: "", + Item: "", + Count: 190, + } + + g := goal{ + ID: 0, + Kind: "", + Who: "", + What: "", + Amount: 366, + gp: nil, + } + + now = func() time.Time { + return time.Date(2020, 07, 13, 11, 23, 00, 00, time.UTC) + } + + p := New(mb) + + expected := 5 + actual := p.calculateRemaining(i, &g) + + assert.Equal(t, expected, actual) +} + +func TestRemainingAheadOfCurve(t *testing.T) { + mb := bot.NewMockBot() + + i := counter.Item{ + DB: mb.DB(), + ID: 0, + Nick: "", + Item: "", + Count: 200, + } + + g := goal{ + ID: 0, + Kind: "", + Who: "", + What: "", + Amount: 366, + gp: nil, + } + + now = func() time.Time { + return time.Date(2020, 07, 13, 11, 23, 00, 00, time.UTC) + } + + p := New(mb) + + expected := -5 + actual := p.calculateRemaining(i, &g) + + assert.Equal(t, expected, actual) +}