Get Leaderboards
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 get the list of leaderboards using the @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, LeaderboardEnv } from '@myria/leaderboard-ts-sdk';
const developerApiKey = "YOUR_DEVELOPER_API_KEY";
const leaderboardParams: InitLeaderboardParams = {
env: LeaderboardEnv.staging, // Use your desired environment type
apiKey: developerApiKey,
};
const leaderboardManager = new LeaderboardManager(leaderboardParams);
Step 2: Define Query Leaderboard Parameters
Next, define the parameters for your leaderboard. This includes properties like the name of the leaderboard, description, game name, duration, and score update strategy.
import { IQueryLeaderboardsParams } from '@myria/leaderboard-ts-sdk';
const queryLeaderboardParams: IQueryLeaderboardsParams = {
page: 1, // Replace with your actual page number that you'd want to retrieve
limit: 10 // Retrieve the list of leaderboard to retrieve per one page
};
Input Details:
Optional Fields:
- page: number - Specifies the page number to retrieve. Default is
1
. - limit: number - Specifies the number of leaderboards to retrieve per page. Default is
10
Step 3: Retrieve list of Leaderboards
Once you have defined the parameters, you can get the list of leaderboards by calling the getLeaderboards method on the LeaderboardManager instance.
const result = await leaderboardManager.getLeaderboards(queryLeaderboardParams);
console.log('Leaderboard retrieved response:', result);
Output Details:
The response from getLeaderboards
is an APIResponseType
object containing pagination data and the list of leaderboards:
export interface APIResponseType<T> {
status: string;
data: T;
errors?: any;
}
export interface CommonPaginateDataTypes<T> {
items: T;
meta: {
totalItems: number;
itemCount: number;
itemsPerPage: number;
totalPages: number;
currentPage: number;
};
}
export interface LeaderboardData {
name: string;
description: string;
updateScoreStrategy: string;
gameName: string;
totalPeriods?: number;
livePeriodInDays: number;
availableAt: string;
enableMetadata?: boolean;
developerId?: string;
status: string;
currentPeriod: number;
dataPersistenceInDays: number;
expireAt: string;
updatedAt?: string;
createdAt?: string;
id: number;
enablePeriodRefresh?: boolean;
}
Example Output:
{
"status": "success",
"data": {
"items": [
{
"name": "Leaderboard 1",
"description": "Test leaderboard 1",
"updateScoreStrategy": "ACCUMULATE",
"gameName": "Test Game",
"livePeriodInDays": 30,
"availableAt": "2024-01-01T00:00:00Z",
"status": "active",
"currentPeriod": 1,
"dataPersistenceInDays": 365,
"expireAt": "2025-01-01T00:00:00Z",
"id": 1
}
],
"meta": {
"totalItems": 1,
"itemCount": 1,
"itemsPerPage": 10,
"totalPages": 1,
"currentPage": 1
}
}
}
Summary
By following these steps, you can successfully get the list of leaderboards using the @myria/leaderboard-ts-sdk
. This process involves initializing the LeaderboardManager, defining the leaderboard parameters, and invoking the getLeaderboards method to finalize the creation.
For more details on the available options and additional features, refer to the full documentation.