Skip to content

Commit 23decc1

Browse files
committed
Added some options for TTWars
1 parent fcbb93f commit 23decc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+778
-309
lines changed

TbsCore/Helpers/HeroHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
using System.Threading.Tasks;
55
using TbsCore.Helpers;
66
using TbsCore.Models.AccModels;
7+
using TbsCore.Models.MapModels;
78
using TbsCore.Models.ResourceModels;
89
using TbsCore.Models.VillageModels;
910
using TbsCore.Parsers;
1011
using TbsCore.Tasks.LowLevel;
12+
using TbsCore.TravianData;
1113
using static TbsCore.Helpers.Classificator;
1214

1315
namespace TbsCore.Helpers
@@ -25,7 +27,7 @@ public static bool AdventureInRange(Account acc)
2527
if (heroHome == null) return false;
2628

2729
return acc.Hero.Adventures.Any(x =>
28-
MapHelper.CalculateDistance(acc, x.Coordinates, heroHome.Coordinates) <= acc.Hero.Settings.MaxDistance
30+
heroHome.Coordinates.CalculateDistance(acc, x.Coordinates) <= acc.Hero.Settings.MaxDistance
2931
);
3032
}
3133

@@ -184,7 +186,7 @@ public static void UpdateHeroVillage(Account acc)
184186
return;
185187
case ServerVersionEnum.T4_5:
186188
// Convert from coordinates id -> coordinates -> villageId
187-
var coordinates = MapHelper.CoordinatesFromKid(hrefId ?? 0, acc);
189+
var coordinates = new Coordinates(acc, hrefId ?? 0);
188190
var vill = acc.Villages.FirstOrDefault(x => x.Coordinates.Equals(coordinates));
189191
if (vill == null) return;
190192
acc.Hero.HomeVillageId = vill.Id;

TbsCore/Helpers/MapHelper.cs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using TbsCore.Models.VillageModels;
1212
using TbsCore.TravianData;
1313
using TbsCore.Parsers;
14+
using TbsCore.Models.TroopsModels;
1415

1516
namespace TbsCore.Helpers
1617
{
@@ -22,46 +23,6 @@ public static List<object> jsonToVillTypes(string json)
2223
//from cmd=mapPositionData json get villtypenum's and corresponding coordinates
2324
}
2425

