mirror of https://github.com/velour/catbase.git
goals: add remaining/ahead text
This commit is contained in:
parent
7b3179fe1d
commit
f6b7bcf4ff
|
@ -6,6 +6,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/rs/zerolog/log"
|
"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
|
perc := float64(item.Count) / float64(g.Amount) * 100.0
|
||||||
|
remaining := p.remainingText(item, g)
|
||||||
|
|
||||||
if perc >= 100 {
|
if perc >= 100 {
|
||||||
p.deregister(c, ch, g.Kind, g.What, g.Who)
|
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)
|
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)
|
p.b.Send(c, bot.Message, ch, m)
|
||||||
} else {
|
} else {
|
||||||
m := fmt.Sprintf("You have %d out of %d for %s. You're %.2f%% of the way there!",
|
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)
|
item.Count, g.Amount, what, perc, remaining)
|
||||||
p.b.Send(c, bot.Message, ch, m)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue