Skip to content

Commit d35381e

Browse files
committed
Fixed menu/building navigation
1 parent 1c47098 commit d35381e

29 files changed

+283
-240
lines changed

TbsCore/Core/PostLoadHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static List<Action> GetPostLoadTasks(Account acc)
5555
var ran = new Random();
5656

5757
//Web browser not initialized
58+
// Update villages
5859
if (!UpdateAccountObject.UpdateVillages(html, acc)) return new List<Action>();
5960

6061
//Didnt check version yet

TbsCore/Helpers/DriverHelper.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ public static string GetBearerToken(Account acc, bool log = true)
8787

8888
/// <summary>
8989
/// Write coordinates into the number inputs. Used when sending troops, resources etc.
90+
/// If coordinates are already there (embedded in url), skip this task.
9091
/// </summary>
9192
internal static async Task WriteCoordinates(Account acc, Coordinates coordinates)
9293
{
93-
await WriteById(acc, "xCoordInput", coordinates.x);
94-
await WriteById(acc, "yCoordInput", coordinates.y);
94+
if (string.IsNullOrEmpty(acc.Wb.Html.GetElementbyId("xCoordInput").GetAttributeValue("value", "")))
95+
await WriteById(acc, "xCoordInput", coordinates.x);
96+
if (string.IsNullOrEmpty(acc.Wb.Html.GetElementbyId("yCoordInput").GetAttributeValue("value", "")))
97+
await WriteById(acc, "yCoordInput", coordinates.y);
9598
}
9699

97100
#region By Id
@@ -110,32 +113,32 @@ public static async Task<bool> SelectIndexById(Account acc, string query, int in
110113

111114
#region By Class Name
112115

113-
public static async Task<bool> ClickByClassName(Account acc, string query, bool log = true) =>
114-
await ExecuteAction(acc, new QueryByClassName(query), new ActionClick(), log);
116+
public static async Task<bool> ClickByClassName(Account acc, string query, int qindex = 0, bool log = true) =>
117+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionClick(), log);
115118

116-
public static async Task<bool> WriteByClassName(Account acc, string query, object text, bool log = true) =>
117-
await ExecuteAction(acc, new QueryByClassName(query), new ActionWrite(text), log);
119+
public static async Task<bool> WriteByClassName(Account acc, string query, object text, int qindex = 0, bool log = true) =>
120+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionWrite(text), log);
118121

119-
public static async Task<bool> CheckByClassName(Account acc, string query, bool check, bool log = true) =>
120-
await ExecuteAction(acc, new QueryByClassName(query), new ActionCheck(check), log);
122+
public static async Task<bool> CheckByClassName(Account acc, string query, bool check, int qindex = 0, bool log = true) =>
123+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionCheck(check), log);
121124

122-
public static async Task<bool> SelectIndexByClassName(Account acc, string query, int index, bool log = true) =>
123-
await ExecuteAction(acc, new QueryByClassName(query), new ActionSelectIndex(index), log);
125+
public static async Task<bool> SelectIndexByClassName(Account acc, string query, int index, int qindex = 0, bool log = true) =>
126+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionSelectIndex(index), log);
124127
#endregion
125128

126129
#region By Name
127130

128-
public static async Task<bool> ClickByName(Account acc, string query, bool log = true) =>
129-
await ExecuteAction(acc, new QueryByName(query), new ActionClick(), log);
131+
public static async Task<bool> ClickByName(Account acc, string query, int qindex = 0, bool log = true) =>
132+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionClick(), log);
130133

131-
public static async Task<bool> WriteByName(Account acc, string query, object text, bool log = true, bool update = true) =>
132-
await ExecuteAction(acc, new QueryByName(query), new ActionWrite(text), log, update);
134+
public static async Task<bool> WriteByName(Account acc, string query, object text, int qindex = 0, bool log = true) =>
135+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionWrite(text), log);
133136

134-
public static async Task<bool> CheckByName(Account acc, string query, bool check, bool log = true) =>
135-
await ExecuteAction(acc, new QueryByName(query), new ActionCheck(check), log);
137+
public static async Task<bool> CheckByName(Account acc, string query, bool check, int qindex = 0, bool log = true) =>
138+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionCheck(check), log);
136139

137-
public static async Task<bool> SelectIndexByName(Account acc, string query, int index, bool log = true) =>
138-
await ExecuteAction(acc, new QueryByName(query), new ActionSelectIndex(index), log);
140+
public static async Task<bool> SelectIndexByName(Account acc, string query, int index, int qindex = 0, bool log = true) =>
141+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionSelectIndex(index), log);
139142
#endregion
140143

