From ec5cfa632eeb607351f67bad6686ec872291bd61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sat, 17 Jan 2026 19:57:28 +0100 Subject: perf(command): top now partially uses state --- user/xp.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'user/xp.go') diff --git a/user/xp.go b/user/xp.go index 985b5f8..9c6c6ab 100644 --- a/user/xp.go +++ b/user/xp.go @@ -78,10 +78,11 @@ func (c *Copaing) GetXPForDays(logger *slog.Logger, n uint) (uint, error) { // GetBestXP returns n Copaing with the best XP within d days (d <= cfg.DaysXPRemain; d < 0 <=> d = cfg.DaysXPRemain) // // This function is slow -func GetBestXP(logger *slog.Logger, guildId string, n uint, d int) ([]CopaingAccess, error) { +func GetBestXP(ctx context.Context, logger *slog.Logger, guildId string, n uint, d int) ([]CopaingCached, error) { if d < 0 { cfg := config.GetGuildConfig(guildId) d = int(cfg.DaysXPRemains) + return getBestXPFull(ctx, guildId, n), nil } rows, err := gokord.DB.Model(&Copaing{}).Where("guild_id = ?", guildId).Rows() if err != nil { @@ -112,9 +113,18 @@ func GetBestXP(logger *slog.Logger, guildId string, n uint, d int) ([]CopaingAcc return int(b.Cxp) - int(a.Cxp) }) m := min(len(l), int(n)) - cs := make([]CopaingAccess, m) + cs := make([]CopaingCached, m) for i, c := range l[:m] { - cs[i] = c + cs[i] = CopaingCached{DiscordID: c.copaing.DiscordID, XPs: c.Cxp} } return cs, nil } + +func getBestXPFull(ctx context.Context, guildId string, n uint) []CopaingCached { + ccs := GetState(ctx).Copaings(guildId) + slices.SortFunc(ccs, func(a, b CopaingCached) int { + return int(b.XPs) - int(a.XPs) + }) + m := min(len(ccs), int(n)) + return ccs[:m] +} -- cgit v1.2.3