public override async Task <TaskRes> Execute(Account acc) { // Tribe => id="questmasterButton", class name vid_{tribeId} // If no questmasterButton, check tribe after updating villages => rally point/barracks base.MinWait = 500; base.MaxWait = 3000; await base.Execute(acc); var questMaster = acc.Wb.Html.GetElementbyId("questmasterButton"); if (questMaster != null) { var vid = questMaster.GetClasses().FirstOrDefault(x => x.StartsWith("vid")); var tribeId = Parser.RemoveNonNumeric(vid); SetTribe(acc, (Classificator.TribeEnum)tribeId); return(TaskRes.Executed); } await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf2.php"); await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id=39"); List <int> idsChecked = new List <int>(acc.Villages.Count); // If no rally point, navigate somewhere else while (acc.Wb.Html.GetElementbyId("contract") == null) { idsChecked.Add(this.Vill.Id); var nextId = NextVillCheck(acc, idsChecked); if (nextId == 0) { throw new System.Exception("Can't get account tribe! Please build rally point!"); } await VillageHelper.SwitchVillage(acc, nextId); } await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id=39&tt=2"); var unitImg = acc.Wb.Html.DocumentNode.Descendants("img").First(x => x.HasClass("unit")); var unitInt = Parser.RemoveNonNumeric(unitImg.GetClasses().First(x => x != "unit")); int tribeInt = (int)(unitInt / 10); // ++ since the first element in Classificator.TribeEnum is Any, second is Romans. tribeInt++; SetTribe(acc, (Classificator.TribeEnum)tribeInt); return(TaskRes.Executed); }
private async void NewTick() { try { if (acc.Tasks.Count == 0) { return; //No tasks } // Another task is already in progress. wait if (acc.Tasks.Any(x => x.Stage != TaskStage.Start)) { return; } var tasks = acc.Tasks.Where(x => x.ExecuteAt <= DateTime.Now).ToList(); if (tasks.Count == 0) { NoTasks(acc); return; } BotTask firstTask = tasks.FirstOrDefault(x => x.Priority == TaskPriority.High); if (firstTask == null) { firstTask = tasks.FirstOrDefault(x => x.Priority == TaskPriority.Medium); } if (firstTask == null) { firstTask = tasks.FirstOrDefault(); } firstTask.Stage = TaskStage.Executing; //If correct village is selected, otherwise change village if (firstTask.Vill != null) { var active = acc.Villages.FirstOrDefault(x => x.Active); if (active != null && active != firstTask.Vill) { await VillageHelper.SwitchVillage(acc, firstTask.Vill.Id); } } await TaskExecutor.Execute(acc, firstTask); } catch (Exception e) { acc?.Wb?.Log($"Error in TaskTimer! {e.Message}\n{e.StackTrace}"); } }
private async void NewTick() { if (acc.Tasks.Count == 0) { return; //No tasks } // Another task is already in progress. wait if (acc.Tasks.IsTaskExcuting()) { return; } var tasks = acc.Tasks.GetTasksReady(); if (tasks.Count == 0) { NoTasks(acc); return; } BotTask firstTask = tasks.FirstOrDefault(x => x.Priority == TaskPriority.High); if (firstTask == null) { firstTask = tasks.FirstOrDefault(x => x.Priority == TaskPriority.Medium); } if (firstTask == null) { firstTask = tasks.FirstOrDefault(); } firstTask.Stage = TaskStage.Executing; //If correct village is selected, otherwise change village if (firstTask.Vill != null) { var active = acc.Villages.FirstOrDefault(x => x.Active); if (active != null && active != firstTask.Vill) { try { await VillageHelper.SwitchVillage(acc, firstTask.Vill.Id); } catch (WebDriverException e) when(e.Message.Contains("chrome not reachable") || e.Message.Contains("no such window:")) { acc.Logger.Warning($"Chrome has problem. Try reopen Chrome"); acc.Tasks.Add(new ReopenDriver() { ExecuteAt = DateTime.MinValue, Priority = TaskPriority.High, ReopenAt = DateTime.MinValue }); firstTask.Stage = TaskStage.Start; return; } } } await TaskExecutor.Execute(acc, firstTask); }
public override async Task <TaskRes> Execute(Account acc) { var wb = acc.Wb.Driver; await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?tt=99&id=39"); var flNode = GetFlNode(acc.Wb.Html, acc.AccInfo.ServerVersion); // If there is no rally point, switch to different village if (flNode == null) { var mainVill = AccountHelper.GetMainVillage(acc); if (mainVill == this.Vill) { return(TaskRes.Executed); // No gold account? } await VillageHelper.SwitchVillage(acc, mainVill.Id); flNode = GetFlNode(acc.Wb.Html, acc.AccInfo.ServerVersion); if (flNode == null) { return(TaskRes.Retry); } } if (acc.Farming.TrainTroopsAfterFL) // For TTWars servers { TaskExecutor.AddTask(acc, new TrainTroops() { ExecuteAt = DateTime.Now.AddSeconds(2), Troop = Vill.Troops.TroopToTrain ?? Classificator.TroopsEnum.Hero, Vill = this.Vill, HighSpeedServer = true }); } // If FL is collapsed, expand it if (acc.AccInfo.ServerVersion == ServerVersionEnum.T4_4 || flNode.Descendants("div").Any(x => x.HasClass("expandCollapse") && x.HasClass("collapsed"))) { await DriverHelper.ExecuteScript(acc, $"Travian.Game.RaidList.toggleList({this.FL.Id});"); // Update flNode! flNode = GetFlNode(acc.Wb.Html, acc.AccInfo.ServerVersion); } foreach (var farm in flNode.Descendants("tr").Where(x => x.HasClass("slotRow"))) { //iReport2 = yellow swords, iReport3 = red swords, iReport1 = successful raid var img = farm.ChildNodes.FirstOrDefault(x => x.HasClass("lastRaid"))?.Descendants("img"); //there has to be an image (we already have a report) and wrong raid style to not check this farmlist: if (img.Count() != 0 && ( //no image -> no recent attack (img.FirstOrDefault(x => x.HasClass("iReport3")) != null && this.FL.RaidStyle != RaidStyle.RaidLost) || //raid was lost and we don't have RaidLost raidstyle (img.FirstOrDefault(x => x.HasClass("iReport2")) != null && (this.FL.RaidStyle == RaidStyle.RaidSuccessful)) //some casualities, but we only attack on successful )) { continue; } var checkbox = farm.Descendants("input").FirstOrDefault(x => x.HasClass("markSlot")); await DriverHelper.CheckById(acc, checkbox.Id, true, update : false); } await Task.Delay(AccountHelper.Delay() * 2); switch (acc.AccInfo.ServerVersion) { case ServerVersionEnum.T4_4: var sendFlScript = $"document.getElementById('{flNode.Id}').childNodes[1].submit()"; wb.ExecuteScript(sendFlScript); break; case ServerVersionEnum.T4_5: var startRaid = flNode.Descendants("button").FirstOrDefault(x => x.HasClass("startButton")); acc.Wb.Driver.FindElementById(startRaid.Id).Click(); break; } acc.Wb.Log($"FarmList '{this.FL.Name}' was sent"); return(TaskRes.Executed); }