Skip to content

Commit e2fa56c

Browse files
committed
first try at point downloader
1 parent ed47452 commit e2fa56c

9 files changed

Lines changed: 414 additions & 0 deletions

File tree

DeepBotPointFucker.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.24720.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeepBotPointFucker", "DeepBotPointFucker\DeepBotPointFucker.csproj", "{0BBAD9AB-EF35-4CBC-A586-EC600D46EA8D}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{0BBAD9AB-EF35-4CBC-A586-EC600D46EA8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0BBAD9AB-EF35-4CBC-A586-EC600D46EA8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0BBAD9AB-EF35-4CBC-A586-EC600D46EA8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0BBAD9AB-EF35-4CBC-A586-EC600D46EA8D}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

DeepBotPointFucker/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
5+
</startup>
6+
</configuration>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Newtonsoft.Json;
2+
3+
namespace DeepBotPointFucker
4+
{
5+
public class CommandResult
6+
{
7+
/*
8+
[JsonProperty(PropertyName = "function")]
9+
public string Function {get;set;}
10+
11+
[JsonProperty(PropertyName = "param")]
12+
public string Parameter {get;set;}
13+
*/
14+
15+
[JsonProperty(PropertyName = "msg")]
16+
public string Message {get;set;}
17+
}
18+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{0BBAD9AB-EF35-4CBC-A586-EC600D46EA8D}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DeepBotPointFucker</RootNamespace>
11+
<AssemblyName>DeepBotPointFucker</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Windows.Forms" />
43+
<Reference Include="System.Xml.Linq" />
44+
<Reference Include="System.Data.DataSetExtensions" />
45+
<Reference Include="Microsoft.CSharp" />
46+
<Reference Include="System.Data" />
47+
<Reference Include="System.Net.Http" />
48+
<Reference Include="System.Xml" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Compile Include="CommandResult.cs" />
52+
<Compile Include="PointDownloader.cs" />
53+
<Compile Include="Program.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
<Compile Include="UserResult.cs" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<None Include="App.config" />
59+
<None Include="packages.config" />
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
63+
Other similar extension points exist, see Microsoft.Common.targets.
64+
<Target Name="BeforeBuild">
65+
</Target>
66+
<Target Name="AfterBuild">
67+
</Target>
68+
-->
69+
</Project>
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net.WebSockets;
6+
using System.Text;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using System.Windows.Forms;
10+
using Newtonsoft.Json;
11+
12+
namespace DeepBotPointFucker
13+
{
14+
public class PointDownloader
15+
{
16+
private readonly ClientWebSocket _socket;
17+
18+
public PointDownloader()
19+
{
20+
_socket = new ClientWebSocket();
21+
}
22+
23+
public async Task<bool> Connect(string apiKey)
24+
{
25+
try
26+
{
27+
Log("Establishing connection.");
28+
29+
const string url = "ws://localhost:3337";
30+
31+
Log($"Connecting to `{url}`.");
32+
33+
await _socket.ConnectAsync(new Uri(url), CancellationToken.None);
34+
}
35+
catch
36+
{
37+
Log("Connection failed.");
38+
39+
return false;
40+
}
41+
42+
try
43+
{
44+
var command = $"api|register|{apiKey}";
45+
46+
Log($"Sending command `{command}`.");
47+
48+
await SendMessage(command);
49+
}
50+
catch
51+
{
52+
Log("Command failed.");
53+
54+
return false;
55+
}
56+
57+
try
58+
{
59+
var registerResult = await ReceiveMessage();
60+
61+
Log("Response received.");
62+
63+
if (registerResult?.Message == "success")
64+
{
65+
Log("Registering with DeepBot's API was successful.");
66+
67+
return true;
68+
}
69+
}
70+
catch
71+
{
72+
// ignored
73+
}
74+
75+
Log("Registering with DeepBot's API was not successful.");
76+
77+
return false;
78+
}
79+
80+
public async Task<List<UserResult>> Download()
81+
{
82+
var allUsers = new List<UserResult>();
83+
var currentOffset = 0;
84+
const int limit = 100;
85+
86+
var command = $"api|get_users|{currentOffset}|{limit}";
87+
88+
List<UserResult> users;
89+
do
90+
{
91+
users = await GetUsers(command);
92+
93+
allUsers.AddRange(users);
94+
95+
currentOffset += users.Count;
96+
} while(users.Any());
97+
98+
return allUsers;
99+
}
100+
101+
private async Task<List<UserResult>> GetUsers(string command)
102+
{
103+
try
104+
{
105+
Log($"Sending command `{command}`.");
106+
107+
await SendMessage(command);
108+
}
109+
catch
110+
{
111+
Log("Command failed.");
112+
113+
return new List<UserResult>();
114+
}
115+
116+
try
117+
{
118+
var result = await ReceiveMessage();
119+
120+
Log("Response received.");
121+
122+
return JsonConvert.DeserializeObject<List<UserResult>>(result.Message);
123+
}
124+
catch
125+
{
126+
Log("There was an error receiving the response.");
127+
128+
return new List<UserResult>();
129+
}
130+
}
131+
132+
private void Log(string message)
133+
{
134+
Console.WriteLine(message);
135+
}
136+
137+
private async Task SendMessage(string message)
138+
{
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());
147+
148+
await _socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
149+
}
150+
}
151+
152+
private async Task<CommandResult> ReceiveMessage()
153+
{
154+
using(var memoryStream = new MemoryStream())
155+
{
156+
var buffer = new ArraySegment<byte>(new byte[8192]);
157+
WebSocketReceiveResult result;
158+
do
159+
{
160+
result = await _socket.ReceiveAsync(buffer, CancellationToken.None);
161+
memoryStream.Write(buffer.Array, buffer.Offset, result.Count);
162+
} while(!result.EndOfMessage);
163+
164+
memoryStream.Seek(0, SeekOrigin.Begin);
165+
166+
if(result.MessageType != WebSocketMessageType.Text)
167+
{
168+
return null;
169+
}
170+
171+
using(var reader = new StreamReader(memoryStream, Encoding.UTF8))
172+
{
173+
var message = await reader.ReadToEndAsync();
174+
175+
return JsonConvert.DeserializeObject<CommandResult>(message);
176+
}
177+
}
178+
}
179+
180+
public void WriteResultsToFile(List<UserResult> results)
181+
{
182+
var ofd = new OpenFileDialog();
183+
if(ofd.ShowDialog() != DialogResult.OK)
184+
{
185+
return;
186+
}
187+
188+
var stringBuilder = new StringBuilder();
189+
stringBuilder.AppendLine("User,Points");
190+
191+
var text = results.Aggregate(stringBuilder, (builder, result) => builder.AppendLine($"{result.User},{result.Points}"), builder => builder.ToString());
192+
193+
File.WriteAllText(ofd.FileName, text);
194+
}
195+
}
196+
}

DeepBotPointFucker/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace DeepBotPointFucker
5+
{
6+
public class Program
7+
{
8+
private static void Main(string[] args)
9+
{
10+
var apiKey = "8E7DCJWLUcXSbUYLcIJQdSfOfDeDQccECITOa";
11+
12+
Task.Run(async () =>
13+
{
14+
var pointDownloader = new PointDownloader();
15+
16+
var result = await pointDownloader.Connect(apiKey);
17+
18+
if(result)
19+
{
20+
var results = await pointDownloader.Download();
21+
22+
pointDownloader.WriteResultsToFile(results);
23+
}
24+
25+
Console.WriteLine("Press any key to quit.");
26+
Console.ReadLine();
27+
}).Wait();
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DeepBotPointFucker")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("DeepBotPointFucker")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("0bbad9ab-ef35-4cbc-a586-ec600d46ea8d")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)