public override async Task <TaskRes> Execute(Account acc) { // First navigate to dorf2 and then to the main building, to make sure the currently demolish list is refreshed if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.MainBuilding, update: true)) { return(TaskRes.Executed); } if (Vill.Build.DemolishTasks.Count == 0) { return(TaskRes.Executed); //No more demolish tasks } var id = BuildingToDemolish(Vill, acc.Wb.Html); if (id == null) { return(TaskRes.Executed); //No more demolish tasks } await DriverHelper.WriteById(acc, "demolish", id); await DriverHelper.ClickById(acc, "btn_demolish"); this.NextExecute = TimeHelper.RanDelay(acc, await NextDemolishTime(acc)); return(TaskRes.Executed); }
} //once / twice / 3 times public override async Task <TaskRes> Execute(Account acc) { if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Marketplace, "&t=5")) { return(TaskRes.Executed); } if (this.Resources == null) { this.Resources = Vill.Res.Stored.Resources; } // Check if we have enough resources in main village var resToSend = MarketHelper.SendResCapToStorage(acc, this.Resources); var targetVillage = acc.Villages.FirstOrDefault(x => x.Coordinates == this.Coordinates); var duration = await MarketHelper.MarketSendResource(acc, resToSend, targetVillage, this); var targetVill = acc.Villages.FirstOrDefault(x => x.Coordinates == Coordinates); targetVill.Market.Settings.Configuration.TransitArrival = DateTime.Now.Add(duration); if (this.Configuration != null && duration != null) { this.Configuration.TransitArrival = DateTime.Now.Add(duration); } // When you send resources there actually isn't a page load return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { var wb = acc.Wb.Driver; if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.MainBuilding)) { return(TaskRes.Executed); } if (Vill.Build.DemolishTasks.Count == 0) { return(TaskRes.Executed); //No more demolish tasks } var id = BuildingToDemolish(Vill, acc.Wb.Html); if (id == null) { return(TaskRes.Executed); //No more demolish tasks } await DriverHelper.WriteById(acc, "demolish", id); await DriverHelper.ClickById(acc, "btn_demolish"); this.NextExecute = NextDemolishTime(acc); return(TaskRes.Executed); }
//If Troop == null, just update the troop levels public override async Task <TaskRes> Execute(Account acc) { await base.Execute(acc); // Navigate to dorf2 if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Academy)) { return(TaskRes.Executed); } var troop = Vill.Troops.ToResearch.FirstOrDefault(); if (troop == TroopsEnum.None) { return(TaskRes.Executed); //We have researched all troops that were on the list } var troopNode = acc.Wb.Html.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)troop)); if (troopNode == null) { acc.Wb.Log($"Researching {troop} was not possible! Bot assumes you already have it researched"); Vill.Troops.Researched.Add(troop); return(TaskRes.Retry); } while (!troopNode.HasClass("research")) { troopNode = troopNode.ParentNode; } var button = troopNode.Descendants("button").FirstOrDefault(x => x.HasClass("green")); if (button == null) { RepeatTask(Vill, troop, DateTime.Now); } (TimeSpan dur, Resources cost) = TroopsParser.AcademyResearchCost(acc.Wb.Html, troop); // Check if we have enough resources to research the troop if (!ResourcesHelper.IsEnoughRes(Vill, cost.ToArray())) { ResourcesHelper.NotEnoughRes(acc, Vill, cost, this); return(TaskRes.Executed); } await DriverHelper.ClickById(acc, button.Id); var executeNext = DateTime.Now.Add(dur).AddMilliseconds(10 * AccountHelper.Delay()); if (Vill.Settings.AutoImprove) { TaskExecutor.AddTask(acc, new ImproveTroop() { Vill = this.Vill, ExecuteAt = DateTime.Now.Add(dur) }); } RepeatTask(Vill, troop, executeNext); return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { await base.Execute(acc); if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Marketplace, "&t=5")) { return(TaskRes.Executed); } //get troop resource/time cost var troopCost = TroopCost.GetResourceCost(TrainTask.Troop, TrainTask.Great); var trainNum = TroopsHelper.TroopsToFill(acc, TargetVill, TrainTask.Troop, TrainTask.Great); //how many troops we can train with resources that we have var mainVillResStored = Vill.Res.Stored.Resources.ToArray(); var targetVillStoredRes = TargetVill.Res.Stored.Resources.ToArray(); // Max troops we can train with resources that we have var maxTroopsToTrain = ResourcesHelper.MaxTroopsToTrain(mainVillResStored, targetVillStoredRes, troopCost); // If we don't have enough rsoruces to train the number of troops that we want, we will train max number of troops that we can if (maxTroopsToTrain < trainNum) { trainNum = maxTroopsToTrain; } //calculate how many resources we need to train trainNum of troops long[] neededRes = troopCost.Select(x => x * trainNum).ToArray(); //if we have already enough resources in the target village, no need to send anything if (ResourcesHelper.IsEnoughRes(targetVillStoredRes, neededRes)) { this.TrainTask.ExecuteAt = DateTime.Now; TaskExecutor.ReorderTaskList(acc); return(TaskRes.Executed); } //amount of resources we want to transit to target village var sendRes = ResourcesHelper.SendAmount(targetVillStoredRes, neededRes); // Check how many merchants we have. If we have 0, wait till some come back. var transitTimespan = await MarketHelper.MarketSendResource(acc, sendRes, TargetVill, this); //train the troops in the target village after we send the needed this.TrainTask.ExecuteAt = DateTime.Now.Add(transitTimespan).AddSeconds(5); TaskExecutor.ReorderTaskList(acc); //TODO: Update marketplace sending return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { var wb = acc.Wb.Driver; if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Marketplace)) { return(TaskRes.Executed); } var npcMerchant = acc.Wb.Html.DocumentNode.Descendants("div").FirstOrDefault(x => x.HasClass("npcMerchant")); var npcButton = npcMerchant.Descendants("button").FirstOrDefault(x => x.HasClass("gold")); // Exchange resources button await DriverHelper.ClickById(acc, npcButton.Id); var resSum = Parser.RemoveNonNumeric(acc.Wb.Html.GetElementbyId("remain").InnerText); var targetRes = MarketHelper.NpcTargetResources(Vill, resSum); if (!Vill.Market.Npc.NpcIfOverflow && MarketHelper.NpcWillOverflow(Vill, targetRes)) { return(TaskRes.Executed); } for (int i = 0; i < 4; i++) { //await acc.Wb.Driver.FindElementById($"m2[{i}]").Write(targetRes[i]); switch (acc.AccInfo.ServerVersion) { case Classificator.ServerVersionEnum.T4_4: await DriverHelper.ExecuteScript(acc, $"document.getElementById('m2[{i}]').value='{targetRes[i]}'"); break; case Classificator.ServerVersionEnum.T4_5: await DriverHelper.ExecuteScript(acc, $"document.getElementsByName('desired{i}')[0].value='{targetRes[i]}'"); break; } } var submit = acc.Wb.Html.GetElementbyId("submitText"); var distribute = submit.Descendants("button").FirstOrDefault(); await DriverHelper.ExecuteScript(acc, $"document.getElementById('{distribute.Id}').click()"); wb.ExecuteScript($"document.getElementById('npc_market_button').click()"); //Exchange resources button return(TaskRes.Executed); }
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); }
public override async Task <TaskRes> Execute(Account acc) { if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.TownHall)) { return(TaskRes.Executed); } var celebrationEnd = TimeParser.GetCelebrationTime(acc.Wb.Html); if (DateTime.Now <= celebrationEnd) { // We already have a celebration running Vill.Expansion.CelebrationEnd = celebrationEnd; this.NextExecute = celebrationEnd; return(TaskRes.Executed); } var buildingNode = acc.Wb.Html.GetElementbyId("build"); (_, var level) = InfrastructureParser.UpgradeBuildingGetInfo(buildingNode); var bigCeleb = Vill.Expansion.Celebrations == CelebrationEnum.Big && 10 <= level; // Check if enough resources to start a celebration if (!MiscCost.EnoughResForCelebration(Vill, bigCeleb)) { ResourcesHelper.NotEnoughRes(acc, Vill, MiscCost.CelebrationCost(bigCeleb), this); return(TaskRes.Executed); } await StartCelebration(acc, bigCeleb); // Post task check for celebration duration Vill.Expansion.CelebrationEnd = TimeParser.GetCelebrationTime(acc.Wb.Html); if (Vill.Expansion.Celebrations != CelebrationEnum.None) { this.NextExecute = Vill.Expansion.CelebrationEnd; } return(TaskRes.Executed); }
/// <summary> /// Checks demolish time. /// </summary> /// <param name="acc">account</param> public async Task <DateTime> NextDemolishTime(Account acc) { // Is this needed? if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.MainBuilding)) { return(DateTime.Now); } var table = acc.Wb.Html.GetElementbyId("demolish"); if (table == null) //No building is being demolished { return(DateTime.Now); } //Re-execute the demolish building task var time = DateTime.Now.Add(TimeParser.ParseTimer(table)); return(time.AddSeconds(2)); }
public async Task UpdateTroopsTraining(Account acc) { foreach (var trainingBuilding in trainingBuildings) { if (!await VillageHelper.EnterBuilding(acc, Vill, trainingBuilding)) { continue; } // Mark troops that user can train in building as researched TroopsHelper.UpdateTroopsResearched(Vill, acc.Wb.Html); var ct = TroopsParser.GetTroopsCurrentlyTraining(acc.Wb.Html); switch (trainingBuilding) { case Classificator.BuildingEnum.Barracks: Vill.Troops.CurrentlyTraining.Barracks = ct; break; case Classificator.BuildingEnum.Stable: Vill.Troops.CurrentlyTraining.Stable = ct; break; case Classificator.BuildingEnum.GreatBarracks: Vill.Troops.CurrentlyTraining.GB = ct; break; case Classificator.BuildingEnum.GreatStable: Vill.Troops.CurrentlyTraining.GS = ct; break; case Classificator.BuildingEnum.Workshop: Vill.Troops.CurrentlyTraining.Workshop = ct; break; } await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf2.php"); await Task.Delay(AccountHelper.Delay()); } }
public override async Task <TaskRes> Execute(Account acc) { if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Marketplace, "&t=5")) { return(TaskRes.Executed); } if (this.Vill.Settings.Type == VillType.Support && this.Vill.Settings.SendRes) { // Repeat this task var ran = new Random(); this.NextExecute = DateTime.Now.AddMinutes(ran.Next(30, 60)); } var mainVill = AccountHelper.GetMainVillage(acc); var res = MarketHelper.GetResToMainVillage(Vill); var ret = await MarketHelper.MarketSendResource(acc, res, mainVill, this); return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { TaskExecutor.RemoveSameTasksForVillage(acc, Vill, typeof(UpdateDorf1), this); TaskExecutor.RemoveSameTasksForVillage(acc, Vill, typeof(UpdateDorf2), this); await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php"); // Update dorf1 await Task.Delay(AccountHelper.Delay()); await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf2.php"); // Update dorf2 // On new village import the building tasks if (ImportTasks && !string.IsNullOrEmpty(acc.NewVillages.BuildingTasksLocationNewVillage)) { IoHelperCore.AddBuildTasksFromFile(acc, Vill, acc.NewVillages.BuildingTasksLocationNewVillage); } await UpdateTroopsResearchedAndLevels(acc); await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf2.php"); await Task.Delay(AccountHelper.Delay()); await UpdateTroopsTraining(acc); var firstTroop = TroopsHelper.TribeFirstTroop(acc.AccInfo.Tribe); Vill.Troops.TroopToTrain = firstTroop; Vill.Troops.Researched.Add(firstTroop); if (await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.TownHall)) { // Village has town hall, parse celebration duration Vill.Expansion.CelebrationEnd = TimeParser.GetCelebrationTime(acc.Wb.Html); } return(TaskRes.Executed); }
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); }
public override async Task <TaskRes> Execute(Account acc) { var building = Vill.Build.Buildings .FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Residence || x.Type == Classificator.BuildingEnum.Palace || x.Type == Classificator.BuildingEnum.CommandCenter ); if (!await VillageHelper.EnterBuilding(acc, Vill, building, "&s=1")) { return(TaskRes.Executed); } var settler = TroopsData.TribeSettler(acc.AccInfo.Tribe); var troopNode = acc.Wb.Html.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)settler)); if (troopNode == null) { acc.Wb.Log("No new settler can be trained, probably because 3 settlers are already (being) trained"); SendSettlersTask(acc); return(TaskRes.Executed); } while (!troopNode.HasClass("details")) { troopNode = troopNode.ParentNode; } var maxNum = Parser.RemoveNonNumeric(troopNode.ChildNodes.First(x => x.Name == "a").InnerText); var available = TroopsParser.ParseAvailable(troopNode); var costNode = acc.Wb.Html.DocumentNode.Descendants("div").FirstOrDefault(x => x.HasClass("resourceWrapper")); var cost = ResourceParser.GetResourceCost(costNode); if (!ResourcesHelper.IsEnoughRes(Vill, cost.ToArray())) { ResourcesHelper.NotEnoughRes(acc, Vill, cost, this); return(TaskRes.Executed); } acc.Wb.Driver.ExecuteScript($"document.getElementsByName('t10')[0].value='{maxNum}'"); await Task.Delay(AccountHelper.Delay()); // Click Train button await TbsCore.Helpers.DriverHelper.ExecuteScript(acc, "document.getElementById('s1').click()"); Vill.Troops.Settlers = (int)available + (int)maxNum; var training = TroopsHelper.TrainingDuration(acc.Wb.Html); if (training < DateTime.Now) { training = DateTime.Now; } if (Vill.Troops.Settlers < 3) { //In 1 minute, do the same task (to get total of 3 settlers) this.NextExecute = training.AddSeconds(3); } else { if (acc.NewVillages.AutoSettleNewVillages) { SendSettlersTask(acc); } } return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { var wb = acc.Wb.Driver; if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Marketplace, "&t=0")) { return(TaskRes.Executed); } var npcMerchant = acc.Wb.Html.DocumentNode.Descendants("div").FirstOrDefault(x => x.HasClass("npcMerchant")); var npcButton = npcMerchant.Descendants("button").FirstOrDefault(x => x.HasClass("gold")); // Exchange resources button await DriverHelper.ClickById(acc, npcButton.Id); //wait npc form show var timeout = DateTime.Now.AddSeconds(100); HtmlNode remainRes = null; do { await Task.Delay(1000); remainRes = acc.Wb.Html.GetElementbyId("remain"); if (timeout < DateTime.Now) { acc.Wb.Log($"NPC in village {Vill.Name} is time out. Retry after 3 mins"); this.NextExecute = DateTime.Now.AddMinutes(3); return(TaskRes.Executed); } }while (remainRes == null); var resSum = Parser.RemoveNonNumeric(remainRes.InnerText); var targetRes = MarketHelper.NpcTargetResources(Vill, resSum); if (!Vill.Market.Npc.NpcIfOverflow && MarketHelper.NpcWillOverflow(Vill, targetRes)) { acc.Wb.Log($"NPC in village {Vill.Name} will be overflow. Stop NPC"); return(TaskRes.Executed); } for (int i = 0; i < 4; i++) { //await acc.Wb.Driver.FindElementById($"m2[{i}]").Write(targetRes[i]); switch (acc.AccInfo.ServerVersion) { case Classificator.ServerVersionEnum.T4_4: await DriverHelper.ExecuteScript(acc, $"document.getElementById('m2[{i}]').value='{targetRes[i]}'"); break; case Classificator.ServerVersionEnum.T4_5: await DriverHelper.ExecuteScript(acc, $"document.getElementsByName('desired{i}')[0].value='{targetRes[i]}'"); break; } } var submit = acc.Wb.Html.GetElementbyId("submitText"); var distribute = submit.Descendants("button").FirstOrDefault(); await DriverHelper.ClickById(acc, distribute.Id); await DriverHelper.ClickById(acc, "npc_market_button"); return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { var building = Vill.Build.Buildings .FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Residence || x.Type == Classificator.BuildingEnum.Palace || x.Type == Classificator.BuildingEnum.CommandCenter ); if (!await VillageHelper.EnterBuilding(acc, building, "&s=1")) { return(TaskRes.Executed); } var settler = TroopsData.TribeSettler(acc.AccInfo.Tribe); var troopNode = acc.Wb.Html.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)settler)); if (troopNode == null) { acc.Wb.Log("No new settler can be trained, probably because 3 settlers are already (being) trained"); SendSettlersTask(acc); return(TaskRes.Executed); } while (!troopNode.HasClass("details")) { troopNode = troopNode.ParentNode; } var div = troopNode.Descendants("div"); Vill.Troops.Settlers = (int)Parser.RemoveNonNumeric(div.FirstOrDefault(x => x.HasClass("tit")).Descendants("span").FirstOrDefault().InnerText); string innertext = ""; switch (acc.AccInfo.ServerVersion) { case Classificator.ServerVersionEnum.T4_4: innertext = troopNode.ChildNodes.First(x => x.Name == "a").InnerText; break; case Classificator.ServerVersionEnum.T4_5: // no expansion slot if (div.FirstOrDefault(x => x.HasClass("noExpansionSlot")) != null) { if (Vill.Troops.Settlers >= 3) { if (acc.NewVillages.AutoSettleNewVillages) { TaskExecutor.AddTaskIfNotExists(acc, new SendSettlers() { ExecuteAt = DateTime.Now.AddHours(-3), Vill = this.Vill, // For high speed servers, you want to train settlers asap Priority = 1000 < acc.AccInfo.ServerSpeed ? TaskPriority.High : TaskPriority.Medium, }); } acc.Wb.Log("Have enoung settlers"); } else { acc.Wb.Log("Don't have enough expansion slot or settlers are training."); } return(TaskRes.Executed); } innertext = div.FirstOrDefault(x => x.HasClass("cta")).Descendants("a").FirstOrDefault().InnerText; break; } var maxNum = Parser.RemoveNonNumeric(innertext); Vill.Troops.Settlers = (int)TroopsParser.ParseAvailable(troopNode); var costNode = troopNode.Descendants("div").FirstOrDefault(x => x.HasClass("resourceWrapper")); var cost = ResourceParser.GetResourceCost(costNode); if (!ResourcesHelper.IsEnoughRes(Vill, cost.ToArray())) { ResourcesHelper.NotEnoughRes(acc, Vill, cost, this); return(TaskRes.Executed); } acc.Wb.Driver.ExecuteScript($"document.getElementsByName('t10')[0].value='{maxNum}'"); await Task.Delay(AccountHelper.Delay()); // Click Train button await TbsCore.Helpers.DriverHelper.ExecuteScript(acc, "document.getElementById('s1').click()"); Vill.Troops.Settlers += (int)maxNum; if (Vill.Troops.Settlers < 3) { // random train next settlers after 30 - 60 mins var ran = new Random(); this.NextExecute = DateTime.Now.AddMinutes(ran.Next(30, 60)); } else { if (acc.NewVillages.AutoSettleNewVillages) { SendSettlersTask(acc); } } return(TaskRes.Executed); }
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); }