Skip to content

Commit 3b3f9ef

Browse files
committed
Refactored navigation of most tasks
1 parent d35381e commit 3b3f9ef

32 files changed

+220
-96
lines changed

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public string CurrentUrl
2424
get
2525
{
2626
CheckChromeOpen();
27-
2827
return this.Driver.Url;
2928
}
3029
}
@@ -157,6 +156,11 @@ internal Dictionary<string, string> GetCookies()
157156
return cookiesDir;
158157
}
159158

159+
/// <summary>
160+
/// Refresh page. Same as clicking F5
161+
/// </summary>
162+
public async Task Refresh() => await Navigate(this.CurrentUrl);
163+
160164
public async Task Navigate(string url)
161165
{
162166
if (string.IsNullOrEmpty(url)) return;
@@ -191,23 +195,19 @@ public async Task Navigate(string url)
191195
while (repeat);
192196

193197
await Task.Delay(AccountHelper.Delay(acc));
194-
195198
UpdateHtml();
196-
197199
await TaskExecutor.PageLoaded(acc);
198200
}
199201

200202
public void UpdateHtml()
201203
{
202204
CheckChromeOpen();
203-
204205
Html.LoadHtml(Driver.PageSource);
205206
}
206207

207208
public void ExecuteScript(string script)
208209
{
209210
CheckChromeOpen();
210-
211211
Driver.ExecuteScript(script);
212212
}
213213

@@ -231,29 +231,25 @@ public T GetJsObj<T>(string obj)
231231
public string GetBearerToken()
232232
{
233233
CheckChromeOpen();
234-
235234
IJavaScriptExecutor js = acc.Wb.Driver;
236235
return (string)js.ExecuteScript("for(let field in Travian) { if (Travian[field].length == 32) return Travian[field]; }");
237236
}
238237

239238
public IWebElement FindElementById(string element)
240239
{
241240
CheckChromeOpen();
242-
243241
return Driver.FindElementById(element);
244242
}
245243

246244
public IWebElement FindElementByXPath(string xPath)
247245
{
248246
CheckChromeOpen();
249-
250247
return Driver.FindElementByXPath(xPath);
251248
}
252249

253250
public ITargetLocator SwitchTo()
254251
{
255252
CheckChromeOpen();
256-
257253
return Driver.SwitchTo();
258254
}
259255

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Threading.Tasks;
2+
using TbsCore.Helpers;
3+
using TbsCore.Models.AccModels;
4+
using TbsCore.Models.VillageModels;
5+
using static TbsCore.Helpers.Classificator;
6+
using System;
7+
8+
namespace TbsCore.Extensions
9+
{
10+
/// <summary>
11+
/// WIP!
12+
/// </summary>
13+
static class RallyPointNavigationHelper
14+
{
15+
/// <summary>
16+
/// Gets name of the task
17+
/// </summary>
18+
/// <returns>Name of the task</returns>
19+
//public async static Task<bool> ToRallyPointIncoming(this NavigationHelper nav, Account acc, Village vill)
20+
//{
21+
// await NavigationHelper.ToRallyPoint(acc, vill, NavigationHelper.RallyPointTab.Overview);
22+
// var url = new Uri(acc.Wb.CurrentUrl);
23+
// //url.
24+
// return true;
25+
//}
26+
27+
//public enum RallyPointFilter
28+
//{
29+
// Incoming,
30+
// Outgoing,
31+
// TroopsInVillage,
32+
// TroopsInOtherVillages
33+
//}
34+
//public enum RallyPointSubFilters
35+
//{
36+
// None = 0,
37+
// IncomingAttacks = 1,
38+
// ReturningTroops,
39+
// IncomingReinforcements,
40+
// OutgoingAttacks,
41+
// OutgoingReinforcements,
42+
//}
43+
}
44+
}

TbsCore/Helpers/HeroHelper.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,5 @@ public static bool SwitchHelmet(Account acc, Village trainVill, BuildingEnum bui
257257
}, true);
258258
return true;
259259
}
260-
261-
internal static async Task NavigateToHeroAttributes(Account acc)
262-
{
263-
// Even if default tab is switched to other tab (not attributes), navigate
264-
// to attributes tab
265-
await VersionHelper.Navigate(acc, "/hero.php?t=1", "/hero/attributes");
266-
}
267260
}
268261
}

