This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathLogicHandler.cs
291 lines (233 loc) · 10.3 KB
/
LogicHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text.RegularExpressions;
namespace ValorantStreamOverlay
{
class LogicHandler
{
public static string AccessToken { get; set; }
public static string EntitlementToken { get; set; }
public static string UserID { get; set; }
public static string username, password, region;
public static int refreshTimeinSeconds;
public Timer relogTimer, pointTimer;
public static ValorantOverStream ValorantOver;
public LogicHandler logic;
public RankDetection rankDetect;
//Twitch Bot Variables
public static int currentRankPoints, currentMMRorELO;
private bool botEnabled;
public LogicHandler(ValorantOverStream instance)
{
logic = this;
ValorantOver = instance;
Trace.Write("Reading Settings");
ReadSettings();
}
void ReadSettings()
{
if (string.IsNullOrEmpty(Properties.Settings.Default.password) || string.IsNullOrEmpty(Properties.Settings.Default.username))
MessageBox.Show("Welcome, You have to set your username and password in the settings menu");
else
{
username = Properties.Settings.Default.username;
password = Properties.Settings.Default.password;
region = new SettingsParser().ReadRegion(Properties.Settings.Default.region).GetAwaiter().GetResult();
refreshTimeinSeconds = new SettingsParser().ReadDelay(Properties.Settings.Default.region).GetAwaiter().GetResult();
new SettingsParser().ReadSkin(Properties.Settings.Default.skin).GetAwaiter();
botEnabled = new SettingsParser().ReadTwitchBot().GetAwaiter().GetResult();
RiotGamesLogin();
UpdateToLatestGames();
new RankDetection();
StartPointRefresh();
StartRELOGTimer();
StartTwitchBot();
}
}
void RiotGamesLogin()
{
try
{
CookieContainer cookie = new CookieContainer();
Authentication.GetAuthorization(cookie);
var authJson = JsonConvert.DeserializeObject(Authentication.Authenticate(cookie, username, password));
JToken authObj = JObject.FromObject(authJson);
if (authObj.ToString().Contains("error"))
{
// error time lmfao
MessageBox.Show("Login is Incorrect, please fix login in settings.");
}
else
{
string authURL = authObj["response"]["parameters"]["uri"].Value<string>();
var access_tokenVar = Regex.Match(authURL, @"access_token=(.+?)&scope=").Groups[1].Value;
AccessToken = $"{access_tokenVar}";
RestClient client = new RestClient(new Uri("https://entitlements.auth.riotgames.com/api/token/v1"));
RestRequest request = new RestRequest(Method.POST);
request.AddHeader("Authorization", $"Bearer {AccessToken}");
request.AddJsonBody("{}");
string response = client.Execute(request).Content;
var entitlement_token = JsonConvert.DeserializeObject(response);
JToken entitlement_tokenObj = JObject.FromObject(entitlement_token);
EntitlementToken = entitlement_tokenObj["entitlements_token"].Value<string>();
RestClient userid_client = new RestClient(new Uri("https://auth.riotgames.com/userinfo"));
RestRequest userid_request = new RestRequest(Method.POST);
userid_request.AddHeader("Authorization", $"Bearer {AccessToken}");
userid_request.AddJsonBody("{}");
string userid_response = userid_client.Execute(userid_request).Content;
dynamic userid = JsonConvert.DeserializeObject(userid_response);
JToken useridObj = JObject.FromObject(userid);
//Console.WriteLine(userid_response);
UserID = useridObj["sub"].Value<string>();
}
}
catch (Exception e)
{
MessageBox.Show("Your Login was invalid, please check your settings.");
}
}
async Task UpdateToLatestGames()
{
Trace.Write("UPDATING");
dynamic response = GetCompApiAsync().GetAwaiter().GetResult();
if (response != null)
{
int[] points = new int[3];
dynamic matches = response["Matches"];
int count = 0, i = 0;
foreach (var game in matches)
{
if (game["TierAfterUpdate"] == 0)
{
// riot said fuck off to this one i guess LMAO
}
else if (game["TierAfterUpdate"] > game["TierBeforeUpdate"]) // Promoted meaning, that afterupdate is more than beforeupdate
{
// player promoted
int before = game["RankedRatingBeforeUpdate"];
int after = game["RankedRatingAfterUpdate"];
int differ = (after - before) + 100;
points[i++] = differ;
count++;
}
else if (game["TierAfterUpdate"] < game["TierBeforeUpdate"])
{
// player demoted
int before = game["RankedRatingBeforeUpdate"];
int after = game["RankedRatingAfterUpdate"];
int differ = (after - before) - 100;
points[i++] = differ;
count++;
}
else
{
int before = game["RankedRatingBeforeUpdate"];
int after = game["RankedRatingAfterUpdate"];
points[i++] = after - before;
count++;
}
if (count >= 3) // 3 recent matches found
break;
}
//Send Points to Function that changes the UI
SetChangesToOverlay(points).GetAwaiter();
}
}
private async Task<JObject> GetCompApiAsync()
{
IRestClient compClient = new RestClient(new Uri($"https://pd.{region}.a.pvp.net/mmr/v1/players/{UserID}/competitiveupdates?startIndex=0&endIndex=20"));
RestRequest compRequest = new RestRequest(Method.GET);
compRequest.AddHeader("Authorization", $"Bearer {AccessToken}");
compRequest.AddHeader("X-Riot-Entitlements-JWT", EntitlementToken);
compRequest.AddHeader("X-Riot-ClientPlatform",
"ew0KCSJwbGF0Zm9ybVR5cGUiOiAiUEMiLA0KCSJwbGF0Zm9ybU9TIjogIldpbmRvd3MiLA0KCSJwbGF0Zm9ybU9TVmVyc2lvbiI6ICIxMC4wLjE5MDQyLjEuMjU2LjY0Yml0IiwNCgkicGxhdGZvcm1DaGlwc2V0IjogIlVua25vd24iDQp9");
IRestResponse rankedResp = compClient.Get(compRequest);
return rankedResp.IsSuccessful ? JsonConvert.DeserializeObject<JObject>(rankedResp.Content) : null;
}
private async Task SetChangesToOverlay(int[] pointchange)
{
Label[] rankChanges = { ValorantOver.recentGame1, ValorantOver.recentGame2, ValorantOver.recentGame3 };
for (int i = 0; i < pointchange.Length; i++)
{
// neg num represents decrease in pts
if (pointchange[i] < 0)
{
//In the case of a demotion or a loss
pointchange[i] *= -1;
string change;
change = pointchange[i] <= 9 ? $"0{pointchange[i]}" : pointchange[i].ToString();
rankChanges[i].ForeColor = Color.Red;
rankChanges[i].Text = $"-{change}";
}
else if (pointchange[i] > 0)
{
//int checker = pointchange[i] * -1;
string change;
change = pointchange[i] <= 9 ? $"0{pointchange[i]}" : pointchange[i].ToString();
rankChanges[i].ForeColor = Color.LimeGreen;
rankChanges[i].Text = $"+{change}";
}
else
{
rankChanges[i].ForeColor = Color.SlateGray;
rankChanges[i].Text = "0";
}
}
}
private void StartPointRefresh()
{
pointTimer = new Timer();
pointTimer.Tick += new EventHandler(pointTimer_Tick);
pointTimer.Interval = refreshTimeinSeconds * 1000;
pointTimer.Start();
}
private void pointTimer_Tick(object sender, EventArgs e)
{
UpdateToLatestGames().GetAwaiter();
if (rankDetect == null)
{
rankDetect = new RankDetection();
}
else
{
rankDetect.UpdateRank();
}
}
private void StartRELOGTimer()
{
relogTimer = new Timer();
relogTimer.Tick += new EventHandler(relogTimer_Tick);
relogTimer.Interval = 2700 * 1000;
relogTimer.Start();
}
private void relogTimer_Tick(object sender, EventArgs e)
{
pointTimer.Stop();
RiotGamesLogin();
pointTimer.Start();
}
public void StartTwitchBot()
{
if (botEnabled)
{
Trace.WriteLine("Bot enabled");
TwitchIntegration bot = new TwitchIntegration();
}
else
{
Trace.WriteLine("Bot not enabled");
}
}
}
}