Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by David Jones · Sep 10, 2019 at 09:14 PM · CloudScriptLeaderboards and Statistics

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

People who like this

0 Show 1
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image David Jones · Sep 10, 2019 at 10:23 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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
});
Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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

});

}

1.png (12.6 kB)
Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by David Jones · Sep 12, 2019 at 12:11 PM

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

Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    3 People are following this question.

    avatar image avatar image avatar image

    Related Questions

    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

    Tags does not show in GetFriendLeaderboard 1 Answer

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges