Skip to main content

Get Scores By User 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 and by userId 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 queryScoreParams: IQueryScoreByLeaderboardAndUserIdParams = {
leaderboardId, // Replace with your actual leaderboard ID
userId: '10', // Replace with your actual user ID
period: 1, // Optional: Specify the leaderboard period
};

Input Details:

Mandatory Fields:

  • leaderboardId (number): The ID of the leaderboard from which to query the score.
  • userId (string): The ID of the user whose score you want to retrieve.

Optional Fields:

  • period (number): The specific period of the leaderboard for which to query the score. Note: The response includes details about the leaderboard and the user's score.

Step 3: Get Scores For Players In Leaderboards

Once you have defined the query parameters, you can retrieve the user's score by calling the getScoresByLeaderboardIdAndUserId method on the LeaderboardManager instance.

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

Ouput Details:

The method returns an IScoreByUserResp object that contains detailed information about the user's score and the associated leaderboard.

  • score (number): The user's score in the leaderboard.
  • leaderboardId (number): The ID of the leaderboard.
  • userId (string): The ID of the user.
  • period (number, optional): The period in which the score was recorded.
  • leaderboard (LeaderboardData): Detailed information about the leaderboard.
  • user (IUserLeaderboard): Detailed information about the user.

Leaderboard Data Object (LeaderboardData)

  • name (string): The name of the leaderboard.
  • description (string): A description of the leaderboard.
  • updateScoreStrategy (string): The strategy used to update scores (e.g., OVERWRITE, ACCUMULATE).
  • gameName (string): The name of the game associated with the leaderboard.
  • totalPeriods (number, optional): The total number of periods in the leaderboard.
  • livePeriodInDays (number): The number of days the leaderboard is live.
  • availableAt (string): The timestamp when the leaderboard is available.
  • enableMetadata (boolean, optional): Indicates whether metadata is enabled for the leaderboard.
  • developerId (string, optional): The ID of the developer who created the leaderboard.
  • status (string): The current status of the leaderboard (e.g., ACTIVE, INACTIVE).
  • currentPeriod (number): The current period of the leaderboard.
  • dataPersistenceInDays (number): The number of days the leaderboard data is persisted.
  • expireAt (string): The timestamp when the leaderboard will expire.
  • updatedAt (string, optional): The timestamp when the leaderboard was last updated.
  • createdAt (string, optional): The timestamp when the leaderboard was created.
  • id (number): The unique ID of the leaderboard.
  • enablePeriodRefresh (boolean, optional): Indicates whether period refresh is enabled for the leaderboard.

User Leaderboard Object (IUserLeaderboard)

  • userId (string): The unique ID of the user.
  • username (string): The username of the user.
  • displayName (string): The display name of the user.

Summary

By following these steps, you can successfully retrieve a specific user's score from a leaderboard using the @myria/leaderboard-ts-sdk. This process involves initializing the LeaderboardManager, defining the query parameters, and invoking the getScoresByLeaderboardIdAndUserId method to get the user's score.

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