예제 #1
0
        /// <summary>
        /// When new village is found by the bot, it should firstly check barracks, then  stable and then workshop,
        /// to see which troops are researched
        /// </summary>
        public override async Task <TaskRes> Execute(Account acc)
        {
            var wb = acc.Wb.Driver;

            // If we have Plus account, just check that.
            if (acc.AccInfo.PlusAccount)
            {
                await VersionHelper.Navigate(acc, "/dorf3.php?s=5&su=2", "/village/statistics/troops?su=2");

                OverviewParser.UpdateTroopsLevels(acc.Wb.Html, ref acc);
                // We have updated all villages at the same time. No need to continue.
                acc.Tasks.RemoveAll(x => x.GetType() == typeof(UpdateTroops));
                return(TaskRes.Executed);
            }

            var smithy = Vill.Build.Buildings.FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Smithy);

            if (smithy != null)
            {
                await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={smithy.Id}");

                Vill.Troops.Levels = TroopsParser.GetTroopLevels(acc.Wb.Html);
                TroopsHelper.UpdateResearchedTroops(Vill);
                return(TaskRes.Executed);
            }

            for (int i = 0; i < 3; i++)
            {
                //var building = GetBuilding(i);
                //await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={building.Id}");
                // TODO: parse content
            }
            return(TaskRes.Executed);
        }
예제 #2
0
        /// <summary>
        /// When new village is found by the bot, it should firstly check barracks, then  stable and then workshop,
        /// to see which troops are researched
        /// </summary>
        public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc)
        {
            // If we have Plus account, just check that.
            if (acc.AccInfo.PlusAccount)
            {
                await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf3.php?s=5&su=2");

                OverviewParser.UpdateTroopsLevels(htmlDoc, ref acc);
                // We have updated all villages at the same time. No need to continue.
                acc.Tasks.RemoveAll(x => x.GetType() == typeof(UpdateTroops));
                return(TaskRes.Executed);
            }

            var smithy = vill.Build.Buildings.FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Smithy);

            if (smithy != null)
            {
                await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={smithy.Id}");

                vill.Troops.Levels = TroopsParser.GetTroopLevels(htmlDoc);
                UpdateResearchedTroops(vill);
                return(TaskRes.Executed);
            }

            for (int i = 0; i < 3; i++)
            {
                //var building = GetBuilding(i);
                //await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={building.Id}");
                // TODO: parse content
            }
            return(TaskRes.Executed);
        }
예제 #3
0
        // Copied from UpdateTroops BotTask
        public async Task UpdateTroopsResearchedAndLevels(Account acc)
        {
            if (acc.AccInfo.PlusAccount)
            {
                // From overview we get all researched troops and their levels
                switch (acc.AccInfo.ServerVersion)
                {
                case Classificator.ServerVersionEnum.T4_4:
                    await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf3.php?s=5&su=2");

                    break;

                case Classificator.ServerVersionEnum.T4_5:
                    await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/village/statistics/troops?su=2");

                    break;
                }
                OverviewParser.UpdateTroopsLevels(acc.Wb.Html, ref acc);
                // We have updated all villages at the same time. No need to continue.
                acc.Tasks.RemoveAll(x => x.GetType() == typeof(UpdateTroops));
                return;
            }

            var smithy = Vill.Build.Buildings.FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Smithy);

            if (smithy != null)
            {
                // If smithy exists, we get all researched troops and their levels
                await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={smithy.Id}");

                Vill.Troops.Levels = TroopsParser.GetTroopLevels(acc.Wb.Html);
                TroopsHelper.UpdateResearchedTroops(Vill);
                return;
            }
        }
