question

anudeep avatar image
anudeep asked

How to get One Statistic through Azure Function, GetPlayerStatisticsAsync

using PlayFab.ServerModels;
using PlayFab.BuffbaerMuses.Models;
using System;

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PlayFab.BuffbaerMuses.Util{
    public class StatisticUtil{
        public static async Task UpdateStatValue(string playFabId, string statName, int deltaValue){
            var apiSettings = new PlayFabApiSettings
            {
                TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process),
                DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
            };

            var serverApi = new PlayFabServerInstanceAPI(apiSettings);

            var currentStatResult = await serverApi.GetPlayerStatisticsAsync(
                new GetPlayerStatisticsRequest{
                    PlayFabId = playFabId,
                }
            );
            
            var newValue = deltaValue;

            var updateStatResult = await serverApi.UpdatePlayerStatisticsAsync(
                new UpdatePlayerStatisticsRequest{
                    Statistics = new List<StatisticUpdate>{
                        new StatisticUpdate{
                            StatisticName = statName,
                            Value = newValue
                        }
                    },
                    PlayFabId = playFabId
                });
        }

        public static async Task CheckStatValue(string playFabId, string statName, int rewardValue){
            var apiSettings = new PlayFabApiSettings
            {
                TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process),
                DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
            };
            var serverApi = new PlayFabServerInstanceAPI(apiSettings);

            var currentStatResult = await serverApi.GetPlayerStatisticsAsync(
                new GetPlayerStatisticsRequest{
                    //OnGetStatistics,
                    //error => Debug.Log(error.GenerateErrorReport())
                    //PlayFabId = playFabId,
                    
                }
            );

            /*if(currentStatResult == "Premium Choices"){
                AddVC(playFabId,50);
            }*/
            /*foreach(var eachStat in currentStatResult){
                switch(eachStat.StatisticName){
                    case "Premium Choices":
                        if(eachStat.Value == 1){
                            //Reward player 50 coins.
                            
                            //serverApi.AddUserVirtualCurrency(playFabId, VirtualCurrency:"BC", ArgumentOutOfRangeException:"50");
                        }
                        //Premium Choices function after checking the condition

                }
            }*/

            
                
            

        }

        void OnGetStatistics(GetPlayerStatisticsResult result){

        }
        public static async Task AddVC(string playFabId,int vc){
            var apiSettings = new PlayFabApiSettings
            {
                TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process),
                DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
            };

            var serverApi = new PlayFabServerInstanceAPI(apiSettings);
            var request = new AddUserVirtualCurrencyRequest{ 
                    PlayFabId = playFabId,
                    VirtualCurrency = "BC",
                    Amount = vc 
                };
                
            var AddUserVirtualCurrencyResult = await serverApi.AddUserVirtualCurrencyAsync(request);
            }

        
    }
}

I am trying to access only one of the statistic to check if it hits my condition.
I am not able to do so. Every I have checked, It's always GetPlayerStatisitcs, but as you can see i am using async. How can i get one statistic value?

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

1 Answer

·
Xiao Zha avatar image
Xiao Zha answered

You can specify a Statistic Name in the GetPlayerStatisticsRequest to get one statistic value.

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

anudeep avatar image anudeep commented ·
if(currentStatResult.StatisticNames[0].Value == 1){
                Debug.Log(currentStatResult.StatisticNames.Value);
            }


How am i supposed to the get the statistic result value from above currentStatResult?

0 Likes 0 ·
Xiao Zha avatar image Xiao Zha anudeep commented ·

As you can see in the documentation, it is StatisticNames of type List. You may correct your code.

1 Like 1 ·
anudeep avatar image anudeep Xiao Zha commented ·

Yes, noticed that and had changed as mentioned.

0 Likes 0 ·
Show more comments
anudeep avatar image anudeep anudeep commented ·
var currentStatResult = await serverApi.GetPlayerStatisticsAsync(
                new GetPlayerStatisticsRequest{
                    StatisticNames = new List<string> {statName},
                    
                    PlayFabId = playFabId,
                    
                }
            );


The above is the code for currentStatResult.

0 Likes 0 ·
anudeep avatar image anudeep commented ·

@Xiao Zha sorry for the inconvienience , but how do i access the result's value though .

if(currentStatResult.StatisticNames[0].Value == 1){
                Debug.Log(currentStatResult.StatisticNames.Value);
            }

0 Likes 0 ·
Xiao Zha avatar image Xiao Zha anudeep commented ·

[Edit]:

You may try this code:

currentStatResult.Result.Statistics[0].Value
0 Likes 0 ·
anudeep avatar image anudeep Xiao Zha commented ·

I have tried that and get this error in return.

error CS1061: 'GetPlayerStatisticsResult' does not contain a definition for 'Statistic' and no accessible extension method 'Statistic' accepting a first argument of type 'GetPlayerStatisticsResult' could be found (are you missing a using directive or an assembly reference?)

0 Likes 0 ·
Show more comments

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.