@@ -16,6 +16,7 @@ namespace TbsCore.Tasks.LowLevel
16
16
public class UpgradeBuilding : BotTask
17
17
{
18
18
private BuildingTask _buildingTask ;
19
+ private readonly Random rand = new Random ( ) ;
19
20
20
21
public override async Task < TaskRes > Execute ( Account acc )
21
22
{
@@ -137,16 +138,32 @@ public override async Task<TaskRes> Execute(Account acc)
137
138
}
138
139
}
139
140
141
+ bool result ;
140
142
if ( construct )
141
143
{
142
- await Construct ( acc , contractNode ) ;
144
+ result = await Construct ( acc , contractNode ) ;
143
145
}
144
146
else
145
147
{
146
- await Upgrade ( acc , contractNode ) ;
148
+ result = await Upgrade ( acc , contractNode ) ;
147
149
}
148
-
149
- await PostTaskCheckDorf ( acc ) ;
150
+ if ( ! result )
151
+ {
152
+ var chanceDorf2 = rand . Next ( 1 , 100 ) ;
153
+ if ( chanceDorf2 >= 50 )
154
+ {
155
+ await NavigationHelper . ToDorf2 ( acc ) ;
156
+ }
157
+ else
158
+ {
159
+ await NavigationHelper . ToDorf1 ( acc ) ;
160
+ }
161
+ }
162
+ else
163
+ {
164
+ await DriverHelper . WaitPageChange ( acc , "dorf" ) ;
165
+ }
166
+ PostTaskCheckDorf ( acc ) ;
150
167
}
151
168
while ( true ) ;
152
169
}
@@ -156,14 +173,14 @@ public override async Task<TaskRes> Execute(Account acc)
156
173
/// </summary>
157
174
/// <param name="acc">Account</param>
158
175
/// <returns>TaskResult</returnss>
159
- private async Task Construct ( Account acc , HtmlNode node )
176
+ private async Task < bool > Construct ( Account acc , HtmlNode node )
160
177
{
161
178
var button = node . Descendants ( "button" ) . FirstOrDefault ( x => x . HasClass ( "new" ) ) ;
162
179
163
180
// Check for prerequisites
164
181
if ( button == null )
165
182
{
166
- return ;
183
+ return false ;
167
184
}
168
185
169
186
await DriverHelper . ClickById ( acc , button . Id ) ;
@@ -176,23 +193,23 @@ private async Task Construct(Account acc, HtmlNode node)
176
193
RemoveCurrentTask ( ) ;
177
194
}
178
195
179
- return ;
196
+ return true ;
180
197
}
181
198
182
199
/// <summary>
183
200
/// Building is already constructed, upgrade it
184
201
/// </summary>
185
202
/// <param name="acc">Account</param>
186
203
/// <returns>TaskResult</returns>
187
- private async Task Upgrade ( Account acc , HtmlNode node )
204
+ private async Task < bool > Upgrade ( Account acc , HtmlNode node )
188
205
{
189
206
( var buildingEnum , var lvl ) = InfrastructureParser . UpgradeBuildingGetInfo ( node ) ;
190
207
191
208
if ( buildingEnum == BuildingEnum . Site || lvl == - 1 )
192
209
{
193
210
acc . Logger . Warning ( $ "Can't upgrade building { _buildingTask . Building } in village { Vill . Name } . Will be removed from the queue.") ;
194
211
RemoveCurrentTask ( ) ;
195
- return ;
212
+ return false ;
196
213
}
197
214
198
215
// Basic task already on/above desired level, don't upgrade further
@@ -210,15 +227,15 @@ private async Task Upgrade(Account acc, HtmlNode node)
210
227
{
211
228
acc . Logger . Information ( $ "{ _buildingTask . Building } is already level { lvl } in village { Vill . Name } . Will be removed from the queue.") ;
212
229
RemoveCurrentTask ( ) ;
213
- return ;
230
+ return false ;
214
231
}
215
232
216
233
var container = acc . Wb . Html . DocumentNode . Descendants ( "div" ) . FirstOrDefault ( x => x . HasClass ( "upgradeButtonsContainer" ) ) ;
217
234
var buttons = container ? . Descendants ( "button" ) ;
218
235
if ( buttons == null )
219
236
{
220
237
acc . Logger . Warning ( $ "We wanted to upgrade { _buildingTask . Building } , but no 'upgrade' button was found! Url={ acc . Wb . CurrentUrl } ") ;
221
- return ;
238
+ return false ;
222
239
}
223
240
224
241
var errorMessage = acc . Wb . Html . GetElementbyId ( "build" )
@@ -231,14 +248,14 @@ private async Task Upgrade(Account acc, HtmlNode node)
231
248
if ( upgradeButton == null )
232
249
{
233
250
acc . Logger . Warning ( $ "We wanted to upgrade { _buildingTask . Building } , but no 'upgrade' button was found!") ;
234
- return ;
251
+ return false ;
235
252
}
236
253
237
254
// Not enough resources?
238
255
if ( acc . AccInfo . ServerVersion == ServerVersionEnum . T4_5 && errorMessage != null )
239
256
{
240
257
acc . Logger . Warning ( $ "We wanted to upgrade { _buildingTask . Building } , but there was an error message:\n { errorMessage . InnerText } ") ;
241
- return ;
258
+ return false ;
242
259
}
243
260
244
261
var buildDuration = InfrastructureParser . GetBuildDuration ( container , acc . AccInfo . ServerVersion ) ;
@@ -261,14 +278,13 @@ private async Task Upgrade(Account acc, HtmlNode node)
261
278
{
262
279
RemoveCurrentTask ( ) ;
263
280
}
281
+ return true ;
264
282
}
265
283
266
284
private void RemoveCurrentTask ( ) => Vill . Build . Tasks . Remove ( this . _buildingTask ) ;
267
285
268
- private async Task PostTaskCheckDorf ( Account acc )
286
+ private void PostTaskCheckDorf ( Account acc )
269
287
{
270
- await DriverHelper . WaitPageChange ( acc , "dorf" ) ;
271
-
272
288
// Check if residence is getting upgraded to level 10 => train settlers
273
289
var cbResidence = Vill . Build
274
290
. CurrentlyBuilding
@@ -377,7 +393,7 @@ private async Task<bool> TTWarsTryFastUpgrade(Account acc, string url)
377
393
378
394
var build = acc . Wb . Html . GetElementbyId ( "build" ) ;
379
395
if ( build != null ) RemoveCurrentTask ( ) ; // Already on max lvl
380
- else await PostTaskCheckDorf ( acc ) ;
396
+ else PostTaskCheckDorf ( acc ) ;
381
397
return true ;
382
398
}
383
399
0 commit comments