Skip to content

Commit cfb58cf

Browse files
committed
refactor a bit, add interface and move logging around
1 parent 68624c9 commit cfb58cf

File tree

5 files changed

+156
-63
lines changed

5 files changed

+156
-63
lines changed

DeepBotPointFucker/App.config

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
23
<configuration>
3-
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
4+
<startup>
5+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
56
</startup>
67
</configuration>

DeepBotPointFucker/Box.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace DeepBotPointFucker
2+
{
3+
public class Box<T>
4+
{
5+
public bool HasValue {get;set;}
6+
public T Value {get;set;}
7+
}
8+
}

DeepBotPointFucker/DeepBotPointFucker.csproj

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,25 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>DeepBotPointFucker</RootNamespace>
1111
<AssemblyName>DeepBotPointFucker</AssemblyName>
12-
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<TargetFrameworkProfile />
16+
<PublishUrl>publish\</PublishUrl>
17+
<Install>true</Install>
18+
<InstallFrom>Disk</InstallFrom>
19+
<UpdateEnabled>false</UpdateEnabled>
20+
<UpdateMode>Foreground</UpdateMode>
21+
<UpdateInterval>7</UpdateInterval>
22+
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
23+
<UpdatePeriodically>false</UpdatePeriodically>
24+
<UpdateRequired>false</UpdateRequired>
25+
<MapFileExtensions>true</MapFileExtensions>
26+
<ApplicationRevision>0</ApplicationRevision>
27+
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
28+
<IsWebBootstrapper>false</IsWebBootstrapper>
29+
<UseApplicationTrust>false</UseApplicationTrust>
30+
<BootstrapperEnabled>true</BootstrapperEnabled>
1531
</PropertyGroup>
1632
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1733
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -22,6 +38,7 @@
2238
<DefineConstants>DEBUG;TRACE</DefineConstants>
2339
<ErrorReport>prompt</ErrorReport>
2440
<WarningLevel>4</WarningLevel>
41+
<Prefer32Bit>false</Prefer32Bit>
2542
</PropertyGroup>
2643
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2744
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,6 +48,10 @@
3148
<DefineConstants>TRACE</DefineConstants>
3249
<ErrorReport>prompt</ErrorReport>
3350
<WarningLevel>4</WarningLevel>
51+
<Prefer32Bit>false</Prefer32Bit>
52+
</PropertyGroup>
53+
<PropertyGroup>
54+
<StartupObject>DeepBotPointFucker.Program</StartupObject>
3455
</PropertyGroup>
3556
<ItemGroup>
3657
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
@@ -47,6 +68,7 @@
4768
<Reference Include="System.Xml" />
4869
</ItemGroup>
4970
<ItemGroup>
71+
<Compile Include="Box.cs" />
5072
<Compile Include="Models\CommandResult.cs" />
5173
<Compile Include="Models\RegisterResult.cs" />
5274
<Compile Include="Models\User.cs" />
@@ -56,9 +78,18 @@
5678
<Compile Include="Models\UserResult.cs" />
5779
</ItemGroup>
5880
<ItemGroup>
59-
<None Include="App.config" />
81+
<None Include="App.config">
82+
<SubType>Designer</SubType>
83+
</None>
6084
<None Include="packages.config" />
6185
</ItemGroup>
86+
<ItemGroup>
87+
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
88+
<Visible>False</Visible>
89+
<ProductName>.NET Framework 3.5 SP1</ProductName>
90+
<Install>false</Install>
91+
</BootstrapperPackage>
92+
</ItemGroup>
6293
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6394
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6495
Other similar extension points exist, see Microsoft.Common.targets.

DeepBotPointFucker/PointDownloader.cs

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,49 @@
1111

