Skip to content

Commit 5808953

Browse files
committed
version 0.12
- Optimized bot code. - Added a feature where its possible to set different configs for different days of the week.
1 parent b87eb1a commit 5808953

14 files changed

+468
-69
lines changed
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using GrandHeroFarmer.Modules;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Xml.Linq;
8+
9+
namespace GrandHeroFarmer.Bot
10+
{
11+
public class BotConfiguration
12+
{
13+
public readonly ClickArea startButton;
14+
public readonly ClickArea fightButton;
15+
public readonly ClickArea skipDialogButton;
16+
public readonly ClickArea autoBattleButton;
17+
public readonly ClickArea acceptAutoBattle;
18+
19+
public readonly int communicateServerTimer;
20+
public readonly int stageTimer;
21+
22+
public BotConfiguration(string configFolder)
23+
{
24+
XDocument xmlConfiguration = GetXml(configFolder);
25+
26+
startButton = new ClickArea(xmlConfiguration.Descendants("StartGHBButton").FirstOrDefault());
27+
fightButton = new ClickArea(xmlConfiguration.Descendants("FightButton").FirstOrDefault());
28+
skipDialogButton = new ClickArea(xmlConfiguration.Descendants("SkipDialogButton").FirstOrDefault());
29+
autoBattleButton = new ClickArea(xmlConfiguration.Descendants("AutoBattleButton").FirstOrDefault());
30+
acceptAutoBattle = new ClickArea(xmlConfiguration.Descendants("AcceptAutoBattleButton").FirstOrDefault());
31+
32+
communicateServerTimer = (int)xmlConfiguration.Descendants("CommunicateServerTimer").FirstOrDefault() * 1000;
33+
stageTimer = (int)xmlConfiguration.Descendants("StageTimer").FirstOrDefault() * 1000;
34+
}
35+
36+
private XDocument GetXml(string folderPath)
37+
{
38+
var currentDay = DateTime.Today.DayOfWeek;
39+
try
40+
{
41+
switch (currentDay)
42+
{
43+
case DayOfWeek.Friday:
44+
{
45+
return XDocument.Load(String.Format("{0}/Friday.xml", folderPath));
46+
}
47+
case DayOfWeek.Saturday:
48+
{
49+
return XDocument.Load(String.Format("{0}/Saturday.xml", folderPath));
50+
}
51+
case DayOfWeek.Sunday:
52+
{
53+
return XDocument.Load(String.Format("{0}/Sunday.xml", folderPath));
54+
}
55+
case DayOfWeek.Monday:
56+
{
57+
return XDocument.Load(String.Format("{0}/Monday.xml", folderPath));
58+
}
59+
case DayOfWeek.Tuesday:
60+
{
61+
return XDocument.Load(String.Format("{0}/Tuesday.xml", folderPath));
62+
}
63+
case DayOfWeek.Wednesday:
64+
{
65+
return XDocument.Load(String.Format("{0}/Wednesday.xml", folderPath));
66+
}
67+
case DayOfWeek.Thursday:
68+
{
69+
return XDocument.Load(String.Format("{0}/Thursday.xml", folderPath));
70+
}
71+
default:
72+
{
73+
return XDocument.Load(String.Format("{0}/Default.xml", folderPath));
74+
}
75+
}
76+
}
77+
catch
78+
{
79+
return XDocument.Load(String.Format("{0}/Default.xml", folderPath));
80+
}
81+
82+
}
83+
}
84+
}

GrandHeroFarmer/Bot/FehBot.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using GrandHeroFarmer.Modules;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace GrandHeroFarmer.Bot
10+
{
11+
public class FehBot
12+
{
13+
private readonly BotConfiguration config;
14+
private int cicles = 1;
15+
16+
public FehBot(BotConfiguration config)
17+
{
18+
this.config = config;
19+
}
20+
21+
public void Run(AdbWrapper adb)
22+
{
23+
Console.WriteLine();
24+
ConsoleLogger.WriteTime("Starting cicle ", false);
25+
ConsoleLogger.Write(string.Format("[{0}]", cicles.ToString()), textColor: ConsoleColor.Cyan);
26+
27+
// Click Lunatic Button
28+
adb.Tap(config.startButton.GenerateRandomCoords());
29+
30+
// Click Fight Button
31+
adb.Tap(config.fightButton.GenerateRandomCoords());
32+
33+
// Communicate Server
34+
Thread.Sleep(config.communicateServerTimer);
35+
36+
// Click to skip initial animations
37+
adb.Tap(config.fightButton.GenerateRandomCoords());
38+
39+
// Wait until Dialog starts
40+
Thread.Sleep(2000);
41+
42+
// Skip Dialog
43+
ConsoleLogger.WriteTime("Skipping initial dialog");
44+
adb.Tap(config.skipDialogButton.GenerateRandomCoords());
45+
46+
// Wait for initial animations
47+
Thread.Sleep(3000);
48+
49+
// Click Auto Battle
50+
ConsoleLogger.WriteTime("Initializing Auto-Battle");
51+
adb.Tap(config.autoBattleButton.GenerateRandomCoords());
52+
53+
// CLick Accept
54+
adb.Tap(config.acceptAutoBattle.GenerateRandomCoords());
55+
56+
// Wait for battle to end
57+
ConsoleLogger.WriteTime("Waiting for battle to end");
58+
Thread.Sleep(config.stageTimer);
59+
60+
// Click again to skip animations
61+
adb.Tap(config.fightButton.GenerateRandomCoords());
62+
63+
// All done!
64+
ConsoleLogger.WriteTime("Finished cicle ", false);
65+
ConsoleLogger.Write(string.Format("[{0}]", cicles.ToString()), textColor: ConsoleColor.Cyan);
66+
67+
// Wait to send result to server
68+
Thread.Sleep(config.communicateServerTimer + 1000);
69+
70+
cicles++;
71+
}
72+
73+
}
74+
}

