From 319df5b0f3303a805f80a52541b3fa35830f0a46 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Wed, 19 Jun 2024 09:13:12 -0400 Subject: [PATCH] counter: 64bit ints --- plugins/beers/beers.go | 8 ++++---- plugins/counter/api.go | 10 +++++----- plugins/counter/counter.go | 18 +++++++++--------- plugins/goals/goals.go | 6 +++--- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index 3b36d4a..42b7ea9 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -97,7 +97,7 @@ func (p *BeersPlugin) register() { Regex: regexp.MustCompile(`(?i)^beers?\s?(?P(\+=|-=|=))\s?(?P\d+)$`), Handler: func(r bot.Request) bool { op := r.Values["operator"] - count, _ := strconv.Atoi(r.Values["amount"]) + count, _ := strconv.ParseInt(r.Values["amount"], 10, 64) nick := r.Msg.User.Name id := r.Msg.User.ID @@ -246,7 +246,7 @@ func getUserBeers(db *sqlx.DB, user, id, itemName string) counter.Item { 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 int64) { itemName := p.c.Get("beers.itemname", DEFAULT_ITEM) ub := getUserBeers(p.db, user, id, itemName) err := ub.Update(r, amount) @@ -255,7 +255,7 @@ 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 int64) { itemName := p.c.Get("beers.itemname", DEFAULT_ITEM) ub := getUserBeers(p.db, user, id, itemName) err := ub.UpdateDelta(r, delta) @@ -264,7 +264,7 @@ func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) { } } -func (p *BeersPlugin) getBeers(user, id string) int { +func (p *BeersPlugin) getBeers(user, id string) int64 { itemName := p.c.Get("beers.itemname", DEFAULT_ITEM) ub := getUserBeers(p.db, user, id, itemName) return ub.Count diff --git a/plugins/counter/api.go b/plugins/counter/api.go index 99f86be..703db79 100644 --- a/plugins/counter/api.go +++ b/plugins/counter/api.go @@ -42,13 +42,13 @@ type CounterChangeReq struct { UserName string `in:"path=user"` Item string `in:"path=item"` Password string `in:"form=password"` - Delta int `in:"path=delta"` + Delta int64 `in:"path=delta"` Body struct { Message string `json:"message"` } `in:"body=json"` } -func (p *CounterPlugin) incHandler(delta int) http.HandlerFunc { +func (p *CounterPlugin) incHandler(delta int64) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { input := r.Context().Value(httpin.Input).(*CounterChangeReq) if !p.b.CheckPassword("", input.Password) { @@ -66,7 +66,7 @@ func (p *CounterPlugin) incHandler(delta int) http.HandlerFunc { } } -func (p *CounterPlugin) delta(userName, itemName, personalMessage string, delta int) (Item, error) { +func (p *CounterPlugin) delta(userName, itemName, personalMessage string, delta int64) (Item, error) { // Try to find an ID if possible id := "" u, err := p.b.DefaultConnector().Profile(userName) @@ -113,7 +113,7 @@ func (p *CounterPlugin) delta(userName, itemName, personalMessage string, delta return item, nil } -func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWriter, r *http.Request) { +func (p *CounterPlugin) mkIncrementByNAPI(direction int64) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { input := r.Context().Value(httpin.Input).(*CounterChangeReq) if input.Delta == 0 { @@ -169,5 +169,5 @@ type Update struct { // Counter Item What string `json:"what"` // Total counter amount - Amount int `json:"amount"` + Amount int64 `json:"amount"` } diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index ef329f5..60a887a 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -32,7 +32,7 @@ type Item struct { ID int64 Nick string Item string - Count int + Count int64 UserID sql.NullString } @@ -225,7 +225,7 @@ func (i *Item) Create() error { // UpdateDelta sets a value // This will create or delete the item if necessary -func (i *Item) Update(r *bot.Request, value int) error { +func (i *Item) Update(r *bot.Request, value int64) error { i.Count = value if i.Count == 0 && i.ID != -1 { return i.Delete() @@ -237,7 +237,7 @@ func (i *Item) Update(r *bot.Request, value int) error { } log.Debug(). Interface("i", i). - Int("value", value). + Int64("value", value). Msg("Updating item") _, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID) if err == nil { @@ -248,7 +248,7 @@ func (i *Item) Update(r *bot.Request, value int) error { // UpdateDelta changes a value according to some delta // This will create or delete the item if necessary -func (i *Item) UpdateDelta(r *bot.Request, delta int) error { +func (i *Item) UpdateDelta(r *bot.Request, delta int64) error { i.Count += delta return i.Update(r, i.Count) } @@ -479,7 +479,7 @@ func (p *CounterPlugin) inspectCmd(r bot.Request) bool { Str("nick", nick). Str("id", id). Msg("Getting counter") - // pull all of the items associated with "subject" + // pull all the items associated with "subject" items, err := GetItems(p.db, nick, id) if err != nil { log.Error(). @@ -658,7 +658,7 @@ func (p *CounterPlugin) addToCmd(r bot.Request) bool { // Item ain't there, I guess return false } - n, _ := strconv.Atoi(r.Values["amount"]) + n, _ := strconv.ParseInt(r.Values["amount"], 10, 64) log.Debug().Msgf("About to update item by %d: %#v", n, item) p.b.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count+n, item.Item)) @@ -684,7 +684,7 @@ func (p *CounterPlugin) removeFromCmd(r bot.Request) bool { // Item ain't there, I guess return false } - n, _ := strconv.Atoi(r.Values["amount"]) + n, _ := strconv.ParseInt(r.Values["amount"], 10, 64) log.Debug().Msgf("About to update item by -%d: %#v", n, item) p.b.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick, item.Count-n, item.Item)) @@ -726,7 +726,7 @@ func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool { return false } log.Debug().Msgf("About to update item: %#v", item) - delta := 1 + var delta int64 = 1 if item.Count < 0 { delta = -1 } @@ -754,7 +754,7 @@ func RegisterUpdate(f updateFunc) { updateFuncs = append(updateFuncs, f) } -func sendUpdate(r *bot.Request, who, what string, amount int) { +func sendUpdate(r *bot.Request, who, what string, amount int64) { log.Debug(). Msgf("Updating %s for %s with %d", who, what, amount) if r == nil { diff --git a/plugins/goals/goals.go b/plugins/goals/goals.go index 4a7c680..b858cb4 100644 --- a/plugins/goals/goals.go +++ b/plugins/goals/goals.go @@ -173,7 +173,7 @@ func (p *GoalsPlugin) checkCompetition(c bot.Connector, ch, what, who string) { return } - count := 0 + var count int64 for _, i := range items { if i.Nick == who { count = i.Count @@ -336,7 +336,7 @@ func (p *GoalsPlugin) update(r bot.Request, u counter.Update) { var now = time.Now -func (p *GoalsPlugin) calculateRemaining(i counter.Item, g *goal) int { +func (p *GoalsPlugin) calculateRemaining(i counter.Item, g *goal) int64 { 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) @@ -344,7 +344,7 @@ func (p *GoalsPlugin) calculateRemaining(i counter.Item, g *goal) int { perc := today / days shouldHave := float64(g.Amount) * perc - diff := int(shouldHave) - i.Count + diff := int64(shouldHave) - i.Count log.Printf("Today is the %f-th day with %f days in the year", today, days)