Skip to content

Commit 811a7f8

Browse files
authored
Merge pull request #71 from Erol444/develop
New release
2 parents 2f97345 + 6cb09ae commit 811a7f8

File tree

130 files changed

+3835
-955
lines changed

Some content is hidden

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

130 files changed

+3835
-955
lines changed

TbsCore/Core/PostLoadHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static List<Action> GetPostLoadTasks(Account acc)
5555
var ran = new Random();
5656

5757
//Web browser not initialized
58+
// Update villages
5859
if (!UpdateAccountObject.UpdateVillages(html, acc)) return new List<Action>();
5960

6061
//Didnt check version yet
@@ -179,7 +180,8 @@ public static List<Action> GetPostLoadTasks(Account acc)
179180
acc.Tasks.Add(new NPC()
180181
{
181182
ExecuteAt = DateTime.MinValue,
182-
Vill = vill
183+
Vill = vill,
184+
Priority = TaskPriority.High
183185
}, true, vill);
184186
}
185187
},

TbsCore/Core/TaskExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static async Task PageLoaded(Account acc)
6060

6161
if (IsSysMsg(acc)) //Check if there is a system message (eg. Artifacts/WW plans appeared)
6262
{
63-
await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php?ok");
63+
await acc.Wb.Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php?ok=1");
6464
await Task.Delay(AccountHelper.Delay(acc));
6565
}
6666

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace TbsCore.Models.AccModels
1515
{
1616
public class WebBrowserInfo : IDisposable
1717
{
18-
private ChromeDriver Driver { get; set; }
18+
public ChromeDriver Driver { get; private set; }
1919

2020
private ChromeDriverService chromeService;
2121

@@ -24,7 +24,6 @@ public string CurrentUrl
2424
get
2525
{
2626
CheckChromeOpen();
27-
2827
return this.Driver.Url;
2928
}
3029
}
@@ -157,6 +156,11 @@ internal Dictionary<string, string> GetCookies()
157156
return cookiesDir;
158157
}
159158

159+
/// <summary>
160+
/// Refresh page. Same as clicking F5
161+
/// </summary>
162+
public async Task Refresh() => await Navigate(this.CurrentUrl);
163+
160164
public async Task Navigate(string url)
161165
{
162166
if (string.IsNullOrEmpty(url)) return;
@@ -191,23 +195,19 @@ public async Task Navigate(string url)
191195
while (repeat);
192196

193197
await Task.Delay(AccountHelper.Delay(acc));
194-
195198
UpdateHtml();
196-
197199
await TaskExecutor.PageLoaded(acc);
198200
}
199201

200202
public void UpdateHtml()
201203
{
202204
CheckChromeOpen();
203-
204205
Html.LoadHtml(Driver.PageSource);
205206
}
206207

207208
public void ExecuteScript(string script)
208209
{
209210
CheckChromeOpen();
210-
211211
Driver.ExecuteScript(script);
212212
}
213213

@@ -231,29 +231,25 @@ public T GetJsObj<T>(string obj)
231231
public string GetBearerToken()
232232
{
233233
CheckChromeOpen();
234-
235234
IJavaScriptExecutor js = acc.Wb.Driver;
236235
return (string)js.ExecuteScript("for(let field in Travian) { if (Travian[field].length == 32) return Travian[field]; }");
237236
}
238237

239238
public IWebElement FindElementById(string element)
240239
{
241240
CheckChromeOpen();
242-
243241
return Driver.FindElementById(element);
244242
}
245243

246244
public IWebElement FindElementByXPath(string xPath)
247245
{
248246
CheckChromeOpen();
249-
250247
return Driver.FindElementByXPath(xPath);
251248
}
252249

253250
public ITargetLocator SwitchTo()
254251
{
255252
CheckChromeOpen();
256-
257253
return Driver.SwitchTo();
258254
}
259255

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
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Threading.Tasks;
2+
using TbsCore.Helpers;
3+
using TbsCore.Models.AccModels;
4+
using TbsCore.Models.VillageModels;
5+
using static TbsCore.Helpers.Classificator;
6+
using System;
7+
8+
namespace TbsCore.Extensions
9+
{
10+
/// <summary>
11+
/// WIP!
12+
/// </summary>
13+
static class RallyPointNavigationHelper
14+
{
15+
/// <summary>
16+
/// Gets name of the task
17+
/// </summary>
18+
/// <returns>Name of the task</returns>
19+
//public async static Task<bool> ToRallyPointIncoming(this NavigationHelper nav, Account acc, Village vill)
20+
//{
21+
// await NavigationHelper.ToRallyPoint(acc, vill, NavigationHelper.RallyPointTab.Overview);
22+
// var url = new Uri(acc.Wb.CurrentUrl);
23+
// //url.
24+
// return true;
25+
//}
26+
27+
//public enum RallyPointFilter
28+
//{
29+
// Incoming,
30+
// Outgoing,
31+
// TroopsInVillage,
32+
// TroopsInOtherVillages
33+
//}
34+
//public enum RallyPointSubFilters
35+
//{
36+
// None = 0,
37+
// IncomingAttacks = 1,
38+
// ReturningTroops,
39+
// IncomingReinforcements,
40+
// OutgoingAttacks,
41+
// OutgoingReinforcements,
42+
//}
43+
}
44+
}

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/BuildingHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ public static bool RemoveFinishedCB(Village vill)
243243

