diff options
| author | William Hergès <william@herges.fr> | 2026-01-17 19:57:28 +0100 |
|---|---|---|
| committer | William Hergès <william@herges.fr> | 2026-01-17 19:57:28 +0100 |
| commit | ec5cfa632eeb607351f67bad6686ec872291bd61 (patch) | |
| tree | c53b9bf7e14c44b49a17f088737b35eb5ad0b64e /user/xp.go | |
| parent | c9129c2e7edcf6e588cac674dfdb240f1714083d (diff) | |
perf(command): top now partially uses state
Diffstat (limited to 'user/xp.go')
| -rw-r--r-- | user/xp.go | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -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] +} |
