コード例 #1
0
 public static void AddTaskIfNotExistInVillage(Account acc, Village vill, BotTask task)
 {
     if (!TaskExistsInVillage(acc, vill, task.GetType()))
     {
         AddTask(acc, task);
     }
 }
コード例 #2
0
ファイル: TaskExecutor.cs プロジェクト: s3111/TravianBotSharp
 public static void AddTaskIfNotExists(Account acc, BotTask task)
 {
     if (!acc.Tasks.Any(x => x.GetType() == task.GetType()))
     {
         AddTask(acc, task);
     }
 }
コード例 #3
0
 public static void AddTaskIfNotExists(Account acc, BotTask task)
 {
     if (!acc.Tasks.Any(x => x.GetType() == task.GetType() && x.ExecuteAt > DateTime.Now)) //there is no such BotTask (that will be executed in more than 10sec), add it
     {
         AddTask(acc, task);
     }
 }
コード例 #4
0
        /// <summary>
        /// Gets name of the task
        /// </summary>
        /// <returns>Name of the task</returns>
        public static string GetName(this BotTask task)
        {
            var type = task.GetType().ToString().Split('.');

            if (type.Length == 0)
            {
                return(null);
            }
            return(type[type.Length - 1]);
        }
コード例 #5
0
ファイル: TaskExecutor.cs プロジェクト: s3111/TravianBotSharp
        public static void AddTaskIfNotExistInVillage(Account acc, Village vill, BotTask task)
        {
            var taskExists = acc.Tasks.Any(x =>
                                           x.GetType() == task.GetType() &&
                                           x.Vill == vill
                                           );

            if (!taskExists)
            {
                AddTask(acc, task);
            }
        }
コード例 #6
0
ファイル: TaskList.cs プロジェクト: Erol444/TravianBotSharp
        public void Add(BotTask task, bool IfNotExists = false, Village vill = null)
        {
            if (IfNotExists && IsTaskExists(task.GetType(), vill))
            {
                return;
            }

            if (task.ExecuteAt == null)
            {
                task.ExecuteAt = DateTime.Now;
            }

            _tasks.Add(task);
            ReOrder();
        }
コード例 #7
0
ファイル: TaskExecutor.cs プロジェクト: s3111/TravianBotSharp
        /// <summary>
        /// Called PageLoaded (after navigating to a specific url) or from
        /// Task timer, if there is no url/bot is already on the url
        /// </summary>
        /// <param name="acc">Account</param>
        /// <param name="task">Task to be executed</param>
        /// <returns></returns>
        public static async Task Execute(Account acc, BotTask task)
        {
            // Before every execution, wait a random delay
            if (acc.AccInfo.ServerVersion == Classificator.ServerVersionEnum.T4_5)
            {
                await Task.Delay(AccountHelper.Delay());
            }

            if (acc.Wb?.CurrentUrl == null && task.GetType() != typeof(CheckProxy))
            {
                await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php");
            }

            if (task.Vill == null)
            {
                task.Vill = acc.Villages.FirstOrDefault(x => x.Active);
            }

            try
            {
                acc.Wb.Log($"Executing task {task.GetName()}" + (task.Vill == null ? "" : $" in village {task.Vill.Name}"));

                switch (await task.Execute(acc))
                {
                case TaskRes.Retry:
                    task.RetryCounter++;
                    if (task.NextExecute == null)
                    {
                        task.NextExecute = DateTime.Now.AddMinutes(3);
                    }
                    break;

                default:
                    task.RetryCounter = 0;
                    if (task.NextTask != null)
                    {
                        task.NextTask.ExecuteAt = DateTime.MinValue.AddHours(5);
                        task.NextTask.Stage     = TaskStage.Start;
                        TaskExecutor.AddTask(acc, task.NextTask);
                        task.NextTask = null;
                    }
                    break;
                }
            }
            catch (Exception e)
            {
                if (acc.Wb != null)
                {
                    acc.Wb.Log($"Error executing task {task.GetName()}! Vill {task.Vill?.Name}", e);
                }
                task.RetryCounter++;
                if (task.NextExecute == null)
                {
                    task.NextExecute = DateTime.Now.AddMinutes(3);
                }
            }

            //We want to re-execute the same task later
            if (task.NextExecute != null && task.RetryCounter < 3)
            {
                task.ExecuteAt   = task.NextExecute ?? default;
                task.NextExecute = null;
                ReorderTaskList(acc);
                task.Stage = TaskStage.Start;
                return;
            }
            // Remove the task from the task list
            acc.Tasks.Remove(task);
        }
