Skip to content

Commit 2590d76

Browse files
authored
Merge pull request #82 from Erol444/fix_constructing_bug
Fixed construction of military/resource buildings bug
2 parents db27c70 + bf699e7 commit 2590d76

File tree

10 files changed

+67
-70
lines changed

10 files changed

+67
-70
lines changed

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ private void SetupChromeDriver(Access.Access access, string username, string ser
107107
options.AddArguments("--disable-logging");
108108
options.AddArguments("--disable-infobars");
109109

110+
//options.AddAdditionalCapability("deviceOrientation", "landscape", true);
111+
//options.AddAdditionalCapability("resolution", "1280x720", true);
112+
110113
// Mute audio because of the Ads
111114
options.AddArguments("--mute-audio");
112115

TbsCore/Helpers/BuildingHelper.cs

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public static List<string> SetPrereqCombo(Account acc, Village vill)
168168

169169
public static void ReStartBuilding(Account acc, Village vill)
170170
{
171-
RemoveCompletedTasks(vill, acc);
171+
RemoveCompletedTasks(vill);
172172
//remove ongoing building task for this village
173173
acc.Tasks.Remove(typeof(UpgradeBuilding), vill);
174174

@@ -198,12 +198,12 @@ public static void ReStartDemolishing(Account acc, Village vill)
198198
//check cranny/warehouse/grannary/trapper/GG/GW
199199
switch (building)
200200
{
201-
case BuildingEnum.Warehouse: return BuildingIsOnLevel(BuildingEnum.Warehouse, 20, vill);
202-
case BuildingEnum.Granary: return BuildingIsOnLevel(BuildingEnum.Granary, 20, vill);
203-
case BuildingEnum.GreatWarehouse: return BuildingIsOnLevel(BuildingEnum.GreatWarehouse, 20, vill);
204-
case BuildingEnum.GreatGranary: return BuildingIsOnLevel(BuildingEnum.GreatGranary, 20, vill);
205-
case BuildingEnum.Trapper: return BuildingIsOnLevel(BuildingEnum.Trapper, 20, vill);
206-
case BuildingEnum.Cranny: return BuildingIsOnLevel(BuildingEnum.Cranny, 10, vill);
201+
case BuildingEnum.Warehouse: return BuildingAboveLevel(BuildingEnum.Warehouse, 20, vill);
202+
case BuildingEnum.Granary: return BuildingAboveLevel(BuildingEnum.Granary, 20, vill);
203+
case BuildingEnum.GreatWarehouse: return BuildingAboveLevel(BuildingEnum.GreatWarehouse, 20, vill);
204+
case BuildingEnum.GreatGranary: return BuildingAboveLevel(BuildingEnum.GreatGranary, 20, vill);
205+
case BuildingEnum.Trapper: return BuildingAboveLevel(BuildingEnum.Trapper, 20, vill);
206+
case BuildingEnum.Cranny: return BuildingAboveLevel(BuildingEnum.Cranny, 10, vill);
207207
default: return false;
208208
}
209209
}
@@ -220,11 +220,13 @@ public static void ReStartDemolishing(Account acc, Village vill)
220220
return true;
221221
}
222222

223-
private static bool BuildingIsOnLevel(BuildingEnum building, int lvl, Village vill)
223+
/// <summary>
224+
/// Whether there's a building above specific level or there's a task for this building
225+
/// </summary>
226+
public static bool BuildingAboveLevel(BuildingEnum building, int lvl, Village vill)
224227
{
225-
//if there already is a building on specific level or there is a task for this building
226-
// TODO: change FristOrDefault to Any
227-
return (vill.Build.Buildings.FirstOrDefault(x => x.Level == lvl && x.Type == building) != null || vill.Build.Tasks.FirstOrDefault(x => x.Level == lvl && x.Building == building) != null);
228+
return (vill.Build.Buildings.Any(x => x.Type == building && lvl <= x.Level) ||
229+
vill.Build.Tasks.Any(x => x.Building == building && lvl <= x.Level));
228230
}
229231

230232
/// <summary>
@@ -254,7 +256,7 @@ public static bool RemoveFinishedCB(Village vill)
254256
}
255257

