Skip to main content

Get Scores By Leaderboard ID

For more detailed examples and guidance on implementing the Myria Leaderboard SDK, please refer to our TypeScript SDK samples on GitHub here. This repository includes practical examples demonstrating how to create, manage, and interact with leaderboards, making it easier for you to integrate leaderboard functionality into your projects.

To retrieve scores from a leaderboard using from @myria/leaderboard-ts-sdk, follow the steps below.

Step 1: Initialize the Leaderboard Manager

First, you need to initialize the LeaderboardManager with the appropriate parameters. These parameters include your environment type (e.g, STAGING, PRODUCTION) and the developerApiKey you received from the Myria Admin.

import { LeaderboardManager, InitLeaderboardParams } from '@myria/leaderboard-ts-sdk';

const developerApiKey = "YOUR_DEVELOPER_API_KEY";

const leaderboardParams: InitLeaderboardParams = {
env: 'STAGING', // Use your desired environment type
apiKey: developerApiKey,
};

const leaderboardManager = new LeaderboardManager(initLeaderboardParams);

Step 2: Define Query Parameters

Next, define the parameters required to query scores from the leaderboard. This includes the leaderboardId you want to retrieve scores from.

import { IQueryScoreParams } from '@myria/leaderboard-ts-sdk';

const leaderboardId = 62; // Replace with your actual leaderboard ID
const queryScores: IQueryScoreParams = {
leaderboardId,
// Optional: You can add additional query parameters such as pagination and sorting
// page: 1,
// limit: 10,
// sortingField: 'score',
// orderBy: EOrderBy.DESC,
};

Note: The response for the score list includes pagination data with page and limit details.

Input Details:

Mandatory Fields:

  • leaderboardId (number, required): The ID of the leaderboard to query.

Optional Fields:

  • limit (number, optional): The maximum number of results to return per page.
  • page (number, optional): The page number of the results to retrieve.
  • sortingField (string, optional): The field by which to sort the results (e.g., score).
  • orderBy (EOrderBy, optional): The order of the results, either ASC (ascending) or DESC (descending).
  • filters (QueryFilter, optional): Additional filters to apply to the query.

Note: The response for the score list includes pagination data with page and limit details.

Step 3: Get Scores In Leaderboards

Once you have defined the query parameters, you can retrieve the scores by calling the queryScoresByLeaderboardId method on the LeaderboardManager instance.

const queryScoreResp = await leaderboardManager.queryScoresByLeaderboardId(queryScores);
console.log(`Get score in leaderboard ${leaderboardId} response:`, queryScoreResp);

Ouput Details:

The method returns a <CommonPaginateDataTypes<IQueryScoresResp> | null> object containing the scores along with pagination metadata.

  • items (IQueryScoresResp[]): An array of score objects.
  • meta (object): Metadata about the pagination.
  • totalItems (number): The total number of items available.
  • itemCount (number): The number of items on the current page.
  • itemsPerPage (number): The number of items per page.
  • totalPages (number): The total number of pages available.
  • currentPage (number): The current page number.

Score Response Object (IQueryScoresResp)

  • id (number): The unique ID of the score entry.
  • userId (string): The ID of the user associated with the score.
  • leaderboardId (number): The ID of the leaderboard to which the score belongs.
  • period (number): The period in which the score was recorded.
  • score (number): The score value.
  • createdAt (string): The timestamp when the score was created.
  • updatedAt (string): The timestamp when the score was last updated.
  • rank (number): The rank of the user based on the score.

Summary

By following these steps, you can successfully retrieve scores from a leaderboard using the @myria/leaderboard-ts-sdk. This process involves initializing the LeaderboardManager, defining the query parameters, and invoking the queryScoresByLeaderboardId method to get the scores. Remember: The response includes pagination data with page and limit details.

For more details on the available options and additional features, refer to the full documentation.