1111
1212namespace 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}
0 commit comments