256258
/// <summary>
257-
/// Used by building task to get the url for navigation
259+
/// Used by building task to get the url for navigation (for TTWars only)
258260
/// </summary>
259261
/// <param name="vill">Village</param>
260262
/// <param name="task">BotTask</param>
@@ -272,7 +274,7 @@ public static (string, bool) GetUrlForBuilding(Account acc, Village vill, Buildi
272274
return (null, true);
273275
}
274276

275-
public static bool IsTaskCompleted(Village vill, Account acc, BuildingTask task)
277+
public static bool IsTaskCompleted(Village vill, BuildingTask task)
276278
{
277279
if (vill == null) return true;
278280
var building = vill.Build.Buildings.FirstOrDefault(x => x.Id == task.BuildingId);
@@ -336,7 +338,7 @@ private static (string, bool) GetUrlGeneralTask(Village vill, BuildingTask task)
336338
}
337339
else // there's already a building in this spot, construct a building elsewhere
338340
{
339-
if (!BuildingHelper.FindBuildingId(vill, task))
341+
if (!FindBuildingId(vill, task))
340342
{
341343
return (null, false);
342344
}
@@ -548,62 +550,38 @@ public static bool IsWall(BuildingEnum building)
548550
/// </summary>
549551
/// <param name="vill">Village</param>
550552
/// <param name="acc">Account</param>
551-
public static void RemoveCompletedTasks(Village vill, Account acc) =>
552-
vill.Build.Tasks.RemoveAll(task => IsTaskCompleted(vill, acc, task));
553-
554-
/// <summary>
555-
/// When you already build one warehouse to lv 1, you want import template but its warehouse in another poistion
556-
/// This will fix it to current
557-
/// </summary>
558-
/// <param name="vill"></param>
559-
/// <param name="acc"></param>
560-
public static void FixPositionBuilding(Village vill, Account acc)
561-
{
562-
}
553+
public static void RemoveCompletedTasks(Village vill) =>
554+
vill.Build.Tasks.RemoveAll(task => IsTaskCompleted(vill, task));
563555

556+
564557
#region Functions for auto-building resource fields
565558

566559
public static Building FindLowestLevelBuilding(List<Building> buildings)
567560
{
568-
// TODO: test after implementation
569-
//return buildings
570-
// .OrderBy(x => x.Level + (x.UnderConstruction ? 1 : 0))
571-
// .FirstOrDefault();
572-
573-
if (buildings.Count == 0) return null;
574-
int lowestLvl = 100;
575-
Building lowestBuilding = new Building();
576-
for (int i = 0; i < buildings.Count; i++)
577-
{
578-
var buildingLevel = buildings[i].Level;
579-
if (buildings[i].UnderConstruction) buildingLevel++;
580-
if (lowestLvl > buildingLevel)
581-
{
582-
lowestLvl = buildingLevel;
583-
lowestBuilding = buildings[i];
584-
}
585-
}
586-
return lowestBuilding;
561+
return buildings
562+
.OrderBy(x => x.Level + (x.UnderConstruction ? 1 : 0))
563+
.FirstOrDefault();
587564
}
588565

589-
private static Building GetLowestProduction(List<Building> buildings, Village vill)
566+
public static Building GetLowestProduction(List<Building> buildings, Village vill)
590567
{
591-
//get distinct field types
568+
// Get distinct field types
592569
var distinct = buildings.Select(x => x.Type).Distinct().ToList();
593-
long lowestProd = long.MaxValue;
594-
BuildingEnum toUpgrade = BuildingEnum.Cropland;
595570

596-
foreach (var distinctType in distinct)
571+
var prodArr = vill.Res.Production.ToArray();
572+
var dict = new Dictionary<BuildingEnum, long>();
573+
for(int i=0; i<4; i++)
597574
{
598-
if (distinctType == BuildingEnum.Woodcutter && vill.Res.Production.Wood < lowestProd) { lowestProd = vill.Res.Production.Wood; toUpgrade = BuildingEnum.Woodcutter; }
599-
if (distinctType == BuildingEnum.ClayPit && vill.Res.Production.Clay < lowestProd) { lowestProd = vill.Res.Production.Clay; toUpgrade = BuildingEnum.ClayPit; }
600-
if (distinctType == BuildingEnum.IronMine && vill.Res.Production.Iron < lowestProd) { lowestProd = vill.Res.Production.Iron; toUpgrade = BuildingEnum.IronMine; }
601-
if (distinctType == BuildingEnum.Cropland && vill.Res.Production.Crop < lowestProd) { lowestProd = vill.Res.Production.Crop; toUpgrade = BuildingEnum.Cropland; }
575+
var resField = (BuildingEnum)i + 1;
576+
if (!distinct.Any(x => x == resField)) continue;
577+
dict.Add(resField, prodArr[i]);
602578
}
579+
580+
var toUpgrade = dict.First(x => x.Value == dict.Min(y => y.Value)).Key;
603581
return FindLowestLevelBuilding(buildings.Where(x => x.Type == toUpgrade).ToList());
604582
}
605583

606-
private static Building GetLowestRes(Account acc, Village vill, List<Building> buildings)
584+
public static Building GetLowestRes(Account acc, Village vill, List<Building> buildings)
607585
{
608586
//get distinct field types
609587
var distinct = buildings.Select(x => x.Type).Distinct().ToList();

TbsCore/Helpers/Classificator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ public enum DifficultyEnum
508508

509509
public enum BuildingCategoryEnum
510510
{
511-
Infrastructure = 1,
512-
Military = 2,
513-
Resources = 3
511+
Infrastructure = 0,
512+
Military = 1,
513+
Resources = 2,
514514
}
515515

516516
public enum BuildingType

TbsCore/Helpers/IoHelperCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void AddBuildTasksFromFile(Account acc, Village vill, string locat
6262
{
6363
BuildingHelper.AddBuildingTask(acc, vill, task);
6464
}
65-
BuildingHelper.RemoveCompletedTasks(vill, acc);
65+
BuildingHelper.RemoveCompletedTasks(vill);
6666
}
6767

6868
private static List<BuildingTask> DecodeTrbc(TbRoot root)

TbsCore/Helpers/NavigationHelper.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using TbsCore.Models.MapModels;
66
using TbsCore.Models.VillageModels;
77
using TbsCore.Parsers;
8+
using TbsCore.TravianData;
89
using static TbsCore.Helpers.Classificator;
910

1011
namespace TbsCore.Helpers
@@ -78,7 +79,14 @@ function clickFirst(node)
7879
}
7980
await DriverHelper.WaitLoaded(acc);
8081
}
81-
82+
83+
internal static async Task ToConstructionTab(Account acc, BuildingEnum building)
84+
{
85+
BuildingCategoryEnum tab = BuildingsData.GetBuildingsCategory(building);
86+
if ((int)tab == 0) return;
87+
await DriverHelper.ClickByClassName(acc, "tabItem", (int)tab);
88+
}
89+
8290

