Skip to content

Commit 96451b2

Browse files
authored
Merge pull request #66 from Erol444/scout_villages
Added a bunch of things/fixes
2 parents 49d9361 + 23bfa10 commit 96451b2

File tree

76 files changed

+2696
-541
lines changed

Some content is hidden

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

76 files changed

+2696
-541
lines changed

TbsCore/Database/DbRepository.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,4 @@ private static DbAccount ConvertAcc(Account acc) =>
8484
JsonData = JsonConvert.SerializeObject(acc)
8585
};
8686
}
87-
88-
//public class RawAcc
89-
//{
90-
// public string Username { get; set; }
91-
// public string Server { get; set; }
92-
//}
9387
}

TbsCore/Helpers/AttackHelper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using TbsCore.Parsers;
5+
6+
namespace TbsCore.Helpers
7+
{
8+
public static class AttackHelper
9+
{
10+
public static Classificator.ReportType GetReportType(string iReport)
11+
{
12+
int num = (int)Parser.RemoveNonNumeric(iReport);
13+
return (Classificator.ReportType)num;
14+
}
15+
}
16+
}

TbsCore/Helpers/Classificator.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,5 +518,32 @@ public enum BuildingType
518518
General,
519519
AutoUpgradeResFields
520520
}
521+
522+
public enum ReportType
523+
{
524+
TruceReport, // iReport0
525+
AttackNoLosses, // iReport1 - Won as attacker without losses, Green sword
526+
AttackSomeLosses, // iReport2 - Won as attacker with losses
527+
AttackAllLosses, // iReport3 - Lost as attacker with losses
528+
DeffendNoLosses, // iReport4
529+
DeffendSomeLosses, // iReport5
530+
DeffendAllLosses, // iReport6
531+
DeffendNoDeff, // iReport7
532+
Reinforcement, // iReport8 - Reinforcement
533+
Unknown9, // 9
534+
Unknown10, // 9
535+
WoodDelivery, // iReport11 - Wood Delivery
536+
ClayDelivery, // iReport12 - Clay Delivery
537+
IronDelivery, // iReport13 - Iron Delivery
538+
CropDelivery, // iReport14 - Crop Delivery
539+
ScoutNoLosses, // iReport15 - Won scouting as attacker
540+
ScoutSomeLosses, // iReport16 - Won scouting as attacker but defender found out
541+
ScoutAllLosses, // iReport17 - Lost scouting as attacker
542+
ScoutingDeffended, // iReport18
543+
ScoutingPartlyDeffended, // iReport19
544+
AnimalsCaught, // iReport20
545+
AdventureReport, // iReport21
546+
NewVillage, // iReport22 - Settlers founded a new village
547+
}
521548
}
522549
}

TbsCore/Helpers/HeroHelper.cs

Lines changed: 68 additions & 28 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

