Example #1
0
        /// <summary>
        /// Generates a random number of milliseconds for the bot to sleep at night
        /// </summary>
        /// <returns>the number of milliseconds for the bot to sleep</returns>
        private int RandomSleepTime()
        {
            double avgSleepLength    = UnitConversions.HoursToMilliseconds(14);
            double standardDeviation = UnitConversions.MinutesToMilliseconds(30);
            int    breakLength       = (int)Probability.RandomGaussian(avgSleepLength, standardDeviation);

            return(breakLength);
        }
Example #2
0
        /// <summary>
        /// Makes sure that a client is running and starts it if it isn't
        /// </summary>
        /// <param name="forceRestart">Set to true to force a client restart even if the client is already running</param>
        /// <returns>true if client is successfully prepared</returns>
        public bool PrepareClient(bool forceRestart = false)
        {
            if (!forceRestart && ScreenScraper.ProcessExists(Value))
            {
                return(true);
            }

            Process   client    = null;
            Stopwatch longWatch = new Stopwatch();

            longWatch.Start();
            while (longWatch.ElapsedMilliseconds < UnitConversions.HoursToMilliseconds(24) && !BotProgram.StopFlag)
            {
                if (!ScreenScraper.RestartClient(ref client, RunParams.RuneScapeClient, RunParams.ClientFlags))
                {
                    BotProgram.SafeWait(5000);
                    continue;
                }
                //Successful restart
                Value = client;

                Stopwatch watch = new Stopwatch();
                watch.Start();
                //Wait for cient to be visually recognized.
                do
                {
                    BotProgram.SafeWait(UnitConversions.SecondsToMilliseconds(5));
                    if (Screen.ReadWindow(false) && (Screen.IsLoggedOut(false) || Screen.IsLoggedIn(false)))
                    {
                        return(true);
                    }
                }while ((watch.ElapsedMilliseconds < UnitConversions.MinutesToMilliseconds(5)) && !BotProgram.StopFlag);
            }

            if (!BotProgram.StopFlag)
            {
                const string errorMessage = "Client did not start correctly";
                MessageBox.Show(errorMessage);
            }

            Value = null;
            return(false);
        }
Example #3
0
        /// <summary>
        /// Handles break, sleep, and rotation cycles
        /// </summary>
        protected virtual void ManageBot()
        {
            Screen.MakeSureWindowHasBeenRead();
            if ((RunParams.RunLoggedIn && !CheckLogIn(true)) || !Run())
            {
                return;
            }

            if (RunParams.SlaveDriver)
            {
                while (!StopFlag && !Iterate())
                {
                    continue;
                }
                return;
            }

            double    avgWorkTime            = 84.8;
            double    avgRestTime            = 25.1;
            double    avgStatusTimeRemaining = (avgRestTime / (avgRestTime + avgWorkTime)) * (avgRestTime / 2) + (avgWorkTime / (avgRestTime + avgWorkTime)) * (avgWorkTime / 2); //expected time left in minutes to finish working or resting when it is time for the bot to sleep
            int       awakeTime  = UnitConversions.HoursToMilliseconds(10 - (avgStatusTimeRemaining / 60.0));
            Stopwatch sleepWatch = new Stopwatch();
            bool      done       = false;

            sleepWatch.Start();

            //alternate between work periods (Iterate) and break periods
            //Works for an average of 7.716 hours during each 10 hour awake period
            while (!(done = Iterate()))
            {
                if (sleepWatch.ElapsedMilliseconds < awakeTime) //rest before another work cycle
                {
                    Logout();
                    done = Break();
                }
                else  //get a good night's sleep before resuming
                {
                    Logout();
                    done = Sleep();
                    sleepWatch.Restart();
                }
            }
        }
        public void HoursToMillisecondsTest(double hours, int expectedMS)
        {
            int actualMS = UnitConversions.HoursToMilliseconds(hours);

            Assert.AreEqual(actualMS, expectedMS);
        }
Example #5
0
 /// <summary>
 /// Sets the start time and length of a new bot state
 /// </summary>
 /// <param name="stateLength">length of the new bot state in milliseconds</param>
 public void SetNewState(long stateLength)
 {
     stateLength       = (long)Numerical.LimitToRange(stateLength, 0, UnitConversions.HoursToMilliseconds(168)); //limit state length to 1 week as a sanity check
     CurrentStateStart = DateTime.Now;
     CurrentStateEnd   = CurrentStateStart.AddMilliseconds(stateLength);
 }