Leaderboards
Read more about the Leaderboards system in Hiro here.
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.
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
logger | runtime.Logger | Logger instance. |
nk | runtime.NakamaModule | Nakama module. |
userID | string | The ID of the user to retrieve leaderboards for. |
categories | []string | Optional list of categories to filter by. Pass an empty slice to return all leaderboards. |
Returns
| Return | Type | Description |
|---|---|---|
leaderboards | []*hiro.Leaderboard | The list of leaderboards, optionally filtered and personalized. |
err | error | Non-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.
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
logger | runtime.Logger | Logger instance. |
nk | runtime.NakamaModule | Nakama module. |
userID | string | The ID of the user to retrieve the leaderboard for. |
leaderboardID | string | The ID of the leaderboard to retrieve. |
Returns
| Return | Type | Description |
|---|---|---|
leaderboard | *hiro.Leaderboard | The leaderboard, including live reset and timing data. |
err | error | Non-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.
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
logger | runtime.Logger | Logger instance. |
nk | runtime.NakamaModule | Nakama module. |
userID | string | The ID of the requesting user. |
leaderboardID | string | The ID of the leaderboard to list scores from. |
region | string | Optional region filter (for example, "EU"). Pass an empty string for the global leaderboard. |
ownerIds | []string | Optional list of owner IDs to retrieve records for. |
limit | int | Maximum number of records to return. Between 1 and 1000. |
cursor | string | A next or previous page cursor. Pass an empty string for the first page. |
expiry | int64 | Expiry in seconds (since epoch) to filter records. Pass 0 to use current time. |
Returns
| Return | Type | Description |
|---|---|---|
scoreList | *hiro.LeaderboardScoreList | The list of scores, owner scores, cursors, and the leaderboard definition. |
err | error | Non-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.
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
logger | runtime.Logger | Logger instance. |
nk | runtime.NakamaModule | Nakama module. |
userID | string | The ID of the requesting user. |
leaderboardID | string | The ID of the leaderboard to list scores from. |
region | string | Optional region filter. Pass an empty string for the global leaderboard. |
ownerId | string | The owner to centre the results around. |
limit | int | Maximum number of records to return. Between 1 and 100. |
cursor | string | A next or previous page cursor. Pass an empty string for the first page. |
expiry | int64 | Expiry in seconds (since epoch) to filter records. Pass 0 to use current time. |
Returns
| Return | Type | Description |
|---|---|---|
scoreList | *hiro.LeaderboardScoreList | The list of scores centred around the owner, including cursors, rank count, and the leaderboard definition. |
err | error | Non-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.
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
logger | runtime.Logger | Logger instance. |
db | *sql.DB | Database handle. |
nk | runtime.NakamaModule | Nakama module. |
userID | string | The ID of the user submitting the score. |
username | string | The username of the user submitting the score. |
leaderboardID | string | The ID of the leaderboard to submit the score to. |
score | int64 | The primary score value. |
subscore | int64 | The secondary score value. |
metadata | map[string]interface{} | Optional metadata to attach to the record. Include a "region" key to fan out to regional leaderboards. |
overrideOperator | *int | Optional operator override. Pass nil to use the leaderboard’s configured operator. |
Returns
| Return | Type | Description |
|---|---|---|
leaderboardScore | *hiro.LeaderboardScore | The resulting score record. |
err | error | Non-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.
| |
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.
| |
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.
| |
Parameters
| Parameter | Type | Description |
|---|---|---|
ctx | context.Context | The request context. |
logger | runtime.Logger | Logger instance. |
nk | runtime.NakamaModule | Nakama module. |
userID | string | The ID of the user to retrieve configuration for. |
Returns
| Return | Type | Description |
|---|---|---|
leaderboardConfigList | *hiro.LeaderboardConfigList | The leaderboard configuration list. |
err | error | Non-nil if an error occurred. |
