question

David Jones avatar image
David Jones asked

Cloudscript Updating Statistics

Hi, I'm trying to update statistics in cloud script based on the result of the game. Im passing the game results into cloud script as an object parameter then I can update the stats based on these values. My problem is saving these value into an array or object that the UpdatePlayerStatistics can understand. For a basic example, if they won the match then I do

var statsRequest = {
    PlayFabId: currentPlayerId
};
var statsResult = server.GetPlayerStatistics(statsRequest);
var winsKVP = statsResult.Statistics[0];
var xpKVP = statsResult.Statistics[1];


addingStats = {};
addingStats[xpKVP.Key] = 200;
addingStats[winsKVP.Key] = 1;

var updateStatistics = server.UpdatePlayerStatistics({
    PlayFabId: currentPlayerId,
    Statistics: addingStats
});

//The issue I'm getting is invalid input parameters so I just need a better way to be able to save these dynamic values and send the stat request with appropriate values.
//I tried searched for a better way but had no luck.
CloudScriptLeaderboards and Statistics
1 comment
10 |1200

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

David Jones avatar image David Jones commented ·

A reason I do it this way is because I'd like to do the calculations for the amounts in different functions or update different stats for different reasons, that way I can build a list of the changed stats instead of doing different update statistic calls and have one to handle them all.

0 Likes 0 ·
Citrus Yan avatar image
Citrus Yan answered

@David Jones Hi David, I am replying here due to character limits

>> Let's say I have a statistic called "Friend Wins", if they lose would I just update it with 0?

No, according to your description, you might want to just leave it there and don’t add "Friend Wins" to the addingStats array for future statistic update.

>> Or is there a way I can do something like addingStats.Add(); ?

For array operations in Javascript, there is no such method called “Add”. I believe you are referring to “push()” which adds new elements into an array. Anyway, I think the basic flow for you is to 1)do the calculations 2) build a list of the changed statistics based on the results 3) handle statistic changes in one UpdatePlayerStatistics call. Here is the sample code:

//basic flow is like this:


//1) do the calculations


//2)build a list of the changed statistics based on the results 


addingStats = []; //build a empty list that stores statisti KVP 


//for example, construct a statistics KVP like this:
var winsKVP = {
    StatisticName: "winsKVP",
    Value: 200 //you can use a variable representing changed value
};


addingStats.push(statisticKVP); // add it into the list 


//you can construct other KVPs and add them into the list
//it's even better that you calculate all the changes first, then add them into the list using for loops


//3)handle statistic changes in one UpdatePlayerStatistics call
var updateStatistics = server.UpdatePlayerStatistics({
    PlayFabId: currentPlayerId,
    Statistics: addingStats
});
10 |1200

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

Citrus Yan avatar image
Citrus Yan answered

Hi David,

The structure of “Statistics” parameter in server.UpdatePlayerStatistics API is a list of type StatisticUpdate, which looks like this:

The "Version" parameter is optional. And also note that in line 5 and 6, you will probably get a wrong statistic simple get them by the index since they are not returned in order. Use find() to retrieve instead.

I made some changes to the example code you provided and it worked in my title, you can give it a try.

Here is the code:

handlers.updateStatisticsTest
= function(args,context) {

var statsRequest = {

PlayFabId: currentPlayerId

};

var statsResult =
server.GetPlayerStatistics(statsRequest);

var winsKVP =
statsResult.Statistics.find(s => s.StatisticName === "winsKVP");

var xpKVP =
statsResult.Statistics.find(s => s.StatisticName === "xpKVP");

addingStats = [

    {
     
StatisticName: "winsKVP",     
Value: 200

    },
    {
       
StatisticName: "xpKVP",     
Value: 1
    }
    ];

var updateStatistics =
server.UpdatePlayerStatistics({

    PlayFabId: currentPlayerId,

    Statistics: addingStats

});

}

1.png (12.3 KiB)
10 |1200

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

David Jones avatar image
David Jones answered

Thank you so much, however the issue with the solution that I see is that if they lose the game I don't want to update certain stats. Let's say I have a statistic called "Friend Wins", if they lose would I just update it with 0? Or is there a way I can do something like addingStats.Add(); ? Which just adds a dictionary KVP in C#, so that if they lose I won't even add "Friend Wins" to the array.

&& I did trial and error to find the array values of the stats in statsResult. Your way is definitely better but at the time I couldn't find a better way to do it.

10 |1200

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

David Jones avatar image
David Jones answered

Yes! Thank you so much, JavaScript is really new to me!

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.

Error rendering WebPanel (widgets/consolidation-widget.ftl): org.hibernate.hql.internal.ast.QuerySyntaxException: AvailableConsolidation is not mapped [from AvailableConsolidation up where up.node = :node]