Skip to content

Commit ad1d53b

Browse files
authored
Merge pull request #103 from Erol444/fix-19-04-2022
Fix 19 04 2022
2 parents 701337c + 5761600 commit ad1d53b

File tree

11 files changed

+514
-277
lines changed

11 files changed

+514
-277
lines changed

TbsCore/Core/TaskExecutor.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,32 @@ public static class TaskExecutor
2020
/// In first case execute the task, in second remove it.
2121
/// </summary>
2222
/// <param name="acc"></param>
23-
public static async Task PageLoaded(Account acc)
23+
public static async Task<bool> PageLoaded(Account acc)
2424
{
25+
{
26+
var counter = 3;
27+
while (!acc.Wb.CheckChromeOpen())
28+
{
29+
acc.Logger.Warning("Chrome browser missing");
30+
if (counter == 0)
31+
{
32+
acc.Logger.Warning("Chrome still missing after 3 times restart. Pause bot (suggest logout bot before use bot on this account)");
33+
acc.TaskTimer.Stop();
34+
return false;
35+
}
36+
counter--;
37+
acc.Logger.Information("Chrome browser is restarting ...");
38+
var task = new RestartChrome();
39+
await task.Execute(acc);
40+
acc.Logger.Information("Confirm browser opened ...");
41+
await Task.Delay(5000);
42+
}
43+
}
2544
if (IsCaptcha(acc) || IsWWMsg(acc) || IsBanMsg(acc) || IsMaintanance(acc)) //Check if a captcha/ban/end of server/maintanance
2645
{
2746
acc.Logger.Warning("Captcha/WW/Ban/Maintanance found! Stopping bot for this account!");
2847
acc.TaskTimer.Stop();
48+
return false;
2949
}
3050
if (CheckCookies(acc))
3151
await DriverHelper.ExecuteScript(acc, "document.getElementById('CybotCookiebotDialogBodyLevelButtonLevelOptinDeclineAll').click();");
@@ -48,17 +68,20 @@ public static async Task PageLoaded(Account acc)
4868
if (IsLoginScreen(acc)) //Check if you are on login page -> Login task
4969
{
5070
var task = new LoginTask();
51-
await task.Execute(acc);
71+
var result = await task.Execute(acc);
72+
if (result == TaskRes.Retry) return false;
5273
}
5374

5475
if (IsSysMsg(acc)) //Check if there is a system message (eg. Artifacts/WW plans appeared)
5576
{
56-
await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php?ok=1");
5777
await Task.Delay(AccountHelper.Delay(acc));
78+
await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php?ok=1");
79+
return true;
5880
}
5981

6082
//TODO: limit this for performance reasons?
6183
PostLoadTasks(acc);
84+
return true;
6285
}
6386

6487
/// <summary>

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -134,53 +134,33 @@ public ReadOnlyCollection<Cookie> GetCookies()
134134
/// <summary>
135135
/// Refresh page. Same as clicking F5
136136
/// </summary>
137-
public async Task Refresh() => await Navigate(this.CurrentUrl);
137+
public async Task<bool> Refresh() => await Navigate(CurrentUrl);
138138

139-
public async Task Navigate(string url)
139+
public async Task<bool> Navigate(string url)
140140
{
141-
if (string.IsNullOrEmpty(url)) return;
141+
if (string.IsNullOrEmpty(url)) return await Refresh();
142142

143-
int repeatCnt = 0;
144-
bool repeat;
145-
do
143+
try
146144
{
147-
CheckChromeOpen();
148-
149-
try
150-
{
151-
// Will throw exception after timeout
152-
Driver.Navigate().GoToUrl(url);
153-
repeat = false;
154-
}
155-
catch (Exception e)
156-
{
157-
acc.Logger.Error(e, $"Error navigation to {url} - probably due to proxy/Internet or due to chrome still being opened");
158-
repeat = true;
159-
if (5 <= ++repeatCnt && !string.IsNullOrEmpty(acc.Access.GetCurrentAccess().Proxy))
160-
{
161-
// Change access
162-
repeatCnt = 0;
163-
var changeAccess = new ChangeAccess();
164-
await changeAccess.Execute(acc);
165-
await Task.Delay(AccountHelper.Delay(acc) * 5);
166-
}
167-
await Task.Delay(AccountHelper.Delay(acc));
168-
}
145+
// Will throw exception after timeout
146+
Driver.Navigate().GoToUrl(url);
147+
}
148+
catch (Exception e)
149+
{
150+
acc.Logger.Error(e, $"Error navigation to {url} - probably due to proxy/Internet or due to chrome still being opened");
151+
return false;
169152
}
170-
while (repeat);
171153

172-
await DriverHelper.WaitPageLoaded(acc);
154+
return await DriverHelper.WaitPageLoaded(acc);
173155
}
174156

