Retrieving leaderboards info; posible somehow?

Is there any way to retrieve info from created leaderboards (either on client or server)? Like expiring date, sort, operator…

Also… is it posible to list a list containing all created leaderboards? (creating a leaderboard that already exists doesnt’ reset it and there is no way to know if that leaderboard was already available or not)

Thanks!

This isn’t currently possible. At the moment leaderboards are treated more as a “configuration” element, and since they can only be created programatically by the developer it’s expected you’ll have a set of them pre-configured. The risk of accidentally changing the sort/reset/etc isn’t expected to be high.

That said it might be useful to get/list leaderboards by ID, so please open an issue so we can track the request.

Hy zyro
I actually managed to get a lot of info by querying the database on the server directly and returning all that info to the client in json format. Here is a quick snippet if someone else is interested

type LeaderBoardInfo struct {
	Id             string
	Authoritative  bool
	Sort_Order     int
	Operator       int
	Reset_schedule string
	Metadata       string
	Create_time    string
}

func ListLeaderboardsRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
	if rows, err := db.QueryContext(ctx, "SELECT id, authoritative, sort_order, operator, reset_schedule, metadata, create_time FROM leaderboard"); err != nil {
		return "error accessing db", err
	} else {
		defer rows.Close()

		var infos []LeaderBoardInfo
		for rows.Next() {
			var info LeaderBoardInfo
			if err := rows.Scan(&info.Id, &info.Authoritative, &info.Sort_Order, &info.Operator, &info.Reset_schedule, &info.Metadata, &info.Create_time); err != nil {
				log.Fatal(err)
			}
			logger.Info("Leaderboard %v", info)
			infos = append(infos, info)
		}
		var ret, _ = json.Marshal(infos)
		return string(ret), nil
	}
}

That looks good, glad it works for your use case! Just keep in mind that it’s returning every leaderboard you have configured so the response could grow quite large.

1 Like

@Zal0 Nice code. We’ve made the low level database driver available to enable you to fill in gaps where our APIs don’t cover all the functionality developers need for projects yet. It would be great if you could open an issue anyway because I think it’d be useful to provide official runtime functions for this logic.

I noticed in your code that you call log.Fatal(err) which you might want to avoid because it will stop the server if the error occurs. I would log it and return an error but just fail the request.