Skip to content

Commit 1c47098

Browse files
committed
Added querying by attribute value, started refactoring UrlHelper
1 parent 9dc8710 commit 1c47098

File tree

4 files changed

+70
-45
lines changed

4 files changed

+70
-45
lines changed

TbsCore/Helpers/DriverHelper.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ internal static async Task WriteCoordinates(Account acc, Coordinates coordinates
9494
await WriteById(acc, "yCoordInput", coordinates.y);
9595
}
9696

97+
#region By Id
9798
public static async Task<bool> ClickById(Account acc, string query, bool log = true) =>
9899
await ExecuteAction(acc, new QueryById(query), new ActionClick(), log);
99100

@@ -105,6 +106,9 @@ public static async Task<bool> CheckById(Account acc, string query, bool check,
105106

106107
public static async Task<bool> SelectIndexById(Account acc, string query, int index, bool log = true) =>
107108
await ExecuteAction(acc, new QueryById(query), new ActionSelectIndex(index), log);
109+
#endregion
110+
111+
#region By Class Name
108112

109113
public static async Task<bool> ClickByClassName(Account acc, string query, bool log = true) =>
110114
await ExecuteAction(acc, new QueryByClassName(query), new ActionClick(), log);
@@ -117,6 +121,9 @@ public static async Task<bool> CheckByClassName(Account acc, string query, bool
117121

118122
public static async Task<bool> SelectIndexByClassName(Account acc, string query, int index, bool log = true) =>
119123
await ExecuteAction(acc, new QueryByClassName(query), new ActionSelectIndex(index), log);
124+
#endregion
125+
126+
#region By Name
120127

121128
public static async Task<bool> ClickByName(Account acc, string query, bool log = true) =>
122129
await ExecuteAction(acc, new QueryByName(query), new ActionClick(), log);
@@ -129,47 +136,56 @@ public static async Task<bool> CheckByName(Account acc, string query, bool check
129136

130137
public static async Task<bool> SelectIndexByName(Account acc, string query, int index, bool log = true) =>
131138
await ExecuteAction(acc, new QueryByName(query), new ActionSelectIndex(index), log);
139+
#endregion
140+
141+
#region By Attribute Value
142+
public static async Task<bool> ClickByAttributeValue(Account acc, string attribute, string value, bool log = true) =>
143+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionClick(), log);
144+
145+
public static async Task<bool> WriteByAttributeValue(Account acc, string attribute, string value, object text, bool log = true, bool update = true) =>
146+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionWrite(text), log, update);
147+
148+
public static async Task<bool> CheckByAttributeValue(Account acc, string attribute, string value, bool check, bool log = true) =>
149+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionCheck(check), log);
150+
151+
public static async Task<bool> SelectByAttributeValue(Account acc, string attribute, string value, int index, bool log = true) =>
152+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionSelectIndex(index), log);
153+
#endregion
132154

133155
private static async Task<bool> ExecuteAction(Account acc, Query query, Action action, bool log = true, bool update = true) =>
134156
await ExecuteScript(acc, $"document.{query.val}{action.val}", log, update);
135157

136158
public class QueryById : Query
137-
138159
{ public QueryById(string str) => base.val = $"getElementById('{str}')"; }
139160

140161
public class QueryByName : Query
141-
142162
{ public QueryByName(string str) => base.val = $"getElementsByName('{str}')[0]"; }
143163

144164
public class QueryByClassName : Query
145-
146165
{ public QueryByClassName(string str) => base.val = $"getElementsByClassName('{str}')[0]"; }
147166

148-
public class ActionWrite : Action
167+
public class QueryByAttributeVal : Query
168+
{ public QueryByAttributeVal(string attribute, string value) => base.val = $"querySelectorAll('[{attribute}=\"{value}\"]')[0]"; }
149169

170+
171+
public class ActionWrite : Action
150172
{ public ActionWrite(object str) => base.val = $".value='{str}';"; }
151173

152174
public class ActionClick : Action
153-
154175
{ public ActionClick() => base.val = ".click();"; }
155-
156176
public class ActionCheck : Action
157-
158177
{ public ActionCheck(bool check) => base.val = $".checked={(check ? "true" : "false")};"; }
159178

160179
public class ActionSelectIndex : Action
161-
162180
{ public ActionSelectIndex(int index) => base.val = $".selectedIndex = {index};"; }
163181

164-
public abstract class Action
165-
{ public string val; }
182+
public abstract class Action { public string val; }
166183

167-
public abstract class Query
168-
{ public string val; }
184+
public abstract class Query { public string val; }
169185

