예제 #1
0
        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));
        }