141144
#region By Attribute Value
@@ -159,10 +162,10 @@ public class QueryById : Query
159162
{ public QueryById(string str) => base.val = $"getElementById('{str}')"; }
160163

161164
public class QueryByName : Query
162-
{ public QueryByName(string str) => base.val = $"getElementsByName('{str}')[0]"; }
165+
{ public QueryByName(string str, int index = 0) => base.val = $"getElementsByName('{str}')[{index}]"; }
163166

164167
public class QueryByClassName : Query
165-
{ public QueryByClassName(string str) => base.val = $"getElementsByClassName('{str}')[0]"; }
168+
{ public QueryByClassName(string str, int index = 0) => base.val = $"getElementsByClassName('{str}')[{index}]"; }
166169

167170
public class QueryByAttributeVal : Query
168171
{ public QueryByAttributeVal(string attribute, string value) => base.val = $"querySelectorAll('[{attribute}=\"{value}\"]')[0]"; }

TbsCore/Helpers/NavigationHelper.cs

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
using HtmlAgilityPack;
2+
using OpenQA.Selenium;
3+
using OpenQA.Selenium.Support.UI;
4+
using System;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using TbsCore.Models.AccModels;
8+
using TbsCore.Models.MapModels;
9+
using TbsCore.Models.VillageModels;
10+
using TbsCore.TravianData;
11+
using static TbsCore.Helpers.Classificator;
12+
13+
namespace TbsCore.Helpers
14+
{
15+
public static class NavigationHelper
16+
{
17+
public static async Task<bool> MainNavigate(Account acc, MainNavigationButton button)
18+
{
19+
var nav = acc.Wb.Html.GetElementbyId("navigation");
20+
if (nav == null) return false;
21+
await DriverHelper.ClickByAttributeValue(acc, "accesskey", ((int)button).ToString());
22+
return true;
23+
}
24+
25+
/// <summary>
26+
/// Navigates to a specific building id
27+
/// </summary>
28+
private static async Task ToBuildingId(Account acc, int index)
29+
{
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;
35+
36+
if (index < 19) // dorf1
37+
{
38+
if (!acc.Wb.CurrentUrl.Contains("dorf1.php") || acc.Wb.CurrentUrl.Contains("id="))
39+
await MainNavigate(acc, MainNavigationButton.Resources);
40+
await DriverHelper.ClickByClassName(acc, $"buildingSlot{index}");
41+
}
42+
else // dorf2
43+
{
44+
if (!acc.Wb.CurrentUrl.Contains("dorf2.php") || acc.Wb.CurrentUrl.Contains("id="))
45+
await MainNavigate(acc, MainNavigationButton.Buildings);
46+
await DriverHelper.ClickByClassName(acc, $"aid{index}");
47+
}
48+
await DriverHelper.WaitLoaded(acc);
49+
}
50+
51+
public static async Task BuildNavigate(Account acc, Building building) =>
52+
await ToBuildingId(acc, building.Id);
53+
54+
/// <summary>
55+
/// TTWars convert tab into url query
56+
/// </summary>
57+
/// <param name="building"></param>
58+
/// <param name="tab"></param>
59+
/// <returns></returns>
60+
private static string TTWarsTabUrl(BuildingEnum building, int tab)
61+
{
62+
string[] indexMapping;
63+
switch (building)
64+
{
65+
case BuildingEnum.Marketplace:
66+
// t=
67+
// 0, 5, 1, 2
68+
indexMapping = new string[] { "0", "5", "1", "2"};
69+
return $"t={indexMapping[tab]}";
70+
case BuildingEnum.Residence:
71+
case BuildingEnum.Palace:
72+
case BuildingEnum.CommandCenter:
73+
// s=
74+
// 0, 1, 2, 3, 4
75+
indexMapping = new string[] { "0", "1", "2", "3", "4" };
76+
return $"s={indexMapping[tab]}";
77+
case BuildingEnum.RallyPoint:
78+
// tt=
79+
// 0, 1, 2, 3, 99
80+
indexMapping = new string[] { "0", "1", "2", "3", "99" };
81+
return $"tt={indexMapping[tab]}";
82+
case BuildingEnum.Treasury:
83+
// s=
84+
// 0, 5, 1, 2
85+
indexMapping = new string[] { "0", "5", "1", "2" };
86+
return $"s={indexMapping[tab]}";
87+
default: return "";
88+
}
89+
}
90+
91+
/// <summary>
92+
/// Enters a specific building.
93+
/// </summary>
94+
/// <param name="acc">Account</param>
95+
/// <param name="building">Building to enter</param>
96+
/// <param name="tab">Specify tab</param>
97+
/// <returns>Whether it was successful</returns>
98+
public static async Task<bool> EnterBuilding(Account acc, Building building, int? tab = null, Coordinates coords = null)
99+
{
100+
switch (acc.AccInfo.ServerVersion)
101+
{
102+
case ServerVersionEnum.T4_5:
103+
await ToBuildingId(acc, building.Id);
104+
if (tab != null) // Navigate to correct tab
105+
await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
106+
break;
107+
case ServerVersionEnum.TTwars:
108+
// Directly navigate to url
109+
string url = $"{acc.AccInfo.ServerUrl}/build.php?id={building.Id}";
110+
if (tab != null) url += "?" + TTWarsTabUrl(building.Type, tab ?? 0);
111+
if (coords != null) url += "&z=" + coords.GetKid(acc);
112+
await acc.Wb.Navigate(url);
113+
break;
114+
}
115+
return true;
116+
}
117+
118+
public static async Task<bool> EnterBuilding(Account acc, Village vill, BuildingEnum buildingEnum, int? tab = null, Coordinates coords = null)
119+
{
120+
var building = vill.Build.Buildings.FirstOrDefault(x => x.Type == buildingEnum);
121+
if (building == null)
122+
{
123+
acc.Logger.Warning($"Tried to enter {buildingEnum} but couldn't find it in village {vill.Name}!");
124+
return false;
125+
}
126+
return await EnterBuilding(acc, building, tab, coords);
127+
}
128+
129+
public static async Task<bool> ToMarketplace(Account acc, Village vill, MarketplaceTab tab, Coordinates coords = null) =>
130+
await EnterBuilding(acc, vill, BuildingEnum.Marketplace, (int)tab, coords);
131+
132+
/// <summary>
133+
/// Navigate to Residence/Palace/CommandCenter
134+
/// </summary>
135+
public static async Task<bool> ToGovernmentBuilding(Account acc, Village vill, ResidenceTab tab)
136+
{
137+
var building = vill.Build.Buildings
138+
.FirstOrDefault(x =>
139+
x.Type == BuildingEnum.Residence ||
140+
x.Type == BuildingEnum.Palace ||
141+
x.Type == BuildingEnum.CommandCenter
142+
);
143+
return await EnterBuilding(acc, building, (int)tab);
144+
}
145+
public static async Task<bool> ToRallyPoint(Account acc, Village vill, RallyPointTab tab, Coordinates coords = null) =>
146+
await EnterBuilding(acc, vill, BuildingEnum.RallyPoint, (int)tab, coords);
147+
public static async Task<bool> ToTreasury(Account acc, Village vill, TreasuryTab tab) =>
148+
await EnterBuilding(acc, vill, BuildingEnum.Treasury, (int)tab);
149+
150+
151+
//public static async Task<bool> UpgradeBuilding(Account acc, HtmlDocument html, UpgradeButton type)
152+
//{
153+
// var div = html.DocumentNode.Descendants("div").FirstOrDefault(x => x.HasClass($"section{(int)type}"));
154+
// if (div == null) return false;
155+
// var button = div.Descendants("button").FirstOrDefault();
156+
// if (button == null) return false;
157+
// acc.Wb.Driver.FindElementById(button.Id).Click();
158+
// await DriverHelper.WaitLoaded(acc);
159+
// return true;
160+
//}
161+
//public enum UpgradeButton
162+
//{
163+
// Normal = 1,
164+
// Faster,
165+
//}
166+
167+
public enum MainNavigationButton
168+
{
169+
Resources = 1,
170+
Buildings,
171+
Map,
172+
Statistics,
173+
Reports,
174+
Messages,
175+
DailyQuests
176+
}
177+
178+
public enum RallyPointTab
179+
{
180+
Managenment = 0,
181+
Overview,
182+
SendTroops,
183+
CombatSimulator,
184+
Farmlist
185+
};
186+
187+
public enum MarketplaceTab
188+
{
189+
Managenment = 0,
190+
SendResources,
191+
Buy,
192+
Offer
193+
}
194+
195+
public enum ResidenceTab // Or Palace / CommandCenter
196+
{
197+
Managenment = 0,
198+
Train,
199+
CulturePoints,
200+
Loyalty,
201+
Expansion,
202+
}
203+
204+
public enum TreasuryTab
205+
{
206+
Managenment = 0,
207+
ArtsInArea,
208+
SmallArts,
209+
BigArts,
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)