public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { FunctionExecutionContext <dynamic> context = JsonConvert.DeserializeObject <FunctionExecutionContext <dynamic> >(await req.ReadAsStringAsync()); var apiSettings = new PlayFabApiSettings { TitleId = "F6F60", //TitleId = Environment.GetEnvironmentVariable("PLAYFAB_TITLE_ID", EnvironmentVariableTarget.Process), DeveloperSecretKey = "KNFWWBZ3FM4F4HU63E3RA446UBM995GPXXIHGU76977WXC4WJM" //DeveloperSecretKey = Environment.GetEnvironmentVariable("PLAYFAB_DEV_SECRET_KEY", EnvironmentVariableTarget.Process), }; var server = new PlayFabServerInstanceAPI(apiSettings); string Result = null; var args = context.FunctionArgument; string itemsString = args["itemsUsage"]; //"1 1 1"; string[] itemsUsage = itemsString.Split(' '); //Montar el string con esta forma: "vecesUsadoItem1 vecesUsadoItem2 vecesUsadoItem3..." string characterID = args["CharacterID"]; //Personaje al que damos exp var totalExp = 0; List <PlayFab.ServerModels.ItemInstance> ItemInstace = new List <PlayFab.ServerModels.ItemInstance>(); //Check si se puede //Leer catálogo var catalogData = await server.GetCatalogItemsAsync(new PlayFab.ServerModels.GetCatalogItemsRequest() { CatalogVersion = "Consumables" }); //Crear diccionario con CustomData List <Dictionary <string, string> > customData = new List <Dictionary <string, string> >(); customData.Add(JsonConvert.DeserializeObject <Dictionary <string, string> >(catalogData.Result.Catalog.Find(x => x.ItemId == "1").CustomData)); customData.Add(JsonConvert.DeserializeObject <Dictionary <string, string> >(catalogData.Result.Catalog.Find(x => x.ItemId == "2").CustomData)); customData.Add(JsonConvert.DeserializeObject <Dictionary <string, string> >(catalogData.Result.Catalog.Find(x => x.ItemId == "3").CustomData)); //Sumo los costes var coinsUsed = int.Parse(customData[0]["coinsCost"]) * int.Parse(itemsUsage[0]) + int.Parse(customData[1]["coinsCost"]) * int.Parse(itemsUsage[1]) + int.Parse(customData[2]["coinsCost"]) * int.Parse(itemsUsage[2]); //Leo inventario var getInventoryResult = await server.GetUserInventoryAsync(new PlayFab.ServerModels.GetUserInventoryRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, }); if (getInventoryResult.Result.VirtualCurrency["CO"] < coinsUsed) { Result = "Not enough coins "; } //Me guardo el personaje var character = getInventoryResult.Result.Inventory.Find(x => x.ItemId == characterID && x.ItemClass == "character"); if (character == null) { Result += "Character not obtained "; } //Leo el Title Data var getTitleDataResult = await server.GetTitleDataAsync(new PlayFab.ServerModels.GetTitleDataRequest() { }); //Title Data del personaje Dictionary <string, string> characterTitleData = JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data["Character" + characterID]); string auxString = characterTitleData["rarity"] + (character.CustomData["awaken"] == "0" ? "Normal" : "Awaken"); if (int.Parse(JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data["Character Max Exp"])[auxString]) <= int.Parse(character.CustomData["exp"])) { Result += "Max Level "; } //Filtro el inventario para sacar los consumibles solo foreach (PlayFab.ServerModels.ItemInstance item in getInventoryResult.Result.Inventory) { if (item.ItemClass == "Consumable") { ItemInstace.Add(item); } } //if(ItemInstace.Count != 3) Result += "Not enough items "; //else{ foreach (PlayFab.ServerModels.ItemInstance item in ItemInstace) { if (item.ItemId == "1" && item.RemainingUses < int.Parse(itemsUsage[0])) { Result += "Not enough item 1 "; } if (item.ItemId == "2" && item.RemainingUses < int.Parse(itemsUsage[1])) { Result += "Not enough item 2 "; } if (item.ItemId == "3" && item.RemainingUses < int.Parse(itemsUsage[2])) { Result += "Not enough item 3 "; } // } } if (Result == null) { //Consumir monedas var consumeCoins = await server.SubtractUserVirtualCurrencyAsync(new PlayFab.ServerModels.SubtractUserVirtualCurrencyRequest() { Amount = coinsUsed, PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, VirtualCurrency = "CO" }); //Consumir objetos si no ha habido errores foreach (PlayFab.ServerModels.ItemInstance item in ItemInstace) { if (item.ItemId == "1" && itemsUsage[0] != "0") { Console.WriteLine("Consumo 1"); var consumeItem1 = await server.ConsumeItemAsync(new PlayFab.ServerModels.ConsumeItemRequest() { ConsumeCount = int.Parse(itemsUsage[0]), ItemInstanceId = item.ItemInstanceId, PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, }); totalExp += int.Parse(customData[0]["expGiven"]) * int.Parse(itemsUsage[0]); if (consumeItem1.Error != null) { Result += consumeItem1.Error.ErrorMessage; } } else if (item.ItemId == "2" && itemsUsage[1] != "0") { Console.WriteLine("Consumo 2"); var consumeItem2 = await server.ConsumeItemAsync(new PlayFab.ServerModels.ConsumeItemRequest() { ConsumeCount = int.Parse(itemsUsage[1]), ItemInstanceId = item.ItemInstanceId, PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, }); totalExp += int.Parse(customData[1]["expGiven"]) * int.Parse(itemsUsage[1]); if (consumeItem2.Error != null) { Result += consumeItem2.Error.ErrorMessage; } } else if (item.ItemId == "3" && itemsUsage[2] != "0") { Console.WriteLine("Consumo 3"); var consumeItem3 = await server.ConsumeItemAsync(new PlayFab.ServerModels.ConsumeItemRequest() { ConsumeCount = int.Parse(itemsUsage[2]), ItemInstanceId = item.ItemInstanceId, PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, }); totalExp += int.Parse(customData[2]["expGiven"]) * int.Parse(itemsUsage[2]); if (consumeItem3.Error != null) { Result += consumeItem3.Error.ErrorMessage; } } } //Calcular nueva experiencia int newExp = int.Parse(character.CustomData["exp"]) + totalExp; if (int.Parse(JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data["Character Max Exp"])[auxString]) < newExp) { newExp = int.Parse(JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data["Character Max Exp"])[auxString]); } //Actualizar experiencia jugador var updateInventoryDataResult = await server.UpdateUserInventoryItemCustomDataAsync(new PlayFab.ServerModels.UpdateUserInventoryItemDataRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, ItemInstanceId = character.ItemInstanceId, Data = new Dictionary <String, String>() { { "exp", newExp.ToString() } } }); } if (Result == null) { Result = "Everything's ok!"; } return(new OkObjectResult(Result)); }