|
1 | 1 | using HtmlAgilityPack;
|
| 2 | +using OpenQA.Selenium; |
2 | 3 | using System;
|
3 | 4 | using System.Linq;
|
4 | 5 | using System.Threading.Tasks;
|
@@ -35,6 +36,7 @@ public override async Task<TaskRes> Execute(Account acc)
|
35 | 36 | await SwitchVillage(acc);
|
36 | 37 |
|
37 | 38 | var firstComplete = Vill.Build.CurrentlyBuilding.FirstOrDefault();
|
| 39 | + if (firstComplete == null) continue; |
38 | 40 | NextExecute = TimeHelper.RanDelay(acc, firstComplete.Duration);
|
39 | 41 | acc.Logger.Information($"Next building will be contructed after {firstComplete.Building} - level {firstComplete.Level} complete. ({NextExecute})", this);
|
40 | 42 | return TaskRes.Executed;
|
@@ -122,7 +124,7 @@ public override async Task<TaskRes> Execute(Account acc)
|
122 | 124 | acc.Logger.Warning($"Not enough resources to construct {_buildingTask.Building} - Level {_buildingTask.Level}! Needed {cost}. Bot will try finish the task later", this);
|
123 | 125 | DateTime enoughRes = TimeHelper.EnoughResToUpgrade(Vill, stillNeededRes);
|
124 | 126 | NextExecute = TimeHelper.RanDelay(acc, enoughRes);
|
125 |
| - continue; |
| 127 | + return TaskRes.Executed; |
126 | 128 | }
|
127 | 129 | var heroRes = HeroHelper.GetHeroResources(acc);
|
128 | 130 |
|
@@ -265,11 +267,12 @@ private async Task<bool> Upgrade(Account acc, HtmlNode node)
|
265 | 267 | var watchAd = false;
|
266 | 268 | if (acc.AccInfo.ServerVersion == ServerVersionEnum.T4_5 && buildDuration.TotalMinutes > acc.Settings.WatchAdAbove)
|
267 | 269 | {
|
268 |
| - watchAd = await TryFastUpgrade(acc); |
| 270 | + // watchAd = await TryFastUpgrade(acc); |
269 | 271 | }
|
270 | 272 |
|
271 | 273 | if (!watchAd)
|
272 | 274 | {
|
| 275 | + upgradeButton = buttons.FirstOrDefault(x => x.HasClass("build")); |
273 | 276 | await DriverHelper.ClickById(acc, upgradeButton.Id); // Normal upgrade
|
274 | 277 | }
|
275 | 278 |
|
@@ -321,65 +324,68 @@ private void PostTaskCheckDorf(Account acc)
|
321 | 324 | /// <returns>Whether bot watched the ad</returns>
|
322 | 325 | private async Task<bool> TryFastUpgrade(Account acc)
|
323 | 326 | {
|
324 |
| - if (!await DriverHelper.ClickByClassName(acc, "videoFeatureButton green", log: false)) return false; |
325 |
| - await Task.Delay(AccountHelper.Delay(acc)); |
| 327 | + acc.Wb.UpdateHtml(); |
| 328 | + var nodeFastUpgrade = acc.Wb.Html.DocumentNode.Descendants("button").FirstOrDefault(x => x.HasClass("videoFeatureButton") && x.HasClass("green")); |
| 329 | + if (nodeFastUpgrade == null) return false; |
| 330 | + var elementFastUpgrade = acc.Wb.Driver.FindElement(By.XPath(nodeFastUpgrade.XPath)); |
| 331 | + if (elementFastUpgrade == null) return false; |
| 332 | + elementFastUpgrade.Click(); |
| 333 | + |
| 334 | + await Task.Delay(rand.Next(1000, 2000)); |
326 | 335 |
|
327 | 336 | // Confirm
|
328 | 337 | acc.Wb.UpdateHtml();
|
329 |
| - if (acc.Wb.Html.DocumentNode.SelectSingleNode("//input[@name='adSalesVideoInfoScreen']") != null) |
| 338 | + var node = acc.Wb.Html.DocumentNode.SelectSingleNode("//input[@name='adSalesVideoInfoScreen']"); |
| 339 | + if (node != null) |
330 | 340 | {
|
331 |
| - await DriverHelper.ClickByName(acc, "adSalesVideoInfoScreen"); |
332 |
| - await Task.Delay(AccountHelper.Delay(acc)); |
333 |
| - |
| 341 | + var element = acc.Wb.Driver.FindElement(By.XPath(node.XPath)); |
| 342 | + if (element == null) |
| 343 | + { |
| 344 | + await acc.Wb.Refresh(); |
| 345 | + return false; |
| 346 | + } |
| 347 | + element.Click(); |
334 | 348 | await DriverHelper.ExecuteScript(acc, "jQuery(window).trigger('showVideoWindowAfterInfoScreen')");
|
335 |
| - await Task.Delay(AccountHelper.Delay(acc)); |
336 | 349 | }
|
337 | 350 |
|
338 |
| - while (true) |
| 351 | + await Task.Delay(rand.Next(10000, 18000)); |
| 352 | + |
| 353 | + // click to play video |
| 354 | + acc.Wb.UpdateHtml(); |
| 355 | + var nodeIframe = acc.Wb.Html.GetElementbyId("videoFeature"); |
| 356 | + if (nodeIframe == null) |
339 | 357 | {
|
340 |
| - acc.Wb.UpdateHtml(); |
341 |
| - var node = acc.Wb.Html.GetElementbyId("videoFeature"); |
342 |
| - if (node == null) continue; |
343 |
| - var element = acc.Wb.Driver.FindElementById("videoFeature"); |
344 |
| - if (element == null) continue; |
345 |
| - element.Click(); |
346 |
| - break; |
| 358 | + await acc.Wb.Refresh(); |
| 359 | + return false; |
347 | 360 | }
|
348 |
| - // Has to be a legit "click" |
349 |
| - |
350 |
| - // wait for finish watching ads |
351 |
| - var timeout = DateTime.Now.AddSeconds(100); |
352 |
| - do |
| 361 | + var elementIframe = acc.Wb.Driver.FindElementById("videoFeature"); |
| 362 | + if (elementIframe == null) |
353 | 363 | {
|
354 |
| - await Task.Delay(3000); |
355 |
| - |
356 |
| - //skip ads from Travian Games |
357 |
| - //they use ifarme to emebed ads video to their game |
358 |
| - acc.Wb.UpdateHtml(); |
359 |
| - |
360 |
| - if (acc.Wb.Html.GetElementbyId("videoArea") != null) |
361 |
| - { |
362 |
| - acc.Wb.SwitchTo().Frame(acc.Wb.FindElementById("videoArea")); |
363 |
| - |
364 |
| - // trick to skip |
365 |
| - await DriverHelper.ExecuteScript(acc, "var video = document.getElementsByTagName('video')[0];video.currentTime = video.duration - 1;", false, false); |
366 |
| - //back to first page |
| 364 | + await acc.Wb.Refresh(); |
| 365 | + return false; |
| 366 | + } |
| 367 | + elementIframe.Click(); |
367 | 368 |
|
368 |
| - acc.Wb.SwitchTo().DefaultContent(); |
369 |
| - } |
370 |
| - if (timeout < DateTime.Now) return false; |
| 369 | + try |
| 370 | + { |
| 371 | + await DriverHelper.WaitPageChange(acc, "dorf", 3); |
| 372 | + } |
| 373 | + catch |
| 374 | + { |
| 375 | + await acc.Wb.Refresh(); |
| 376 | + return false; |
371 | 377 | }
|
372 |
| - while (acc.Wb.CurrentUrl.Contains("build.php")); |
373 | 378 |
|
374 | 379 | // Don't show again
|
375 |
| - await Task.Delay(1000); |
| 380 | + await Task.Delay(rand.Next(1000, 2000)); |
376 | 381 | acc.Wb.UpdateHtml();
|
377 | 382 |
|
378 | 383 | if (acc.Wb.Html.GetElementbyId("dontShowThisAgain") != null)
|
379 | 384 | {
|
380 | 385 | await DriverHelper.ClickById(acc, "dontShowThisAgain");
|
381 | 386 | await Task.Delay(800);
|
382 | 387 | await DriverHelper.ClickByClassName(acc, "dialogButtonOk ok");
|
| 388 | + await acc.Wb.Refresh(); |
383 | 389 | }
|
384 | 390 |
|
385 | 391 | return true;
|
|
0 commit comments