Exemplo n.º 1
0
        //TODO Pass list of villages
        public Bot(Village village, IWebDriver driver, Form1 mainform)
        {
            mainform.ChangeLabel("Gathering info..", Color.Yellow, Form1.MainLabels.BotStatus);

            //Assing variables
            buildtimer     = new System.Timers.Timer();
            farmtimer      = new System.Timers.Timer();
            FarmTargets    = mainform.farmsettings.GetFarmList();
            this.mainform  = mainform;
            ManageBuilding = mainform.GetCheckBox(Form1.MainCheckBoxes.Build);
            this.driver    = driver;
            this.village   = village;

            //Navigate to main screen
            if (driver.Url.Contains("overview"))
            {
                village.UpdateResourcesOverview();
            }
            string actaullurl = driver.Url;

            if (actaullurl.Contains("overview&intro"))
            {
                actaullurl = actaullurl.Replace("overview&intro", "main");
            }
            else
            {
                actaullurl = actaullurl.Replace("overview", "main");
            }
            driver.Navigate().GoToUrl(actaullurl);
            village.UpdateResourcesMain();


            //Start timer which managing building
            if (buildtimer.Interval == 100)
            {
                buildtimer.Interval = 1000;
            }
            buildtimer.Elapsed  += BuildCheck;
            buildtimer.AutoReset = true;
            buildtimer.Enabled   = true;

            //Start timer which managing farm
            if (farmtimer.Interval == 100)
            {
                farmtimer.Interval = 1000;
            }
            farmtimer.Elapsed  += FarmCheck;
            farmtimer.AutoReset = true;
            farmtimer.Enabled   = true;

            mainform.ChangeLabel("Working!", Color.Green, Form1.MainLabels.BotStatus);
        }
Exemplo n.º 2
0
        //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;
                        }
                    }
                }
            }
        }