aboutsummaryrefslogtreecommitdiff
path: root/user/xp.go
diff options
context:
space:
mode:
authorWilliam Hergès <william@herges.fr>2026-01-17 22:14:37 +0100
committerWilliam Hergès <william@herges.fr>2026-01-17 23:55:26 +0100
commit1dcf4216d473a6ce7063297b3cae1c4cff2a070d (patch)
treee7ee5d89f4367e5c703bc9f6f1ed5081d3b8f197 /user/xp.go
parenta3b0f488f799020d20c6cd2eb6bb082071bb0455 (diff)
fix(state): bad sum for XPs
Diffstat (limited to 'user/xp.go')
-rw-r--r--user/xp.go16
1 files changed, 7 insertions, 9 deletions
diff --git a/user/xp.go b/user/xp.go
index 35c11f3..161a450 100644
--- a/user/xp.go
+++ b/user/xp.go
@@ -40,30 +40,28 @@ func (cc *CopaingCached) AddXP(ctx context.Context, s bot.Session, m *user.Membe
}
}
-func (cc *CopaingCached) GetXPForDays(n uint) uint {
+func (cc *CopaingCached) GetXPForDays(d int) uint {
xp := uint(0)
for _, v := range cc.XPs {
- if v.Time <= time.Duration(n*24)*time.Hour {
+ if v.Time <= time.Duration(d*24)*time.Hour {
xp += v.XP
}
}
return xp + cc.XPToAdd
}
-// GetBestXP returns n Copaing with the best XP within d days (d <= cfg.DaysXPRemain; d < 0 <=> d = cfg.DaysXPRemain)
+// GetBestXP returns n Copaings with the best XP within d days (d <= cfg.DaysXPRemain; d < 0 <=> d = cfg.DaysXPRemain)
func GetBestXP(ctx context.Context, guildId string, n uint, d int) []CopaingCached {
ccs := GetState(ctx).Copaings(guildId)
if d > 0 {
- for _, v := range ccs {
- v.XP = v.GetXPForDays(n)
+ for i, cc := range ccs {
+ cc.XP = cc.GetXPForDays(d)
+ ccs[i] = cc
}
}
slices.SortFunc(ccs, func(a, b CopaingCached) int {
// desc order
return int(b.XP) - int(a.XP)
})
- m := min(len(ccs), int(n))
- res := make([]CopaingCached, m)
- copy(ccs[:m], res)
- return res
+ return ccs[:min(len(ccs), int(n))]
}