170-
public static async Task WaitLoaded(Account acc, int delay = 10)
186+
public static async Task WaitLoaded(Account acc, int delay = 15)
171187
{
172-
var wait = new WebDriverWait(acc.Wb.Driver, TimeSpan.FromMinutes(delay));
188+
var wait = new WebDriverWait(acc.Wb.Driver, TimeSpan.FromSeconds(delay));
173189
wait.Until(driver => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
174190
acc.Wb.UpdateHtml();
175191
await TaskExecutor.PageLoaded(acc);

TbsCore/Helpers/UrlHelper.cs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,29 @@ public static class UrlHelper
1212
{
1313
public static async Task<bool> MainNavigate(Account acc, HtmlDocument html, MainNavigationButton button)
1414
{
15-
var nav = html.DocumentNode.Descendants("div").FirstOrDefault(x => x.Id.CompareTo("navigation") == 0);
15+
var nav = html.GetElementbyId("navigation");
1616
if (nav == null) return false;
1717
var accessKey = (int)button;
18-
var a = nav.Descendants("a").FirstOrDefault(x => x.GetAttributeValue("accesskey", 8) == accessKey);
19-
if (a == null) return false;
20-
21-
acc.Wb.FindElementByXPath($"//a[@accesskey={accessKey}]").Click();
22-
await DriverHelper.WaitLoaded(acc);
18+
await DriverHelper.ClickByAttributeValue(acc, "accesskey", accessKey.ToString());
2319
return true;
2420
}
2521

2622
public static async Task<bool> BuildNavigate(Account acc, HtmlDocument html, int index)
2723
{
2824
if (index < 19) // dorf1
2925
{
30-
var fields = html.DocumentNode.Descendants("div").FirstOrDefault(x => x.Id.CompareTo("resourceFieldContainer") == 0);
31-
if (fields == null) return false;
32-
var field = fields.Descendants("a").FirstOrDefault(x => x.HasClass($"buildingSlot{index}"));
33-
if (field == null) return false;
34-
35-
acc.Wb.FindElementByXPath($"//a[contains(concat(' ', @class, ' '), ' buildingSlot{index} ')]").Click();
36-
await DriverHelper.WaitLoaded(acc);
37-
return true;
26+
if (!acc.Wb.CurrentUrl.Contains("dorf1.php") || acc.Wb.CurrentUrl.Contains("id="))
27+
await MainNavigate(acc, html, MainNavigationButton.Resources);
28+
await DriverHelper.ClickByClassName(acc, $"buildingSlot{index}");
3829
}
3930
else // dorf2
4031
{
41-
var fields = html.DocumentNode.Descendants("div").FirstOrDefault(x => x.Id.CompareTo("villageContent") == 0);
42-
if (fields == null) return false;
43-
var field = fields.Descendants("div").FirstOrDefault(x => x.HasClass($"a{index}"));
44-
if (field == null) return false;
45-
var g = field.Descendants("g").FirstOrDefault();
46-
if (g == null) // empty building
47-
{
48-
acc.Wb.FindElementByXPath($"//div[contains(concat(' ', @class, ' '), ' a{index} ')]/*[name()='svg']/*").Click();
49-
}
50-
else // there is building
51-
{
52-
acc.Wb.FindElementByXPath($"//div[contains(concat(' ', @class, ' '), ' a{index} ')]/a").Click();
53-
}
54-
55-
await DriverHelper.WaitLoaded(acc);
56-
return true;
32+
if (!acc.Wb.CurrentUrl.Contains("dorf2.php") || acc.Wb.CurrentUrl.Contains("id="))
33+
await MainNavigate(acc, html, MainNavigationButton.Buildings);
34+
await DriverHelper.ClickByClassName(acc, $"aid{index}");
5735
}
36+
await DriverHelper.WaitLoaded(acc);
37+
return true;
5838
}
5939

6040
public static async Task<bool> RallyPointNavigate(Account acc, HtmlDocument html, RallyPointTab index)

TbsCore/Helpers/VillageHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using TbsCore.Models.VillageModels;
77
using TbsCore.Parsers;
88
using TbsCore.Tasks.LowLevel;
9+
using TbsCore.TravianData;
910
using static TbsCore.Helpers.Classificator;
1011

1112
namespace TbsCore.Helpers
@@ -110,6 +111,16 @@ public static async Task SwitchVillage(Account acc, int id)
110111
/// <returns>Whether it was successful</returns>
111112
public static async Task<bool> EnterBuilding(Account acc, Building building, string query = "", bool dorf = true, bool update = false)
112113
{
114+
// If multiple tabs, re-navigate to the building!
115+
if (BuildingsData.HasMultipleTabs(building.Type)) update = true;
116+
switch (acc.AccInfo.ServerVersion)
117+
{
118+
case ServerVersionEnum.T4_5:
119+
await UrlHelper.BuildNavigate(acc, acc.Wb.Html, building.Id);
120+
break;
121+
case ServerVersionEnum.TTwars:
122+
break;
123+
}
113124
// If we are already at the desired building (if gid is correct)
114125
Uri currentUri = new Uri(acc.Wb.CurrentUrl);
115126
if (HttpUtility.ParseQueryString(currentUri.Query).Get("gid") == ((int)building.Type).ToString() && !update)

TbsCore/TravianData/BuildingsData.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static int MaxBuildingLevel(Account acc, BuildingEnum building)
7373
case BuildingEnum.Brewery:
7474
switch (acc.AccInfo.ServerVersion)
7575
{
76-
case ServerVersionEnum.T4_4: return 10;
76+
case ServerVersionEnum.TTwars: return 10;
7777
default: return 20;
7878
}
7979

@@ -408,6 +408,24 @@ public static (TribeEnum, List<Prerequisite>) GetBuildingPrerequisites(BuildingE
408408
return (tribe, ret);
409409
}
410410

411+
/// <summary>
412+
/// Whether building has multiple tabs inside
413+
/// </summary>
414+
public static bool HasMultipleTabs(BuildingEnum building) =>
415+
multipleTabsBuildings.Any(x => x == building);
416+
417+
/// <summary>
418+
/// Buildings with multiple tabs inside
419+
/// </summary>
420+
private static BuildingEnum[] multipleTabsBuildings = new BuildingEnum[] {
421+
BuildingEnum.RallyPoint,
422+
BuildingEnum.CommandCenter,
423+
BuildingEnum.Residence,
424+
BuildingEnum.Palace,
425+
BuildingEnum.Marketplace,
426+
BuildingEnum.Treasury,
427+
};
428+
411429
/// <summary>
412430
/// Buildings that are always build in the same spot
413431
/// </summary>

0 commit comments

Comments
 (0)