Skip to content

Commit b30e7f3

Browse files
authored
Merge pull request #99 from Erol444/fix-14-04-2022
Fix 15 04 2022
2 parents 3b0e7c0 + 403c27f commit b30e7f3

File tree

13 files changed

+281
-256
lines changed

13 files changed

+281
-256
lines changed

TbsCore/Core/TaskExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static async Task PageLoaded(Account acc)
2525
if (IsCaptcha(acc) || IsWWMsg(acc) || IsBanMsg(acc) || IsMaintanance(acc)) //Check if a captcha/ban/end of server/maintanance
2626
{
2727
acc.Logger.Warning("Captcha/WW/Ban/Maintanance found! Stopping bot for this account!");
28-
acc.TaskTimer.ForceTimerStop();
28+
acc.TaskTimer.Stop();
2929
}
3030
if (CheckCookies(acc))
3131
await DriverHelper.ExecuteScript(acc, "document.getElementById('CybotCookiebotDialogBodyLevelButtonLevelOptinDeclineAll').click();");
@@ -92,7 +92,7 @@ public static async Task Execute(Account acc, BotTask task)
9292
var result = await acc.Wb.Init(acc);
9393
if (!result)
9494
{
95-
acc.TaskTimer.ForceTimerStop();
95+
acc.TaskTimer.Stop();
9696
return;
9797
}
9898
}

TbsCore/Core/TaskTimer.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ public sealed class TaskTimer : IDisposable
1414
private readonly Random _random;
1515
private readonly Account _acc;
1616
private readonly Timer _mainTimer;
17-
private bool flagStopTimer;
18-
19-
public void ForceTimerStop()
20-
{
21-
_acc.Status = Status.Stopping;
22-
flagStopTimer = true;
23-
}
2417

2518
private long _isTaskExcuting;
2619

@@ -63,21 +56,25 @@ public void Start()
6356
if (IsBotRunning) return;
6457
IsBotRunning = true;
6558
IsTaskExcuting = false;
66-
flagStopTimer = false;
6759
_mainTimer.Start();
6860
}
6961

70-
public async Task Stop(bool force = false)
62+
public void Stop()
7163
{
7264
if (!IsBotRunning) return;
7365

7466
IsBotRunning = false;
7567
_mainTimer.Stop();
7668

77-
if (!force)
78-
{
79-
await Task.Run(() => { while (IsTaskExcuting) { } });
80-
}
69+
var currentTask = _acc.Tasks.CurrentTask;
70+
if (currentTask != null) currentTask.StopFlag = true;
71+
_acc.Status = Status.Pausing;
72+
}
73+
74+
public async Task WaitStop()
75+
{
76+
await Task.Run(() => { while (IsTaskExcuting) { } });
77+
_acc.Status = Status.Paused;
8178
}
8279

8380
private void MainTimerElapsed(object source, ElapsedEventArgs e) => NewTick();
@@ -86,12 +83,6 @@ private async void NewTick()
8683
{
8784
if (!IsBotRunning) return;
8885
if (IsTaskExcuting) return;
89-
if (flagStopTimer)
90-
{
91-
await Stop(true);
92-
_acc.Status = Status.Offline;
93-
return;
94-
}
9586
IsTaskExcuting = true;
9687

9788
if (_acc.Tasks.Count == 0)

TbsCore/Extensions/AccountExtension.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Threading.Tasks;
45
using TbsCore.Models.AccModels;
56

67
namespace TbsCore.Extensions

TbsCore/Helpers/IoHelperCore.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ public static async Task<bool> Login(Account acc)
190190
/// <param name="acc"></param>
191191
public static async Task Logout(Account acc)
192192
{
193-
await acc.TaskTimer.Stop();
193+
acc.TaskTimer.Stop();
194+
await acc.TaskTimer.WaitStop();
195+
acc.Status = Status.Stopping;
194196
acc.Wb.Close();
197+
acc.Status = Status.Offline;
195198
}
196199
}
197200
}

TbsCore/Helpers/NavigationHelper.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,42 @@ private static async Task ToBuildingId(Account acc, int index)
105105
while (true);
106106
}
107107

