Skip to content

Commit 2773a11

Browse files
authored
Merge pull request #84 from Erol444/develop
New version out
2 parents 811a7f8 + 1c11682 commit 2773a11

23 files changed

+143
-129
lines changed

.github/workflows/pre-release.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ jobs:
9999
path: TravBotSharp/bin/Release
100100

101101
- name: Archive pre release
102-
uses: papeloto/action-zip@v1
102+
uses: montudor/action-zip@v1
103103
with:
104-
files: TravBotSharp/bin/Release
105-
dest: TBS-${{needs.version.outputs.version}}.zip
106-
104+
args: zip -qq -r TBS-${{needs.version.outputs.version}}.zip TravBotSharp/bin/Release
105+
107106
- name: Create pre release
108107
uses: ncipollo/release-action@v1
109108
with:

.github/workflows/release.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ jobs:
100100
with:
101101
name: TBS-release
102102
path: TravBotSharp/bin/Release
103-
103+
104104
- name: Archive Release
105-
uses: papeloto/action-zip@v1
105+
uses: montudor/action-zip@v1
106106
with:
107-
files: TravBotSharp/bin/Release
108-
dest: TBS-${{needs.version.outputs.version}}.zip
109-
107+
args: zip -qq -r TBS-${{needs.version.outputs.version}}.zip TravBotSharp/bin/Release
108+
109+
110110
- name: Create release
111111
uses: ncipollo/release-action@v1
112112
with:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<a href="https://discord.gg/mBa4f2K"><img src="https://discordapp.com/api/guilds/740829801446637601/widget.png?style=banner2" width="30%"></a>
44

55
### About
6-
I have worked on this bot for over 2 years, ~15k LoC. It has been since completely ported to the newest version of Travian T4.5. Bot is under active development (at least until summer 2021). Instructions are on the Discord channel. If you have any question/recommendation, feel free to contact me (on Discord).
6+
I have worked on this bot for over 3 years, ~15k LoC. It has been since completely ported to the newest version of Travian T4.5. Bot is under active development. Instructions are on the Discord channel. If you have any question/recommendation, feel free to contact me (on Discord).
77

88
## Features
99
#### - Auto-Change proxy

TbsCore/Core/TaskExecutor.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,8 @@ private static void UpdateCurrentlyBuilding(Account acc, Village vill)
232232
var cb = InfrastructureParser.CurrentlyBuilding(acc.Wb.Html, acc);
233233
if (cb == null) return; // Nothing is currently building
234234

235-
var bldJson = DriverHelper.GetJsObj<string>(acc, "JSON.stringify(bld);");
236-
if (string.IsNullOrEmpty(bldJson)) return;
237-
var bldJs = JsonConvert.DeserializeObject<List<Bld>>(bldJson);
238-
239-
// Combine data from two sources about currently building (JS object and HTML table)
240-
// We get time duration and level from HTML
241-
// and build location, level and building (type) from JSON
242235
for (int i = 0; i < cb.Count; i++)
243236
{
244-
cb[i].Building = bldJs[i].Building;
245-
cb[i].Location = bldJs[i].Location;
246-
cb[i].Level = (byte)bldJs[i].Level;
247-
248237
vill.Build.CurrentlyBuilding.Add(cb[i]);
249238
}
250239
}

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: 47 additions & 65 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>
@@ -243,18 +245,18 @@ public static bool RemoveFinishedCB(Village vill)
243245

244246
foreach (var taskDone in tasksDone)
245247
{
246-
var building = vill.Build.Buildings.FirstOrDefault(x => x.Id == taskDone.Location);
247-
if (building == null) building = vill.Build.Buildings.FirstOrDefault(x => x.Type == taskDone.Building);
248-
if (building == null || building.Type != taskDone.Building) continue;
248+
//var building = vill.Build.Buildings.FirstOrDefault(x => x.Id == taskDone.Location);
249+
//if (building == null) building = vill.Build.Buildings.FirstOrDefault(x => x.Type == taskDone.Building);
250+
//if (building == null || building.Type != taskDone.Building) continue;
249251

250-
if (building.Level < taskDone.Level) building.Level = taskDone.Level;
252+
//if (building.Level < taskDone.Level) building.Level = taskDone.Level;
251253
vill.Build.CurrentlyBuilding.Remove(taskDone);
252254
}
253255
return true;
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);
@@ -294,11 +296,11 @@ public static bool IsTaskCompleted(Village vill, Account acc, BuildingTask task)
294296
if (task.Level <= building.Level) return true;
295297

296298
// If the building is being upgraded to the desired level, task is complete
297-
var cb = vill.Build
298-
.CurrentlyBuilding
299-
.OrderByDescending(x => x.Level)
300-
.FirstOrDefault(x => x.Location == task.BuildingId);
301-
if (cb != null && task.Level <= cb.Level) return true;
299+
//var cb = vill.Build
300+
// .CurrentlyBuilding
301+
// .OrderByDescending(x => x.Level)
302+
// .FirstOrDefault(x => x.Location == task.BuildingId);
303+
//if (cb != null && task.Level <= cb.Level) return true;
302304
break;
303305

