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.
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.
Answer by Citrus Yan · Sep 12, 2019 at 08:35 AM
@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 });
Answer by Citrus Yan · Sep 11, 2019 at 08:18 AM
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 }); }
Answer by David Jones · Sep 11, 2019 at 04:35 PM
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.
Answer by David Jones · Sep 12, 2019 at 12:11 PM
Yes! Thank you so much, JavaScript is really new to me!
Cloudscript update multiple statistics 2 Answers
Playfab cloudcripts Wondering 2 Answers
"Internal Server Error" in GetFriendLeaderboard API 0 Answers
Invoke Admin API from Server. 1 Answer