question

Philipp Gehring avatar image
Philipp Gehring asked

Getting player data in an azure function

I have some difficulties in operating on player data in an azure function.

Here is a simple exemple:

public static class ServerAccountManagement
    {
        [FunctionName("CreateServerAccount")]
        public static async Task<IActionResult> RunCreateServerAccount(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
            ILogger log)
        {
            var context = await FunctionContext<dynamic>.Create(req);

            //Check if that player already has an account
            log.LogInformation($"Server account call made by: {context.CurrentPlayerId}|{context.CallerEntityProfile.DisplayName}|{context.CallerEntityProfile.EntityChain}");

            var request = new PlayFab.AdminModels.GetUserDataRequest
            {
                PlayFabId = context.CurrentPlayerId,
                Keys    = new List<string> { "owned_game_server" }
            };

            var serverApi = new PlayFab.PlayFabAdminInstanceAPI(context.ApiSettings, context.AuthenticationContext);
            var result = await serverApi.GetUserReadOnlyDataAsync(request);

            log.LogInformation(result.Error.ErrorMessage);

            return new OkObjectResult("");
        }
    }

If i call this via either the game manager on a player or from the PlayFab sdk, the user data request fails with:
"

master_player_account 300923418170631384 not found in namespace 3268C20C0AF90B24"

while the initial information logs:
"

42D184124A824D8||title_player_account!3268C20C0AF90B24/44B9F/28B30D653E05AC40/42D184124A824D8/"

which also seems to be missing the display name.
I have been following the github samples but i am not quite sure if i am using the correct FunctionContext or data types.

10 |1200

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

Citrus Yan avatar image
Citrus Yan answered

GetUserData requires the player’s PlayFabId, which is the master_player_account id, however, in your code, line 15, “context.CurrentPlayerId” refers to the title_player_account id, please take a look at its definition:

CurrentPlayerId = contextInternal.CallerEntityProfile.Lineage.TitlePlayerAccountId

https://github.com/PlayFab/CSharpSDK/blob/d61ba9f23bf459e6b766bc3fd6b0945ede9ef553/Plugins/CloudScript/source/PlayFabFunctionContexts.cs#L64

Therefore, please use the master player id instead (change line 15 to the following):

PlayFabId= context.CallerEntityProfile.Lineage.MasterPlayerAccountId,

And, regarding the display name issue, it’s because that the player entity doesn’t have a display name yet, and currently there is no API to set it, for more details, please see: https://community.playfab.com/questions/41170/cant-get-display-name-through-entity-api.html

That being said, please use GetPlayerProfile to get the player’s profile (the classic model) and retrieve its display name from it(if it has one).

In addition, the context FunctionContext used in our sample is still in beta mode and is subject to change at any time, please see the following code:

https://github.com/PlayFab/CSharpSDK/blob/d61ba9f23bf459e6b766bc3fd6b0945ede9ef553/Plugins/CloudScript/source/PlayFabFunctionContexts.cs#L2

Instead, we provide several context models for developers to use when dealing with CloudScript using Azure Functions, please check out this tutorial:

https://docs.microsoft.com/en-us/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context

10 |1200

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

Elvis avatar image
Elvis answered

Thanks for the answer @Citrus Yan, this solved my issue.

PlayFabId= context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
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.