fix(reducer): sql error integer > int64max

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-16 15:15:30 +02:00
parent d673c055d2
commit c3dc81bd29
No known key found for this signature in database
GPG key ID: CF4550297832A29F
2 changed files with 60 additions and 6 deletions

11
main.go
View file

@ -120,7 +120,18 @@ func main() {
} }
func afterInit(dg *discordgo.Session) { func afterInit(dg *discordgo.Session) {
// handlers
dg.AddHandler(xp.OnMessage) dg.AddHandler(xp.OnMessage)
dg.AddHandler(xp.OnVoiceUpdate) dg.AddHandler(xp.OnVoiceUpdate)
dg.AddHandler(xp.OnLeave) dg.AddHandler(xp.OnLeave)
// setup timer for periodic reducer
//d := 24 * time.Hour
//if gokord.Debug {
// // reduce for debug
// d = time.Minute
//}
//utils.NewTimer(d, func(stop chan struct{}) {
// xp.PeriodicReducer(dg)
//})
} }

View file

@ -47,26 +47,69 @@ func LastEventUpdate(c *Copaing) {
h := c.HourSinceLastEvent() h := c.HourSinceLastEvent()
l := Lose(h, c.XP) l := Lose(h, c.XP)
xp := c.XPAlreadyRemoved() xp := c.XPAlreadyRemoved()
oldXP := c.XP
if l-xp < 0 { if l-xp < 0 {
utils.SendWarn("lose - xp already removed is negative", "lose", l, "xp", xp) utils.SendWarn("lose - xp already removed is negative", "lose", l, "xp", xp)
c.XP = 0 c.XP = 0
} else { } else {
c.XP = c.XP - l + c.XPAlreadyRemoved() calc := int(c.XP) - int(l) + int(c.XPAlreadyRemoved())
if calc < 0 {
c.XP = 0
} else {
c.XP = uint(calc)
} }
}
if oldXP != c.XP {
c.Save() c.Save()
}
c.SetLastEvent() c.SetLastEvent()
} }
func XPUpdate(c *Copaing) { func XPUpdate(c *Copaing) {
oldXP := c.XP
if oldXP == 0 {
return
}
h := c.HourSinceLastEvent() h := c.HourSinceLastEvent()
l := Lose(h, c.XP) l := Lose(h, c.XP)
xp := c.XPAlreadyRemoved() xp := c.XPAlreadyRemoved()
if l-xp < 0 { if l-xp < 0 {
utils.SendWarn("lose - xp already removed is negative", "lose", l, "xp", xp) utils.SendWarn("lose - xp_removed is negative", "lose", l, "xp removed", xp)
c.AddXPAlreadyRemoved(0) c.AddXPAlreadyRemoved(0)
} else { } else {
c.XP = c.XP - l + xp calc := int(c.XP) - int(l) + int(xp)
if calc < 0 {
c.AddXPAlreadyRemoved(c.XP)
c.XP = 0
} else {
c.XP = uint(calc)
c.AddXPAlreadyRemoved(l - xp) c.AddXPAlreadyRemoved(l - xp)
} }
}
if oldXP != c.XP {
utils.SendDebug("Save XP", "old", oldXP, "new", c.XP, "user", c.DiscordID)
c.Save() c.Save()
}
} }
//func PeriodicReducer(s *discordgo.Session) {
// var wg sync.WaitGroup
// for _, g := range s.State.Guilds {
// for _, m := range utils.FetchGuildUser(s, g.ID) {
// if m.User.Bot {
// continue
// }
// wg.Add(1)
// go func() {
// utils.SendDebug("Async reducer", "user", m.DisplayName(), "guild", g.Name)
// c := GetCopaing(m.User.ID, g.ID)
// XPUpdate(c)
// wg.Done()
// }()
// }
// wg.Wait() // finish the entire guild before starting another
// utils.SendDebug("Guild finished", "guild", g.Name)
// time.Sleep(10 * time.Second) // sleep prevents from spamming the Discord API
// }
// utils.SendDebug("Periodic reduce finished", "len(guilds)", len(s.State.Guilds))
//}