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
| // Register leaderboard reset function to handle promotions and relegations
initializer.RegisterLeaderboardReset(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, leaderboard *api.Leaderboard, reset int64) error {
// We're only interested in our top/bottom tier leaderboards so return if it isn't them
if leaderboard.Id != topTierId && leaderboard.Id != bottomTierId {
return nil
}
// Get all leaderboard records (assuming the tier has no more than 10,000 players)
records, _, _, _, _ := nk.LeaderboardRecordsList(ctx, leaderboard.Id, []string{}, 10000, "", reset)
// If leaderboard is top tier and has 10 or more players, relegate bottom 3 players
if leaderboard.Id == topTierId && len(records) >= 10 {
for _, record := range records[len(records)-3:] {
// Relegate record owner by copying their record into the bottom tier and deleting their current top tier record
nk.LeaderboardRecordWrite(ctx, bottomTierId, record.OwnerId, record.Username.Value, record.Score, record.Subscore, nil, nil)
nk.LeaderboardRecordDelete(ctx, topTierId, record.OwnerId)
}
}
// If leaderboard is bottom tier and has 10 or more players, promote top 3 players
if leaderboard.Id == bottomTierId && len(records) >= 10 {
for _, record := range records[0:2] {
// Promote record owner by copying their record into the top tier and deleting their current bottom tier record
nk.LeaderboardRecordWrite(ctx, topTierId, record.OwnerId, record.Username.Value, record.Score, record.Subscore, nil, nil)
nk.LeaderboardRecordDelete(ctx, bottomTierId, record.OwnerId)
}
}
return nil
})
|