question

MerryChristmas avatar image
MerryChristmas asked

How do Azure Function call Playfab UpdateUserData ?

I am using CloudScript using Azure Functions, I use ExecuteFunction in my game server to call Azure Function.

In Playfab cloudscript, I can use server.UpdateUserData(update_request) to update data directly, but In Azure Functions, I can only get the Function parameters, which is passed by ExecuteFunction, I don't know how to access "server" to update user data directly.

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

You can follow -- Quickstart: Writing a PlayFab CloudScript using Azure Functions to learn how to create an Azure Function, and here is the code for your reference.

[FunctionName("UpdateUserData")]
        public static async Task<dynamic> UpdateUserData(
            [HttpTrigger(AuthorizationLevel.Function,"post", Route = null)] HttpRequest req, ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            dynamic args = context.FunctionArgument;
            string key = args["key"];
            string value =args["value"];
            var updateUserDataRequest = new UpdateUserDataRequest
            {
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                Data = new Dictionary<string, string>()
                {
                     { key, value }
                }
            };
            var settings = new PlayFabApiSettings
            {
                TitleId = context.TitleAuthenticationContext.Id,
                DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process)
            };
            var authContext = new PlayFabAuthenticationContext
            {
                EntityToken = context.TitleAuthenticationContext.EntityToken
            };
            var serverApi = new PlayFabServerInstanceAPI(settings,authContext);
            return await serverApi.UpdateUserDataAsync(updateUserDataRequest);
        }
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

May I know why you want to call UpdateUserData with Azure Functions? Since your game has a dedicated server, you can call ServerAPI UpdateUserData directly in your server. There should be no need to do this with Azure Functions.

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.

MerryChristmas avatar image MerryChristmas commented ·

Hi, @Gosen Gao. Thanks for reply.

I am a mod author of Dota2, the dedicated server is dota2 server, so I can only call ServerAPI when the game isn't over. I cannot call ServerAPI after all the player disconnected(the dedicated server stop working).

And I have an Azure Function which create a payment order that can be completed any time(60s to expired), even the game is over. So I want to make sure that players can get their items when they pay the trade order no matter the game is over or not.

It seems that I have to update user data in Azure Functions, otherwise the players cannot get items when they complete payment order.

0 Likes 0 ·

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.