//main method to decide what bot should build void ManageBuildings(Village vill) { managingbuildings = true; //to delete, temp try for debug try { mainform.ChangeLabel("Building", Color.Yellow, Form1.MainLabels.BotTask); } catch (Exception ex) { } //if (!ManageBuilding) return; unnecessary? if (vill != village) { //navigate to correct village } //Check if queue is not full string actuallurl = driver.Url; int idx = actuallurl.IndexOf("screen") + 6; actuallurl = actuallurl.Substring(0, idx + 1) + "main"; driver.Navigate().GoToUrl(actuallurl); village.UpdateResourcesMain(); //TODO Add checking when queue will be empty then apply to timer try //catch is happen when theres no buildings in queue { if (driver.FindElement(By.Id("build_queue")) != null) { List <IWebElement> queueelements = new List <IWebElement>(driver.FindElement(By.Id("build_queue")).FindElements(By.TagName("tr"))); if (queueelements.Count > 3) { mainform.ChangeLabel("Place in build queue", Color.Black, Form1.MainLabels.Missing); buildtimer.Interval = 300 * 1000; //TODO rework return; // cuz of no place in queue } } } catch (Exception ex) { } string[] buildorder = StaticVariables.BuildOrder; List <order> helplist = new List <order>(); // keeping levels needed to be for (int i = 0; i < buildorder.Length; i++) { bool founded = false; int mainorderidx = 0; foreach (order ord in helplist) { if (ord.buildid == buildorder[i]) { ord.level++; founded = true; mainorderidx = helplist.IndexOf(ord); break; } } if (!founded) { helplist.Add(new order() { buildid = buildorder[i], level = 1 }); mainorderidx = helplist.Count - 1; } order mainorder = helplist[mainorderidx]; foreach (Building build in vill.buildings) { if (mainorder.buildid == build.name) { mainform.ChangeLabel(StaticVariables.BuildingIDtoName(build.name), Color.Black, Form1.MainLabels.Bulding); if (build.level < mainorder.level) { if (build.nlclay < vill.stone && build.nliron < vill.iron && build.nlpop < (vill.popCapMax - vill.popCap) && build.nlwood < vill.wood) { Build(mainorder.buildid, mainorder.level); buildtimer.Interval = 10000; return; } else { mainform.ChangeLabel("Resources", Color.OrangeRed, Form1.MainLabels.Missing); //Calculate time to avaibilty try { string timetoavastring = driver.FindElement(By.Id(mainorder.buildid)).FindElement(By.ClassName("inactive")).Text; int middle = timetoavastring.IndexOf(':'); timetoavastring = timetoavastring.Substring(middle - 2, 5); DateTime timeToAva = DateTime.Parse(timetoavastring); TimeSpan time = timeToAva - DateTime.Now; buildtimer.Interval = time.TotalSeconds > 300 ? 300 * 1000 : time.TotalMilliseconds; } catch (Exception ex) { buildtimer.Interval = 10000; } managingbuildings = false; // not enough resources return; } } else { break; } } } } }
public async void LogginIn() { //Launching browser try { ChromeOptions options = new ChromeOptions(); options.AddArguments("--incognito"); driver = new ChromeDriver("./", options); driver.Navigate().GoToUrl(StaticVariables.TribalWarsURL); driver.Navigate().GoToUrl(StaticVariables.TribalWarsURL); }catch (Exception ex) { MessageBox.Show("Problem with launching chromedriver : " + ex); throw new Exception("Problem occured while launching chromedriver : " + ex); } if (autologin.Checked) { //logging try { driver.FindElement(By.Id("user")).SendKeys(usernamebox.Text); driver.FindElement(By.Id("password")).SendKeys(passwordbox.Text); var pagecontent = driver.PageSource; driver.FindElement(By.ClassName("btn-login")).Click(); int timeout = 10000; while (pagecontent == driver.PageSource) { timeout--; if (timeout < 0) { BotStatusLabel.Text = "LOGIN IN FAILED"; BotStatusLabel.ForeColor = Color.Red; } } } catch (Exception ex) { throw new Exception("Error while loggin in " + ex); } //Entering world try { checkforworlds: List <IWebElement> activeworlds = new List <IWebElement>(driver.FindElements(By.ClassName("world_button_active"))); if (activeworlds.Count == 0) { goto checkforworlds; } bool worldlocated = false; foreach (IWebElement element in activeworlds) { string worldnumber = element.Text; if (-1 < worldnumber.IndexOf(WorldNumberBox.Text)) { worldlocated = true; element.Click(); break; } } if (!worldlocated) { //throw error } //Enabling village //TODO Find all villages villagefirst = new Village(driver); villagefirst.UpdateResources(); bot = new Bot(villagefirst, driver, this); //Starting timer for update villages aTimer = new System.Timers.Timer(); aTimer.Interval = 2000; aTimer.Elapsed += OnTimedEvent; aTimer.AutoReset = true; aTimer.Enabled = true; } catch (Exception ex) { throw new Exception("Problem while entering world" + ex); } } else { throw new NotImplementedException("Manual loggin feauture is disabled for now"); } }