Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
        }