Skip to content

Commit b87eb1a

Browse files
committed
Small Optimizations.
1 parent cd3e9c7 commit b87eb1a

File tree

4 files changed

+41
-59
lines changed

4 files changed

+41
-59
lines changed

GrandHeroFarmer/Modules/Android.cs

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using SharpAdbClient;
22
using System;
3-
using System.Collections.Generic;
43
using System.IO;
54
using System.Linq;
65
using System.Net;
7-
using System.Text;
8-
using System.Threading.Tasks;
96
using System.Xml.Linq;
107

118
namespace GrandHeroFarmer.Modules
@@ -30,7 +27,7 @@ public Android()
3027
}
3128
else
3229
{
33-
ConsoleLogger.Write("adb.exe not found. ", textColor: ConsoleColor.Red);
30+
ConsoleLogger.Write("adb.exe not found.", textColor: ConsoleColor.Red);
3431
ConsoleLogger.Write("Did you extract everything from the zip?");
3532
ConsoleLogger.Write("Press Enter to exit.");
3633

@@ -48,9 +45,8 @@ private void ConnectToAndroid()
4845
while (!foundDevice)
4946
{
5047
ConsoleLogger.WriteTime("Trying to get device information... ", false);
51-
if(AdbClient.Instance.GetDevices().Count != 0)
48+
if (AdbClient.Instance.GetDevices().Count != 0)
5249
{
53-
5450
_device = AdbClient.Instance.GetDevices().First();
5551
foundDevice = true;
5652
ConsoleLogger.Write(_device.Name + " connected!", textColor: ConsoleColor.Cyan);
@@ -70,19 +66,18 @@ private void ConnectToAndroid()
7066
/// <param name="coordenates">Coordenates Tuple (X, Y)</param>
7167
public void Tap(Tuple<int, int> coordenates)
7268
{
73-
7469
//TODO Check if device is null
7570
string command = "input tap " + coordenates.Item1 + " " + coordenates.Item2;
7671

7772
IShellOutputReceiver receiver = null;
7873

7974
AdbClient.Instance.ExecuteRemoteCommand(command, _device, receiver);
8075
}
81-
76+
8277
// Monitor Functions
83-
void OnDeviceDisconnected(object sender, DeviceDataEventArgs e)
78+
private void OnDeviceDisconnected(object sender, DeviceDataEventArgs e)
8479
{
85-
ConsoleLogger.WriteTime("The device " + _device.Name +" has disconnected to this PC", textColor: ConsoleColor.Red);
80+
ConsoleLogger.WriteTime("The device " + _device.Name + " has disconnected to this PC", textColor: ConsoleColor.Red);
8681
ConsoleLogger.WriteTime("Please restart the application and go back to the Mission Select Menu.");
8782

8883
Environment.Exit(0);
@@ -107,14 +102,15 @@ public ClickArea(int pTopLeftX, int pTopLeftY, int pBottomRightX, int pBottomRig
107102
/// Creates a click area from a XElement in a XML File.
108103
/// </summary>
109104
/// <param name="element"></param>
110-
public ClickArea(XElement element)
105+
public ClickArea(XElement element)
111106
: this
112107
(
113-
(int) element.Descendants("TopLeftX").FirstOrDefault(),
114-
(int) element.Descendants("TopLeftY").FirstOrDefault(),
115-
(int) element.Descendants("BottomRightX").FirstOrDefault(),
116-
(int) element.Descendants("BottomRightY").FirstOrDefault()
117-
) { }
108+
(int)element.Descendants("TopLeftX").FirstOrDefault(),
109+
(int)element.Descendants("TopLeftY").FirstOrDefault(),
110+
(int)element.Descendants("BottomRightX").FirstOrDefault(),
111+
(int)element.Descendants("BottomRightY").FirstOrDefault()
112+
)
113+
{ }
118114

119115
public Tuple<int, int> GenerateRandomCoords()
120116
{
@@ -125,4 +121,4 @@ public Tuple<int, int> GenerateRandomCoords()
125121
return new Tuple<int, int>(xRand, yRand);
126122
}
127123
}
128-
}
124+
}

GrandHeroFarmer/Modules/ConsoleLogger.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace GrandHeroFarmer.Modules
84
{
9-
enum Type { Info = ConsoleColor.Cyan, Error = ConsoleColor.Red, Default = ConsoleColor.Gray };
5+
internal enum Type
6+
{ Info = ConsoleColor.Cyan, Error = ConsoleColor.Red, Default = ConsoleColor.Gray };
107

11-
static class ConsoleLogger
8+
/// <summary>
9+
/// Internal Helper logger class.
10+
/// </summary>
11+
internal static class ConsoleLogger
1212
{
1313
public static void Write(string value, bool newLine = true, ConsoleColor textColor = ConsoleColor.Gray, ConsoleColor backgroundColor = ConsoleColor.Black)
1414
{
@@ -34,9 +34,7 @@ public static void WriteTime(string value, bool newLine = true, ConsoleColor tex
3434
public static void WriteCenter(string value, bool newLine = true, ConsoleColor textColor = ConsoleColor.Gray, ConsoleColor backgroundColor = ConsoleColor.Black)
3535
{
3636
Console.SetCursorPosition((Console.WindowWidth - value.Length) / 2, Console.CursorTop);
37-
3837
Write(value, newLine, textColor, backgroundColor);
3938
}
40-
4139
}
42-
}
40+
}

GrandHeroFarmer/Modules/Helpers.cs

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Diagnostics;
72

83
namespace GrandHeroFarmer.Modules
94
{
10-
static class Helpers
5+
internal static class Helpers
116
{
12-
137
public static string GetProgramVersion()
148
{
159
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
@@ -18,4 +12,4 @@ public static string GetProgramVersion()
1812
return fvi.FileVersion;
1913
}
2014
}
21-
}
15+
}

GrandHeroFarmer/Program.cs

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
using GrandHeroFarmer.Modules;
2-
using SharpAdbClient;
32
using System;
43
using System.Collections.Generic;
5-
using System.Diagnostics;
4+
using System.IO;
65
using System.Linq;
7-
using System.Text;
86
using System.Threading;
9-
using System.Threading.Tasks;
10-
using System.Xml;
117
using System.Xml.Linq;
128

139
namespace GrandHeroFarmer
1410
{
15-
class Program
11+
internal class Program
1612
{
1713
private static readonly List<string> puns = new List<string>
1814
{
@@ -26,22 +22,21 @@ class Program
2622

2723
public static readonly Random rnd = new Random();
2824

29-
static void Main(string[] args)
25+
private static void Main(string[] args)
3026
{
31-
Console.Title = "Grand Hero Farmer v" + Helpers.GetProgramVersion();
27+
Console.Title = string.Format("Grand Hero Farmer v{0}", Helpers.GetProgramVersion());
3228
Console.Clear();
3329

3430
Console.WriteLine();
3531
ConsoleLogger.WriteCenter("Grand Hero Farmer", textColor: ConsoleColor.Cyan, backgroundColor: ConsoleColor.DarkCyan);
36-
37-
ConsoleLogger.WriteCenter(puns[rnd.Next(puns.Count)], true, textColor: ConsoleColor.Magenta);
38-
32+
ConsoleLogger.WriteCenter(puns[rnd.Next(puns.Count)], newLine: true, textColor: ConsoleColor.Magenta);
3933
ConsoleLogger.WriteTime("Starting program...");
34+
4035
try
4136
{
4237
Android phone = new Android();
4338

44-
//Initializing Service Configurations
39+
// Initializing Service Configurations
4540
ConsoleLogger.WriteTime("Loading service configurations from xml... ", false);
4641
XDocument doc = XDocument.Load("Configurations/Default.xml");
4742
ClickArea startGBHButton = new ClickArea(doc.Descendants("StartGHBButton").FirstOrDefault());
@@ -50,25 +45,25 @@ static void Main(string[] args)
5045
ClickArea autoBattleButton = new ClickArea(doc.Descendants("AutoBattleButton").FirstOrDefault());
5146
ClickArea acceptAutoBattleButton = new ClickArea(doc.Descendants("AcceptAutoBattleButton").FirstOrDefault());
5247

53-
int communicateServerTimer = ((int)(doc.Descendants("CommunicateServerTimer").FirstOrDefault()) * 1000);
54-
int stageTimer = ((int)(doc.Descendants("StageTimer").FirstOrDefault()) * 1000);
48+
int communicateServerTimer = (int)doc.Descendants("CommunicateServerTimer").FirstOrDefault() * 1000;
49+
int stageTimer = (int)doc.Descendants("StageTimer").FirstOrDefault() * 1000;
5550
ConsoleLogger.Write("OK", textColor: ConsoleColor.Cyan);
5651

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

61-
int cicles = 1;
56+
int ciclesCompleted = 1;
6257

63-
Thread thread = new Thread(() => {
64-
65-
Console.Title = "Grand Hero Farmer - Farming";
58+
Thread thread = new Thread(() =>
59+
{
60+
Console.Title = string.Format("{0} - Farming", Console.Title);
6661

6762
while (true)
6863
{
6964
Console.WriteLine();
7065
ConsoleLogger.WriteTime("Starting cicle ", false);
71-
ConsoleLogger.Write("[" + cicles.ToString() + "]", textColor: ConsoleColor.Cyan);
66+
ConsoleLogger.Write(string.Format("[{0}]", ciclesCompleted.ToString()), textColor: ConsoleColor.Cyan);
7267

7368
// Click Lunatic Button
7469
phone.Tap(startGBHButton.GenerateRandomCoords());
@@ -108,19 +103,18 @@ static void Main(string[] args)
108103

109104
// All done!
110105
ConsoleLogger.WriteTime("Finished cicle ", false);
111-
ConsoleLogger.Write("[" + cicles.ToString() + "]", textColor: ConsoleColor.Cyan);
106+
ConsoleLogger.Write(string.Format("[{0}]", ciclesCompleted.ToString()), textColor: ConsoleColor.Cyan);
112107

113108
// Wait to send result to server
114109
Thread.Sleep(communicateServerTimer + 1000);
115110

116-
cicles++;
111+
ciclesCompleted++;
117112
}
118113
});
119114

120115
thread.Start();
121-
122116
}
123-
catch (System.IO.FileNotFoundException)
117+
catch (FileNotFoundException)
124118
{
125119
ConsoleLogger.Write("FAILED", textColor: ConsoleColor.Red);
126120
ConsoleLogger.WriteTime("Couldn't Load XML File. Press Enter to exit the application.");
@@ -140,4 +134,4 @@ static void Main(string[] args)
140134
}
141135
}
142136
}
143-
}
137+
}

0 commit comments

Comments
 (0)