@@ -43,10 +45,10 @@ public static Village GetHeroHomeVillage(Account acc) =>
4345
/// <param name="acc">Account</param>
4446
public static void AutoEquipHero(Account acc)
4547
{
46-
foreach (Classificator.HeroItemCategory category
47-
in (Classificator.HeroItemCategory[])Enum.GetValues(typeof(Classificator.HeroItemCategory)))
48+
foreach (HeroItemCategory category
49+
in (HeroItemCategory[])Enum.GetValues(typeof(HeroItemCategory)))
4850
{
49-
if (category == Classificator.HeroItemCategory.Others) continue; // Don't equip into hero bag
51+
if (category == HeroItemCategory.Others) continue; // Don't equip into hero bag
5052
int currentTier = 0;
5153
if (acc.Hero.Equipt.TryGetValue(category, out var item))
5254
{
@@ -66,7 +68,7 @@ public static void AutoEquipHero(Account acc)
6668
acc.Tasks.Add(new HeroEquip()
6769
{
6870
ExecuteAt = DateTime.Now,
69-
Items = new List<(Classificator.HeroItemEnum, int)>()
71+
Items = new List<(HeroItemEnum, int)>()
7072
{
7173
(equipWith.Item, 0)
7274
}
@@ -80,35 +82,74 @@ public static void AutoEquipHero(Account acc)
8082
/// </summary>
8183
/// <param name="item">Hero item enum</param>
8284
/// <returns>Hero item (category, name, tier)</returns>
83-
public static (Classificator.HeroItemCategory, string, int) ParseHeroItem(Classificator.HeroItemEnum item)
85+
public static (HeroItemCategory, string, int) ParseHeroItem(HeroItemEnum item)
8486
{
8587
var attr = item.ToString().Split('_');
8688

87-
Enum.TryParse(attr[0], out Classificator.HeroItemCategory category);
89+
Enum.TryParse(attr[0], out HeroItemCategory category);
8890
string name = attr[1];
8991
int tier = int.Parse(attr[2]);
9092

9193
return (category, name, tier);
9294
}
9395

96+
/// <summary>
97+
/// Parses hero weapon
98+
/// </summary>
99+
/// <param name="item">Hero item</param>
100+
/// <returns>(Troop boost, boost)</returns>
101+
public static (TroopsEnum, int) ParseWeapon(HeroItemEnum item)
102+
{
103+
var (_, name, tier) = ParseHeroItem(item);
104+
if(Enum.TryParse(name, out TroopsEnum troop))
105+
{
106+
return (troop, GetWeaponBoost(troop, tier));
107+
}
108+
return (TroopsEnum.None, 0);
109+
}
110+
111+
public static int GetArmorStrength(string name)
112+
{
113+
switch (name)
114+
{
115+
case "Breastplate": return 500;
116+
case "Segmented": return 250;
117+
default: return 0;
118+
}
119+
}
120+
121+
public static int GetArmorDmgReduce(string name, int tier)
122+
{
123+
switch (name)
124+
{
125+
case "Scale": return 2 + 2 * tier;
126+
case "Segmented": return 2 + tier;
127+
default: return 0;
128+
}
129+
}
130+
131+
private static int GetWeaponBoost(TroopsEnum troop, int tier)
132+
{
133+
var upkeep = TroopsData.GetTroopUpkeep(troop);
134+
return (tier + 2) * upkeep;
135+
}
136+
94137
/// <summary>
95138
/// Gets the tier of the hero item
96139
/// </summary>
97140
/// <param name="item">HeroItem</param>
98141
/// <returns>Tier</returns>
99-
public static int GetHeroItemTier(Classificator.HeroItemEnum item)
142+
public static int GetHeroItemTier(HeroItemEnum item)
100143
{
101144
var (_, _, itemTier) = ParseHeroItem(item);
102145
return itemTier;
103146
}
104-
105-
public static string GetHeroItemName(Classificator.HeroItemEnum item)
147+
public static string GetHeroItemName(HeroItemEnum item)
106148
{
107149
var (_, name, _) = ParseHeroItem(item);
108150
return name;
109151
}
110-
111-
public static Classificator.HeroItemCategory GetHeroItemCategory(Classificator.HeroItemEnum item)
152+
public static HeroItemCategory GetHeroItemCategory(HeroItemEnum item)
112153
{
113154
var (category, _, _) = ParseHeroItem(item);
114155
return category;
@@ -129,7 +170,7 @@ public static void ParseHeroPage(Account acc)
129170

130171
if (acc.Hero.Settings.AutoEquip)
131172
{
132-
HeroHelper.AutoEquipHero(acc);
173+
AutoEquipHero(acc);
133174
}
134175
}
135176

@@ -140,13 +181,12 @@ public static void UpdateHeroVillage(Account acc)
140181

141182
switch (acc.AccInfo.ServerVersion)
142183
{
143-
case Classificator.ServerVersionEnum.T4_4:
184+
case ServerVersionEnum.T4_4:
144185
acc.Hero.HomeVillageId = hrefId ?? 0;
145186
return;
146-
147-
case Classificator.ServerVersionEnum.T4_5:
187+
case ServerVersionEnum.T4_5:
148188
// Convert from coordinates id -> coordinates -> villageId
149-
var coordinates = MapHelper.CoordinatesFromKid(hrefId ?? 0, acc);
189+
var coordinates = new Coordinates(acc, hrefId ?? 0);
150190
var vill = acc.Villages.FirstOrDefault(x => x.Coordinates.Equals(coordinates));
151191
if (vill == null) return;
152192
acc.Hero.HomeVillageId = vill.Id;
@@ -159,10 +199,10 @@ public static long[] GetHeroResources(Account acc)
159199
var heroItems = acc.Hero.Items;
160200
return new long[]
161201
{
162-
heroItems.FirstOrDefault(x => x.Item == Classificator.HeroItemEnum.Others_Wood_0)?.Count ?? 0,
163-
heroItems.FirstOrDefault(x => x.Item == Classificator.HeroItemEnum.Others_Clay_0)?.Count ?? 0,
164-
heroItems.FirstOrDefault(x => x.Item == Classificator.HeroItemEnum.Others_Iron_0)?.Count ?? 0,
165-
heroItems.FirstOrDefault(x => x.Item == Classificator.HeroItemEnum.Others_Crop_0)?.Count ?? 0
202+
heroItems.FirstOrDefault(x => x.Item == HeroItemEnum.Others_Wood_0)?.Count ?? 0,
203+
heroItems.FirstOrDefault(x => x.Item == HeroItemEnum.Others_Clay_0)?.Count ?? 0,
204+
heroItems.FirstOrDefault(x => x.Item == HeroItemEnum.Others_Iron_0)?.Count ?? 0,
205+
heroItems.FirstOrDefault(x => x.Item == HeroItemEnum.Others_Crop_0)?.Count ?? 0
166206
};
167207
}
168208

@@ -172,21 +212,21 @@ public static long[] GetHeroResources(Account acc)
172212
/// <param name="acc">Account</param>
173213
/// <param name="troop">Troop to train</param>
174214
/// <returns>Whether to switch helmets first</returns>
175-
public static bool SwitchHelmet(Account acc, Village trainVill, Classificator.BuildingEnum building, TrainTroops task)
215+
public static bool SwitchHelmet(Account acc, Village trainVill, BuildingEnum building, TrainTroops task)
176216
{
177217
if (!acc.Hero.Settings.AutoSwitchHelmets) return false;
178218

179219
// In T4.5, helmet will only have effect in hero home village
180220
// In TTWars, helmets have acc-wide effect
181221
// TODO: for T4.5, add auto-move hero feature (for helmet effect purposes)
182222
if (GetHeroHomeVillage(acc) != trainVill &&
183-
acc.AccInfo.ServerVersion != Classificator.ServerVersionEnum.T4_4) return false;
223+
acc.AccInfo.ServerVersion != ServerVersionEnum.T4_4) return false;
184224

185225
string type = "";
186-
if (building == Classificator.BuildingEnum.Barracks ||
187-
building == Classificator.BuildingEnum.GreatBarracks) type = "Infantry";
188-
if (building == Classificator.BuildingEnum.Stable ||
189-
building == Classificator.BuildingEnum.GreatStable) type = "Cavalry";
226+
if (building == BuildingEnum.Barracks ||
227+
building == BuildingEnum.GreatBarracks) type = "Infantry";
228+
if (building == BuildingEnum.Stable ||
229+
building == BuildingEnum.GreatStable) type = "Cavalry";
190230

191231
// No helmet helps us for training in workshop
192232
if (string.IsNullOrEmpty(type)) return false;
@@ -199,7 +239,7 @@ public static bool SwitchHelmet(Account acc, Village trainVill, Classificator.Bu
199239

200240
var (equipCategory, equipName, equipTier) = ParseHeroItem(equipWith.Item);
201241

202-
if (acc.Hero.Equipt.TryGetValue(Classificator.HeroItemCategory.Helmet, out var equiped))
242+
if (acc.Hero.Equipt.TryGetValue(HeroItemCategory.Helmet, out var equiped))
203243
{
204244
var (category, name, tier) = ParseHeroItem(equiped);
205245
if (name == type &&

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 = "";

0 commit comments

Comments
 (0)