private static async Task <List <MonsterMB> > GetTitleData(FunctionExecutionContext <dynamic> context) { var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext); var result = await serverApi.GetTitleDataAsync(new PlayFab.ServerModels.GetTitleDataRequest() { Keys = new List <string>() { "MonsterMB" }, }); var monsterList = JsonConvert.DeserializeObject <List <MonsterMB> >(result.Result.Data["MonsterMB"]); return(monsterList); }
// マスタデータを取得する public static async Task <List <T> > GetMasterAsyncOf <T>(FunctionExecutionContext <dynamic> context) where T : MasterBookBase { var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext); var masterDataName = TextUtil.GetDescriptionAttribute <T>(); var result = await serverApi.GetTitleDataAsync(new GetTitleDataRequest() { Keys = new List <string>() { masterDataName }, }); var masterDataJson = result.Result.Data[masterDataName]; var masterDataList = JsonConvert.DeserializeObject <List <T> >(masterDataJson); return(masterDataList); }
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); var args = context.FunctionArgument; string stageId = args["stageId"]; string stageType = args["stageType"]; string characterString = args["characters"]; string[] characterID = characterString.Split(' '); //Montar el string con esta forma: "characterID1 characterID2 characterID3..." string Result = null; //Check si se puede completar //Comprobar si estaba haciendo la stage // var GetUserDataResult = await server.GetUserReadOnlyDataAsync(new PlayFab.ServerModels.GetUserDataRequest(){ // PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, // Keys = new List<string>() {"CurrentStage"} // }); // if(stageType+stageId != GetUserDataResult.Result.Data["CurrentStage"].Value) Result += "Never started the quest "; if (Result == null) { //Comprobar qué items hay que dar //Leo el Title Data //Creo lista auxiliar de keys List <String> key = new List <string>(); key.Add(stageType + stageId); foreach (string chID in characterID) { key.Add("Character" + chID); } key.Add("Character Max Exp"); var getTitleDataResult = await server.GetTitleDataAsync(new PlayFab.ServerModels.GetTitleDataRequest() { Keys = key }); //Title Data del stage Dictionary <string, string> stageTitleData = JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data[stageType + stageId]); //Entregar monedas var giveCoinsResult = await server.AddUserVirtualCurrencyAsync(new PlayFab.ServerModels.AddUserVirtualCurrencyRequest() { Amount = int.Parse(stageTitleData["coins"]), PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, VirtualCurrency = "CO" }); stageTitleData.Remove("coins"); //Dar exp de rango //Dar exp de personajes //Leo inventario var getInventoryResult = await server.GetUserInventoryAsync(new PlayFab.ServerModels.GetUserInventoryRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId,//context.CallerEntityProfile.Lineage.MasterPlayerAccountId, }); foreach (string character in characterID) { //Me guardo el personaje var characterData = getInventoryResult.Result.Inventory.Find(x => x.ItemId == character && x.ItemClass == "character"); if (characterData == null) { Result += "Character not obtained "; } //Title Data del personaje Dictionary <string, string> characterTitleData = JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data["Character" + character]); string auxString = characterTitleData["rarity"] + (characterData.CustomData["awaken"] == "0" ? "Normal" : "Awaken"); if (int.Parse(JsonConvert.DeserializeObject <Dictionary <string, string> >(getTitleDataResult.Result.Data["Character Max Exp"])[auxString]) <= int.Parse(characterData.CustomData["exp"])) { Console.Write("Character" + character + " max Level "); } else { //Calcular nueva experiencia int newExp = int.Parse(characterData.CustomData["exp"]) + int.Parse(stageTitleData["exp"]); 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,//context.CallerEntityProfile.Lineage.MasterPlayerAccountId, ItemInstanceId = characterData.ItemInstanceId, Data = new Dictionary <String, String>() { { "exp", newExp.ToString() } } }); } } //Random para los rates Random rnd = new Random(); //Creo listas de IDs List <string> characterIDs = new List <string>(); List <string> consumableIDs = new List <string>(); List <string> equipmentIDs = new List <string>(); //Items a través de bundles playfab for (int i = 1; i <= stageTitleData.Count() / 4; i++) { //Check de si es tu primera vez //Extraer Rates int rate = stageTitleData["rate" + i.ToString()] == "FirstClear" ? 100 : int.Parse(stageTitleData["rate" + i.ToString()]); //Calculo del random if (rnd.Next(1, 101) <= rate) { switch (stageTitleData["class" + i]) { case "Equipment": for (int j = 0; j < int.Parse(stageTitleData["ammount" + i]); j++) { equipmentIDs.Add(stageTitleData["id" + i]); } Result += "Equipment " + stageTitleData["id" + i] + " " + stageTitleData["ammount" + i] + "|"; break; case "Characters": for (int j = 0; j < int.Parse(stageTitleData["ammount" + i]); j++) { characterIDs.Add(stageTitleData["id" + i]); } Result += "Characters " + stageTitleData["id" + i] + " " + stageTitleData["ammount" + i] + "|"; break; case "Consumables": for (int j = 0; j < int.Parse(stageTitleData["ammount" + i]); j++) { consumableIDs.Add(stageTitleData["id" + i]); } Result += "Consumables " + stageTitleData["id" + i] + " " + stageTitleData["ammount" + i] + "|"; break; default: Result = "CLASE INCORRECTA"; break; } } } var GrantEquipmentToUser = await server.GrantItemsToUserAsync(new PlayFab.ServerModels.GrantItemsToUserRequest() { CatalogVersion = "Equipment", ItemIds = equipmentIDs, PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId }); var GrantConsumableToUser = await server.GrantItemsToUserAsync(new PlayFab.ServerModels.GrantItemsToUserRequest() { CatalogVersion = "Consumables", ItemIds = equipmentIDs, PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId }); //Gestión de duplicados // var GrantCharacterToUser = await server.GrantItemsToUserAsync(new PlayFab.ServerModels.GrantItemsToUserRequest(){ // CatalogVersion = "Characters", // ItemIds = characterIDs, // PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId // }); //Sumar mission cleared y limpiar current var modifyReadOnlyData = await server.UpdateUserReadOnlyDataAsync(new PlayFab.ServerModels.UpdateUserDataRequest() { }); } return(new OkObjectResult(Result)); }
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)); }