コード例 #8
0
ファイル: TaskExecutor.cs プロジェクト: s3111/TravianBotSharp
 /// <summary>
 /// Removes all pending BotTasks of specific type except for the task calling it
 /// </summary>
 /// <param name="acc">Account</param>
 /// <param name="thisTask">Task not to remove</param>
 public static void RemoveSameTasks(Account acc, BotTask thisTask) =>
 RemoveSameTasks(acc, thisTask.GetType(), thisTask);
コード例 #9
0
        private async void NewTick()
        {
            // Dirty hack. TODO fix the code, so building/troops filling tasks won't fail by themselves
            restartTasksCounter++;
            if (restartTasksCounter > 7200)
            {
                restartTasksCounter = 0;
                foreach (var vill in acc.Villages)
                {
                    if (!TroopsHelper.EverythingFilled(acc, vill))
                    {
                        TroopsHelper.ReStartTroopTraining(acc, vill);
                    }
                    BuildingHelper.ReStartBuilding(acc, vill);
                }
            }

            if (acc.Tasks.Count == 0)
            {
                return;                       //No tasks
            }
            // Another task is already in progress. wait
            var taskInProgress = acc.Tasks.FirstOrDefault(x => x.Stage != TaskStage.Start);

            if (taskInProgress != null)
            {
                if (taskInProgress.DurationCounter++ > 30) //after 20sec try to re-execute the task
                {
                    Console.WriteLine($"Task {taskInProgress} timed out. Restarting it..");
                    taskInProgress.DurationCounter = 15;
                    taskInProgress.Stage           = TaskStage.Start; //re-navigate & execute
                                                                      //We have tried re-executing the task 3 times already, something is clearly wrong. Just delete the task.
                    if (++taskInProgress.RetryCounter > 3)
                    {
                        acc.Tasks.Remove(taskInProgress);
                    }
                }
                return;
            }

            var tasks = acc.Tasks.Where(x => x.ExecuteAt <= DateTime.Now).ToList();

            if (tasks.Count == 0)
            {
                return;                   // No tasks yet
            }
            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();
            }

            //Console.WriteLine($"---Tasks: {acc.Tasks.Count}, first one {firstTask.GetType()}");


            if (firstTask.Stage != TaskStage.Start)
            {
                return;
            }
            //If correct village is selected, otherwise change village
            if (firstTask.vill != null)
            {
                var active = acc.Villages.FirstOrDefault(x => x.Active);
                if (active != null) //error handling
                {
                    if (firstTask.vill != active && firstTask.GetType() != typeof(SwitchVillage))
                    {
                        //wrong village selected, reselect village
                        TaskExecutor.AddTask(acc, new SwitchVillage()
                        {
                            vill = firstTask.vill, Priority = TaskPriority.High
                        });
                        return;
                    }
                }
            }

            firstTask.Stage = TaskStage.Executing;
            _ = TaskExecutor.Execute(acc, firstTask);
        }
コード例 #10
0
        private static string LogHelper(Account acc, BotTask task, string type)
        {
            var msg = $"Account {acc.AccInfo.Nickname}, \nserver {acc.AccInfo.ServerUrl}, \ncurrent url {acc.Wb.CurrentUrl}\n";

            return(msg + $"Task: {task.GetType()}, village {task.vill?.Name} encountered a {type}");
        }