question

contact-19 avatar image
contact-19 asked

Cloudscript speed

Hi,

Ive struggled to get a cloudscript working as most of the examples dont seem to work with the .net 6 and Playfab v4 but anyway, Ive now got my first cloudscript working and accessible from inside my Unity game, but they are taking around 1-2 seconds just to subtract 1 item from the inventory which makes me wonder if maybe my codes just a bit too bloated.

Here's my cloud script code:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PlayFab.EconomyModels;
using PlayFab.Samples;
using PlayFab.ServerModels;
using PlayFab.Json;
using System.Collections.Generic;
using PlayFab.DataModels;
using System.Net.Http;
using System.Net.Http.Headers;
using PlayFab.CloudScriptModels;
using PlayFab;
using PlayFab.AzureFunctions;

namespace TestNamespace.Blah
{

    public static class OpenLootbox
    {
        [FunctionName("OpenLootbox")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var serverSettings = new PlayFab.PlayFabApiSettings()
            {
                TitleId = "XXXXX",
                DeveloperSecretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            };
            
            var authAPI = new PlayFabAuthenticationInstanceAPI(serverSettings);
            var titleResponse = await authAPI.GetEntityTokenAsync(new PlayFab.AuthenticationModels.GetEntityTokenRequest());
            var title = titleResponse.Result.Entity;
            var titleToken = titleResponse.Result.EntityToken;

            var titleAuthContext = new PlayFabAuthenticationContext();
            titleAuthContext.EntityToken = titleToken;
            
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());         

            var economyApi = new PlayFabEconomyInstanceAPI(serverSettings, titleAuthContext);

            var lootboxItemRef = new PlayFab.EconomyModels.InventoryItemReference
            {
                Id = "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXXXX",
                StackId = "default"
            };

            PlayFab.EconomyModels.EntityKey entity = new PlayFab.EconomyModels.EntityKey { Id = context.CallerEntityProfile.Entity.Id, Type = context.CallerEntityProfile.Entity.Type };

            var request = new SubtractInventoryItemsRequest(){
                Amount = 1,
                Entity = entity,
                Item = lootboxItemRef
            };
            return await economyApi.SubtractInventoryItemsAsync(request);
        }
    }
}


My understanding of the entity tokens is still rather limited, do I need to call this GetEntityTokenAsync method everytime I run a cloud script? I would of assumed the entity token could be included in the request from Unity?

await authAPI.GetEntityTokenAsync(new PlayFab.AuthenticationModels.GetEntityTokenRequest());

Thanks,

In-Game Economy
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.

drew avatar image drew commented ·

this helped me figure out how to use the new economy stuff, thanks for sharing.

But how did you implement adding the items? Can you share your code here? I only see the subtraction of the lootbox, but not the adding of the items that wer opened.

There is very little Azure Function PlayFab SDK code out in the wild, so this would help a lot.

thanks so much,

0 Likes 0 ·

1 Answer

·
Xiao Zha avatar image
Xiao Zha answered

Since the default authentication context in Cloud Script/Azure Function is title level, you don’t need to call GetEntityTokenAsync method in your code to get the title entity token. You can use context.TitleAuthenticationContext.EntityToken to obtain the title entity token.

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.