Skip to content

Commit 513d8f6

Browse files
authored
Merge pull request #95 from Erol444/weekly-02-04-2022
Weekly 02 04 2022
2 parents 25eebc2 + e4dabf1 commit 513d8f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+7935
-1221
lines changed

TbsCore/Core/TaskExecutor.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +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.Stop();
29-
return;
28+
acc.TaskTimer.ForceTimerStop();
3029
}
3130
if (CheckCookies(acc))
3231
await DriverHelper.ExecuteScript(acc, "document.getElementById('CybotCookiebotDialogBodyLevelButtonLevelOptinDeclineAll').click();");
@@ -48,8 +47,8 @@ public static async Task PageLoaded(Account acc)
4847

4948
if (IsLoginScreen(acc)) //Check if you are on login page -> Login task
5049
{
51-
acc.Tasks.Add(new LoginTask() { ExecuteAt = DateTime.MinValue });
52-
return;
50+
var task = new LoginTask();
51+
await task.Execute(acc);
5352
}
5453

5554
if (IsSysMsg(acc)) //Check if there is a system message (eg. Artifacts/WW plans appeared)
@@ -89,7 +88,13 @@ public static async Task Execute(Account acc, BotTask task)
8988
acc.Logger.Warning($"Chrome has problem while executing task {task.GetName()}! Vill {task.Vill?.Name}. Try reopen Chrome");
9089

