Skip to content

Commit

Permalink
make bot less supicious
Browse files Browse the repository at this point in the history
  • Loading branch information
vinaghost committed Nov 15, 2021
1 parent faef1be commit c04ae20
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
4 changes: 2 additions & 2 deletions TbsCore/Helpers/TimeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public static async Task SleepUntilPrioTask(Account acc, TaskPriority lowestPrio
do
{
await Task.Delay(1000);
nextTask = TimeHelper.NextPrioTask(acc, lowestPrio);
nextTask = NextPrioTask(acc, lowestPrio);

var log = $"Chrome will reopen in {(int)nextTask.TotalMinutes} min";
var log = $"Chrome will reopen in {(int)nextTask.TotalMinutes} min(s)";
if (log != previousLog)
{
acc.Logger.Information(log);
Expand Down
16 changes: 9 additions & 7 deletions TbsCore/Tasks/LowLevel/Basic/ChangeAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ namespace TbsCore.Tasks.LowLevel
/// </summary>
public class ChangeAccess : BotTask
{
public int? WaitSecMin { get; set; }
public int? WaitSecMax { get; set; }
private int WaitSecMin;
private int WaitSecMax;

public ChangeAccess(int min = 30, int max = 600, DateTime executeAt = default) : base(null, executeAt, TaskPriority.High)
{
WaitSecMin = min;
WaitSecMax = max;
}

public override async Task<TaskRes> Execute(Account acc)
{
acc.Wb.Dispose();

//TODO: make this configurable (wait time between switches)
var rand = new Random();
int sleepSec = rand.Next(WaitSecMin ?? 30, WaitSecMax ?? 600);
var sleepSec = rand.Next(WaitSecMin, WaitSecMax);
var sleepEnd = DateTime.Now.AddSeconds(sleepSec);

await TimeHelper.SleepUntilPrioTask(acc, TaskPriority.High, sleepEnd);

await acc.Wb.InitSelenium(acc);

// Remove all other ChangeAccess tasks
acc.Tasks.Remove(typeof(ChangeAccess), thisTask: this);

var nextProxyChange = TimeHelper.GetNextProxyChange(acc);
if (nextProxyChange != TimeSpan.MaxValue)
{
Expand Down
5 changes: 1 addition & 4 deletions TbsCore/Tasks/LowLevel/Basic/UpdateVillage.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Linq;
using System;
using System.Threading.Tasks;
using TbsCore.Helpers;
using TbsCore.Models.AccModels;
using TbsCore.TravianData;
using TbsCore.Parsers;
using System;
using TbsCore.Models.VillageModels;
using static TbsCore.Helpers.Classificator;

namespace TbsCore.Tasks.LowLevel
{
Expand Down
39 changes: 30 additions & 9 deletions TbsCore/Tasks/LowLevel/BotTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ public abstract class BotTask
/// <summary>
/// If of the village to execute the task. If null, don't change village before executing the task
/// </summary>
public Village Vill { get; set; }
protected Village _vill;

public Village Vill
{
get
{
return _vill;
}
}

/// <summary>
/// Stage in which task is currently in.
Expand All @@ -32,14 +40,6 @@ public abstract class BotTask
/// </summary>
public BotTask NextTask { get; set; }

/// <summary>
/// After each execution, if return bool is true, there has to be a new browser load event. Bot will wait for that event.
/// If there is no browser load event (just parsing some data eg. GetMapSize, return false and browser will navigate to dorf1/2.
/// </summary>
/// <param name="acc">Account</param>
/// <returns>TaskRes</returns>
public abstract Task<TaskRes> Execute(Account acc);

/// <summary>
/// Counts how many times we retried executing the task. After 3rd try, stop retrying. Something is clearly wrong
/// Used in TaskExecutor and TaskTimer
Expand All @@ -52,6 +52,27 @@ public abstract class BotTask
/// </summary>
public TaskPriority Priority { get; set; }

/// <summary>
///
/// </summary>
/// <param name="vill"></param>
/// <param name="executeAt"></param>
/// <param name="priority"></param>
public BotTask(Village vill = null, DateTime executeAt = default, TaskPriority priority = TaskPriority.Medium)
{
_vill = vill;
ExecuteAt = executeAt;
Priority = priority;
}

/// <summary>
/// After each execution, if return bool is true, there has to be a new browser load event. Bot will wait for that event.
/// If there is no browser load event (just parsing some data eg. GetMapSize, return false and browser will navigate to dorf1/2.
/// </summary>
/// <param name="acc">Account</param>
/// <returns>TaskRes</returns>
public abstract Task<TaskRes> Execute(Account acc);

public enum TaskRes
{
Executed,
Expand Down

0 comments on commit c04ae20

Please sign in to comment.