244244
foreach (var taskDone in tasksDone)
245245
{
246-
var building = vill.Build.Buildings.First(x => x.Id == taskDone.Location);
247-
if (building.Type != taskDone.Building) continue;
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;
248249

249250
if (building.Level < taskDone.Level) building.Level = taskDone.Level;
250251
vill.Build.CurrentlyBuilding.Remove(taskDone);

TbsCore/Helpers/Classificator.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ public enum TribeEnum
363363

364364
public enum ServerVersionEnum
365365
{
366-
T4_4,
366+
TTwars,
367367
T4_5,
368368
}
369369

@@ -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/DriverHelper.cs

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OpenQA.Selenium;
2+
using OpenQA.Selenium.Support.UI;
23
using System;
34
using System.Threading.Tasks;
45
using TbsCore.Models.AccModels;
@@ -86,13 +87,17 @@ public static string GetBearerToken(Account acc, bool log = true)
8687

8788
/// <summary>
8889
/// Write coordinates into the number inputs. Used when sending troops, resources etc.
90+
/// If coordinates are already there (embedded in url), skip this task.
8991
/// </summary>
9092
internal static async Task WriteCoordinates(Account acc, Coordinates coordinates)
9193
{
92-
await WriteById(acc, "xCoordInput", coordinates.x);
93-
await WriteById(acc, "yCoordInput", coordinates.y);
94+
if (string.IsNullOrEmpty(acc.Wb.Html.GetElementbyId("xCoordInput").GetAttributeValue("value", "")))
95+
await WriteById(acc, "xCoordInput", coordinates.x);
96+
if (string.IsNullOrEmpty(acc.Wb.Html.GetElementbyId("yCoordInput").GetAttributeValue("value", "")))
97+
await WriteById(acc, "yCoordInput", coordinates.y);
9498
}
9599

100+
#region By Id
96101
public static async Task<bool> ClickById(Account acc, string query, bool log = true) =>
97102
await ExecuteAction(acc, new QueryById(query), new ActionClick(), log);
98103

@@ -104,50 +109,89 @@ public static async Task<bool> CheckById(Account acc, string query, bool check,
104109

105110
public static async Task<bool> SelectIndexById(Account acc, string query, int index, bool log = true) =>
106111
await ExecuteAction(acc, new QueryById(query), new ActionSelectIndex(index), log);
112+
#endregion
107113

108-
public static async Task<bool> ClickByClassName(Account acc, string query, bool log = true) =>
109-
await ExecuteAction(acc, new QueryByClassName(query), new ActionClick(), log);
114+
#region By Class Name
110115

111-
public static async Task<bool> WriteByClassName(Account acc, string query, object text, bool log = true) =>
112-
await ExecuteAction(acc, new QueryByClassName(query), new ActionWrite(text), log);
116+
public static async Task<bool> ClickByClassName(Account acc, string query, int qindex = 0, bool log = true) =>
117+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionClick(), log);
113118

114-
public static async Task<bool> CheckByClassName(Account acc, string query, bool check, bool log = true) =>
115-
await ExecuteAction(acc, new QueryByClassName(query), new ActionCheck(check), log);
119+
public static async Task<bool> WriteByClassName(Account acc, string query, object text, int qindex = 0, bool log = true) =>
120+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionWrite(text), log);
116121

117-
public static async Task<bool> SelectIndexByClassName(Account acc, string query, int index, bool log = true) =>
118-
await ExecuteAction(acc, new QueryByClassName(query), new ActionSelectIndex(index), log);
122+
public static async Task<bool> CheckByClassName(Account acc, string query, bool check, int qindex = 0, bool log = true) =>
123+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionCheck(check), log);
119124

120-
public static async Task<bool> ClickByName(Account acc, string query, bool log = true) =>
121-
await ExecuteAction(acc, new QueryByName(query), new ActionClick(), log);
125+
public static async Task<bool> SelectIndexByClassName(Account acc, string query, int index, int qindex = 0, bool log = true) =>
126+
await ExecuteAction(acc, new QueryByClassName(query, qindex), new ActionSelectIndex(index), log);
127+
#endregion
122128

123-
public static async Task<bool> WriteByName(Account acc, string query, object text, bool log = true, bool update = true) =>
124-
await ExecuteAction(acc, new QueryByName(query), new ActionWrite(text), log, update);
129+
#region By Name
125130

