question

removed avatar image
removed asked

"Player Statistic with the specified name already exists" but I cannot see that statistic in the Dashboard

I'm trying to create a statistic for a player if it doesn't exist. (Ideally, I'd like all players to be initialized with the statistic on first login but not sure how to do that.)

The problem is that CreatePlayerStatisticDefinition returns "Player Statistic with the specified name already exists", but when I go to my dashboard under the appropriate player, I can't see that statistic. Are statistic definitions and statistics different?

       private void OnPlayerStatCreated(CreatePlayerStatisticDefinitionResult createPlayerStatisticDefinitionResult, string playerPlayFabId)
        {
            Debug.LogFormat("Fetching wins.");
            Action<GetPlayerStatisticsResult> onStatsFetched = result =>
            {
                foreach (var resultStatistic in result.Statistics)
                {
                    Debug.Log("Retrieved " + resultStatistic);
                }
                var updateRequest = new UpdatePlayerStatisticsRequest
                {
                    PlayFabId = playerPlayFabId,
                    Statistics = new List<StatisticUpdate>
                    {
                        new StatisticUpdate {StatisticName = MagicIntsAndStringsForSocialGaming.Wins, Value = result.Statistics.First(x=>x.StatisticName == MagicIntsAndStringsForSocialGaming.Wins).Value + 1}
                    },
                };
                PlayFabServerAPI.UpdatePlayerStatistics(updateRequest, statisticsResult => { }, ConnectionErrorHandler.HandlePlayFabError);
            };
            Debug.LogFormat("Updating wins.");
            var fetchRequest = new GetPlayerStatisticsRequest
            {
                PlayFabId = playerPlayFabId
            };
            PlayFabServerAPI.GetPlayerStatistics(fetchRequest, onStatsFetched, ConnectionErrorHandler.HandlePlayFabError);
        }


        // TODO: Lots of refactorings!!!!
        private void UpdateWins(Player player)
        {
            Debug.LogFormat("Creating wins for {0}.", player.Number);
            var id = player.PlayFabId;
            PlayFabAdminAPI.CreatePlayerStatisticDefinition(new CreatePlayerStatisticDefinitionRequest() { AggregationMethod = StatisticAggregationMethod.Sum, StatisticName = MagicIntsAndStringsForSocialGaming.Wins }, result => { OnPlayerStatCreated(result, id);}, ConnectionErrorHandler.HandlePlayFabError);
        }
apisAccount Management
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.

removed avatar image removed commented ·

I'm not able to edit but "UpdatWins" is called first.

0 Likes 0 ·

1 Answer

·
brendan avatar image
brendan answered

The CreatePlayerStatisticDefinition call creates the statistic for the title, not the player. All you have to do is use UpdatePlayerStatistic to set the specific value for the player.

And one important note: If this is the client code, you have a problem - the client must not have access to the Admin (or Server) API set. Doing so means that you're giving the client the Secret Key for your title, which would allow a hacker to do anything that Secret Key allows to your title, including deleting all player accounts, changing your Title Data, etc.

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

removed avatar image removed commented ·

Thanks Brendan. I figured it out. Is there a way to see all Statistics Definition through the Dashboard?

This is all server code. This was really rough code and I changed it to this:

        private static void UpdateMatchesPlayed(Player player, string winsOrLossesKey)
        {
            var updateRequest = new UpdatePlayerStatisticsRequest
            {
                PlayFabId = player.PlayFabId,
                Statistics = new List<StatisticUpdate>
                {
                    new StatisticUpdate {StatisticName = winsOrLossesKey, Value = 1}
                },
            };
            PlayFabServerAPI.UpdatePlayerStatistics(updateRequest, statisticsResult => { },
                ConnectionErrorHandler.HandlePlayFabError);
        }

0 Likes 0 ·
brendan avatar image brendan removed commented ·

Yes, you can find all the stats for your game in the Leaderboards tab. You can also query for all stats via the Admin API call GetPlayerStatisticDefinitions.

1 Like 1 ·

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.