GrandHeroFarmer/Grand Hero Farmer.csproj

+25-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
<Reference Include="System.Xml" />
5353
</ItemGroup>
5454
<ItemGroup>
55-
<Compile Include="Modules\Android.cs" />
55+
<Compile Include="Bot\FehBot.cs" />
56+
<Compile Include="Bot\BotConfiguration.cs" />
57+
<Compile Include="Modules\AdbWrapper.cs" />
5658
<Compile Include="Modules\ConsoleLogger.cs" />
5759
<Compile Include="Modules\Helpers.cs" />
5860
<Compile Include="Program.cs" />
@@ -69,7 +71,28 @@
6971
</ItemGroup>
7072
<ItemGroup>
7173
<Content Include="Assemblies\SharpAdbClient.dll" />
72-
<Content Include="Configurations\Default.xml">
74+
<Content Include="configs\Thursday.xml">
75+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
76+
</Content>
77+
<Content Include="configs\Wednesday.xml">
78+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
79+
</Content>
80+
<Content Include="configs\Tuesday.xml">
81+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
82+
</Content>
83+
<Content Include="configs\Monday.xml">
84+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
85+
</Content>
86+
<Content Include="configs\Sunday.xml">
87+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
88+
</Content>
89+
<Content Include="configs\Saturday.xml">
90+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
91+
</Content>
92+
<Content Include="configs\Friday.xml">
93+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
94+
</Content>
95+
<Content Include="configs\Default.xml">
7396
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
7497
</Content>
7598
<None Include="Resources\razz-berry.ico" />

