Skip to content

Commit 68624c9

Browse files
committed
IT IS DONE
1 parent e2fa56c commit 68624c9

File tree

6 files changed

+76
-57
lines changed

6 files changed

+76
-57
lines changed

DeepBotPointFucker/DeepBotPointFucker.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
</Reference>
4040
<Reference Include="System" />
4141
<Reference Include="System.Core" />
42-
<Reference Include="System.Windows.Forms" />
4342
<Reference Include="System.Xml.Linq" />
4443
<Reference Include="System.Data.DataSetExtensions" />
4544
<Reference Include="Microsoft.CSharp" />
@@ -48,11 +47,13 @@
4847
<Reference Include="System.Xml" />
4948
</ItemGroup>
5049
<ItemGroup>
51-
<Compile Include="CommandResult.cs" />
50+
<Compile Include="Models\CommandResult.cs" />
51+
<Compile Include="Models\RegisterResult.cs" />
52+
<Compile Include="Models\User.cs" />
5253
<Compile Include="PointDownloader.cs" />
5354
<Compile Include="Program.cs" />
5455
<Compile Include="Properties\AssemblyInfo.cs" />
55-
<Compile Include="UserResult.cs" />
56+
<Compile Include="Models\UserResult.cs" />
5657
</ItemGroup>
5758
<ItemGroup>
5859
<None Include="App.config" />
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
using Newtonsoft.Json;
22

