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