108-
internal static async Task ToConstructionTab(Account acc, BuildingEnum building)
108+
internal static async Task<bool> ToConstructionTab(Account acc, BuildingEnum building)
109109
{
110-
BuildingCategoryEnum tab = BuildingsData.GetBuildingsCategory(building);
111-
if ((int)tab == 0) return;
112-
await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
113-
await DriverHelper.WaitPageLoaded(acc);
110+
var tab = BuildingsData.GetBuildingsCategory(building);
111+
if (tab == BuildingCategoryEnum.Infrastructure) return true;
112+
acc.Logger.Information($"{building} is in {tab} section, switch tab");
113+
string classNode;
114+
switch (tab)
115+
{
116+
case BuildingCategoryEnum.Military:
117+
classNode = "military";
118+
break;
119+
120+
case BuildingCategoryEnum.Resources:
121+
classNode = "resources";
122+
break;
123+
124+
default:
125+
classNode = "";
126+
break;
127+
}
128+
var node = acc.Wb.Html.DocumentNode.Descendants("a").FirstOrDefault(x => x.HasClass("tabItem") && x.HasClass(classNode));
129+
if (node == null) return false;
130+
131+
var element = acc.Wb.Driver.FindElement(By.XPath(node.XPath));
132+
if (element == null) return false;
133+
element.Click();
134+
acc.Logger.Information($"Waitting tab change");
135+
try
136+
{
137+
await DriverHelper.WaitPageChange(acc, "category", 0.5);
138+
}
139+
catch
140+
{
141+
await DriverHelper.WaitPageLoaded(acc);
142+
}
143+
return true;
114144
}
115145

116146
/// <summary>

TbsCore/Models/AccModels/TaskList.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public BotTask FirstTask
152152
get => _tasks.FirstOrDefault();
153153
}
154154

155+
public BotTask CurrentTask
156+
{
157+
get => _tasks.FirstOrDefault(x => x.Stage == TaskStage.Executing);
158+
}
159+
155160
public void Save()
156161
{
157162
var list = new List<TaskFileModel>();

TbsCore/Tasks/LowLevel/BotTask.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using TbsCore.Models.AccModels;
45
using TbsCore.Models.VillageModels;
@@ -44,14 +45,43 @@ public abstract class BotTask
4445
/// Counts how many times we retried executing the task. After 3rd try, stop retrying. Something is clearly wrong
4546
/// Used in TaskExecutor and TaskTimer
4647
/// </summary>
47-
public int RetryCounter { get; set; }
48+
public int RetryCounter { get; set; } = 0;
4849

4950
/// <summary>
5051
/// How high of a priority does this task have.
5152
/// Tasks like attacking and deffending (waves) have highest priority and should as such be executed first
5253
/// </summary>
5354
public TaskPriority Priority { get; set; }
5455

56+
private long stopFlag;
57+
58+
public bool StopFlag
59+
{
60+
get
61+
{
62+
return Interlocked.Read(ref stopFlag) == 1;
63+
}
64+
set
65+
{
66+
Interlocked.Exchange(ref stopFlag, Convert.ToInt64(value));
67+
}
68+
}
69+
70+
protected void Retry(Account acc, string message)
71+
{
72+
if (RetryCounter < 4)
73+
{
74+
RetryCounter++;
75+
acc.Logger.Information($"{message}. Try again. ({RetryCounter} time(s))", this);
76+
}
77+
else
78+
{
79+
acc.Logger.Information($"{message}.", this);
80+
acc.Logger.Warning($"Already tries 3 times. Considering there is error, please check account's browser.", this);
81+
StopFlag = true;
82+
}
83+
}
84+
5585
public enum TaskRes
5686
{
5787
Executed,

TbsCore/Tasks/LowLevel/LoginTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override async Task<TaskRes> Execute(Account acc)
3535
{
3636
// Wrong password/nickname
3737
acc.Logger.Warning("Password is incorrect!");
38-
acc.TaskTimer.ForceTimerStop();
38+
acc.TaskTimer.Stop();
3939
}
4040
else
4141
{

TbsCore/Tasks/LowLevel/ReopenDriver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public override async Task<TaskRes> Execute(Account acc)
5050
var result = await acc.Wb.Init(acc, ChangeAccess);
5151
if (!result)
5252
{
53-
acc.TaskTimer.ForceTimerStop();
53+
acc.TaskTimer.Stop();
5454
}
5555

5656
return TaskRes.Executed;

0 commit comments

Comments
 (0)