Skip to content

Commit 0009346

Browse files
committed
add task delay to upgrade building task
1 parent df1a4e8 commit 0009346

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

TbsCore/Helpers/AccountHelper.cs

Lines changed: 9 additions & 7 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 TbsCore.Models.AccModels;
45
using TbsCore.Models.VillageModels;
56
using TbsCore.Tasks.LowLevel;
@@ -9,6 +10,8 @@ namespace TbsCore.Helpers
910
{
1011
public static class AccountHelper
1112
{
13+
private static Random rnd = new Random();
14+
1215
public static Village GetMainVillage(Account acc)
1316
{
1417
var main = acc.Villages.FirstOrDefault(x => x.Id == acc.Settings.MainVillage);
@@ -46,15 +49,14 @@ public static Village GetQuestsClaimVillage(Account acc)
4649
}
4750

4851
/// <summary>
49-
/// Returns a random delay (click delay, ~0.5-1.6sec).
52+
/// Returns a random
53+
///
54+
/// (click delay, ~0.5-1.6sec).
5055
/// </summary>
5156
/// <returns>Random delay in milliseconds</returns>
52-
public static int Delay(Account acc)
53-
{
54-
//Return random delay
55-
Random rnd = new Random();
56-
return rnd.Next(acc.Settings.DelayClickingMin, acc.Settings.DelayClickingMax);
57-
}
57+
public static int Delay(Account acc) => rnd.Next(acc.Settings.DelayClickingMin, acc.Settings.DelayClickingMax);
58+
59+
public static Task DelayWait(Account acc) => Task.Delay(Delay(acc));
5860

5961
public static void StartAccountTasks(Account acc)
6062
{

TbsCore/Tasks/LowLevel/UpgradeBuilding.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public override async Task<TaskRes> Execute(Account acc)
5353
if (acc.AccInfo.ServerVersion == ServerVersionEnum.TTwars && !_buildingTask.ConstructNew)
5454
{
5555
acc.Logger.Information("Try using TTWars fast build method", this);
56+
await Task.Delay(AccountHelper.Delay(acc));
5657
var fastUpgrade = await TTWarsTryFastUpgrade(acc, $"{acc.AccInfo.ServerUrl}/build.php?id={_buildingTask.BuildingId}");
5758
if (fastUpgrade) continue;
5859
acc.Logger.Information("Using TTWars fast build method failed. Continue normal method", this);
@@ -99,11 +100,19 @@ private async Task<bool> Construct(Account acc)
99100
// Check for prerequisites
100101
if (button == null)
101102
{
103+
Retry(acc, "Button disappear.");
102104
return false;
103105
}
104106

105-
await DriverHelper.ClickById(acc, button.Id);
106-
107+
await Task.Delay(AccountHelper.Delay(acc));
108+
acc.Logger.Information($"Starting contruct {_buildingTask.Building} in {Vill.Name}");
109+
var element = acc.Wb.Driver.FindElement(By.XPath(button.XPath));
110+
if (element == null)
111+
{
112+
Retry(acc, "Button disappear.");
113+
return false;
114+
}
115+
element.Click();
107116
_buildingTask.ConstructNew = false;
108117

109118
acc.Logger.Warning($"Started construction of {_buildingTask.Building} in {Vill.Name}");
@@ -168,14 +177,14 @@ private async Task<bool> Upgrade(Account acc)
168177

169178
if (upgradeButton == null)
170179
{
171-
acc.Logger.Information($"We wanted to upgrade {_buildingTask.Building}, but no 'upgrade' button was found!", this);
180+
Retry(acc, $"We wanted to upgrade {_buildingTask.Building}, but no 'upgrade' button was found!");
172181
return false;
173182
}
174183

175184
// Not enough resources?
176185
if (acc.AccInfo.ServerVersion == ServerVersionEnum.T4_5 && errorMessage != null)
177186
{
178-
acc.Logger.Information($"We wanted to upgrade {_buildingTask.Building}, but there was an error message:\n{errorMessage.InnerText}", this);
187+
Retry(acc, $"We wanted to upgrade {_buildingTask.Building}, but there was an error message:\n{errorMessage.InnerText}");
179188
return false;
180189
}
181190

@@ -188,12 +197,23 @@ private async Task<bool> Upgrade(Account acc)
188197
if (acc.AccInfo.ServerVersion == ServerVersionEnum.T4_5 && buildDuration.TotalMinutes > acc.Settings.WatchAdAbove)
189198
{
190199
// watchAd = await TryFastUpgrade(acc);
200+
acc.Logger.Information("Try using watch ads upgrade button");
201+
acc.Logger.Information("Watch ads function is disable because of bugs from Chrome. We cannot do anything about this");
191202
}
192203

193204
if (!watchAd)
194205
{
206+
acc.Logger.Information("Using normal upgrade button");
207+
195208
upgradeButton = buttons.FirstOrDefault(x => x.HasClass("build"));
196-
await DriverHelper.ClickById(acc, upgradeButton.Id); // Normal upgrade
209+
210+
var element = acc.Wb.Driver.FindElement(By.XPath(upgradeButton.XPath));
211+
if (element == null)
212+
{
213+
Retry(acc, "Button disappear.");
214+
return false;
215+
}
216+
element.Click();
197217
}
198218

199219
acc.Logger.Information($"Upgraded {_buildingTask.Building} to level {lvl + 1} in {Vill.Name}", this);
@@ -440,11 +460,13 @@ private async Task<bool> FreeCropCondition(Account acc)
440460

441461
private async Task MoveIntoBuilding(Account acc)
442462
{
463+
await Task.Delay(AccountHelper.Delay(acc));
443464
acc.Logger.Information($"Move into building {_buildingTask.Building}", this);
444465
await NavigationHelper.EnterBuilding(acc, Vill, (int)_buildingTask.BuildingId);
445466
var build = Vill.Build.Buildings.FirstOrDefault(x => x.Id == _buildingTask.BuildingId);
446467
if (build.Type == BuildingEnum.Site)
447468
{
469+
await Task.Delay(AccountHelper.Delay(acc));
448470
acc.Logger.Information($"This is contruct task, choose correct tab for building {_buildingTask.Building}", this);
449471
await NavigationHelper.ToConstructionTab(acc, _buildingTask.Building);
450472
}

0 commit comments

Comments
 (0)