Skip to main content

Post Scores

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 post a score to a leaderboard for players and users 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 } 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 Score Parameters

Next, define the parameters required to post a score to the leaderboard. You can submit scores for multiple users at the same time by including multiple items in the items array.

Note: Each score must be greater than 0.

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

const leaderboardId = 62; // Replace with your actual leaderboard ID
const postNewScoreParams: IPostScoreParams = {
leaderboardId,
items: [
{
score: 240, // The score to be posted (must be > 0)
displayName: 'userDisplayname1', // The display name of the first user
userId: '10', // The first user's ID
username: 'Ste7en', // The first user's username
},
{
score: 320, // Another score (must be > 0)
displayName: 'userDisplayname2', // The display name of the second user
userId: '11', // The second user's ID
username: 'David', // The second user's username
},
// Add more users as needed
],
};

Input Details:

Here are the detailed input fields required for posting a score:

IUserLeaderboard Interface:

  • userId: string (Mandatory) Unique identifier for the user.

  • username: string (Mandatory) The username of the user.

  • displayName: string (Mandatory) The display name of the user.

IItemsPostScore Interface:

  • score: number (Mandatory) The score to be posted to the leaderboard. This score must be greater than 0.

Important: The behavior of the postScore method depends on the updateScoreStrategy of the leaderboard:

  • If the updateScoreStrategy is ACCUMULATE, the score for each user will be accumulated with every update.
  • If the updateScoreStrategy is OVERWRITE, the user's score will be updated to reflect the latest value and will overwrite the existing value.

Step 3: Post the Score

Once you have defined the score parameters, you can post the score by calling the postScore method on the LeaderboardManager instance.

const postScoreResp = await leaderboardManager.postScore(postNewScoreParams);
console.log('Post leaderboard response:', postScoreResp);

Output Details:

The response from the postScore method will include the following fields:

IPostScoreResp Interface:

  • id: number The unique identifier for the posted score.

  • score: number The score that was posted.

  • userId: string The unique identifier of the user.

  • username: string The username of the user.

  • leaderboardId: number The identifier of the leaderboard to which the score was posted.

  • period: number The period during which the score was posted.

  • updatedAt: string The timestamp when the score was last updated.

  • createdAt: string The timestamp when the score was created.

Summary

By following these steps, you can successfully post a score to a leaderboard using the @myria/leaderboard-ts-sdk. This process involves initializing the LeaderboardManager, defining the score parameters, and invoking the postScore method to submit the score.

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