175157
public void UpdateHtml()
176158
{
177-
CheckChromeOpen();
178159
Html.LoadHtml(Driver.PageSource);
179160
}
180161

181162
public void ExecuteScript(string script)
182163
{
183-
CheckChromeOpen();
184164
Driver.ExecuteScript(script);
185165
}
186166

@@ -203,35 +183,39 @@ public T GetJsObj<T>(string obj)
203183
/// </summary>
204184
public string GetBearerToken()
205185
{
206-
CheckChromeOpen();
207186
IJavaScriptExecutor js = acc.Wb.Driver;
208187
return (string)js.ExecuteScript("for(let field in Travian) { if (Travian[field].length == 32) return Travian[field]; }");
209188
}
210189

211190
public IWebElement FindElementById(string element)
212191
{
213-
CheckChromeOpen();
214192
return Driver.FindElementById(element);
215193
}
216194

217195
public IWebElement FindElementByXPath(string xPath)
218196
{
219-
CheckChromeOpen();
220197
return Driver.FindElementByXPath(xPath);
221198
}
222199

223200
public ITargetLocator SwitchTo()
224201
{
225-
CheckChromeOpen();
226202
return Driver.SwitchTo();
227203
}
228204

229205
/// <summary>
230206
/// catch (WebDriverException e) when (e.Message.Contains("chrome not reachable") || e.Message.Contains("no such window:"))
231207
/// </summary>
232-
public void CheckChromeOpen()
208+
public bool CheckChromeOpen()
233209
{
234-
_ = Driver.Title;
210+
try
211+
{
212+
_ = Driver.Title;
213+
return true;
214+
}
215+
catch
216+
{
217+
return false;
218+
}
235219
}
236220

237221
public async Task<bool> CheckProxy(Account acc)

TbsCore/Helpers/DriverHelper.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,26 @@ public abstract class Action
202202
public abstract class Query
203203
{ public string val; }
204204

205-
public static async Task WaitPageLoaded(Account acc, double delay = 1)
205+
public static async Task<bool> WaitPageLoaded(Account acc, double delay = 1)
206206
{
207207
var wait = new WebDriverWait(acc.Wb.Driver, TimeSpan.FromMinutes(delay));
208208
wait.Until(driver => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
209209
acc.Wb.UpdateHtml();
210-
await TaskExecutor.PageLoaded(acc);
210+
return await TaskExecutor.PageLoaded(acc);
211211
}
212212

213-
public static async Task WaitPageChange(Account acc, string part, double delay = 1)
213+
public static async Task<bool> WaitPageChange(Account acc, string part, double delay = 1)
214214
{
215215
var wait = new WebDriverWait(acc.Wb.Driver, TimeSpan.FromMinutes(delay));
216216
try
217217
{
218218
wait.Until(driver => driver.Url.Contains(part));
219219
}
220-
catch { }
221-
await WaitPageLoaded(acc, delay);
220+
catch
221+
{
222+
acc.Logger.Warning($"Chrome failed when change to page have url part is {part}");
223+
}
224+
return await WaitPageLoaded(acc, delay);
222225
}
223226
}
224227
}

