Пример #1
0
        public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc)
        {
            if (vill == null)
            {
                vill = AccountHelper.GetMainVillage(acc);
            }

            Classificator.BuildingEnum building = (Great == false) ? TroopsHelper.GetTroopBuilding(troop, false) : TroopsHelper.GetTroopBuilding(troop, true);

            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
                });
                Console.WriteLine($"There is no {building} in this village!");
                return(TaskRes.Executed);
            }
            await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={buildId.Id}");

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

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

            //finding the correct "Exchange resources" button
            var exchangeResButton = troopNode.Descendants("button").FirstOrDefault(x => x.HasClass("gold"));

            wb.ExecuteScript($"document.getElementById('{exchangeResButton.GetAttributeValue("id", "")}').click()"); //Exchange resources button

            await Task.Delay(AccountHelper.Delay());

            htmlDoc.LoadHtml(wb.PageSource);
            await Task.Delay(AccountHelper.Delay());

            var distribute = htmlDoc.DocumentNode.SelectNodes("//*[text()[contains(., 'Distribute remaining resources.')]]")[0];

            while (distribute.Name != "button")
            {
                distribute = distribute.ParentNode;
            }
            string distributeid = distribute.GetAttributeValue("id", "");

            wb.ExecuteScript($"document.getElementById('{distributeid}').click()"); //Distribute resources button

            await Task.Delay(AccountHelper.Delay());

            wb.ExecuteScript($"document.getElementById('npc_market_button').click()"); //Exchange resources button

            return(TaskRes.Executed);
        }
Пример #2
0
        public override async Task <TaskRes> Execute(Account acc)
        {
            var wb = acc.Wb.Driver;

            if (Vill == null)
            {
                Vill = AccountHelper.GetMainVillage(acc);
            }

            await base.Execute(acc);

            var building = TroopsHelper.GetTroopBuilding(troop, Great);

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

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

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

            //finding the correct "Exchange resources" button
            var exchangeResButton = troopNode.Descendants("button").FirstOrDefault(x => x.HasClass("gold"));

            string script = $"document.getElementById('{exchangeResButton.GetAttributeValue("id", "")}').click()";
            await DriverHelper.ExecuteScript(acc, script);

            var distribute = acc.Wb.Html.DocumentNode.SelectNodes("//*[text()[contains(., 'Distribute remaining resources.')]]")[0];

            while (distribute.Name != "button")
            {
                distribute = distribute.ParentNode;
            }
            string distributeid = distribute.GetAttributeValue("id", "");

            wb.ExecuteScript($"document.getElementById('{distributeid}').click()"); //Distribute resources button

            await Task.Delay(AccountHelper.Delay());

            wb.ExecuteScript($"document.getElementById('npc_market_button').click()"); //Exchange resources button

            return(TaskRes.Executed);
        }
Пример #3
0
        //You would probably have to take into account ally training bonus, artifacts, helmets, horse fountain...
        public static TimeSpan GetTrainingTime(Account acc, Village vill, Classificator.TroopsEnum troop, bool great)
        {
            var buildingType = TroopsHelper.GetTroopBuilding(troop, great);
            var building     = vill.Build.Buildings.FirstOrDefault(x => x.Type == buildingType);
            var troopId      = (int)troop;

            if (troopId > 40)
            {
                troopId -= 20;               //since we don't have values for nature/natars
            }
            var baseTime     = TrainTime[troopId];
            var sec          = Math.Pow(0.9, building.Level - 1) * baseTime;
            var millis       = sec % 1;
            var baseTimespan = new TimeSpan(0, 0, 0, (int)sec, (int)millis * 1000);

            return(new TimeSpan(baseTimespan.Ticks / acc.AccInfo.ServerSpeed));
        }
Пример #4
0
 private string[] GetPossibleTroops(Classificator.BuildingEnum building)
 {
     List<string> ret = new List<string>();
     ret.Add("None");
     var acc = getSelectedAcc();
     if (acc.Villages.Count == 0) return ret.ToArray(); //Acc has now been initialised
     int troopsEnum = ((int)acc.AccInfo.Tribe - 1) * 10;
     for (var i = troopsEnum + 1; i < troopsEnum + 11; i++)
     {
         Classificator.TroopsEnum troop = (Classificator.TroopsEnum)i;
         if (TroopsHelper.GetTroopBuilding(troop, false) == building)
         {
             ret.Add(VillageHelper.EnumStrToString(troop.ToString()));
         }
     }
     return ret.ToArray();
 }
Пример #5
0
        private string[] GetPossibleTroops(Classificator.BuildingEnum building)
        {
            List <string> ret = new List <string>();

            ret.Add("None");
            var acc = GetSelectedAcc();

            if (acc.Villages.Count == 0)
            {
                return(ret.ToArray());                         //Acc has now been initialised
            }
            var tribes = new List <Classificator.TribeEnum>(5);

            if (NYS.Checked)
            {
                tribes.Add(Classificator.TribeEnum.Egyptians);
                tribes.Add(Classificator.TribeEnum.Gauls);
                tribes.Add(Classificator.TribeEnum.Huns);
                tribes.Add(Classificator.TribeEnum.Romans);
                tribes.Add(Classificator.TribeEnum.Teutons);
            }
            else
            {
                tribes.Add(acc.AccInfo.Tribe ?? Classificator.TribeEnum.Any);
            }

            foreach (var tribe in tribes)
            {
                int troopsEnum = ((int)tribe - 1) * 10;
                for (var i = troopsEnum + 1; i < troopsEnum + 11; i++)
                {
                    Classificator.TroopsEnum troop = (Classificator.TroopsEnum)i;
                    if (TroopsHelper.GetTroopBuilding(troop, false) == building)
                    {
                        ret.Add(VillageHelper.EnumStrToString(troop.ToString()));
                    }
                }
            }
            return(ret.ToArray());
        }
Пример #6
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);
        }
Пример #7
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);
        }