3-
namespace DeepBotPointFucker
3+
namespace DeepBotPointFucker.Models
44
{
55
public class CommandResult
66
{
7-
/*
87
[JsonProperty(PropertyName = "function")]
98
public string Function {get;set;}
109

1110
[JsonProperty(PropertyName = "param")]
1211
public string Parameter {get;set;}
13-
*/
14-
15-
[JsonProperty(PropertyName = "msg")]
16-
public string Message {get;set;}
1712
}
1813
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace DeepBotPointFucker.Models
4+
{
5+
public class RegisterResult : CommandResult
6+
{
7+
[JsonProperty(PropertyName = "msg")]
8+
public string Message {get;set;}
9+
}
10+
}

DeepBotPointFucker/UserResult.cs renamed to DeepBotPointFucker/Models/User.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
using Newtonsoft.Json;
1+
using System;
2+
using Newtonsoft.Json;
23

3-
namespace DeepBotPointFucker
4+
namespace DeepBotPointFucker.Models
45
{
5-
public class UserResult
6+
public class User
67
{
78
[JsonProperty(PropertyName = "user")]
8-
public string User {get;set;}
9+
public string Name {get;set;}
910

1011
[JsonProperty(PropertyName = "points")]
1112
public decimal Points {get;set;}
1213

13-
/*
1414
[JsonProperty(PropertyName = "watch_time")]
15-
public int WatchTime {get;set;}
15+
public decimal WatchTime {get;set;}
1616

1717
[JsonProperty(PropertyName = "vip")]
1818
public int Vip {get;set;}
@@ -28,6 +28,5 @@ public class UserResult
2828

2929
[JsonProperty(PropertyName = "vip_expiry")]
3030
public DateTime VipExpiry {get;set;}
31-
*/
3231
}
3332
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace DeepBotPointFucker.Models
5+
{
6+
public class UserResult : CommandResult
7+
{
8+
[JsonProperty(PropertyName = "msg")]
9+
public List<User> Message {get;set;}
10+
}
11+
}

DeepBotPointFucker/PointDownloader.cs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Text;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using System.Windows.Forms;
9+
using DeepBotPointFucker.Models;
1010
using Newtonsoft.Json;
1111

1212
namespace DeepBotPointFucker
@@ -45,7 +45,7 @@ public async Task<bool> Connect(string apiKey)
4545

4646
Log($"Sending command `{command}`.");
4747

48-
await SendMessage(command);
48+
await SendCommand(command);
4949
}
5050
catch
5151
{
@@ -56,11 +56,11 @@ public async Task<bool> Connect(string apiKey)
5656

5757
try
5858
{
59-
var registerResult = await ReceiveMessage();
59+
var registerResult = await ReceiveMessage<RegisterResult>();
6060

6161
Log("Response received.");
6262

63-
if (registerResult?.Message == "success")
63+
if(registerResult?.Message == "success")
6464
{
6565
Log("Registering with DeepBot's API was successful.");
6666

@@ -77,55 +77,58 @@ public async Task<bool> Connect(string apiKey)
7777
return false;
7878
}
7979

80-
public async Task<List<UserResult>> Download()
80+
public async Task<List<User>> Download()
8181
{
82-
var allUsers = new List<UserResult>();
82+
Log("Beginning download.");
83+
var allUsers = new List<User>();
8384
var currentOffset = 0;
8485
const int limit = 100;
8586

86-
var command = $"api|get_users|{currentOffset}|{limit}";
87-
88-
List<UserResult> users;
8987
do
9088
{
91-
users = await GetUsers(command);
89+
string command = $"api|get_users|{currentOffset}|{limit}";
90+
91+
var users = await GetUsers(command);
9292

9393
allUsers.AddRange(users);
9494

9595
currentOffset += users.Count;
96-
} while(users.Any());
96+
} while(currentOffset % limit == 0);
9797

98+
Log("Finished download.");
9899
return allUsers;
99100
}
100101

101-
private async Task<List<UserResult>> GetUsers(string command)
102+
private async Task<List<User>> GetUsers(string command)
102103
{
103104
try
104105
{
105106
Log($"Sending command `{command}`.");
106107

107-
await SendMessage(command);
108+
await SendCommand(command);
108109
}
109110
catch
110111
{
111112
Log("Command failed.");
112113

113-
return new List<UserResult>();
114+
return new List<User>();
114115
}
115116

116117
try
117118
{
118-
var result = await ReceiveMessage();
119+
var result = await ReceiveMessage<UserResult>();
119120

120121
Log("Response received.");
121122

122-
return JsonConvert.DeserializeObject<List<UserResult>>(result.Message);
123+
return result.Message;
123124
}
124-
catch
125+
catch(Exception e)
125126
{
127+
Log(e.Message);
128+
126129
Log("There was an error receiving the response.");
127130

128-
return new List<UserResult>();
131+
return new List<User>();
129132
}
130133
}
131134

@@ -134,22 +137,15 @@ private void Log(string message)
134137
Console.WriteLine(message);
135138
}
136139

137-
private async Task SendMessage(string message)
140+
private async Task SendCommand(string command)
138141
{
139-
using(var memoryStream = new MemoryStream())
140-
{
141-
using(var writer = new StreamWriter(memoryStream, Encoding.UTF8))
142-
{
143-
await writer.WriteLineAsync(message);
144-
}
145-
146-
var arraySegment = new ArraySegment<byte>(memoryStream.ToArray());
142+
var buffer = Encoding.UTF8.GetBytes(command);
143+
var arraySegment = new ArraySegment<byte>(buffer);
147144

148-
await _socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
149-
}
145+
await _socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
150146
}
151147

152-
private async Task<CommandResult> ReceiveMessage()
148+
private async Task<T> ReceiveMessage<T>()
153149
{
154150
using(var memoryStream = new MemoryStream())
155151
{
@@ -165,32 +161,39 @@ private async Task<CommandResult> ReceiveMessage()
165161

166162
if(result.MessageType != WebSocketMessageType.Text)
167163
{
168-
return null;
164+
return default(T);
169165
}
170166

171167
using(var reader = new StreamReader(memoryStream, Encoding.UTF8))
172168
{
173169
var message = await reader.ReadToEndAsync();
174170

175-
return JsonConvert.DeserializeObject<CommandResult>(message);
171+
return JsonConvert.DeserializeObject<T>(message);
176172
}
177173
}
178174
}
179175

180-
public void WriteResultsToFile(List<UserResult> results)
176+
public void WriteResultsToFile(List<User> results)
181177
{
182-
var ofd = new OpenFileDialog();
183-
if(ofd.ShowDialog() != DialogResult.OK)
184-
{
185-
return;
186-
}
187-
178+
Log("Beginning writing results to file.");
179+
188180
var stringBuilder = new StringBuilder();
189181
stringBuilder.AppendLine("User,Points");
190182

191-
var text = results.Aggregate(stringBuilder, (builder, result) => builder.AppendLine($"{result.User},{result.Points}"), builder => builder.ToString());
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);
192195

193-
File.WriteAllText(ofd.FileName, text);
196+
Log("Completed writing results to file.");
194197
}
195198
}
196199
}

0 commit comments

Comments
 (0)