8391
/// <summary>
8492
/// TTWars convert tab into url query

TbsCore/Helpers/UpgradeBuildingHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static (BuildingTask, DateTime) NextBuildingTask(Account acc, Village vil
5050
task = vill.Build.Tasks.First();
5151

5252
//If this task is already complete, remove it and repeat the finding process
53-
if (BuildingHelper.IsTaskCompleted(vill, acc, task))
53+
if (BuildingHelper.IsTaskCompleted(vill, task))
5454
{
5555
vill.Build.Tasks.Remove(task); //task has been completed
5656
return NextBuildingTask(acc, vill);

TbsCore/Models/ResourceModels/Resources.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ public Resources(long[] res)
1010
Iron = res[2];
1111
Crop = res[3];
1212
}
13+
public Resources(long wood, long clay, long iron, long crop)
14+
{
15+
Wood = wood;
16+
Clay = clay;
17+
Iron = iron;
18+
Crop = crop;
19+
}
1320

1421
public long Wood { get; set; }
1522
public long Clay { get; set; }

TbsCore/Tasks/LowLevel/UpgradeBuilding.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ await TTWarsTryFastUpgrade(acc, $"{acc.AccInfo.ServerUrl}/build.php?id={urlId}")
6060
}
6161

6262
await NavigationHelper.EnterBuilding(acc, Vill, (int)Task.BuildingId);
63+
await NavigationHelper.ToConstructionTab(acc, Task.Building);
6364