126-
public static async Task<bool> CheckByName(Account acc, string query, bool check, bool log = true) =>
127-
await ExecuteAction(acc, new QueryByName(query), new ActionCheck(check), log);
131+
public static async Task<bool> ClickByName(Account acc, string query, int qindex = 0, bool log = true) =>
132+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionClick(), log);
128133

129-
public static async Task<bool> SelectIndexByName(Account acc, string query, int index, bool log = true) =>
130-
await ExecuteAction(acc, new QueryByName(query), new ActionSelectIndex(index), log);
134+
public static async Task<bool> WriteByName(Account acc, string query, object text, int qindex = 0, bool log = true) =>
135+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionWrite(text), log);
136+
137+
public static async Task<bool> CheckByName(Account acc, string query, bool check, int qindex = 0, bool log = true) =>
138+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionCheck(check), log);
139+
140+
public static async Task<bool> SelectIndexByName(Account acc, string query, int index, int qindex = 0, bool log = true) =>
141+
await ExecuteAction(acc, new QueryByName(query, qindex), new ActionSelectIndex(index), log);
142+
#endregion
143+
144+
#region By Attribute Value
145+
public static async Task<bool> ClickByAttributeValue(Account acc, string attribute, string value, bool log = true) =>
146+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionClick(), log);
147+
148+
public static async Task<bool> WriteByAttributeValue(Account acc, string attribute, string value, object text, bool log = true, bool update = true) =>
149+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionWrite(text), log, update);
150+
151+
public static async Task<bool> CheckByAttributeValue(Account acc, string attribute, string value, bool check, bool log = true) =>
152+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionCheck(check), log);
153+
154+
public static async Task<bool> SelectByAttributeValue(Account acc, string attribute, string value, int index, bool log = true) =>
155+
await ExecuteAction(acc, new QueryByAttributeVal(attribute, value), new ActionSelectIndex(index), log);
156+
#endregion
131157

132158
private static async Task<bool> ExecuteAction(Account acc, Query query, Action action, bool log = true, bool update = true) =>
133159
await ExecuteScript(acc, $"document.{query.val}{action.val}", log, update);
134160

135-
public class QueryById : Query { public QueryById(string str) => base.val = $"getElementById('{str}')"; }
161+
public class QueryById : Query
162+
{ public QueryById(string str) => base.val = $"getElementById('{str}')"; }
163+
164+
public class QueryByName : Query
165+
{ public QueryByName(string str, int index = 0) => base.val = $"getElementsByName('{str}')[{index}]"; }
136166

137-
public class QueryByName : Query { public QueryByName(string str) => base.val = $"getElementsByName('{str}')[0]"; }
167+
public class QueryByClassName : Query
168+
{ public QueryByClassName(string str, int index = 0) => base.val = $"getElementsByClassName('{str}')[{index}]"; }
138169

139-
public class QueryByClassName : Query { public QueryByClassName(string str) => base.val = $"getElementsByClassName('{str}')[0]"; }
170+
public class QueryByAttributeVal : Query
171+
{ public QueryByAttributeVal(string attribute, string value) => base.val = $"querySelectorAll('[{attribute}=\"{value}\"]')[0]"; }
140172

141-
public class ActionWrite : Action { public ActionWrite(object str) => base.val = $".value='{str}';"; }
142173

143-
public class ActionClick : Action { public ActionClick() => base.val = ".click();"; }
174+
public class ActionWrite : Action
175+
{ public ActionWrite(object str) => base.val = $".value='{str}';"; }
144176

145-
public class ActionCheck : Action { public ActionCheck(bool check) => base.val = $".checked={(check ? "true" : "false")};"; }
177+
public class ActionClick : Action
178+
{ public ActionClick() => base.val = ".click();"; }
179+
public class ActionCheck : Action
180+
{ public ActionCheck(bool check) => base.val = $".checked={(check ? "true" : "false")};"; }
146181

147-
public class ActionSelectIndex : Action { public ActionSelectIndex(int index) => base.val = $".selectedIndex = {index};"; }
182+
public class ActionSelectIndex : Action
183+
{ public ActionSelectIndex(int index) => base.val = $".selectedIndex = {index};"; }
148184

149185
public abstract class Action { public string val; }
150186

151187
public abstract class Query { public string val; }
188+
189+
public static async Task WaitLoaded(Account acc, int delay = 15)
190+
{
191+
var wait = new WebDriverWait(acc.Wb.Driver, TimeSpan.FromSeconds(delay));
192+
wait.Until(driver => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
193+
acc.Wb.UpdateHtml();
194+
await TaskExecutor.PageLoaded(acc);
195+
}
152196
}
153197
}

0 commit comments

Comments
 (0)