public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc) { var building = vill.Build.Buildings.FirstOrDefault(x => x.Type == Classificator.BuildingEnum.Marketplace); if (building == null) { //update dorg, no buildingId found? TaskExecutor.AddTask(acc, new UpdateDorf2() { ExecuteAt = DateTime.Now, vill = vill }); Console.WriteLine($"There is no {building} in this village!"); return(TaskRes.Executed); } await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={building.Id}&t=5"); //get troop resource/time cost var troopCost = TroopCost.GetResourceCost(TrainTask.Troop, TrainTask.Great); var trainNum = TroopsHelper.TroopsToFill(acc, TargetVill, TrainTask.Troop, TrainTask.Great); //how many troops we can train with resources that we have var mainVillResStored = ResourcesHelper.ResourcesToArray(vill.Res.Stored.Resources); var targetVillStoredRes = ResourcesHelper.ResourcesToArray(TargetVill.Res.Stored.Resources); // Max troops we can train with resources that we have var maxTroopsToTrain = ResourcesHelper.MaxTroopsToTrain(mainVillResStored, targetVillStoredRes, troopCost); // If we don't have enough rsoruces to train the number of troops that we want, we will train max number of troops that we can if (maxTroopsToTrain < trainNum) { trainNum = maxTroopsToTrain; } //calculate how many resources we need to train trainNum of troops long[] neededRes = troopCost.Select(x => x * trainNum).ToArray(); //if we have already enough resources in the target village, no need to send anything if (ResourcesHelper.EnoughRes(targetVillStoredRes, neededRes)) { this.TrainTask.ExecuteAt = DateTime.Now; TaskExecutor.ReorderTaskList(acc); return(TaskRes.Executed); } //amount of resources we want to transit to target village var sendRes = ResourcesHelper.SendAmount(targetVillStoredRes, neededRes); // Check how many merchants we have. If we have 0, wait till some come back. var transitTimespan = await MarketHelper.MarketSendResource(acc, sendRes, TargetVill, this); //train the troops in the target village after we send the needed this.TrainTask.ExecuteAt = DateTime.Now.Add(transitTimespan).AddSeconds(5); TaskExecutor.ReorderTaskList(acc); //TODO: Update marketplace sending return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { await base.Execute(acc); if (!await VillageHelper.EnterBuilding(acc, Vill, Classificator.BuildingEnum.Marketplace, "&t=5")) { return(TaskRes.Executed); } //get troop resource/time cost var troopCost = TroopCost.GetResourceCost(TrainTask.Troop, TrainTask.Great); var trainNum = TroopsHelper.TroopsToFill(acc, TargetVill, TrainTask.Troop, TrainTask.Great); //how many troops we can train with resources that we have var mainVillResStored = Vill.Res.Stored.Resources.ToArray(); var targetVillStoredRes = TargetVill.Res.Stored.Resources.ToArray(); // Max troops we can train with resources that we have var maxTroopsToTrain = ResourcesHelper.MaxTroopsToTrain(mainVillResStored, targetVillStoredRes, troopCost); // If we don't have enough rsoruces to train the number of troops that we want, we will train max number of troops that we can if (maxTroopsToTrain < trainNum) { trainNum = maxTroopsToTrain; } //calculate how many resources we need to train trainNum of troops long[] neededRes = troopCost.Select(x => x * trainNum).ToArray(); //if we have already enough resources in the target village, no need to send anything if (ResourcesHelper.IsEnoughRes(targetVillStoredRes, neededRes)) { this.TrainTask.ExecuteAt = DateTime.Now; TaskExecutor.ReorderTaskList(acc); return(TaskRes.Executed); } //amount of resources we want to transit to target village var sendRes = ResourcesHelper.SendAmount(targetVillStoredRes, neededRes); // Check how many merchants we have. If we have 0, wait till some come back. var transitTimespan = await MarketHelper.MarketSendResource(acc, sendRes, TargetVill, this); //train the troops in the target village after we send the needed this.TrainTask.ExecuteAt = DateTime.Now.Add(transitTimespan).AddSeconds(5); TaskExecutor.ReorderTaskList(acc); //TODO: Update marketplace sending return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(Account acc) { building = TroopsHelper.GetTroopBuilding(Troop, Great); // Switch hero helmet. If hero will be switched, this TrainTroops task // will be executed right after the hero helmet switch if (HeroHelper.SwitchHelmet(acc, this.Vill, building, this)) { return(TaskRes.Executed); } await base.Execute(acc); if (!await VillageHelper.EnterBuilding(acc, Vill, building)) { return(TaskRes.Executed); } if (this.UpdateOnly || this.Troop == TroopsEnum.None) { return(TaskRes.Executed); } (TimeSpan dur, Resources cost) = TroopsParser.GetTrainCost(acc.Wb.Html, this.Troop); var troopNode = acc.Wb.Html.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)Troop)); if (troopNode == null) { acc.Wb.Log($"Bot tried to train {Troop} in {Vill.Name}, but couldn't find it in {building}! Are you sure you have {Troop} researched?"); return(TaskRes.Executed); } while (!troopNode.HasClass("details")) { troopNode = troopNode.ParentNode; } var inputName = troopNode.Descendants("input").FirstOrDefault().GetAttributeValue("name", ""); long maxNum = 0; switch (acc.AccInfo.ServerVersion) { case ServerVersionEnum.T4_4: maxNum = Parser.RemoveNonNumeric( troopNode.ChildNodes .FirstOrDefault(x => x.Name == "a")?.InnerText ?? "0" ); break; case ServerVersionEnum.T4_5: maxNum = Parser.RemoveNonNumeric( troopNode.ChildNodes .First(x => x.HasClass("cta")) .ChildNodes .First(x => x.Name == "a") .InnerText); break; } if (!HighSpeedServer) { var trainNum = TroopsHelper.TroopsToFill(acc, Vill, this.Troop, this.Great); // Don't train too many troops, just fill up the training building if (maxNum > trainNum) { maxNum = trainNum; } } if (maxNum < 0) { // We have already enough troops in training. return(TaskRes.Executed); } acc.Wb.Driver.ExecuteScript($"document.getElementsByName('{inputName}')[0].value='{maxNum}'"); await Task.Delay(100); await DriverHelper.ExecuteScript(acc, "document.getElementsByName('s1')[0].click()"); UpdateCurrentlyTraining(acc.Wb.Html, acc); if (!HighSpeedServer) { RepeatTrainingCycle(acc.Wb.Html, acc); } return(TaskRes.Executed); }
public override async Task <TaskRes> Execute(HtmlDocument htmlDoc, ChromeDriver wb, Files.Models.AccModels.Account acc) { building = TroopsHelper.GetTroopBuilding(Troop, Great); var buildId = vill.Build.Buildings.FirstOrDefault(x => x.Type == building); if (buildId == null) { //update dorf, no buildingId found? TaskExecutor.AddTask(acc, new UpdateDorf2() { ExecuteAt = DateTime.Now, vill = vill }); Console.WriteLine($"There is no {building} in this village!"); return(TaskRes.Executed); } await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/build.php?id={buildId.Id}"); //after finishing task, update currently training this.PostTaskCheck.Add(UpdateCurrentlyTraining); if (!HighSpeedServer) { this.PostTaskCheck.Add(RepeatTrainingCycle); } if (this.UpdateOnly || this.Troop == TroopsEnum.None) { return(TaskRes.Executed); } (TimeSpan dur, Resources cost) = TroopsParser.GetTrainCost(htmlDoc, this.Troop); var troopNode = htmlDoc.DocumentNode.Descendants("img").FirstOrDefault(x => x.HasClass("u" + (int)Troop)); while (!troopNode.HasClass("details")) { troopNode = troopNode.ParentNode; } var inputName = troopNode.Descendants("input").FirstOrDefault().GetAttributeValue("name", ""); var maxNum = Parser.RemoveNonNumeric(troopNode.ChildNodes.FirstOrDefault(x => x.Name == "a").InnerText); if (!HighSpeedServer) { var trainNum = TroopsHelper.TroopsToFill(acc, vill, this.Troop, this.Great); // Don't train too many troops, just fill up the training building if (maxNum > trainNum) { maxNum = trainNum; } } if (maxNum < 0) { // We have already enough troops in training. return(TaskRes.Executed); } wb.ExecuteScript($"document.getElementsByName('{inputName}')[0].value='{maxNum}'"); await Task.Delay(100); wb.ExecuteScript("document.getElementsByName('s1')[0].click()"); //Train button return(TaskRes.Executed); }