private static Building GetLowestRes(Account acc, Village vill, List <Building> buildings) { //get distinct field types var distinct = buildings.Select(x => x.Type).Distinct().ToList(); long lowestRes = long.MaxValue; BuildingEnum toUpgrade = BuildingEnum.Cropland; var heroRes = vill.Settings.UseHeroRes ? HeroHelper.GetHeroResources(acc) : new long[] { 0, 0, 0, 0 }; var resSum = ResourcesHelper.SumArr(vill.Res.Stored.Resources.ToArray(), heroRes); foreach (var distinctType in distinct) { if (distinctType == BuildingEnum.Woodcutter && resSum[0] < lowestRes) { lowestRes = resSum[0]; toUpgrade = BuildingEnum.Woodcutter; } else if (distinctType == BuildingEnum.ClayPit && resSum[1] < lowestRes) { lowestRes = resSum[1]; toUpgrade = BuildingEnum.ClayPit; } else if (distinctType == BuildingEnum.IronMine && resSum[2] < lowestRes) { lowestRes = resSum[2]; toUpgrade = BuildingEnum.IronMine; } else if (distinctType == BuildingEnum.Cropland && resSum[3] < lowestRes) { lowestRes = resSum[3]; toUpgrade = BuildingEnum.Cropland; } } return(FindLowestLevelBuilding(buildings.Where(x => x.Type == toUpgrade).ToList())); }
/// <summary> /// If there are enough resources, return TimeSpan(0) /// Otherwise calculate how long it will take to get enough resources and transit res from /// main village, if we have that enabled. Return the one that takes less time. /// DateTime for usage in nextExecution time /// </summary> /// <param name="acc">Account</param> /// <param name="vill">(target) Village</param> /// <param name="requiredRes">Resources required</param> /// <param name="task">Bot task that doesn't have enough resources</param> /// <param name="buildingTask">Potential building task</param> /// <returns>When next village update should occur</returns> private static DateTime?NewUnfinishedTask(Account acc, Village vill, Resources requiredRes, BotTask task, BuildingTask buildingTask = null) { var stillNeededRes = SubtractResources(requiredRes.ToArray(), vill.Res.Stored.Resources.ToArray(), true); // Whether we have enough resources. This should already be checked before calling this method! if (IsZeroResources(stillNeededRes)) { ResSpendingHelper.AddUnfinishedTask(vill, task, requiredRes); return(DateTime.Now); } acc.Wb.Log($"Not enough resources for the task {task.GetName()}! Needed {requiredRes}. Bot will try finish the task later"); if (IsStorageTooLow(acc, vill, requiredRes)) { acc.Wb.Log($"Storage is too low."); ResSpendingHelper.AddUnfinishedTask(vill, task, requiredRes); return(null); } // Try to use hero resources first if (vill.Settings.UseHeroRes && acc.AccInfo.ServerVersion == ServerVersionEnum.T4_5) // Only T4.5 has resources in hero inv { var heroRes = HeroHelper.GetHeroResources(acc); // If we have some hero resources, we should use those first if (!IsZeroResources(heroRes)) { var heroEquipTask = UseHeroResources(acc, vill, ref stillNeededRes, heroRes, buildingTask); // If we have enough hero res for our task, execute the task // right after hero equip finishes if (IsZeroResources(SubtractResources(stillNeededRes, heroRes, true))) { heroEquipTask.NextTask = task; return(null); } } } ResSpendingHelper.AddUnfinishedTask(vill, task, requiredRes); // When will we have enough resources from production DateTime enoughRes = TimeHelper.EnoughResToUpgrade(vill, stillNeededRes); var mainVill = AccountHelper.GetMainVillage(acc); if (mainVill == vill) { return(enoughRes); } DateTime resTransit = MarketHelper.TransitResourcesFromMain(acc, vill); if (resTransit < enoughRes) { enoughRes = resTransit; } if (enoughRes < DateTime.Now) { return(DateTime.Now); } return(enoughRes); }
/// <summary> /// Will be called before each task to update resources/msg?,villages,quests,hero health, adventures num, gold/silver /// </summary> /// <param name="acc">Account</param> /// <returns>True if successful, false if error</returns> private static bool PreTaskRefresh(Account acc) { var html = acc.Wb.Html; try { //check & update dorf1/dorf2 if (!UpdateAccountObject.UpdateVillages(html, acc)) { return(false); //Web browser not initiali } var activeVill = acc.Villages.FirstOrDefault(x => x.Active); //update dorf1/dorf2 if (acc.Wb.CurrentUrl.Contains("dorf1")) { UpdateDorf1Info(acc); } else if (acc.Wb.CurrentUrl.Contains("dorf2")) { UpdateDorf2Info(acc); } acc.AccInfo.CulturePoints = RightBarParser.GetCulurePoints(html, acc.AccInfo.ServerVersion); var villExpansionReady = acc.Villages.FirstOrDefault(x => x.Expansion.ExpensionAvailable); if (acc.AccInfo.CulturePoints.MaxVillages > acc.AccInfo.CulturePoints.VillageCount && villExpansionReady != null) { villExpansionReady.Expansion.ExpensionAvailable = false; TaskExecutor.AddTaskIfNotExists(acc, new SendSettlers() { ExecuteAt = DateTime.Now, vill = villExpansionReady }); } acc.AccInfo.Tribe = LeftBarParser.GetAccountTribe(acc, html); acc.Quests = RightBarParser.GetQuests(html); var goldSilver = RightBarParser.GetGoldAndSilver(html, acc.AccInfo.ServerVersion); acc.AccInfo.Gold = goldSilver[0]; acc.AccInfo.Silver = goldSilver[1]; acc.AccInfo.PlusAccount = RightBarParser.HasPlusAccount(html, acc.AccInfo.ServerVersion); //Check reports/msg count if (MsgParser.UnreadMessages(html, acc.AccInfo.ServerVersion) > 0 && !acc.Wb.CurrentUrl.Contains("messages.php") && !IsTaskOnQueue(acc, typeof(ReadMessage))) { TaskExecutor.AddTask(acc, new ReadMessage() { ExecuteAt = DateTime.Now.AddMilliseconds(AccountHelper.Delay() * 30) }); } //update loyalty of village activeVill.Res.FreeCrop = RightBarParser.GetFreeCrop(html); activeVill.Res.Capacity = ResourceParser.GetResourceCapacity(html, acc.AccInfo.ServerVersion); activeVill.Res.Stored = ResourceParser.GetResources(html); float ratio = (float)activeVill.Res.Stored.Resources.Crop / activeVill.Res.Capacity.GranaryCapacity; if (ratio >= 0.99 && acc.AccInfo.Gold >= 3 && activeVill.Market.Npc.Enabled && (activeVill.Market.Npc.NpcIfOverflow || !MarketHelper.NpcWillOverflow(activeVill))) { //npc crop! TaskExecutor.AddTaskIfNotExistInVillage(acc, activeVill, new NPC() { ExecuteAt = DateTime.MinValue, vill = activeVill }); } if (acc.Settings.AutoActivateProductionBoost && CheckProductionBoost(acc)) { TaskExecutor.AddTask(acc, new TTWarsPlusAndBoost() { ExecuteAt = DateTime.Now.AddSeconds(1) }); } acc.Hero.AdventureNum = HeroParser.GetAdventureNum(html, acc.AccInfo.ServerVersion); acc.Hero.Status = HeroParser.HeroStatus(html, acc.AccInfo.ServerVersion); acc.Hero.HeroInfo.Health = HeroParser.GetHeroHealth(html, acc.AccInfo.ServerVersion); bool heroReady = (acc.Hero.HeroInfo.Health > acc.Hero.Settings.MinHealth && acc.Hero.Settings.AutoSendToAdventure && acc.Hero.Status == Hero.StatusEnum.Home && acc.Hero.NextHeroSend < DateTime.Now); // Update adventures if (heroReady && (acc.Hero.AdventureNum != acc.Hero.Adventures.Count() || HeroHelper.AdventureInRange(acc))) //update adventures { AddTaskIfNotExists(acc, new StartAdventure() { ExecuteAt = DateTime.Now.AddSeconds(10) }); } if (acc.Hero.AdventureNum == 0 && acc.Hero.Settings.BuyAdventures) //for UNL servers, buy adventures { AddTaskIfNotExists(acc, new TTWarsBuyAdventure() { ExecuteAt = DateTime.Now.AddSeconds(5) }); } if (acc.Hero.Status == Hero.StatusEnum.Dead && acc.Hero.Settings.AutoReviveHero) //if hero is dead, revive him { AddTaskIfNotExists(acc, new ReviveHero() { ExecuteAt = DateTime.Now.AddSeconds(5), vill = AccountHelper.GetHeroReviveVillage(acc) }); } if (HeroParser.LeveledUp(html) && acc.Hero.Settings.AutoSetPoints) { AddTaskIfNotExists(acc, new HeroSetPoints() { ExecuteAt = DateTime.Now }); } return(true); } catch (Exception e) { Console.WriteLine("Error in PreTask " + e.Message + "\n\nStack Trace: " + e.StackTrace + "\n-----------------------"); return(false); } }