View as Markdown

Leaderboards

Read more about the Leaderboards system in Hiro here.

The functions below require Hiro 1.33 or later. If you are on an earlier version, see Hiro 1.32 and earlier.

Functions #

ListLeaderboard #

List available leaderboards for a user, optionally filtered by category. The result is personalized for the given user if a personalizer is configured on the system.

1
2
3
4
5
6
7
userID := "userId"
categories := []string{"Tournament"} // pass nil or empty slice to return all

leaderboards, err := systems.GetLeaderboardsSystem().ListLeaderboard(ctx, logger, nk, userID, categories)
if err != nil {
    return err
}

Parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
loggerruntime.LoggerLogger instance.
nkruntime.NakamaModuleNakama module.
userIDstringThe ID of the user to retrieve leaderboards for.
categories[]stringOptional list of categories to filter by. Pass an empty slice to return all leaderboards.

Returns

ReturnTypeDescription
leaderboards[]*hiro.LeaderboardThe list of leaderboards, optionally filtered and personalized.
errerrorNon-nil if an error occurred.

GetLeaderboard #

Retrieve a single leaderboard by ID for a user. The result is personalized for the given user if a personalizer is configured on the system.

1
2
3
4
5
6
7
userID := "userId"
leaderboardID := "GalacticChampions"

leaderboard, err := systems.GetLeaderboardsSystem().GetLeaderboard(ctx, logger, nk, userID, leaderboardID)
if err != nil {
    return err
}

Parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
loggerruntime.LoggerLogger instance.
nkruntime.NakamaModuleNakama module.
userIDstringThe ID of the user to retrieve the leaderboard for.
leaderboardIDstringThe ID of the leaderboard to retrieve.

Returns

ReturnTypeDescription
leaderboard*hiro.LeaderboardThe leaderboard, including live reset and timing data.
errerrorNon-nil if an error occurred.

ListLeaderboardScores #

List scores on a leaderboard, optionally filtered by owner IDs and region. Supports both standard leaderboards and tournament leaderboards.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
userID := "userId"
leaderboardID := "GalacticChampions"
region := "EU"       // pass empty string for the global leaderboard
ownerIds := []string{"userId1", "userId2"}
limit := 100
cursor := ""
expiry := int64(0)

scoreList, err := systems.GetLeaderboardsSystem().ListLeaderboardScores(ctx, logger, nk, userID, leaderboardID, region, ownerIds, limit, cursor, expiry)
if err != nil {
    return err
}

Parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
loggerruntime.LoggerLogger instance.
nkruntime.NakamaModuleNakama module.
userIDstringThe ID of the requesting user.
leaderboardIDstringThe ID of the leaderboard to list scores from.
regionstringOptional region filter (for example, "EU"). Pass an empty string for the global leaderboard.
ownerIds[]stringOptional list of owner IDs to retrieve records for.
limitintMaximum number of records to return. Between 1 and 1000.
cursorstringA next or previous page cursor. Pass an empty string for the first page.
expiryint64Expiry in seconds (since epoch) to filter records. Pass 0 to use current time.

Returns

ReturnTypeDescription
scoreList*hiro.LeaderboardScoreListThe list of scores, owner scores, cursors, and the leaderboard definition.
errerrorNon-nil if an error occurred.

ListLeaderboardScoresAroundOwner #

List scores on a leaderboard centred around a specific owner. Returns a window of records above and below the given owner. Supports both standard leaderboards and tournament leaderboards.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
userID := "userId"
leaderboardID := "GalacticChampions"
region := ""
ownerId := "userId"
limit := 10
cursor := ""
expiry := int64(0)

scoreList, err := systems.GetLeaderboardsSystem().ListLeaderboardScoresAroundOwner(ctx, logger, nk, userID, leaderboardID, region, ownerId, limit, cursor, expiry)
if err != nil {
    return err
}

Parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
loggerruntime.LoggerLogger instance.
nkruntime.NakamaModuleNakama module.
userIDstringThe ID of the requesting user.
leaderboardIDstringThe ID of the leaderboard to list scores from.
regionstringOptional region filter. Pass an empty string for the global leaderboard.
ownerIdstringThe owner to centre the results around.
limitintMaximum number of records to return. Between 1 and 100.
cursorstringA next or previous page cursor. Pass an empty string for the first page.
expiryint64Expiry in seconds (since epoch) to filter records. Pass 0 to use current time.

Returns

ReturnTypeDescription
scoreList*hiro.LeaderboardScoreListThe list of scores centred around the owner, including cursors, rank count, and the leaderboard definition.
errerrorNon-nil if an error occurred.

UpdateLeaderboard #

Submit a score to a leaderboard for a user. Handles both standard leaderboards and tournament leaderboards, and automatically fans out the score to any configured regional leaderboards.

The onBeforeUpdateScore and onAfterUpdateScore hooks run before and after the score is written, if they are set.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
userID := "userId"
username := "username"
leaderboardID := "GalacticChampions"
score := int64(1500)
subscore := int64(0)
metadata := map[string]interface{}{"region": "EU"}

leaderboardScore, err := systems.GetLeaderboardsSystem().UpdateLeaderboard(ctx, logger, db, nk, userID, username, leaderboardID, score, subscore, metadata, nil)
if err != nil {
    return err
}

Parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
loggerruntime.LoggerLogger instance.
db*sql.DBDatabase handle.
nkruntime.NakamaModuleNakama module.
userIDstringThe ID of the user submitting the score.
usernamestringThe username of the user submitting the score.
leaderboardIDstringThe ID of the leaderboard to submit the score to.
scoreint64The primary score value.
subscoreint64The secondary score value.
metadatamap[string]interface{}Optional metadata to attach to the record. Include a "region" key to fan out to regional leaderboards.
overrideOperator*intOptional operator override. Pass nil to use the leaderboard’s configured operator.

Returns

ReturnTypeDescription
leaderboardScore*hiro.LeaderboardScoreThe resulting score record.
errerrorNon-nil if an error occurred.

SetOnBeforeUpdateScore #

Register a hook function that runs before a leaderboard score is written. Use this to validate or modify the score before it is submitted.

1
2
3
4
systems.GetLeaderboardsSystem().SetOnBeforeUpdateScore(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, userID, leaderboardID string, score, subscore int64, metadata map[string]interface{}) (int64, int64, map[string]interface{}, error) {
    // Validate or modify score before write.
    return score, subscore, metadata, nil
})

SetOnAfterUpdateScore #

Register a hook function that runs after a leaderboard score is written. Use this to trigger side effects such as reward grants or notifications.

1
2
3
4
systems.GetLeaderboardsSystem().SetOnAfterUpdateScore(func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, userID, leaderboardID string, score, subscore int64, metadata map[string]interface{}) (int64, int64, map[string]interface{}, error) {
    // React to the score being written.
    return score, subscore, metadata, nil
})

Hiro 1.32 and earlier #

Prior to Hiro 1.33, Get was the only server-side function for retrieving available leaderboards, returning a *hiro.LeaderboardConfigList with basic configuration data. This function is deprecated in Hiro 1.33 and later. Use ListLeaderboard instead.

Get #

Retrieve the leaderboard configuration list for a user.

1
2
3
4
5
6
userID := "userId"

leaderboardConfigList, err := systems.GetLeaderboardsSystem().Get(ctx, logger, nk, userID)
if err != nil {
    return err
}

Parameters

ParameterTypeDescription
ctxcontext.ContextThe request context.
loggerruntime.LoggerLogger instance.
nkruntime.NakamaModuleNakama module.
userIDstringThe ID of the user to retrieve configuration for.

Returns

ReturnTypeDescription
leaderboardConfigList*hiro.LeaderboardConfigListThe leaderboard configuration list.
errerrorNon-nil if an error occurred.