question

sammy89 avatar image
sammy89 asked

how to get stats from player

Are there only 2 ways to get for example player highscore? either direct from the player (which you don't recommend) or with cloud scripts. Is there another way? Can I use playstream?

10 |1200

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

Gosen Gao avatar image
Gosen Gao answered

We do not recommend clients to update statistics, but it is normal to use the Client API GetPlayerStatistics to get it. In the common scenario, there is no need to call a CloudScript to get the statistics. If you are asking about updating, then you are correct, we recommend using CloudScript to update the statistics. I don't quiet understand what you mean using Playstream, if you want to use Rules to interact with Playstream events to update statistics, you can refer to Actions and rules quickstart.

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.

sammy89 avatar image sammy89 commented ·

I'm new to all this. Can you give me a code example how to update the player statistics? I'm finding cloudscripts hard to get started with the whole azur part of it. I also can't get m head around setting up rules in playstream. Could you walk me through it or direct me to a step by step tutorial? your own docs are hard to follow.

0 Likes 0 ·
Gosen Gao avatar image
Gosen Gao answered

Please follow Quickstart: Writing a PlayFab CloudScript using Azure Functions to implement the Azure Function, and here is the code to update player's statistics for reference. You can execute this function by calling API ExecuteFunction.

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PlayFab;
using PlayFab.Samples;
using PlayFab.ServerModels;

namespace Company.Function
{
    public static class HttpTrigger1
    {
        [FunctionName("UpdatePlayerStatistics")]
        public static async Task<PlayFabResult<UpdatePlayerStatisticsResult>> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            var settings = new PlayFabApiSettings
            {
                TitleId = context.TitleAuthenticationContext.Id,
                DeveloperSecretKey = "Your Secretkey Here"
            };
            var serverApi = new PlayFabServerInstanceAPI(settings);
            var request = new UpdatePlayerStatisticsRequest{
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                Statistics = new System.Collections.Generic.List<StatisticUpdate>{
                    new StatisticUpdate{
                        StatisticName = "Level",
                        Value = 10
                    }
                }
            };
            return await serverApi.UpdatePlayerStatisticsAsync(request);
        }
    }
}

As for the Rules, please follow Actions and rules quickstart to set up a rule to interact with Playstream event.

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.