6465
var constructContract = acc.Wb.Html.GetElementbyId($"contract_building{(int)Task.Building}");
6566
var upgradeContract = acc.Wb.Html.GetElementbyId("build");
@@ -161,7 +162,7 @@ private async Task<TaskRes> Upgrade(Account acc, HtmlNode node)
161162
{
162163
acc.Logger.Warning($"{this.Task.Building} is on level {lvl}, on/above desired {Task.Level}. Removing it from queue.");
163164
RemoveCurrentTask();
164-
RemoveCompletedTasks(this.Vill, acc);
165+
RemoveCompletedTasks(this.Vill);
165166
return TaskRes.Executed;
166167
}
167168

@@ -196,7 +197,7 @@ private async Task<TaskRes> Upgrade(Account acc, HtmlNode node)
196197

197198
var buildDuration = InfrastructureParser.GetBuildDuration(container, acc.AccInfo.ServerVersion);
198199

199-
if (IsTaskCompleted(Vill, acc, this.Task))
200+
if (IsTaskCompleted(Vill, this.Task))
200201
{
201202
acc.Logger.Warning($"Building {this.Task.Building} in village {this.Vill.Name} is already on desired level. Will be removed from the queue.");
202203
RemoveCurrentTask();

TravBotSharp/Views/GeneralUc.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private void button16_Click(object sender, EventArgs e) //all villages farm task
112112
foreach (var vill in acc.Villages)
113113
{
114114
DefaultConfigurations.FarmVillagePlan(acc, vill);
115-
BuildingHelper.RemoveCompletedTasks(vill, acc);
115+
BuildingHelper.RemoveCompletedTasks(vill);
116116
}
117117
}
118118

@@ -122,7 +122,7 @@ private void button17_Click(object sender, EventArgs e) //all villages support t
122122
foreach (var vill in acc.Villages)
123123
{
124124
DefaultConfigurations.SupplyVillagePlan(acc, vill);
125-
BuildingHelper.RemoveCompletedTasks(vill, acc);
125+
BuildingHelper.RemoveCompletedTasks(vill);
126126
}
127127
}
128128

@@ -132,7 +132,7 @@ private void button14_Click(object sender, EventArgs e) //all villages deff task
132132
foreach (var vill in acc.Villages)
133133
{
134134
DefaultConfigurations.DeffVillagePlan(acc, vill);
135-
BuildingHelper.RemoveCompletedTasks(vill, acc);
135+
BuildingHelper.RemoveCompletedTasks(vill);
136136
}
137137
}
138138

@@ -142,7 +142,7 @@ private void button4_Click(object sender, EventArgs e) //all villages off tasks
142142
foreach (var vill in acc.Villages)
143143
{
144144
DefaultConfigurations.OffVillagePlan(acc, vill);
145-
BuildingHelper.RemoveCompletedTasks(vill, acc);
145+
BuildingHelper.RemoveCompletedTasks(vill);
146146
}
147147
}
148148

TravBotSharp/Views/VillageViews/BuildUc.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private void button20_Click(object sender, EventArgs e) //support vill button
347347
var acc = GetSelectedAcc();
348348
var vill = GetSelectedVillage();
349349
DefaultConfigurations.SupplyVillagePlan(acc, vill);
350-
BuildingHelper.RemoveCompletedTasks(vill, acc);
350+
BuildingHelper.RemoveCompletedTasks(vill);
351351
UpdateUc();
352352
}
353353

@@ -356,7 +356,7 @@ private void button19_Click(object sender, EventArgs e) //off vill button
356356
var acc = GetSelectedAcc();
357357
var vill = GetSelectedVillage();
358358
DefaultConfigurations.OffVillagePlan(acc, vill);
359-
BuildingHelper.RemoveCompletedTasks(vill, acc);
359+
BuildingHelper.RemoveCompletedTasks(vill);
360360
UpdateUc();
361361
}
362362

@@ -365,7 +365,7 @@ private void button6_Click(object sender, EventArgs e) //deff vill button
365365
var acc = GetSelectedAcc();
366366
var vill = GetSelectedVillage();
367367
DefaultConfigurations.DeffVillagePlan(acc, vill);
368-
BuildingHelper.RemoveCompletedTasks(vill, acc);
368+
BuildingHelper.RemoveCompletedTasks(vill);
369369
UpdateUc();
370370
}
371371

0 commit comments

Comments
 (0)