1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: add maximum default leaderboard length

This commit is contained in:
Ferdinand Mütsch
2022-10-19 18:28:30 +02:00
parent ffbcfc7467
commit b1d7f87095
7 changed files with 124 additions and 9 deletions

View File

@ -19,14 +19,36 @@ type LeaderboardItem struct {
CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
}
func (l1 *LeaderboardItem) Equals(l2 *LeaderboardItem) bool {
return l1.ID == l2.ID
}
type Leaderboard []*LeaderboardItem
func (l *Leaderboard) Add(item *LeaderboardItem) {
if _, found := slice.Find[*LeaderboardItem](*l, func(i int, item2 *LeaderboardItem) bool {
return item.Equals(item2)
}); !found {
*l = append(*l, item)
}
}
func (l *Leaderboard) AddMany(items []*LeaderboardItem) {
for _, item := range items {
l.Add(item)
}
}
func (l Leaderboard) UserIDs() []string {
return slice.Unique[string](slice.Map[*LeaderboardItem, string](l, func(i int, item *LeaderboardItem) string {
return item.UserID
}))
}
func (l Leaderboard) HasUser(userId string) bool {
return slice.Contain(l.UserIDs(), userId)
}
func (l Leaderboard) TopByKey(by uint8, key string) Leaderboard {
return slice.Filter[*LeaderboardItem](l, func(i int, item *LeaderboardItem) bool {
return item.By != nil && *item.By == by && item.Key != nil && strings.ToLower(*item.Key) == strings.ToLower(key)