// インベントリカスタムデータを更新 private static async Task UpdateUserInventoryCustomDataAsync(FunctionExecutionContext <dynamic> context, DevelopUpdateUserInventoryCustomDataApiRequest request) { var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext); var result = await serverApi.UpdateUserInventoryItemCustomDataAsync(new UpdateUserInventoryItemDataRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, ItemInstanceId = request.itemInstanceId, Data = request.data, }); }
// 指定したモンスターのカスタムデータを更新 public static async Task UpdateUserMonsterCustomDataAsync(FunctionExecutionContext <dynamic> context, string itemInstanceId, UserMonsterCustomData customData) { var serverApi = new PlayFabServerInstanceAPI(context.ApiSettings, context.AuthenticationContext); var customDataDict = UserDataUtil.GetCustomDataDict(customData); var result = await serverApi.UpdateUserInventoryItemCustomDataAsync(new UpdateUserInventoryItemDataRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, ItemInstanceId = itemInstanceId, Data = customDataDict, }); }
public static async Task <dynamic> 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 newName = args["Name"]; //Modificar Datos de la base de datos var updateUserDataResult = await server.UpdateUserReadOnlyDataAsync(new PlayFab.ServerModels.UpdateUserDataRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, // usuario de testing: "39212472BC0FB314" Data = new Dictionary <String, String>() { { "Name", newName }, { "RankExp", "0" }, //Misma idea que con la experiencia de personajes, guardo solo la exp y en el cliente ya calculamos el rango { "StoryCleared", "0" }, //Apuntamos cuantas stages ha completado. { "MissionsCleared", " " } //Separamos por espacios los ID de las misiones que ha completado } }); // REGALAR PERSONAJES var updateInventoryResult = await server.GrantItemsToUserAsync(new PlayFab.ServerModels.GrantItemsToUserRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, ItemIds = new List <string> { "6", "5", "3", "4", "46", "47", "48", "49" }, CatalogVersion = "Characters" }); foreach (PlayFab.ServerModels.GrantedItemInstance elements in updateInventoryResult.Result.ItemGrantResults) { var updateInventoryDataResult = await server.UpdateUserInventoryItemCustomDataAsync(new PlayFab.ServerModels.UpdateUserInventoryItemDataRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, ItemInstanceId = elements.ItemInstanceId, Data = new Dictionary <String, String>() { { "exp", "0" }, { "awaken", "0" } } }); } ////////////////// //Leer Datos de la base de datos var result = await server.GetUserReadOnlyDataAsync(new PlayFab.ServerModels.GetUserDataRequest() { PlayFabId = context.CallerEntityProfile.Lineage.MasterPlayerAccountId, //context.CallerEntityProfile.Lineage.MasterPlayerAccountId, // usuario de testing: "39212472BC0FB314" }); var UserName = "******"; if (result.Error != null) { // Handle error if any } else { if (result.Result.Data == null || !result.Result.Data.ContainsKey("Name")) { Console.WriteLine("No Country"); } else { if (result.Result.Data.ContainsKey("Name")) { Console.WriteLine("Name: " + result.Result.Data["Name"].Value); UserName = result.Result.Data["Name"].Value; } } } var message = $"Hello there!"; log.LogInformation(message); return(new { messageValue = newName }); }
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)); }