示例#1
0
        public override async Task <TaskRes> Execute(Account acc)
        {
            building = TroopsHelper.GetTroopBuilding(Troop, Great);

            // Switch hero helmet. If hero will be switched, this TrainTroops task
            // will be executed right after the hero helmet switch
            if (HeroHelper.SwitchHelmet(acc, this.Vill, building, this))
            {
                return(TaskRes.Executed);
            }

            await base.Execute(acc);

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

            if (this.UpdateOnly || this.Troop == TroopsEnum.None)
            {
                return(TaskRes.Executed);
            }

            (TimeSpan dur, Resources cost) = TroopsParser.GetTrainCost(acc.Wb.Html, this.Troop);

            var troopNode = acc.Wb.Html.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)Troop));

            if (troopNode == null)
            {
                acc.Wb.Log($"Bot tried to train {Troop} in {Vill.Name}, but couldn't find it in {building}! Are you sure you have {Troop} researched?");
                return(TaskRes.Executed);
            }
            while (!troopNode.HasClass("details"))
            {
                troopNode = troopNode.ParentNode;
            }
            var inputName = troopNode.Descendants("input").FirstOrDefault().GetAttributeValue("name", "");

            long maxNum = 0;

            switch (acc.AccInfo.ServerVersion)
            {
            case ServerVersionEnum.T4_4:
                maxNum = Parser.RemoveNonNumeric(
                    troopNode.ChildNodes
                    .FirstOrDefault(x => x.Name == "a")?.InnerText ?? "0"
                    );
                break;

            case ServerVersionEnum.T4_5:
                maxNum = Parser.RemoveNonNumeric(
                    troopNode.ChildNodes
                    .First(x => x.HasClass("cta"))
                    .ChildNodes
                    .First(x => x.Name == "a")
                    .InnerText);
                break;
            }

            if (!HighSpeedServer)
            {
                var trainNum = TroopsHelper.TroopsToFill(acc, Vill, this.Troop, this.Great);

                // Don't train too many troops, just fill up the training building
                if (maxNum > trainNum)
                {
                    maxNum = trainNum;
                }
            }

            if (maxNum < 0)
            {
                // We have already enough troops in training.
                return(TaskRes.Executed);
            }

            acc.Wb.Driver.ExecuteScript($"document.getElementsByName('{inputName}')[0].value='{maxNum}'");

            await Task.Delay(100);

            await DriverHelper.ExecuteScript(acc, "document.getElementsByName('s1')[0].click()");

            UpdateCurrentlyTraining(acc.Wb.Html, acc);

            if (!HighSpeedServer)
            {
                RepeatTrainingCycle(acc.Wb.Html, acc);
            }

            return(TaskRes.Executed);
        }
示例#2
0
        public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc)
        {
            building = TroopsHelper.GetTroopBuilding(Troop, Great);

            var buildId = vill.Build.Buildings.FirstOrDefault(x => x.Type == building);

            if (buildId == null)
            {
                //update dorf, no buildingId found?
                TaskExecutor.AddTask(acc, new UpdateDorf2()
                {
                    ExecuteAt = DateTime.Now, vill = vill
                });
                Console.WriteLine($"There is no {building} in this village!");
                return(TaskRes.Executed);
            }
            await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={buildId.Id}");

            //after finishing task, update currently training
            this.PostTaskCheck.Add(UpdateCurrentlyTraining);
            if (!HighSpeedServer)
            {
                this.PostTaskCheck.Add(RepeatTrainingCycle);
            }
            if (this.UpdateOnly || this.Troop == TroopsEnum.None)
            {
                return(TaskRes.Executed);
            }

            (TimeSpan dur, Resources cost) = TroopsParser.GetTrainCost(htmlDoc, this.Troop);

            var troopNode = htmlDoc.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)Troop));

            while (!troopNode.HasClass("details"))
            {
                troopNode = troopNode.ParentNode;
            }
            var inputName = troopNode.Descendants("input").FirstOrDefault().GetAttributeValue("name", "");

            var maxNum = Parser.RemoveNonNumeric(troopNode.ChildNodes.FirstOrDefault(x => x.Name == "a").InnerText);

            if (!HighSpeedServer)
            {
                var trainNum = TroopsHelper.TroopsToFill(acc, vill, this.Troop, this.Great);

                // Don't train too many troops, just fill up the training building
                if (maxNum > trainNum)
                {
                    maxNum = trainNum;
                }
            }

            if (maxNum < 0)
            {
                // We have already enough troops in training.
                return(TaskRes.Executed);
            }

            wb.ExecuteScript($"document.getElementsByName('{inputName}')[0].value='{maxNum}'");
            await Task.Delay(100);

            wb.ExecuteScript("document.getElementsByName('s1')[0].click()"); //Train button
            return(TaskRes.Executed);
        }