TbsCore/Helpers/NavigationHelper.cs

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,48 +62,60 @@ public static async Task<bool> MainNavigate(Account acc, MainNavigationButton bu
6262
/// <summary>
6363
/// Navigates to a specific building id
6464
/// </summary>
65-
private static async Task ToBuildingId(Account acc, int index)
65+
private static async Task<bool> ToBuildingId(Account acc, int index)
6666
{
67-
do
67+
if (!await DriverHelper.WaitPageLoaded(acc)) return false;
68+
// If we are already at the correct building, don't re-enter it, just navigate to correct tab afterwards.
69+
if (acc.Wb.CurrentUrl.Contains($"?id={index}&"))
6870
{
69-
// If we are already at the correct building, don't re-enter it, just navigate to correct tab afterwards.
70-
if (acc.Wb.CurrentUrl.Contains($"build.php?id={index}"))
71-
{
72-
// If we have just updated the village, don't re-navigate
73-
var lastUpdate = DateTime.Now - VillageHelper.ActiveVill(acc).Res.Stored.LastRefresh;
74-
if (lastUpdate < TimeSpan.FromSeconds(10)) return;
71+
// If we have just updated the village, don't re-navigate
72+
var lastUpdate = DateTime.Now - VillageHelper.ActiveVill(acc).Res.Stored.LastRefresh;
73+
if (lastUpdate < TimeSpan.FromMinutes(1)) return true;
74+
75+
// If we haven't updated it recently (last 1 min), refresh
76+
await acc.Wb.Refresh();
77+
return true;
78+
}
7579

76-
// If we haven't updated it recently (last 10sec), refresh
77-
await acc.Wb.Refresh();
78-
return;
80+
if (index < 19) // dorf1
81+
{
82+
if (!acc.Wb.CurrentUrl.Contains("dorf1.php"))
83+
await MainNavigate(acc, MainNavigationButton.Resources);
84+
var divBuilding = acc.Wb.Html.DocumentNode.Descendants().FirstOrDefault(x => x.HasClass($"buildingSlot{index}"));
85+
if (divBuilding == null)
86+
{
87+
acc.Logger.Warning($"Cannot find resfield has id {index}");
88+
return false;
7989
}
90+
var elementBuilding = acc.Wb.Driver.FindElement(By.XPath(divBuilding.XPath));
91+
elementBuilding.Click();
92+
await DriverHelper.WaitPageChange(acc, $"?id={index}&");
93+
}
94+
else // dorf2
95+
{
96+
if (!acc.Wb.CurrentUrl.Contains("dorf2.php"))
97+
await MainNavigate(acc, MainNavigationButton.Buildings);
8098

81-
if (index < 19) // dorf1
99+
//*[@id="villageContent"]/div[1] => data-aid = 19
100+
var location = index - 18; // - 19 + 1
101+
var divBuilding = acc.Wb.Html.DocumentNode.SelectSingleNode($"//*[@id='villageContent']/div[{location}]");
102+
if (divBuilding == null)
82103
{
83-
if (!acc.Wb.CurrentUrl.Contains("dorf1.php") || acc.Wb.CurrentUrl.Contains("id="))
84-
await MainNavigate(acc, MainNavigationButton.Resources);
85-
await DriverHelper.ClickByClassName(acc, $"buildingSlot{index}");
104+
acc.Logger.Warning($"Cannot find building has id {index}");
105+
return false;
86106
}
87-
else // dorf2
107+
var pathBuilding = divBuilding.Descendants("path").FirstOrDefault();
108+
if (pathBuilding == null)
88109
{
89-
acc.Wb.UpdateHtml();
90-
if (!acc.Wb.CurrentUrl.Contains("dorf2.php") || acc.Wb.CurrentUrl.Contains("id="))
91-
await MainNavigate(acc, MainNavigationButton.Buildings);
92-
93-
//*[@id="villageContent"]/div[1] => data-aid = 19
94-
var location = index - 18; // - 19 + 1
95-
var divBuilding = acc.Wb.Html.DocumentNode.SelectSingleNode($"//*[@id='villageContent']/div[{location}]");
96-
if (divBuilding == null) continue;
97-
var pathBuilding = divBuilding.Descendants("a").FirstOrDefault();
98-
if (pathBuilding == null) continue;
99-
var href = pathBuilding.GetAttributeValue("href", "");
100-
var url = $"{acc.AccInfo.ServerUrl}{href.Replace("&amp;", "&")}";
101-
await acc.Wb.Navigate(url);
110+
acc.Logger.Warning($"Cannot find place to click on building has id {index}");
111+
return false;
102112
}
103-
await DriverHelper.WaitPageChange(acc, $"id={index}");
104-
break;
113+
var href = pathBuilding.GetAttributeValue("onclick", "");
114+
var script = href.Replace("&amp;", "&");
115+
acc.Wb.Driver.ExecuteScript(script);
116+
await DriverHelper.WaitPageChange(acc, $"?id={index}");
105117
}
106-
while (true);
118+
return true;
107119
}
108120

109121
internal static async Task<bool> ToConstructionTab(Account acc, BuildingEnum building)
@@ -212,11 +224,14 @@ public static async Task<bool> EnterBuilding(Account acc, Building building, int
212224
// Enter building (if not already there)
213225
await ToBuildingId(acc, building.Id);
214226

215-
if (tab != null) // Navigate to correct tab
227+
if (BuildingsData.HasMultipleTabs(building.Type))
216228
{
217-
var currentTab = InfrastructureParser.CurrentlyActiveTab(acc.Wb.Html);
218-
// Navigate to correct tab if not already on it
219-
if (currentTab != tab) await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
229+
if (tab != null) // Navigate to correct tab
230+
{
231+
var currentTab = InfrastructureParser.CurrentlyActiveTab(acc.Wb.Html);
232+
// Navigate to correct tab if not already on it
233+
if (currentTab != tab) await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
234+
}
220235
}
221236
break;
222237

@@ -394,6 +409,18 @@ public static async Task<bool> ToOverview(Account acc, OverviewTab tab, TroopOve
394409
// Faster,
395410
//}
396411

412+
public static async Task<bool> SwitchVillage(Account acc, Village vill)
413+
{
414+
await DriverHelper.WaitPageLoaded(acc);
415+
var active = acc.Villages.FirstOrDefault(x => x.Active);
416+
if (active != null && active.Id != vill.Id)
417+
{
418+
acc.Logger.Information($"Now in village {active.Name}. Move to correct village {vill.Name}");
419+
return await VillageHelper.SwitchVillage(acc, vill.Id);
420+
}
421+
return true;
422+
}
423+
397424
public enum MainNavigationButton
398425
{
399426
Resources = 1,

TbsCore/Helpers/UpgradeBuildingHelper.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ public class UpgradeBuildingHelper
1919
{
2020
public static BuildingTask NextBuildingTask(Account acc, Village vill)
2121
{
22-
if (vill.Build.Tasks.Count == 0) return null;
22+
if (vill.Build.Tasks.Count == 0)
23+
{
24+
acc.Logger.Information("Building queue empty.");
25+
return null;
26+
}
27+
2328
RemoveFinishedCB(vill);
2429

2530
var totalBuild = vill.Build.CurrentlyBuilding.Count;
@@ -28,7 +33,11 @@ public static BuildingTask NextBuildingTask(Account acc, Village vill)
2833
var maxBuild = 1;
2934
if (acc.AccInfo.PlusAccount) maxBuild++;
3035
if (acc.AccInfo.Tribe == TribeEnum.Romans) maxBuild++;
31-
if (totalBuild == maxBuild) return null;
36+
if (totalBuild == maxBuild)
37+
{
38+
acc.Logger.Information("Amount of currently building is equal with maximum building can build in same time");
39+
return null;
40+
}
3241

3342
if (maxBuild - totalBuild == 1)
3443
{
@@ -40,12 +49,17 @@ public static BuildingTask NextBuildingTask(Account acc, Village vill)
4049

4150
if (numRes > numInfra)
4251
{
43-
if (vill.Res.FreeCrop <= 5) return null;
52+
if (vill.Res.FreeCrop <= 5)
53+
{
54+
acc.Logger.Information("Don't have enough slot building because of freecrop ");
55+
return null;
56+
}
4457
return GetFirstInfrastructureTask(acc, vill);
4558
}
4659
else if (numInfra > numRes)
4760
{
4861
// no need check free crop, there is magic make sure this always choose crop
62+
// just kidding, because of how we check free crop later, first res task is always crop
4963
return GetFirstResTask(acc, vill);
5064
}
5165
// if same means 1 R and 1 I already, 1 ANY will be choose below

0 commit comments

Comments
 (0)