Untitled

 avatar
unknown
plain_text
2 years ago
6.4 kB
5
Indexable
    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<dynamic> 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());

                PlayFabSettings.staticSettings.DeveloperSecretKey = Environment.GetEnvironmentVariable("PlayFabDeveloperSecretKey");
                PlayFabSettings.staticSettings.TitleId = "77769";
                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"];
                }

                PlayFab.AdminModels.GetStoreItemsRequest storeRequest = new PlayFab.AdminModels.GetStoreItemsRequest
                {
                    CatalogVersion = "Cities",
                    StoreId = "CitiesStore"
                };
                var storeInfo = PlayFabAdminAPI.GetStoreItemsAsync(storeRequest);

                foreach (var item in storeInfo.Result.Result.Store)
                {
                    if (item.ItemId == inputValue.ToString())
                    {
                        if (!string.IsNullOrEmpty(item.CustomData.ToString()))
                        {
                            /// <summary>
                            /// Someone already own this city.
                            /// </summary>
                            return false;
                        }
                    }
                }

                /// <summary>
                /// Getting user display name in order to send it to the store custom data
                /// </summary>
                /// <value></value>
                PlayFab.ServerModels.GetUserAccountInfoRequest userrequest = new PlayFab.ServerModels.GetUserAccountInfoRequest { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, };
                var userinfo = PlayFabServerAPI.GetUserAccountInfoAsync(userrequest);
                string playerUsername = "";
                if (userinfo != null && userinfo.Result.Result.UserInfo != null)
                {
                    playerUsername = userinfo.Result.Result.UserInfo.TitleInfo.DisplayName;
                }
                else
                {
                    return false;
                }
                


                
                // 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.ToString())
                    {
                        expirationDate = item.Expiration;
                        break;
                    }
                }
                if (expirationDate == null)
                {
                    log.LogError($"Item {inputValue.ToString()} 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 = "CitiesStore",
                    CatalogVersion = "Cities",
                    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 = playerUsername,
                        expirationDate = expirationDate.Value.ToString("yyyy-MM-dd"),
                    }),
                },
            },
                };
                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();
            }
        }
    }
Editor is loading...