1212
namespace DeepBotPointFucker
1313
{
14-
public class PointDownloader
14+
public interface IPointDownloader
1515
{
16+
void Initialize();
17+
Task<bool> Connect();
18+
Task<List<User>> Download();
19+
void WriteResultsToFile(List<User> results);
20+
}
21+
22+
public class PointDownloader : IPointDownloader
23+
{
24+
private const int ApiLimit = 100;
1625
private readonly ClientWebSocket _socket;
26+
private string _apiKey;
27+
private int _minimumPointValue;
1728

1829
public PointDownloader()
1930
{
2031
_socket = new ClientWebSocket();
2132
}
2233

23-
public async Task<bool> Connect(string apiKey)
34+
#region IPointDownloader Members
35+
public void Initialize()
36+
{
37+
_apiKey = GetValueFromConsole("Please enter your DeepBot Api Key. This is located in your master settings.",
38+
value => new Box<string> {HasValue = !string.IsNullOrWhiteSpace(value), Value = value});
39+
40+
_minimumPointValue = GetValueFromConsole("Please enter the minimum number of points required for export.",
41+
value =>
42+
{
43+
var box = new Box<int>();
44+
45+
int outVal;
46+
if(int.TryParse(value, out outVal))
47+
{
48+
box.HasValue = true;
49+
box.Value = outVal;
50+
}
51+
52+
return box;
53+
});
54+
}
55+
56+
public async Task<bool> Connect()
2457
{
2558
try
2659
{
@@ -41,9 +74,7 @@ public async Task<bool> Connect(string apiKey)
4174

4275
try
4376
{
44-
var command = $"api|register|{apiKey}";
45-
46-
Log($"Sending command `{command}`.");
77+
var command = $"api|register|{_apiKey}";
4778

4879
await SendCommand(command);
4980
}
@@ -58,8 +89,6 @@ public async Task<bool> Connect(string apiKey)
5889
{
5990
var registerResult = await ReceiveMessage<RegisterResult>();
6091

61-
Log("Response received.");
62-
6392
if(registerResult?.Message == "success")
6493
{
6594
Log("Registering with DeepBot's API was successful.");
@@ -69,10 +98,10 @@ public async Task<bool> Connect(string apiKey)
6998
}
7099
catch
71100
{
72-
// ignored
73-
}
101+
Log("Registering with DeepBot's API was not successful.");
74102

75-
Log("Registering with DeepBot's API was not successful.");
103+
return false;
104+
}
76105

77106
return false;
78107
}
@@ -82,53 +111,94 @@ public async Task<List<User>> Download()
82111
Log("Beginning download.");
83112
var allUsers = new List<User>();
84113
var currentOffset = 0;
85-
const int limit = 100;
86114

87115
do
88116
{
89-
string command = $"api|get_users|{currentOffset}|{limit}";
117+
var command = $"api|get_users|{currentOffset}|{ApiLimit}";
90118

91119
var users = await GetUsers(command);
92120

121+
if(users == null)
122+
{
123+
break;
124+
}
125+
93126
allUsers.AddRange(users);
94127

95128
currentOffset += users.Count;
96-
} while(currentOffset % limit == 0);
129+
} while(currentOffset % ApiLimit == 0);
97130

98131
Log("Finished download.");
132+
99133
return allUsers;
100134
}
101135

136+
public void WriteResultsToFile(List<User> results)
137+
{
138+
Log("Beginning writing results to file.");
139+
140+
var stringBuilder = new StringBuilder();
141+
stringBuilder.AppendLine("User,Points");
142+
143+
var text = results.Where(x => x.Points >= _minimumPointValue)
144+
.OrderByDescending(x => x.Points)
145+
.Aggregate(stringBuilder, (builder, result) => builder.AppendLine($"{result.Name},{result.Points}"), builder => builder.ToString());
146+
147+
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "results.txt");
148+
149+
if(!File.Exists(filePath))
150+
{
151+
File.Create(filePath);
152+
}
153+
154+
File.WriteAllText(filePath, text);
155+
156+
Log("Completed writing results to file.");
157+
}
158+
#endregion
159+
160+
private T GetValueFromConsole<T>(string initMessage, Func<string, Box<T>> handler)
161+
{
162+
Box<T> box;
163+
164+
Log(initMessage);
165+
166+
do
167+
{
168+
var value = Console.ReadLine();
169+
170+
box = handler(value);
171+
} while(!box.HasValue);
172+
173+
return box.Value;
174+
}
175+
102176
private async Task<List<User>> GetUsers(string command)
103177
{
104178
try
105179
{
106-
Log($"Sending command `{command}`.");
107-
108180
await SendCommand(command);
109181
}
110182
catch
111183
{
112184
Log("Command failed.");
113185

114-
return new List<User>();
186+
return null;
115187
}
116188

117189
try
118190
{
119191
var result = await ReceiveMessage<UserResult>();
120192

121-
Log("Response received.");
122-
123193
return result.Message;
124194
}
125195
catch(Exception e)
126196
{
127-
Log(e.Message);
128-
129197
Log("There was an error receiving the response.");
130198

131-
return new List<User>();
199+
Log(e.Message);
200+
201+
return null;
132202
}
133203
}
134204