25-
//Used in cmd=mapPositionData, gets the map JSON where
26-
27-
public static int KidFromCoordinates(Coordinates coords, Account acc)
28-
{
29-
return 1 + ((acc.AccInfo.MapSize - coords.y) * (acc.AccInfo.MapSize * 2 + 1)) + acc.AccInfo.MapSize + coords.x;
30-
}
31-
32-
public static Coordinates CoordinatesFromKid(int? kid, Account acc)
33-
{
34-
if (kid == null) return null;
35-
return CoordinatesFromKid(kid ?? 0, acc);
36-
}
37-
38-
public static Coordinates CoordinatesFromKid(int kid, Account acc)
39-
{
40-
var size = acc.AccInfo.MapSize;
41-
kid--;
42-
var y = size - (kid / (size * 2 + 1));
43-
var x = (kid % (size * 2 + 1)) - size;
44-
return new Coordinates()
45-
{
46-
x = x,
47-
y = y
48-
};
49-
}
50-
51-
/// <summary>
52-
/// Calculate distance between two coordinates. This function takes into the account the map size.
53-
/// </summary>
54-
public static float CalculateDistance(Account acc, Coordinates coord1, Coordinates coord2)
55-
{
56-
var size = acc.AccInfo.MapSize;
57-
var xDiff = Math.Abs(coord1.x - coord2.x);
58-
var yDiff = Math.Abs(coord1.y - coord2.y);
59-
if (xDiff > size) xDiff = 2 * size - xDiff;
60-
if (yDiff > size) yDiff = 2 * size - yDiff;
61-
var distance = Math.Sqrt(xDiff * xDiff + yDiff * yDiff); //Pitagoras theorem
62-
return (float)distance;
63-
}
64-
6526
/// <summary>
6627
/// Send raw HTTP request to the server and request the map tiles around the coords. This mimics browser on the map page.
6728
/// </summary>
@@ -178,7 +139,7 @@ public static List<MapTile> GetMapTiles(Account acc, Coordinates coords)
178139
/// <summary>
179140
/// Sends HTTP request to the server and gets number of animals inside the oasis
180141
/// </summary>
181-
public static int[] GetOasisAnimals(Account acc, Coordinates oasis)
142+
public static TroopsBase GetOasisAnimals(Account acc, Coordinates oasis)
182143
{
183144
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
184145
string html = "";

TbsCore/Helpers/MarketHelper.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ public static async Task<TimeSpan> MarketSendResource(Account acc, Resources res
108108
/// <returns>Time it will take for transit to complete</returns>
109109
public static async Task<TimeSpan> MarketSendResource(Account acc, long[] resources, Village targetVillage, BotTask botTask)
110110
{
111-
var times = 1;
112-
if (acc.AccInfo.GoldClub ?? false) times = 3;
113-
else if (acc.AccInfo.PlusAccount) times = 2;
111+
var times = MarketParser.MaxMerchantTimes(acc.Wb.Html);
114112

115113
// No resources to send
116114
if (resources.Sum() == 0) return TimeSpan.Zero;
@@ -168,15 +166,25 @@ public static async Task<TimeSpan> MarketSendResource(Account acc, long[] resour
168166
acc.Wb.ExecuteScript($"document.getElementById('x2').value='{times}'");
169167
await Task.Delay(AccountHelper.Delay(acc) / 5);
170168
}
171-
await DriverHelper.ClickById(acc, "enabledButton");
172169

173-
var durNode = acc.Wb.Html.GetElementbyId("target_validate");
170+
HtmlNode durNode;
171+
int cnt = 0;
172+
do
173+
{
174+
await DriverHelper.ClickById(acc, "enabledButton");
175+
await Task.Delay(300);
176+
durNode = acc.Wb.Html.GetElementbyId("target_validate");
177+
if (10 < cnt++) throw new Exception("Send resources failed!");
178+
}
179+
while (durNode == null);
174180

175181
if (durNode == null && acc.Wb.Html.GetElementbyId("prepareError") != null)
176182
{
177183
// Error "Abuse! You have not enough resources." is displayed.
178184
}
179185
//get duration of transit
186+
// Class destination when ok.
187+
180188
var dur = durNode.Descendants("td").ToList()[3].InnerText.Replace("\t", "").Replace("\n", "");
181189

182190
// Will NOT trigger a page reload! Thus we should await some time before continuing.
@@ -211,7 +219,7 @@ private static TimeSpan CalculateTransitTime(Account acc, Village vill1, Village
211219
var mainVill = AccountHelper.GetMainVillage(acc);
212220
if (mainVill == null) acc.Villages.First();
213221

214-
var distance = MapHelper.CalculateDistance(acc, vill1.Coordinates, vill2.Coordinates);
222+
var distance = vill1.Coordinates.CalculateDistance(acc, vill2.Coordinates);
215223
//Speed is per hour
216224
var speed = GetMerchantsSpeed(acc.AccInfo.Tribe ?? Classificator.TribeEnum.Any);
217225
speed *= acc.AccInfo.ServerSpeed;
@@ -277,7 +285,7 @@ public static long[] SendResCapToStorage(Account acc, Resources resources)
277285
/// </summary>
278286
/// <param name="vill">Village</param>
279287
/// <returns>Resources to be sent</returns>
280-
public static long[] GetResToMainVillage(Village vill)
288+
public static Resources GetResToMainVillage(Village vill)
281289
{
282290
var ret = new long[4];
283291
var res = vill.Res.Stored.Resources.ToArray();
@@ -294,7 +302,7 @@ public static long[] GetResToMainVillage(Village vill)
294302
ret[i] = res[i] - limit[i];
295303
if (ret[i] < 0) ret[i] = 0;
296304
}
297-
return ret;
305+
return new Resources(ret);
298306
}
299307

300308
/// <summary>
@@ -347,7 +355,7 @@ public static void ReStartSendingToMain(Account acc, Village vill)
347355
{
348356
acc.Tasks.Remove(typeof(SendResToMain), vill);
349357

350-
if (vill.Settings.Type == VillType.Support && vill.Settings.SendRes)
358+
if (vill.Settings.Type == VillType.Support && vill.Settings.SendRes && AccountHelper.GetMainVillage(acc) != vill)
351359
{
352360
acc.Tasks.Add(new SendResToMain()
353361
{

TbsCore/Models/AttackModels/AttackReport.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using TbsCore.Helpers;
5+
using TbsCore.Models.MapModels;
56
using TbsCore.Models.ResourceModels;
7+
using TbsCore.Models.TroopsModels;
68

79
namespace TbsCore.Models.AttackModels
810
{
911
/// <summary>
10-
/// Report of an attack
12+
/// Report of an attack TODO: combine with CombatModels, DRY
1113
/// </summary>
1214
public class AttackReport
1315
{
14-
// TODO: Attacker (nickname, id), Attacker Village (id, coords, name), Deffender (nickname, id), Deffending village (id, coords, name)
15-
// Attacker troops, killed, tribe, additional information (bounty / scouting report / ram&cata report)
16-
// Deffender List<troops, tribe, killed>
16+
public AttackReport(bool init = false)
17+
{
18+
if (init)
19+
{
20+
Deffender = new CombatParticipant();
21+
Attacker = new CombatParticipant();
22+
AttackerArmy = new CombatTribeParticipant();
23+
DeffenderArmy = new List<CombatTribeParticipant>();
24+
}
25+
}
26+
public CombatParticipant Deffender { get; set; }
27+
public CombatParticipant Attacker { get; set; }
28+
29+
public CombatTribeParticipant AttackerArmy { get; set; }
30+
public List<CombatTribeParticipant> DeffenderArmy { get; set; }
1731

1832
/// <summary>
1933
/// Resources raided / scouted
@@ -46,4 +60,16 @@ public Resources GetRaidableRes()
4660
return ResourcesHelper.ArrayToResources(res);
4761
}
4862
}
63+
64+
public class CombatParticipant
65+
{
66+
public string Username { get; set; }
67+
public int UserId { get; set; }
68+
public string VillageName { get; set; }
69+
public int VillageId { get; set; }
70+
}
71+
public class CombatTribeParticipant : TroopsBase
72+
{
73+
public int[] Casualties { get; set; }
74+
}
4975
}

TbsCore/Models/CombatModels/Combat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4-
using TravBotSharp.Files.TravianData;
5-
using static TravBotSharp.Files.Helpers.Classificator;
4+
using TbsCore.TravianData;
5+
using static TbsCore.Helpers.Classificator;
66

77
namespace TbsCore.Models.CombatModels
88
{

TbsCore/Models/CombatModels/CombatArmy.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Text;
55
using TbsCore.TravianData;
6-
using TravBotSharp.Files.Helpers;
76

87
namespace TbsCore.Models.CombatModels
98
{

TbsCore/Models/CombatModels/CombatArmyBase.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using TbsCore.Helpers;
6+
using TbsCore.Models.TroopsModels;
57
using TbsCore.TravianData;
6-
using TravBotSharp.Files.Helpers;
78

89
namespace TbsCore.Models.CombatModels
910
{
10-
public class CombatArmyBase
11+
public class CombatArmyBase : TroopsBase
1112
{
12-
public Classificator.TribeEnum Tribe { get; set; }
13-
public int[] Troops { get; set; }
1413
public int[] Improvements { get; set; }
1514
public CombatHero Hero { get; set; }
1615

TbsCore/Models/CombatModels/CombatDeffender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4-
using TravBotSharp.Files.Helpers;
4+
using TbsCore.Helpers;
55

66
namespace TbsCore.Models.CombatModels
77
{

TbsCore/Models/CombatModels/CombatHero.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Collections.Generic;
2+
using TbsCore.Helpers;
23
using TbsCore.Models.AccModels;
34
using TbsCore.Models.TroopsModels;
4-
using TravBotSharp.Files.Helpers;
5-
using static TravBotSharp.Files.Helpers.Classificator;
5+
using static TbsCore.Helpers.Classificator;
66

77
namespace TbsCore.Models.CombatModels
88
{

TbsCore/Models/MapModels/Coordinates.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.ComponentModel;
33
using System.Globalization;
4+
using TbsCore.Models.AccModels;
45

56
namespace TbsCore.Models.MapModels
67
{
@@ -16,6 +17,14 @@ public Coordinates(int x, int y)
1617
this.y = y;
1718
}
1819

20+
public Coordinates(Account acc, int kid)
21+
{
22+
var size = acc.AccInfo.MapSize;
23+
kid--;
24+
this.y = size - (kid / (size * 2 + 1));
25+
this.x = (kid % (size * 2 + 1)) - size;
26+
}
27+
1928
public int x { get; set; }
2029
public int y { get; set; }
2130

@@ -25,6 +34,26 @@ public bool Equals(Coordinates other)
2534
return other.x == x && other.y == y;
2635
}
2736

37+
// Used in cmd=mapPositionData, gets the map JSON where
38+
public int GetKid(Account acc)
39+
{
40+
return 1 + ((acc.AccInfo.MapSize - this.y) * (acc.AccInfo.MapSize* 2 + 1)) + acc.AccInfo.MapSize + this.x;
41+
}
42+
43+
/// <summary>
44+
/// Calculate distance between two coordinates. This function takes into the account the map size.
45+
/// </summary>
46+
public float CalculateDistance(Account acc, Coordinates coords)
47+
{
48+
var size = acc.AccInfo.MapSize;
49+
var xDiff = Math.Abs(this.x - coords.x);
50+
var yDiff = Math.Abs(this.y - coords.y);
51+
if (xDiff > size) xDiff = 2 * size - xDiff;
52+
if (yDiff > size) yDiff = 2 * size - yDiff;
53+
var distance = Math.Sqrt(xDiff * xDiff + yDiff * yDiff); //Pitagoras theorem
54+
return (float)distance;
55+
}
56+
2857
public override string ToString() => $"({x}|{y})";
2958
}
3059
}

0 commit comments

Comments
 (0)