question

julianmsmith1 avatar image
julianmsmith1 asked

Limited Edition Item and Search For How Many are in Stock

Hey guys,

I've been searching for the past couple of days and I just can't find an answer. I am looking to have (I assume 2 seperate scripts) where with one you could purchase a limited edition catalog item and then in the other you could search for the name of the item and get the result of how many are left in stock. If anyone has any code examples that would be amazing! Thanks in advance!

Player DataAccount ManagementdataPlayer InventoryCharacter Data
10 |1200

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

1 Answer

·
Made Wang avatar image
Made Wang answered

Currently, limited edition items cannot be purchased directly with virtual currency, but you can grant items to the player via GrantItemsToUser. Then subtract the corresponding amount of virtual currency via SubtractUserVirtualCurrency. You can implement this function on Azure Function Cloud Script.

For the remaining number of limited edition items, you can get it via CheckLimitedEditionItemAvailability, you can also call this API on Azure Function Cloud Script.

You can refer to the code below.

// Azure Function Cloud Script
        [FunctionName("Test")]
        public static async Task<IActionResult> Test(
             [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
             ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());
            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);
            var adminApi=new PlayFabAdminInstanceAPI(settings);

            string Itemid=context.FunctionArgument["Itemid"];
            string VCtype=context.FunctionArgument["VCtype"];
            int Price=context.FunctionArgument["Price"];

            var CheckLimitedEditionItemAvailabilityRequest=new CheckLimitedEditionItemAvailabilityRequest()
            {
                ItemId=Itemid
            };
            var result=await adminApi.CheckLimitedEditionItemAvailabilityAsync(CheckLimitedEditionItemAvailabilityRequest);
            if(result.Result.Amount<=0)
            {
                return new OkObjectResult(new{
                    message="No remaining."
                });
            };

            var GrantItemsToUserRequest=new GrantItemsToUserRequest()
            {
                ItemIds=new List<string>(){
                    Itemid
                },
                PlayFabId=context.CallerEntityProfile.Lineage.MasterPlayerAccountId
            };
            await serverApi.GrantItemsToUserAsync(GrantItemsToUserRequest);

            var SubtractUserVirtualCurrencyRequest=new PlayFab.ServerModels.SubtractUserVirtualCurrencyRequest()
            {
                Amount=Price,
                PlayFabId=context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
                VirtualCurrency=VCtype
            };
            await serverApi.SubtractUserVirtualCurrencyAsync(SubtractUserVirtualCurrencyRequest);

            return null;
        } 
//Client
    public void Purchase()
    {
        PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest
        {
            FunctionName = "Test",
            FunctionParameter = new Dictionary<string, object>
            {
                {"Itemid"," " },
                {"VCtype"," " },
                {"Price",  }
            }
        },
        (result) =>
        {
            Debug.Log("Execute Success");
            Debug.Log(result.FunctionResult);
        },
        (error) =>
        {
            Debug.LogError(error.GenerateErrorReport());
        });
    }
3 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.

julianmsmith1 avatar image julianmsmith1 commented ·

Hey thank you for the quick response! This looks great but I think I may need to set some more things up before I use it. When I tried to add the first one to the cloud script I kept getting this error:

SyntaxError: Unexpected identifier at Script [3]:324:16 -> public static async Task<IActionResult> Test(

Is there a variable I should change?

Thanks again!

0 Likes 0 ·
Made Wang avatar image Made Wang julianmsmith1 commented ·

The above code is Cloud Script implemented in Azure Function instead of Cloud Script (Legacy), please refer to PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Docs to get started.

0 Likes 0 ·
julianmsmith1 avatar image julianmsmith1 Made Wang commented ·

Thank you! I'll go check that out.

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.