GrandHeroFarmer/Modules/Android.cs GrandHeroFarmer/Modules/AdbWrapper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace GrandHeroFarmer.Modules
99
{
10-
public class Android
10+
public class AdbWrapper
1111
{
1212
private readonly AdbServer _server;
1313
private DeviceData _device;
1414

15-
public Android()
15+
public AdbWrapper()
1616
{
1717
if (File.Exists(@"adb\adb.exe"))
1818
{

GrandHeroFarmer/Modules/ConsoleLogger.cs

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ namespace GrandHeroFarmer.Modules
55
internal enum Type
66
{ Info = ConsoleColor.Cyan, Error = ConsoleColor.Red, Default = ConsoleColor.Gray };
77

8-
/// <summary>
9-
/// Internal Helper logger class.
10-
/// </summary>
118
internal static class ConsoleLogger
129
{
1310
public static void Write(string value, bool newLine = true, ConsoleColor textColor = ConsoleColor.Gray, ConsoleColor backgroundColor = ConsoleColor.Black)

GrandHeroFarmer/Program.cs

+10-62
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using GrandHeroFarmer.Modules;
1+
using GrandHeroFarmer.Bot;
2+
using GrandHeroFarmer.Modules;
23
using System;
34
using System.Collections.Generic;
45
using System.IO;
@@ -34,81 +35,28 @@ private static void Main(string[] args)
3435

3536
try
3637
{
37-
Android phone = new Android();
38+
AdbWrapper adb = new AdbWrapper();
3839

39-
// Initializing Service Configurations
40-
ConsoleLogger.WriteTime("Loading service configurations from xml... ", false);
41-
XDocument doc = XDocument.Load("Configurations/Default.xml");
42-
ClickArea startGBHButton = new ClickArea(doc.Descendants("StartGHBButton").FirstOrDefault());
43-
ClickArea fightButton = new ClickArea(doc.Descendants("FightButton").FirstOrDefault());
44-
ClickArea skipDialogButton = new ClickArea(doc.Descendants("SkipDialogButton").FirstOrDefault());
45-
ClickArea autoBattleButton = new ClickArea(doc.Descendants("AutoBattleButton").FirstOrDefault());
46-
ClickArea acceptAutoBattleButton = new ClickArea(doc.Descendants("AcceptAutoBattleButton").FirstOrDefault());
40+
// Initializing Bot Configurations
41+
ConsoleLogger.WriteTime("Loading bot configurations from xml... ", false);
42+
BotConfiguration botConfig = new BotConfiguration("configs");
43+
ConsoleLogger.Write("OK", textColor: ConsoleColor.Cyan);
4744

48-
int communicateServerTimer = (int)doc.Descendants("CommunicateServerTimer").FirstOrDefault() * 1000;
49-
int stageTimer = (int)doc.Descendants("StageTimer").FirstOrDefault() * 1000;
45+
ConsoleLogger.WriteTime("Initializing bot instance... ", false);
46+
FehBot bot = new FehBot(botConfig);
5047
ConsoleLogger.Write("OK", textColor: ConsoleColor.Cyan);
5148

5249
ConsoleLogger.WriteTime("Setup is done, press enter to start earning feathers.", textColor: ConsoleColor.Cyan);
5350
ConsoleLogger.WriteTime("Press 'Ctrl + C' to exit the application.", newLine: false, textColor: ConsoleColor.Cyan);
5451
Console.ReadLine();
5552

56-
int ciclesCompleted = 1;
57-
5853
Thread thread = new Thread(() =>
5954
{
6055
Console.Title = string.Format("{0} - Farming", Console.Title);
6156

6257
while (true)
6358
{
64-
Console.WriteLine();
65-
ConsoleLogger.WriteTime("Starting cicle ", false);
66-
ConsoleLogger.Write(string.Format("[{0}]", ciclesCompleted.ToString()), textColor: ConsoleColor.Cyan);
67-
68-
// Click Lunatic Button
69-
phone.Tap(startGBHButton.GenerateRandomCoords());
70-
71-
// Click Fight Button
72-
phone.Tap(fightButton.GenerateRandomCoords());
73-
74-
// Communicate Server
75-
Thread.Sleep(communicateServerTimer);
76-
77-
// Click to skip initial animations
78-
phone.Tap(fightButton.GenerateRandomCoords());
79-
80-
// Wait until Dialog starts
81-
Thread.Sleep(2000);
82-
83-
// Skip Dialog
84-
ConsoleLogger.WriteTime("Skipping initial dialog");
85-
phone.Tap(skipDialogButton.GenerateRandomCoords());
86-
87-
// Wait for initial animations
88-
Thread.Sleep(3000);
89-
90-
// Click Auto Battle
91-
ConsoleLogger.WriteTime("Initializing Auto-Battle");
92-
phone.Tap(autoBattleButton.GenerateRandomCoords());
93-
94-
// CLick Accept
95-
phone.Tap(acceptAutoBattleButton.GenerateRandomCoords());
96-
97-
// Wait for battle to end
98-
ConsoleLogger.WriteTime("Waiting for battle to end");
99-
Thread.Sleep(stageTimer);
100-
101-
// Click again to skip animations
102-
phone.Tap(fightButton.GenerateRandomCoords());
103-
104-
// All done!
105-
ConsoleLogger.WriteTime("Finished cicle ", false);
106-
ConsoleLogger.Write(string.Format("[{0}]", ciclesCompleted.ToString()), textColor: ConsoleColor.Cyan);
107-
108-
// Wait to send result to server
109-
Thread.Sleep(communicateServerTimer + 1000);
110-
111-
ciclesCompleted++;
59+
bot.Run(adb);
11260
}
11361
});
11462

File renamed without changes.

GrandHeroFarmer/configs/Friday.xml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Settings>
3+
<Areas>
4+
<StartGHBButton>
5+
<TopLeftX>100</TopLeftX>
6+
<TopLeftY>850</TopLeftY>
7+
<BottomRightX>1000</BottomRightX>
8+
<BottomRightY>1000</BottomRightY>
9+
</StartGHBButton>
10+
<FightButton>
11+
<TopLeftX>200</TopLeftX>
12+
<TopLeftY>1130</TopLeftY>
13+
<BottomRightX>900</BottomRightX>
14+
<BottomRightY>1230</BottomRightY>
15+
</FightButton>
16+
<SkipDialogButton>
17+
<TopLeftX>750</TopLeftX>
18+
<TopLeftY>50</TopLeftY>
19+
<BottomRightX>1000</BottomRightX>
20+
<BottomRightY>100</BottomRightY>
21+
</SkipDialogButton>
22+
<AutoBattleButton>
23+
<TopLeftX>770</TopLeftX>
24+
<TopLeftY>1770</TopLeftY>
25+
<BottomRightX>900</BottomRightX>
26+
<BottomRightY>1900</BottomRightY>
27+
</AutoBattleButton>
28+
<AcceptAutoBattleButton>
29+
<TopLeftX>230</TopLeftX>
30+
<TopLeftY>900</TopLeftY>
31+
<BottomRightX>870</BottomRightX>
32+
<BottomRightY>1000</BottomRightY>
33+
</AcceptAutoBattleButton>
34+
</Areas>
35+
<Timers>
36+
<CommunicateServerTimer>2</CommunicateServerTimer>
37+
<StageTimer>10</StageTimer>
38+
</Timers>
39+
</Settings>

0 commit comments

Comments
 (0)