9190
acc.Wb.Close();
92-
await acc.Wb.Init(acc);
91+
92+
var result = await acc.Wb.Init(acc);
93+
if (!result)
94+
{
95+
acc.TaskTimer.ForceTimerStop();
96+
return;
97+
}
9398
}
9499
catch (Exception e)
95100
{

TbsCore/Core/TaskTimer.cs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Threading.Tasks;
34
using System.Timers;
45
using TbsCore.Helpers;
56
using TbsCore.Tasks;
@@ -13,9 +14,13 @@ public sealed class TaskTimer : IDisposable
1314
private readonly Random _random;
1415
private readonly Account _acc;
1516
private readonly Timer _mainTimer;
17+
private bool flagStopTimer;
1618

17-
// subTimer is for Tbs's Alzheimer disease
18-
private readonly Timer _subTimer;
19+
public void ForceTimerStop()
20+
{
21+
_acc.Status = Status.Stopping;
22+
flagStopTimer = true;
23+
}
1924

2025
private long _isTaskExcuting;
2126

@@ -49,86 +54,72 @@ public TaskTimer(Account Account)
4954
{
5055
_acc = Account;
5156
_mainTimer = new Timer(500);
52-
_subTimer = new Timer(500);
5357
_random = new Random();
5458
_mainTimer.Elapsed += MainTimerElapsed;
55-
_subTimer.Elapsed += SubTimerElapsed;
5659
}
5760

5861
public void Start()
5962
{
63+
if (IsBotRunning) return;
6064
IsBotRunning = true;
6165
IsTaskExcuting = false;
66+
flagStopTimer = false;
6267
_mainTimer.Start();
6368
}
6469

65-
public void Stop()
70+
public async Task Stop(bool force = false)
6671
{
72+
if (!IsBotRunning) return;
73+
6774
IsBotRunning = false;
68-
IsTaskExcuting = false;
6975
_mainTimer.Stop();
76+
77+
if (!force)
78+
{
79+
await Task.Run(() => { while (IsTaskExcuting) { } });
80+
}
7081
}
7182

7283
private void MainTimerElapsed(object source, ElapsedEventArgs e) => NewTick();
7384

74-
private void SubTimerElapsed(object sender, ElapsedEventArgs e)
75-
{
76-
// if there is no task and bot still dont know it can execute next task
77-
if (_acc.Tasks.IsTaskExcuting()) return;
78-
if (!IsTaskExcuting) return;
79-
80-
IsTaskExcuting = false;
81-
}
82-
8385
private async void NewTick()
8486
{
8587
if (!IsBotRunning) return;
8688
if (IsTaskExcuting) return;
87-
89+
if (flagStopTimer)
90+
{
91+
await Stop(true);
92+
_acc.Status = Status.Offline;
93+
return;
94+
}
8895
IsTaskExcuting = true;
8996

9097
if (_acc.Tasks.Count == 0)
9198
{
9299
IsTaskExcuting = false;
93100
return; //No tasks
94101
}
95-
// Another task is already in progress. wait
96-
if (_acc.Tasks.IsTaskExcuting())
97-
{
98-
IsTaskExcuting = false;
99-
return;
100-
}
101102

102-
var tasks = _acc.Tasks.GetTasksReady();
103-
if (tasks.Count == 0)
103+
var task = _acc.Tasks.FirstTask;
104+
if (task.ExecuteAt > DateTime.Now)
104105
{
105106
NoTasks(_acc);
106107
IsTaskExcuting = false;
107108
return;
108109
}
109110

110-
BotTask firstTask = tasks.FirstOrDefault(x => x.Priority == TaskPriority.High);
111-
if (firstTask == null) firstTask = tasks.FirstOrDefault(x => x.Priority == TaskPriority.Medium);
112-
if (firstTask == null) firstTask = tasks.FirstOrDefault();
113-
114-
if (firstTask.Stage == TaskStage.Executing)
115-
{
116-
IsTaskExcuting = false;
117-
return;
118-
}
119-
120-
firstTask.Stage = TaskStage.Executing;
111+
task.Stage = TaskStage.Executing;
121112

122113
//If correct village is selected, otherwise change village
123-
if (firstTask.Vill != null)
114+
if (task.Vill != null && task.GetType() != typeof(UpgradeBuilding))
124115
{
125116
var active = _acc.Villages.FirstOrDefault(x => x.Active);
126-
if (active != null && active != firstTask.Vill)
117+
if (active != null && active != task.Vill)
127118
{
128-
await VillageHelper.SwitchVillage(_acc, firstTask.Vill.Id);
119+
await VillageHelper.SwitchVillage(_acc, task.Vill.Id);
129120
}
130121
}
131-
await TaskExecutor.Execute(_acc, firstTask);
122+
await TaskExecutor.Execute(_acc, task);
132123
IsTaskExcuting = false;
133124
}
134125

@@ -140,7 +131,7 @@ private void NoTasks(Account _acc)
140131
var delay = TimeSpan.FromMinutes(5);
141132
if (nextTask == null || nextTask.ExecuteAt - DateTime.Now > delay)
142133
{
143-
_acc.Tasks.Add(new ReopenDriver()
134+
_acc.Tasks.Add(new TaskSleep()
144135
{
145136
ExecuteAt = DateTime.Now,
146137
});
@@ -166,7 +157,6 @@ private void NoTasks(Account _acc)
166157
public void Dispose()
167158
{
168159
_mainTimer.Dispose();
169-
_subTimer.Dispose();
170160
}
171161
}
172162
}

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public WebBrowserInfo()
3838
public async Task<bool> Init(Account acc, bool newAccess = true)
3939
{
4040
this.acc = acc;
41+
if (acc.Access.AllAccess.Count == 0)
42+
{
43+
acc.Logger.Warning("Account doesn't have any access. Press Edit account to check access");
44+
return false;
45+
}
46+
47+
acc.Access.AllAccess.ForEach(a => a.Ok = true);
48+
4149
Access.Access access = newAccess ? acc.Access.GetNewAccess() : acc.Access.GetCurrentAccess();
4250

4351
if (!string.IsNullOrEmpty(access.Proxy))
@@ -259,7 +267,7 @@ public async Task<bool> CheckProxy(Account acc)
259267
}
260268
else
261269
{
262-
acc.Logger.Warning($"There's only one access to this account! Please check your proxy status");
270+
acc.Logger.Warning($"There's only one proxy to this account! Please check your proxy status");
263271
return false;
264272
}
265273
}
@@ -276,13 +284,17 @@ public void Close()
276284
{
277285
if (Driver != null)
278286
{
279-
var tabs = Driver.WindowHandles;
280-
foreach (var tab in tabs)
287+
try
281288
{
282-
Driver.SwitchTo().Window(tab);
283-
Driver.Close();
289+
var tabs = Driver.WindowHandles;
290+
foreach (var tab in tabs)
291+
{
292+
Driver.SwitchTo().Window(tab);
293+
Driver.Close();
294+
}
295+
Driver.Dispose();
284296
}
285-
Driver.Dispose();
297+
catch { }
286298
}
287299
}
288300

