counter: 64bit ints

This commit is contained in:
Chris Sexton 2024-06-19 09:13:12 -04:00
parent 2697e6a259
commit 319df5b0f3
4 changed files with 21 additions and 21 deletions

View File

@ -97,7 +97,7 @@ func (p *BeersPlugin) register() {
Regex: regexp.MustCompile(`(?i)^beers?\s?(?P<operator>(\+=|-=|=))\s?(?P<amount>\d+)$`), Regex: regexp.MustCompile(`(?i)^beers?\s?(?P<operator>(\+=|-=|=))\s?(?P<amount>\d+)$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
op := r.Values["operator"] op := r.Values["operator"]
count, _ := strconv.Atoi(r.Values["amount"]) count, _ := strconv.ParseInt(r.Values["amount"], 10, 64)
nick := r.Msg.User.Name nick := r.Msg.User.Name
id := r.Msg.User.ID id := r.Msg.User.ID
@ -246,7 +246,7 @@ func getUserBeers(db *sqlx.DB, user, id, itemName string) counter.Item {
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 int64) {
itemName := p.c.Get("beers.itemname", DEFAULT_ITEM) itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName) ub := getUserBeers(p.db, user, id, itemName)
err := ub.Update(r, amount) 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) itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName) ub := getUserBeers(p.db, user, id, itemName)
err := ub.UpdateDelta(r, delta) 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) itemName := p.c.Get("beers.itemname", DEFAULT_ITEM)
ub := getUserBeers(p.db, user, id, itemName) ub := getUserBeers(p.db, user, id, itemName)
return ub.Count return ub.Count

View File

@ -42,13 +42,13 @@ type CounterChangeReq struct {
UserName string `in:"path=user"` UserName string `in:"path=user"`
Item string `in:"path=item"` Item string `in:"path=item"`
Password string `in:"form=password"` Password string `in:"form=password"`
Delta int `in:"path=delta"` Delta int64 `in:"path=delta"`
Body struct { Body struct {
Message string `json:"message"` Message string `json:"message"`
} `in:"body=json"` } `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) { return func(w http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*CounterChangeReq) input := r.Context().Value(httpin.Input).(*CounterChangeReq)
if !p.b.CheckPassword("", input.Password) { 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 // Try to find an ID if possible
id := "" id := ""
u, err := p.b.DefaultConnector().Profile(userName) u, err := p.b.DefaultConnector().Profile(userName)
@ -113,7 +113,7 @@ func (p *CounterPlugin) delta(userName, itemName, personalMessage string, delta
return item, nil 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) { return func(w http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*CounterChangeReq) input := r.Context().Value(httpin.Input).(*CounterChangeReq)
if input.Delta == 0 { if input.Delta == 0 {
@ -169,5 +169,5 @@ type Update struct {
// Counter Item // Counter Item
What string `json:"what"` What string `json:"what"`
// Total counter amount // Total counter amount
Amount int `json:"amount"` Amount int64 `json:"amount"`
} }

View File

@ -32,7 +32,7 @@ type Item struct {
ID int64 ID int64
Nick string Nick string
Item string Item string
Count int Count int64
UserID sql.NullString UserID sql.NullString
} }
@ -225,7 +225,7 @@ func (i *Item) Create() error {
// UpdateDelta sets a value // UpdateDelta sets a value
// This will create or delete the item if necessary // 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 i.Count = value
if i.Count == 0 && i.ID != -1 { if i.Count == 0 && i.ID != -1 {
return i.Delete() return i.Delete()
@ -237,7 +237,7 @@ func (i *Item) Update(r *bot.Request, value int) error {
} }
log.Debug(). log.Debug().
Interface("i", i). Interface("i", i).
Int("value", value). Int64("value", value).
Msg("Updating item") Msg("Updating item")
_, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID) _, err := i.Exec(`update counter set count = ? where id = ?`, i.Count, i.ID)
if err == nil { 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 // UpdateDelta changes a value according to some delta
// This will create or delete the item if necessary // 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 i.Count += delta
return i.Update(r, i.Count) return i.Update(r, i.Count)
} }
@ -479,7 +479,7 @@ func (p *CounterPlugin) inspectCmd(r bot.Request) bool {
Str("nick", nick). Str("nick", nick).
Str("id", id). Str("id", id).
Msg("Getting counter") 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) items, err := GetItems(p.db, nick, id)
if err != nil { if err != nil {
log.Error(). log.Error().
@ -658,7 +658,7 @@ func (p *CounterPlugin) addToCmd(r bot.Request) bool {
// Item ain't there, I guess // Item ain't there, I guess
return false 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) 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, p.b.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count+n, item.Item)) item.Count+n, item.Item))
@ -684,7 +684,7 @@ func (p *CounterPlugin) removeFromCmd(r bot.Request) bool {
// Item ain't there, I guess // Item ain't there, I guess
return false 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) 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, p.b.Send(r.Conn, bot.Message, channel, fmt.Sprintf("%s has %d %s.", nick,
item.Count-n, item.Item)) item.Count-n, item.Item))
@ -726,7 +726,7 @@ func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool {
return false return false
} }
log.Debug().Msgf("About to update item: %#v", item) log.Debug().Msgf("About to update item: %#v", item)
delta := 1 var delta int64 = 1
if item.Count < 0 { if item.Count < 0 {
delta = -1 delta = -1
} }
@ -754,7 +754,7 @@ func RegisterUpdate(f updateFunc) {
updateFuncs = append(updateFuncs, f) 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(). log.Debug().
Msgf("Updating %s for %s with %d", who, what, amount) Msgf("Updating %s for %s with %d", who, what, amount)
if r == nil { if r == nil {

View File

@ -173,7 +173,7 @@ func (p *GoalsPlugin) checkCompetition(c bot.Connector, ch, what, who string) {
return return
} }
count := 0 var count int64
for _, i := range items { for _, i := range items {
if i.Nick == who { if i.Nick == who {
count = i.Count count = i.Count
@ -336,7 +336,7 @@ func (p *GoalsPlugin) update(r bot.Request, u counter.Update) {
var now = time.Now 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()) today := float64(now().YearDay())
thisYear := time.Date(now().Year(), 0, 0, 0, 0, 0, 0, time.UTC) 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) 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 perc := today / days
shouldHave := float64(g.Amount) * perc 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) log.Printf("Today is the %f-th day with %f days in the year", today, days)