304306
case BuildingType.AutoUpgradeResFields:
@@ -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
}
@@ -440,10 +442,14 @@ internal static bool UpgradeBuildingForOneLvl(Account acc, Village vill, Buildin
440442
}, bottom);
441443
}
442444

445+
// Current lvl in bot's data
443446
var currentLvl = (int)upgrade.Level;
444447

445448
RemoveFinishedCB(vill);
446-
currentLvl += vill.Build.CurrentlyBuilding.Count(x => x.Building == building);
449+
450+
// Current lvl in current building list
451+
var currentBuilding = vill.Build.CurrentlyBuilding.FirstOrDefault(x => x.Building == building);
452+
if (currentBuilding != null) currentLvl = currentBuilding.Level;
447453

448454
if (BuildingsData.MaxBuildingLevel(acc, building) == currentLvl)
449455
{
@@ -544,62 +550,38 @@ public static bool IsWall(BuildingEnum building)
544550
/// </summary>
545551
/// <param name="vill">Village</param>
546552
/// <param name="acc">Account</param>
547-
public static void RemoveCompletedTasks(Village vill, Account acc) =>
548-
vill.Build.Tasks.RemoveAll(task => IsTaskCompleted(vill, acc, task));
549-
550-
/// <summary>
551-
/// When you already build one warehouse to lv 1, you want import template but its warehouse in another poistion
552-
/// This will fix it to current
553-
/// </summary>
554-
/// <param name="vill"></param>
555-
/// <param name="acc"></param>
556-
public static void FixPositionBuilding(Village vill, Account acc)
557-
{
558-
}
553+
public static void RemoveCompletedTasks(Village vill) =>
554+
vill.Build.Tasks.RemoveAll(task => IsTaskCompleted(vill, task));
559555

556+
560557
#region Functions for auto-building resource fields
561558

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

585-
private static Building GetLowestProduction(List<Building> buildings, Village vill)
566+
public static Building GetLowestProduction(List<Building> buildings, Village vill)
586567
{
587-
//get distinct field types
568+
// Get distinct field types
588569
var distinct = buildings.Select(x => x.Type).Distinct().ToList();
589-
long lowestProd = long.MaxValue;
590-
BuildingEnum toUpgrade = BuildingEnum.Cropland;
591570

592-
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++)
593574
{
594-
if (distinctType == BuildingEnum.Woodcutter && vill.Res.Production.Wood < lowestProd) { lowestProd = vill.Res.Production.Wood; toUpgrade = BuildingEnum.Woodcutter; }
595-
if (distinctType == BuildingEnum.ClayPit && vill.Res.Production.Clay < lowestProd) { lowestProd = vill.Res.Production.Clay; toUpgrade = BuildingEnum.ClayPit; }
596-
if (distinctType == BuildingEnum.IronMine && vill.Res.Production.Iron < lowestProd) { lowestProd = vill.Res.Production.Iron; toUpgrade = BuildingEnum.IronMine; }
597-
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]);
598578
}
579+
580+
var toUpgrade = dict.First(x => x.Value == dict.Min(y => y.Value)).Key;
599581
return FindLowestLevelBuilding(buildings.Where(x => x.Type == toUpgrade).ToList());
600582
}
601583

602-
private static Building GetLowestRes(Account acc, Village vill, List<Building> buildings)
584+
public static Building GetLowestRes(Account acc, Village vill, List<Building> buildings)
603585
{
604586
//get distinct field types
605587
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: 33 additions & 3 deletions
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
@@ -51,12 +52,41 @@ private static async Task ToBuildingId(Account acc, int index)
5152
{
5253
if (!acc.Wb.CurrentUrl.Contains("dorf2.php") || acc.Wb.CurrentUrl.Contains("id="))
5354
await MainNavigate(acc, MainNavigationButton.Buildings);
54-
55-
await DriverHelper.ExecuteScript(acc, $"document.getElementsByClassName(\"aid{index}\")[0].children[0].click();");
55+
56+
string script = @"
57+
function clickFirst(node)
58+
{
59+
if (node.hasAttribute('href') && node.getAttribute('href'))
60+
{
61+
node.click();
62+
return true;
63+
}
64+
if (node.hasAttribute('onclick') && node.getAttribute('onclick'))
65+
{";
66+
script += "url = node.getAttribute('onclick').split(\"'\")[1];";
67+
script += @"
68+
window.location.href = url
69+
return true;
70+
}
71+
// node doesn't contain href/onlick. Check child nodes
72+
for (child of node.children)
73+
{
74+
if (clickFirst(child)) return true;
75+
}
76+
}";
77+
script += $"node = document.querySelectorAll('[data-aid=\"{index}\"]')[0]; clickFirst(node);";
78+
await DriverHelper.ExecuteScript(acc, script);
5679
}
5780
await DriverHelper.WaitLoaded(acc);
5881
}
59-
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+
6090

6191
/// <summary>
6292
/// 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);

0 commit comments

Comments
 (0)