question

matias avatar image
matias asked

Leaderboard and prices

Hi i know that i can sent a prices to players in the leadeboard option. But i wanted to also send a custom item, lets said i end at 3rd place i want to send a "Trophy" to that player with information:
- Leaderboard Name and Date

- Position in which he ended the season (in this case 3rd place)

Is this possible with cloudscript or something?

Leaderboards and Statistics
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

matias avatar image
matias answered

Hi @Rick Chen it was very useful but i'm running with a problem:

Here is my Cloud Script code to get the position in the leaderboard:

handlers.LeaderboardInfo = function  (args, context)
{
    var result = server.GetLeaderboardAroundPlayer(
    { 
        PlayFabID: currentPlayerId,
        StatisticName: args.Leaderboardname,
        MaxResultsCount: 1
    });
    return result.Leaderboard[0].Position;
}

And The Code i Use to Add an item to the player:

handlers.GiftTrophy= function (args, context)
{
    
    var items = [];
    
    items.push("One");
    
    var itemCustomData = server.GrantItemsToUser(
    {
        PlayFabId: currentPlayerId,
        CatalogVersion: "Secondary",
        ItemIds : items
    });
    
    for(var key in itemCustomData.ItemIds)
    {
        var itemInstance = key;
    
        var dataPayload = {};
        var keyString = "Position";
        dataPayload[keyString] = LeaderboardInfo(args.Leaderboardname);
        var itemCustomData = server.UpdateUserInventoryItemCustomData(
        {
            PlayFabId: currentPlayerId,
            ItemInstanceId : itemInstance,
            Data : dataPayload
        });
    }
    
    return("Updated ");
}


I don't know if i'm doing something wrong, the item is being added correctly but the custom data is not being added. Am i missing something?

I've also made a prize table which runs the "Gift Trophy" function

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rick Chen avatar image Rick Chen ♦ commented ·

Is there any error message? In your 2nd code snippet, if you replace line 19-21 with the following

var dataPayload = {"Position":3};

Will it solve the issue?

0 Likes 0 ·
matias avatar image matias Rick Chen ♦ commented ·

No it didn't but i found how to fix it.

First error was that i was using the wrong function for the leader board:


function UserLeaderboardPosition (Name, User)
{
    var result = server.GetLeaderboardAroundUser(
    { 
        PlayFabID: User,
        StatisticName: Name,
        MaxResultsCount: 1
    });
    var results = result.Leaderboard;
    var position = results[0].Position;
    return (position + 1);
}

Then i was trying to access the wrong variable and using a "for" was not working correctly so i change it to:

var results = itemCustomData.ItemGrantResults;
    var itemInstance = results[0].ItemInstanceId;
    
    var positionLeaderBoard = UserLeaderboardPosition(args.Leaderboardname, currentPlayerId);
    var dataPayload = {};
    
    var keyString = "Position";
    dataPayload[keyString] = positionLeaderBoard;
    var keyStringTwo = "LeadeboardName";
    dataPayload[keyStringTwo] = args.Leaderboardname;
    
    var itemCustomData = server.UpdateUserInventoryItemCustomData(
    {
        PlayFabId: currentPlayerId,
        ItemInstanceId : itemInstance,
        Data : dataPayload
    });

(the code is the second half since i have character limits.

Thanks for your help!

0 Likes 0 ·
Rick Chen avatar image
Rick Chen answered

Yes, you can write a CloudScript that 1) Grant the item “Trophy” to the player with GrantItemsToUser API, 2) Then edit the custom data of the “Trophy” with UpdateUserInventoryItemCustomData API.

Then you can set up a Prize Table to execute this CloudScript for the 3rd place player.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.