-
Notifications
You must be signed in to change notification settings - Fork 1
/
leader_board.go
49 lines (45 loc) · 1.26 KB
/
leader_board.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package querier
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/eco/longy/x/longy/internal/keeper"
"github.com/eco/longy/x/longy/internal/types"
"sort"
"time"
)
//LeaderBoard returns the leader board after building it from the attendees in the event
//nolint:gocritic,unparam,nakedret
func leaderBoard(ctx sdk.Context, keeper keeper.Keeper) (res []byte, err sdk.Error) { //test this
attendees := keeper.GetAllAttendees(ctx)
countAll := len(attendees)
sort.Slice(attendees, func(i, j int) bool { return attendees[i].Rep > attendees[j].Rep })
var lb *types.LeaderBoard
min := types.LeaderBoardCount
if countAll < types.LeaderBoardCount {
min = countAll
}
top := make([]types.Attendee, min, types.LeaderBoardCount)
copy(top, attendees)
withRep := 0
for i := range top {
if top[i].Rep == 0 {
break
}
withRep++
top[i] = types.Attendee{
ID: top[i].ID,
Address: top[i].Address,
Name: top[i].Name,
Rep: top[i].Rep,
}
}
topCleaned := make([]types.Attendee, withRep)
copy(topCleaned, top)
lb = types.NewLeaderBoard(countAll, topCleaned)
lb.Time = time.Now()
res, e := codec.MarshalJSONIndent(keeper.Cdc, lb)
if e != nil {
panic("could not marshal result to JSON")
}
return
}