question

duartedd avatar image
duartedd asked

Azure Functions and how to configure

Hello

just wondering if i can get some help on this as i am going through the docs and samples and am stuck

I am trying to wrap my head around azure functions and trying to create a simple call to azure functioins that will return a random drop table result string

here is the code for azure functions which seems to be alright

    public static class GetCloudScript
    {
        [FunctionName("GetRandomDropItem")]
        public static async Task<dynamic> GetRandomDropItem(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, ILogger log)
        {
            /* Create the function execution's context through the request */
            var context = await FunctionContext<dynamic>.Create(req);
            var args = context.FunctionArgument;
            string tableid;
            if (args.tableid != null)
                tableid = args.tableid;
            else
                tableid = "RandomBadDropTable";
                   
            /* Create the request object through the SDK models */
            var request = new EvaluateRandomResultTableRequest()
            {
                TableId = args.tableId
            };
            /* Use the ApiSettings and AuthenticationContext provided to the function as context for making API calls. */
            var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext);


            /* The PlayFabServerAPI SDK methods provide means of making HTTP request to the PlayFab Main Server without any 
             * extra code needed to issue the HTTP requests. */
            return await serverApi.EvaluateRandomResultTableAsync(request);
        }


    } 

And I am trying to create a function for azurefunction calls and i am trying to use the code in the tictactoe demonstration to create this within unity:

    public static IEnumerator ExecuteRequest(string FunctionName)
    {
        var request = new ExecuteFunctionRequest
        {
            FunctionName = FunctionName,
            FunctionParameter = new PlayFabIdRequest
            {
                PlayFabId = PlayerId
            },
            AuthenticationContext = new PlayFabAuthenticationContext
            {
                EntityToken = Player.EntityToken
            }
        };


        PlayFabCloudScriptAPI.ExecuteFunction(request,
            (result) =>
            {
                ExecutionCompleted = true;
                AIMoveResult = PlayFabSimpleJson.DeserializeObject<TicTacToeMove>(result.FunctionResult.ToString());
            },
            (error) =>
            {
                throw new Exception($"Could: {error.ErrorMessage}, Code: {error.HttpCode}");
            });


        yield return WaitForExecution();
    }

I have the playfabid from the login - but where does this entitytoken come from?

and I am not sure how the below relates to the below from the docs to the above

// Models via ExecuteFunction API
public class FunctionExecutionContext<T>
{
    public PlayFab.ProfilesModels.EntityProfileBody CallerEntityProfile { get; set; }
    public TitleAuthenticationContext TitleAuthenticationContext { get; set; }
    public bool? GeneratePlayStreamEvent { get; set; }
    public T FunctionArgument { get; set; }
}

public class FunctionExecutionContext : FunctionExecutionContext<object>
{
}

then there is the fact that i would have to return via an ienumerator which isnt gonna be so nice and clean - currently the original cloudscript i call via a normal void method and i just raise a callback with the appropriate info provide to a listener and a way it goes....any issues with that - i am thinking not.

If you can help me wrap my head around what i need to put in the executefunction request then i think i can figure out the rest...Also the original cloudscript function automatically provides the playfabid - is this not the case - what automatically is passed ? - like is playstream events true or false by default?

thanks!

Daniel

10 |1200

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

Rick Chen avatar image
Rick Chen answered

>>where does this entitytoken come from?

You can get the EntityToken in the login result, it is result.EntityToken.EntityToken. However, the EntityToken in the code snippet 2, line 12 is not necessary. After the player successfully logs in, the SDK will automatically assign its AuthenticationContext for subsequent API calls. Therefore you can remove from line 10 to line 13. You could also remove the PlayFabId in line 8, it is within the context that is passed to the Azure Function.

>>what automatically is passed ?

The information passed to Azure Function is within the context. The context varies with the way of calling Azure Function. Please refer to: https://docs.microsoft.com/en-gb/gaming/playfab/features/automation/cloudscript-af/cloudscript-af-context. You can check the structure of the context and see what information is passed in the context. For example, in your code snippet 3, that is the context passed via ExecutionFunction API, it includes the caller’s profile, title authentication context (title id, title EntityToken) and function argument.

10 |1200

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

duartedd avatar image
duartedd answered

Ok Great! that makes it easier - seems like its pretty much the same as the executecloudscript api as for the original cloudscript - as playfabid is also passed automatically (guessing the same variable is used - and it passes it in the args like cloudscript as well in a dynamic object ?

eg ....args.PlayfabId - i believe - does that mean if i needed that the token is also in the args too?

eg... args.EntityToken

thanks again!

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.

Rick Chen avatar image Rick Chen ♦ commented ·

It is different from CloudScript. In Azure Function, the PlayFabId is passed by the context. For example, if the context type is FunctionExecutionContext, the PlayFabId will be context.CallerEntityProfile.Lineage.MasterPlayerAccountId. Usually, the title EntityToken is used when calling the Entity APIs, it would be context.TitleAuthenticationContext.EntityToken. In your code snippet 1, I notice that the context type is FunctionContext, not FunctionExecutionContext. You can check the structure of this class and see how to get the PlayFabId and the title EntityToken.

0 Likes 0 ·
duartedd avatar image
duartedd answered

o okay i see - it is in context.currentPlayerID actually didnt realize that

   var context = await FunctionContext<dynamic>.Create(req);
            var args = context.FunctionArgument;
      context.CurrentPlayerId

that seems to exist so i could use that if needed - nice!

as for the entity token i am not even sure what it is and what i would use it for but i am guessing there is some sort of guide that playfab has that i will look for on that

thanks again for all the info!

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.

Rick Chen avatar image Rick Chen ♦ commented ·

There are several types of APIs in PlayFab: Entity APIs, Client APIs, Server APIs and etc. Different types of APIs may require different authentication context. EntityToken is needed to call those Entity APIs. For the structure of the APIs, please refer to: https://docs.microsoft.com/en-us/gaming/playfab/features/data/entities/entity-api-restructure-upgrade-tutorial

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.