예제 #4
0
        public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc)
        {
            if (vill == null)
            {
                vill = acc.Villages.First(x => x.Active);
            }

            var smithy = vill.Build.Buildings.FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Smithy);

            if (smithy == null)
            {
                return(TaskRes.Executed);
            }
            await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={smithy.Id}");

            var levels = TroopsParser.GetTroopLevels(htmlDoc);

            if (levels == null)
            {
                this.ErrorMessage = "There was an error at getting Smithy troop levels";
                return(TaskRes.Executed);
            }
            vill.Troops.Levels = levels;
            UpdateResearchedTroops(vill);

            var currentlyImproving = TroopsParser.GetImprovingTroops(htmlDoc);
            var troop = TroopToImprove(vill, currentlyImproving);

            if (troop == Classificator.TroopsEnum.None)
            {
                return(TaskRes.Executed);
            }

            //If we have plus account we can improve 2 troops at the same time
            int maxImproving = acc.AccInfo.PlusAccount ? 2 : 1;

            if (currentlyImproving.Count() >= maxImproving)
            {
                this.NextExecute = DateTime.Now.Add(currentlyImproving.Last().Time);
                return(TaskRes.Executed);
            }
            //call NextImprove() after enough res OR when this improvement finishes.

            var cost = vill.Troops.Levels.FirstOrDefault(x => x.Troop == troop);

            var nextExecute = ResourcesHelper.EnoughResourcesOrTransit(acc, vill, cost.UpgradeCost);

            if (nextExecute < DateTime.Now.AddMilliseconds(1)) //We have enough resources, click Improve button
            {
                //Click on the button
                var troopNode = htmlDoc.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)troop));
                while (!troopNode.HasClass("research"))
                {
                    troopNode = troopNode.ParentNode;
                }

                var button = troopNode.Descendants("button").FirstOrDefault(x => x.HasClass("green"));
                if (button == null)
                {
                    this.ErrorMessage = $"Could not find Upgrade button to improve {troop}";
                    this.NextExecute  = DateTime.Now.AddMinutes(1);
                    return(TaskRes.Retry);
                }


                wb.ExecuteScript($"document.getElementById('{button.Id}').click()");
                // If we have plus account and there is currently no other troop to improve, go ahead and improve the unit again
                this.NextExecute = (currentlyImproving.Count() == 0 && maxImproving == 2) ?
                                   DateTime.MinValue :
                                   DateTime.Now.Add(cost.TimeCost).AddMilliseconds(5 * AccountHelper.Delay());
                return(TaskRes.Executed);
            }
            else //Retry same task after resources get produced/transited
            {
                this.NextExecute = nextExecute;
                return(TaskRes.Executed);
            }
        }
예제 #5
0
        public override async Task <TaskRes> Execute(Account acc)
        {
            await base.Execute(acc); // Navigate to dorf2

            if (Vill == null)
            {
                Vill = acc.Villages.First(x => x.Active);
            }

            if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Smithy))
            {
                return(TaskRes.Executed);
            }

            var levels = TroopsParser.GetTroopLevels(acc.Wb.Html);

            if (levels == null)
            {
                acc.Wb.Log("There was an error at getting Smithy troop levels");
                return(TaskRes.Executed);
            }
            Vill.Troops.Levels = levels;
            TroopsHelper.UpdateResearchedTroops(Vill);

            var currentlyImproving = TroopsParser.GetImprovingTroops(acc.Wb.Html);
            var troop = TroopToImprove(Vill, currentlyImproving);

            if (troop == Classificator.TroopsEnum.None)
            {
                return(TaskRes.Executed);
            }

            //If we have plus account we can improve 2 troops at the same time
            int maxImproving = acc.AccInfo.PlusAccount ? 2 : 1;

            if (maxImproving <= currentlyImproving.Count())
            {
                this.NextExecute = DateTime.Now.Add(currentlyImproving.Last().Time);
                return(TaskRes.Executed);
            }
            //call NextImprove() after enough res OR when this improvement finishes.

            var cost = Vill.Troops.Levels.FirstOrDefault(x => x.Troop == troop);

            // Check if we have enough resources to improve the troop
            if (!ResourcesHelper.IsEnoughRes(Vill, cost.UpgradeCost.ToArray()))
            {
                ResourcesHelper.NotEnoughRes(acc, Vill, cost.UpgradeCost, this);
                return(TaskRes.Executed);
            }

            //Click on the button
            var troopNode = acc.Wb.Html.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)troop));

            while (!troopNode.HasClass("research"))
            {
                troopNode = troopNode.ParentNode;
            }

            var button = troopNode.Descendants("button").FirstOrDefault(x => x.HasClass("green"));

            if (button == null)
            {
                acc.Wb.Log($"Could not find Upgrade button to improve {troop}");
                this.NextExecute = DateTime.Now.AddMinutes(1);
                return(TaskRes.Retry);
            }


            acc.Wb.Driver.ExecuteScript($"document.getElementById('{button.Id}').click()");
            // If we have plus account and there is currently no other troop to improve, go ahead and improve the unit again
            this.NextExecute = (currentlyImproving.Count() == 0 && maxImproving == 2) ?
                               DateTime.MinValue :
                               DateTime.Now.Add(cost.TimeCost).AddMilliseconds(5 * AccountHelper.Delay());
            return(TaskRes.Executed);
        }