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, 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(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.postPlayerScore(postNewScoreParams);
console.log('Post leaderboard response:', postScoreResp);

Output Details:

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

IPostScoreItemResp 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.

  • displayName: string The display name of the user.

IPostScoreResponse Interface:

  • success[]: IPostScoreItemResp[] An array of IPostScoreItemResp objects representing the post score records that were successfully processed.

  • failed[]: IPostScoreItemResp[] An array of IPostScoreItemResp objects containing the post score items that failed to process.

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.