TbsCore/Database/UseragentDatabase.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void Load()
7373
_userAgentList = modelLoaded.UserAgentList;
7474
_dateTime = modelLoaded.DateTime;
7575

76-
if (_dateTime.IsExpired())
76+
if (_dateTime.IsExpired() || _userAgentList.Count < 1000)
7777
{
7878
Update();
7979
}
@@ -108,7 +108,11 @@ public string GetUserAgent()
108108
}
109109
}
110110
while (duplicate);
111-
return _userAgentList[index]; // i dont think this will loop over 5000 times =)) }
111+
112+
var result = _userAgentList[index];
113+
_userAgentList.RemoveAt(index);
114+
Save();
115+
return result;
112116
}
113117
}
114118

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using TbsCore.Models.AccModels;
5+
6+
namespace TbsCore.Extensions
7+
{
8+
public static class AccountExtension
9+
{
10+
public static bool CanLogin(this Account acc)
11+
{
12+
if (acc.Status == Status.Offline) return true;
13+
return false;
14+
}
15+
16+
public static bool CanLogout(this Account acc)
17+
{
18+
if (acc.Status == Status.Paused) return true;
19+
if (acc.Status == Status.Online) return true;
20+
return false;
21+
}
22+
}
23+
}

TbsCore/Helpers/AccountHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void StartAccountTasks(Account acc)
7373
if (acc.Farming.Enabled) acc.Tasks.Add(new SendFLs() { ExecuteAt = DateTime.Now }, true);
7474

7575
// Bot sleep
76-
acc.Tasks.Add(new Sleep()
76+
acc.Tasks.Add(new TimeSleep()
7777
{
7878
ExecuteAt = DateTime.Now + TimeHelper.GetWorkTime(acc),
7979
}, true);

TbsCore/Helpers/Classificator.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public enum HeroItemCategory
6161
Armor,
6262
Boots,
6363
Horse,
64-
Others // Expand others?
64+
Resource,
65+
Stackable,
66+
NonStackable,
67+
Others,
6568
}
6669

6770
public enum HeroItemEnum
@@ -195,16 +198,16 @@ public enum HeroItemEnum
195198
Horse_Horse_3,
196199

197200
// Others
198-
Others_Ointment_0, // =106
201+
Stackable_Ointment_0, // =106
199202

200-
Others_Scroll_0,
201-
Others_Bucket_0,
202-
Others_Tablets_0,
203-
Others_Book_0, // =110
204-
Others_Artwork_0,
205-
Others_SmallBandage_0,
206-
Others_BigBandage_0,
207-
Others_Cage_0,
203+
Stackable_Scroll_0,
204+
NonStackable_Bucket_0,
205+
NonStackable_Tablets_0,
206+
NonStackable_Book_0, // =110
207+
NonStackable_Artwork_0,
208+
Stackable_SmallBandage_0,
209+
Stackable_BigBandage_0,
210+
Stackable_Cage_0,
208211

209212
// Egyptian weapons
210213
Weapon_SlaveMilitia_1, // =115
@@ -242,12 +245,10 @@ public enum HeroItemEnum
242245
Weapon_Marauder_2,
243246
Weapon_Marauder_3,
244247

245-
// Resources (Since T4.5)
246-
Others_Wood_0, // =145
247-
248-
Others_Clay_0,
249-
Others_Iron_0,
250-
Others_Crop_0 // =148
248+
Resource_Wood_0,
249+
Resource_Clay_0,
250+
Resource_Iron_0,
251+
Resource_Crop_0 // =148
251252
}
252253

253254
public enum TroopsEnum

TbsCore/Helpers/DriverHelper.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,5 @@ public static async Task WaitPageChange(Account acc, string part, double delay =
216216
wait.Until(driver => driver.Url.Contains(part));
217217
await WaitPageLoaded(acc, delay);
218218
}
219-
220-
public static async Task ReopenChrome(Account acc)
221-
{
222-
acc.Wb.Close();
223-
224-
await acc.Wb.Init(acc, false);
225-
}
226219
}
227220
}

0 commit comments

Comments
 (0)