@@ -139,6 +209,8 @@ private void Log(string message)
139209

140210
private async Task SendCommand(string command)
141211
{
212+
Log($"Sending command `{command}`.");
213+
142214
var buffer = Encoding.UTF8.GetBytes(command);
143215
var arraySegment = new ArraySegment<byte>(buffer);
144216

@@ -157,6 +229,8 @@ private async Task<T> ReceiveMessage<T>()
157229
memoryStream.Write(buffer.Array, buffer.Offset, result.Count);
158230
} while(!result.EndOfMessage);
159231

232+
Log("Response received.");
233+
160234
memoryStream.Seek(0, SeekOrigin.Begin);
161235

162236
if(result.MessageType != WebSocketMessageType.Text)
@@ -172,28 +246,5 @@ private async Task<T> ReceiveMessage<T>()
172246
}
173247
}
174248
}
175-
176-
public void WriteResultsToFile(List<User> results)
177-
{
178-
Log("Beginning writing results to file.");
179-
180-
var stringBuilder = new StringBuilder();
181-
stringBuilder.AppendLine("User,Points");
182-
183-
var text = results.Where(x => x.Points > 100)
184-
.OrderByDescending(x => x.Points)
185-
.Aggregate(stringBuilder, (builder, result) => builder.AppendLine($"{result.Name},{result.Points}"), builder => builder.ToString());
186-
187-
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "results.txt");
188-
189-
if(!File.Exists(filePath))
190-
{
191-
File.Create(filePath);
192-
}
193-
194-
File.WriteAllText(filePath, text);
195-
196-
Log("Completed writing results to file.");
197-
}
198249
}
199250
}

DeepBotPointFucker/Program.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ namespace DeepBotPointFucker
55
{
66
public class Program
77
{
8-
private static void Main(string[] args)
8+
private static void Main()
99
{
10-
var apiKey = "8E7DCJWLUcXSbUYLcIJQdSfOfDeDQccECITOa";
10+
Task.Run(GetValue).Wait();
11+
}
1112

12-
Task.Run(async () =>
13-
{
14-
var pointDownloader = new PointDownloader();
13+
private static async Task GetValue()
14+
{
15+
IPointDownloader pointDownloader = new PointDownloader();
1516

16-
var result = await pointDownloader.Connect(apiKey);
17+
pointDownloader.Initialize();
1718

18-
if(result)
19-
{
20-
var results = await pointDownloader.Download();
19+
var result = await pointDownloader.Connect();
20+
21+
if(result)
22+
{
23+
var results = await pointDownloader.Download();
2124

22-
pointDownloader.WriteResultsToFile(results);
23-
}
25+
pointDownloader.WriteResultsToFile(results);
26+
}
2427

25-
Console.WriteLine("Press any key to quit.");
26-
Console.ReadLine();
27-
}).Wait();
28+
Console.WriteLine("Press any key to quit.");
29+
Console.ReadLine();
2830
}
2931
}
3032
}

0 commit comments

Comments
 (0)