Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.5 kB
22
Indexable
Never
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.Samples;
using PlayFab;
using System.Collections.Generic;
using PlayFab.AdminModels;

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

            // Read the request body and deserialize to custom class
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

            dynamic args = context.FunctionArgument;

            var message = $"Hello {context.CallerEntityProfile.Lineage.MasterPlayerAccountId}!";
            log.LogInformation(message);

            dynamic inputValue = null;
            if (args != null && args["itemid"] != null)
            {
                inputValue = args["itemid"];
            }

            // // Authenticate PlayFab API request
            // PlayFabAuthenticationContext authenticationContext = PlayFabContext.GetAuthenticationContext();
            // if (!authenticationContext.IsValid)
            // {
            //     log.LogError("Invalid authentication context");
            //     return new UnauthorizedResult();
            // }


            // Get player inventory
            PlayFab.AdminModels.GetUserInventoryRequest inventoryRequest = new PlayFab.AdminModels.GetUserInventoryRequest
            {
                PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,
            };
            PlayFabResult<PlayFab.AdminModels.GetUserInventoryResult> inventoryResult = await PlayFabAdminAPI.GetUserInventoryAsync(inventoryRequest);
            if (inventoryResult.Error != null)
            {
                log.LogError($"Failed to get player inventory: {inventoryResult.Error.ErrorMessage}");
                return new StatusCodeResult((int)inventoryResult.Error.HttpCode);
            }

            // Find the expiration date of the item in the player's inventory
            DateTime? expirationDate = null;
            foreach (PlayFab.AdminModels.ItemInstance item in inventoryResult.Result.Inventory)
            {
                if (item.ItemId == inputValue)
                {
                    expirationDate = item.Expiration;
                    break;
                }
            }
            if (expirationDate == null)
            {
                log.LogError($"Item {inputValue} not found in player inventory");
                return new NotFoundResult();
            }

            // Update custom store data with player name and expiration date
            PlayFab.AdminModels.UpdateStoreItemsRequest storeItemsRequest = new UpdateStoreItemsRequest
            {
                StoreId = "custom_store",
                CatalogVersion = "my_catalog",
                MarketingData = new PlayFab.AdminModels.StoreMarketingModel
                {
                    DisplayName = "Custom Store",
                },
                Store = new List<PlayFab.AdminModels.StoreItem>
        {
            new PlayFab.AdminModels.StoreItem
            {
                ItemId = inputValue,
                CustomData = JsonConvert.SerializeObject(new
                {
                    playerName = context.CallerEntityProfile.DisplayName,
                    expirationDate = expirationDate.Value.ToString("yyyy-MM-ddTHH:mm:ssZ"),
                }),
            },
        },
            };
            PlayFabResult<UpdateStoreItemsResult> storeItemsResult = await PlayFabAdminAPI.UpdateStoreItemsAsync(storeItemsRequest);
            if (storeItemsResult.Error != null)
            {
                log.LogError($"Failed to update store items: {storeItemsResult.Error.ErrorMessage}");
                return new StatusCodeResult((int)storeItemsResult.Error.HttpCode);
            }

            // Return success response
            return new OkResult();
        }
    }
}