Untitled

 avatar
unknown
plain_text
2 years ago
2.9 kB
9
Indexable
using System;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using PlayFab;
using PlayFab.CloudScriptModels;
using PlayFab.ServerModels;

public static class StoreCityDataFunction
{
    [FunctionName("UpdateCustomStoreData")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        // Get the PlayFab ID and city expiration date from the request body
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        string playFabId = data?.playFabId;
        DateTime cityExpirationDate = data?.cityExpirationDate;

        if (string.IsNullOrEmpty(playFabId) || cityExpirationDate == null)
        {
            return new BadRequestObjectResult("Please pass a valid PlayFab ID and city expiration date in the request body");
        }

        // Get the player's display name from PlayFab
        var getPlayerProfileRequest = new GetPlayerProfileRequest
        {
            PlayFabId = playFabId,
            ProfileConstraints = new PlayerProfileViewConstraints
            {
                ShowDisplayName = true
            }
        };

        var getPlayerProfileResult = await PlayFabServerAPI.GetPlayerProfileAsync(getPlayerProfileRequest);

        if (getPlayerProfileResult.Error != null)
        {
            return new BadRequestObjectResult($"Error getting player profile from PlayFab: {getPlayerProfileResult.Error.ErrorMessage}");
        }

        string displayName = getPlayerProfileResult.Result.PlayerProfile.DisplayName;

        // Update the custom store data for the player
        var updateStoreItemsRequest = new UpdateStoreItemsRequest
        {
            CatalogVersion = "Items", // Replace with the name of your catalog
            StoreId = "myStore", // Replace with the ID of your store
            Store = new StoreItem
            {
                ItemId = "myCity", // Replace with the ID of the city item in your catalog
                CustomData = new { displayName = displayName, cityExpirationDate = cityExpirationDate.ToString() }
            }
        };

        var updateStoreItemsResult = await PlayFabServerAPI.UpdateStoreItemsAsync(updateStoreItemsRequest);

        if (updateStoreItemsResult.Error != null)
        {
            return new BadRequestObjectResult($"Error updating custom store data in PlayFab: {updateStoreItemsResult.Error.ErrorMessage}");
        }

        return new OkObjectResult("Custom store data updated successfully");
    }
}
Editor is loading...