TbsCore/Helpers/NavigationHelper.cs

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
using HtmlAgilityPack;
2-
using OpenQA.Selenium;
3-
using OpenQA.Selenium.Support.UI;
4-
using System;
1+
using System;
52
using System.Linq;
63
using System.Threading.Tasks;
74
using TbsCore.Models.AccModels;
85
using TbsCore.Models.MapModels;
96
using TbsCore.Models.VillageModels;
10-
using TbsCore.TravianData;
7+
using TbsCore.Parsers;
118
using static TbsCore.Helpers.Classificator;
129

1310
namespace TbsCore.Helpers
1411
{
1512
public static class NavigationHelper
1613
{
14+
public static async Task<bool> ToDorf1(Account acc) => await MainNavigate(acc, MainNavigationButton.Resources);
15+
public static async Task<bool> ToDorf2(Account acc) => await MainNavigate(acc, MainNavigationButton.Buildings);
16+
public static async Task<bool> ToMap(Account acc) => await MainNavigate(acc, MainNavigationButton.Map);
17+
1718
public static async Task<bool> MainNavigate(Account acc, MainNavigationButton button)
1819
{
1920
var nav = acc.Wb.Html.GetElementbyId("navigation");
@@ -27,11 +28,17 @@ public static async Task<bool> MainNavigate(Account acc, MainNavigationButton bu
2728
/// </summary>
2829
private static async Task ToBuildingId(Account acc, int index)
2930
{
30-
// If we have just updated the village and we are already at the correct building, don't re-navigate.
31-
// Just navigate to correct tab afterwards.
32-
var lastUpdate = DateTime.Now - VillageHelper.ActiveVill(acc).Res.Stored.LastRefresh;
33-
if (acc.Wb.CurrentUrl.Contains($"build.php?id={index}") &&
34-
lastUpdate < TimeSpan.FromSeconds(10)) return;
31+
// If we are already at the correct building, don't re-enter it, just navigate to correct tab afterwards.
32+
if (acc.Wb.CurrentUrl.Contains($"build.php?id={index}"))
33+
{
34+
// If we have just updated the village, don't re-navigate
35+
var lastUpdate = DateTime.Now - VillageHelper.ActiveVill(acc).Res.Stored.LastRefresh;
36+
if(lastUpdate < TimeSpan.FromSeconds(10)) return;
37+
38+
// If we haven't updated it recently (last 10sec), refresh
39+
await acc.Wb.Refresh();
40+
return;
41+
}
3542

3643
if (index < 19) // dorf1
3744
{
@@ -43,13 +50,11 @@ private static async Task ToBuildingId(Account acc, int index)
4350
{
4451
if (!acc.Wb.CurrentUrl.Contains("dorf2.php") || acc.Wb.CurrentUrl.Contains("id="))
4552
await MainNavigate(acc, MainNavigationButton.Buildings);
46-
await DriverHelper.ClickByClassName(acc, $"aid{index}");
53+
await DriverHelper.ClickByClassName(acc, $"aid{index}", qindex: 1);
4754
}
4855
await DriverHelper.WaitLoaded(acc);
4956
}
50-
51-
public static async Task BuildNavigate(Account acc, Building building) =>
52-
await ToBuildingId(acc, building.Id);
57+
5358

5459
/// <summary>
5560
/// TTWars convert tab into url query
@@ -100,14 +105,20 @@ public static async Task<bool> EnterBuilding(Account acc, Building building, int
100105
switch (acc.AccInfo.ServerVersion)
101106
{
102107
case ServerVersionEnum.T4_5:
108+
// Enter building (if not already there)
103109
await ToBuildingId(acc, building.Id);
110+
104111
if (tab != null) // Navigate to correct tab
105-
await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
112+
{
113+
var currentTab = InfrastructureParser.CurrentlyActiveTab(acc.Wb.Html);
114+
// Navigate to correct tab if not already on it
115+
if (currentTab != tab) await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
116+
}
106117
break;
107118
case ServerVersionEnum.TTwars:
108119
// Directly navigate to url
109120
string url = $"{acc.AccInfo.ServerUrl}/build.php?id={building.Id}";
110-
if (tab != null) url += "?" + TTWarsTabUrl(building.Type, tab ?? 0);
121+
if (tab != null) url += "&" + TTWarsTabUrl(building.Type, tab ?? 0);
111122
if (coords != null) url += "&z=" + coords.GetKid(acc);
112123
await acc.Wb.Navigate(url);
113124
break;
@@ -148,6 +159,54 @@ public static async Task<bool> ToTreasury(Account acc, Village vill, TreasuryTab
148159
await EnterBuilding(acc, vill, BuildingEnum.Treasury, (int)tab);
149160

150161

162+
public static async Task<bool> ToHero(Account acc, HeroTab tab)
163+
{
164+
string query = "";
165+
switch (tab)
166+
{
167+
case HeroTab.Appearance:
168+
case HeroTab.Attributes:
169+
await DriverHelper.ClickById(acc, "heroImageButton");
170+
// Navigate to correct tab
171+
var currentTab = InfrastructureParser.CurrentlyActiveTab(acc.Wb.Html);
172+
if (currentTab != (int)tab) await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
173+
return true;
174+
case HeroTab.Adventures:
175+
query = "adventure";
176+
// .
177+
// ttwars: adventureWhite
178+
break;
179+
case HeroTab.Auctions:
180+
query = "auction";
181+
// auction
182+
//ttwars auctionWhite
183+
break;
184+
}
185+
if (acc.AccInfo.ServerVersion == ServerVersionEnum.TTwars) query += "White";
186+
return await DriverHelper.ClickByClassName(acc, query);
187+
}
188+
189+
public static async Task<bool> ToOverview(Account acc, OverviewTab tab, TroopOverview subTab = TroopOverview.OwnTroops)
190+
{
191+
string query = "overview";
192+
if (acc.AccInfo.ServerVersion == ServerVersionEnum.TTwars) query += "White";
193+
await DriverHelper.ClickByClassName(acc, query);
194+
195+
var currentTab = InfrastructureParser.CurrentlyActiveTab(acc.Wb.Html);
196+
if (currentTab != (int)tab) await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
197+
198+
if (tab == OverviewTab.Troops && subTab != TroopOverview.OwnTroops)
199+
{
200+
await Task.Delay(AccountHelper.Delay(acc));
201+
// Sub tabs are tabItems as well, just further down the html, so you can add +5 (for Village overview tabs)
202+
// to get to subtabs
203+
await DriverHelper.ClickByClassName(acc, "tabItem", (int)subTab + 5);
204+
}
205+
return true;
206+
}
207+
208+
209+
151210
//public static async Task<bool> UpgradeBuilding(Account acc, HtmlDocument html, UpgradeButton type)
152211
//{
153212
// var div = html.DocumentNode.Descendants("div").FirstOrDefault(x => x.HasClass($"section{(int)type}"));
@@ -208,5 +267,27 @@ public enum TreasuryTab
208267
SmallArts,
209268
BigArts,
210269
}
270+
public enum HeroTab
271+
{
272+
Attributes = 0,
273+
Appearance,
274+
Adventures,
275+
Auctions,
276+
}
277+
278+
public enum OverviewTab
279+
{
280+
Overview = 0,
281+
Resources,
282+
Warehouse,
283+
CulturePoints,
284+
Troops
285+
}
286+
public enum TroopOverview
287+
{
288+
OwnTroops = 0,
289+
TroopsInVillage,
290+
Smithy,
291+
}
211292
}
212293
}

TbsCore/Parsers/InfrastructureParser.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
using TbsCore.Models.VillageModels;
66
using TbsCore.Helpers;
77
using TbsCore.TravianData;
8+
using HtmlAgilityPack;
89

910
namespace TbsCore.Parsers
1011
{
1112
public static class InfrastructureParser
1213
{
13-
public static List<Building> GetBuildings(Account acc, HtmlAgilityPack.HtmlDocument htmlDoc)
14+
public static List<Building> GetBuildings(Account acc, HtmlDocument htmlDoc)
1415
{
1516
List<Building> buildings = new List<Building>();
1617
HtmlAgilityPack.HtmlNode villMap = null;
@@ -62,7 +63,7 @@ public static List<Building> GetBuildings(Account acc, HtmlAgilityPack.HtmlDocum
6263
/// <summary>
6364
/// Get currently building (upgrading/constructing) buildings from dorf1/dorf2
6465
/// </summary>
65-
public static List<BuildingCurrently> CurrentlyBuilding(HtmlAgilityPack.HtmlDocument htmlDoc, Account acc)
66+
public static List<BuildingCurrently> CurrentlyBuilding(HtmlDocument htmlDoc, Account acc)
6667
{
6768
var finishButton = htmlDoc.DocumentNode.Descendants("div").FirstOrDefault(x => x.HasClass("finishNow"));
6869
if (finishButton == null) return null;
@@ -82,7 +83,7 @@ public static List<BuildingCurrently> CurrentlyBuilding(HtmlAgilityPack.HtmlDocu
8283
return ret;
8384
}
8485

85-
public static TimeSpan GetBuildDuration(HtmlAgilityPack.HtmlNode node, Classificator.ServerVersionEnum version)
86+
public static TimeSpan GetBuildDuration(HtmlNode node, Classificator.ServerVersionEnum version)
8687
{
8788
var duration = node.Descendants("div").FirstOrDefault(x => x.HasClass("duration"));
8889
if (duration != null)
@@ -104,7 +105,7 @@ public static TimeSpan GetBuildDuration(HtmlAgilityPack.HtmlNode node, Classific
104105
return new TimeSpan();
105106
}
106107

107-
public static (Classificator.BuildingEnum, int) UpgradeBuildingGetInfo(HtmlAgilityPack.HtmlNode node)
108+
public static (Classificator.BuildingEnum, int) UpgradeBuildingGetInfo(HtmlNode node)
108109
{
109110
var classes = node.GetClasses().ToList();
110111

@@ -126,5 +127,16 @@ public static (Classificator.BuildingEnum, int) UpgradeBuildingGetInfo(HtmlAgili
126127

127128
return (building, level);
128129
}
130+
131+
public static int CurrentlyActiveTab(HtmlDocument html)
132+
{
133+
// TODO: fix for ttwars (class="container active")
134+
var tabs = html.DocumentNode.Descendants("a").Where(x => x.HasClass("tabItem")).ToList();
135+
for (int i = 0; i < tabs.Count(); i++)
136+
{
137+
if (tabs[i].HasClass("active")) return i;
138+
}
139+
return -1;
140+
}
129141
}
130142
}

TbsCore/Tasks/LowLevel/AddFarm.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class AddFarm : BotTask
1212

1313
public override async Task<TaskRes> Execute(Account acc)
1414
{
15-
await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id=39&tt=99");
15+
16+
await NavigationHelper.ToRallyPoint(acc, Vill, NavigationHelper.RallyPointTab.Farmlist);
1617

1718
// Show "Add raid" popup
1819
await DriverHelper.ExecuteScript(acc, $"Travian.Game.RaidList.addSlot({this.FarmListId},'','','rallyPoint');");

TbsCore/Tasks/LowLevel/ChangeVillageName.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public override async Task<TaskRes> Execute(Account acc)
2727

2828
foreach (var change in ChangeList)
2929
{
30-
var script = $"document.getElementsByName('dname[{change.Item1}]=')[0].value='{change.Item2}'";
31-
acc.Wb.ExecuteScript(script); //insert new name into the textbox
30+
await DriverHelper.WriteByName(acc, $"dname[{change.Item1}]=", change.Item2);
3231
}
3332

3433
await Task.Delay(AccountHelper.Delay(acc));
@@ -57,9 +56,8 @@ public override async Task<TaskRes> Execute(Account acc)
5756
//seem like they want we typing instead of setting value (= =)
5857
//if u only setting value by javascript the button still disable
5958

60-
var script = $"document.getElementsByName('dname[{change.Item1}]=')[0].value=''";
61-
//empty value of textbox
62-
acc.Wb.ExecuteScript(script);
59+
// Empty text box
60+
await DriverHelper.WriteByName(acc, $"dname[{change.Item1}]=", "");
6361
//insert new name into the textbox
6462
acc.Wb.FindElementByXPath($"//input[@name='dname[{change.Item1}]=']").SendKeys(change.Item2);
6563
}

0 commit comments

Comments
 (0)