-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
405 lines (336 loc) · 11 KB
/
Program.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using CSharpDiscordWebhook.NET.Discord;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SteamKit2;
using System.Drawing;
using System.Net;
namespace OuterWildsBranchWatcher;
public class BranchInfo
{
[JsonProperty("branchName")]
public string BranchName = "";
[JsonProperty("timeUpdated")]
public int TimeUpdated;
[JsonProperty("description")]
public string Description = "";
[JsonProperty("buildId")]
public int BuildId = -1;
[JsonProperty("pwdRequired")]
public int PwdRequired = 0;
}
public class PriceInfo
{
public int initialPrice = 0;
public int currentPrice = 0;
public int discountPercent = 0;
}
public class Program
{
const string BUILDID = "buildid";
const string DEPOTS = "depots";
const string BRANCHES = "branches";
const string TIMEUPDATED = "timeupdated";
const string PWDREQUIRED = "pwdrequired";
const string DESCRIPTION = "description";
const string COMMON = "common";
const string APP_NAME = "name";
public static void Main(params string[] args)
{
var user = args[0];
var pass = args[1];
var webhook = args[2];
var appid = uint.Parse(args[3]);
var steamClient = new SteamClient();
var manager = new CallbackManager(steamClient);
var steamUser = steamClient.GetHandler<SteamUser>();
var appHandler = steamClient.GetHandler<SteamApps>();
manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
manager.Subscribe<SteamApps.PICSProductInfoCallback>(OnPICSProductInfo);
var isRunning = true;
Console.WriteLine($"Trying to connect to Steam...");
steamClient.Connect();
while (isRunning)
{
manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
}
void OnConnected(SteamClient.ConnectedCallback callback)
{
Console.WriteLine($"Connected to Steam. Logging on...");
steamUser.LogOn(new SteamUser.LogOnDetails
{
Username = user,
Password = pass,
});
}
void OnDisconnected(SteamClient.DisconnectedCallback callback)
{
Console.WriteLine($"Disconnected from Steam.");
isRunning = false;
}
async void OnLoggedOn(SteamUser.LoggedOnCallback callback)
{
if (callback.Result != EResult.OK)
{
Console.WriteLine($"Failed to log into Steam. Result:{callback.Result} ExtendedResult:{callback.Result}");
isRunning = false;
return;
}
Console.WriteLine($"Logged into Steam.");
await appHandler.PICSGetProductInfo(new SteamApps.PICSRequest(appid), null, false);
}
void OnPICSProductInfo(SteamApps.PICSProductInfoCallback callback)
{
Console.WriteLine($"Recieved PICS data.");
var item = callback.Apps.Single();
var KeyValues = item.Value.KeyValues;
var depots = KeyValues[DEPOTS];
var branches = depots[BRANCHES];
var common = KeyValues[COMMON];
var appName = common[APP_NAME].Value;
var newBranchInfoArray = new BranchInfo[branches.Children.Count];
for (var i = 0; i < branches.Children.Count; i++)
{
var child = branches.Children[i];
var timeupdated = child[TIMEUPDATED];
newBranchInfoArray[i] = new BranchInfo() { BranchName = child.Name, TimeUpdated = int.Parse(timeupdated.Value), BuildId = int.Parse(child[BUILDID].Value)};
if (child[DESCRIPTION] != KeyValue.Invalid)
{
newBranchInfoArray[i].Description = child[DESCRIPTION].Value;
}
if (child[PWDREQUIRED] != KeyValue.Invalid)
{
newBranchInfoArray[i].PwdRequired = int.Parse(child[PWDREQUIRED].Value);
}
}
var newBranches = new List<BranchInfo>();
var deletedBranches = new List<BranchInfo>();
var updatedBranches = new List<BranchInfo>();
if (!File.Exists("branches.json"))
{
File.WriteAllText("branches.json", JsonConvert.SerializeObject(new BranchInfo[] {}));
}
var previous = JsonConvert.DeserializeObject<BranchInfo[]>(File.ReadAllText("branches.json"));
File.WriteAllText("branches.json", JsonConvert.SerializeObject(newBranchInfoArray));
foreach (var newBranchInfo in newBranchInfoArray)
{
var existingBranch = previous.FirstOrDefault(x => x.BranchName == newBranchInfo.BranchName);
if (existingBranch == default)
{
newBranches.Add(newBranchInfo);
}
else if (existingBranch.TimeUpdated != newBranchInfo.TimeUpdated)
{
updatedBranches.Add(newBranchInfo);
}
}
foreach (var oldBranch in previous)
{
if (!newBranchInfoArray.Any(x => x.BranchName == oldBranch.BranchName))
{
deletedBranches.Add(oldBranch);
}
}
// check for price update
var json = new WebClient().DownloadString($"https://store.steampowered.com/api/appdetails?appids={appid}&cc=us&filters=price_overview");
var jObject = JObject.Parse(json);
var priceOverview = jObject[$"{appid}"]["data"]["price_overview"];
var initialPrice = (int)priceOverview["initial"];
var currentPrice = (int)priceOverview["final"];
var discountPercent = (int)priceOverview["discount_percent"];
if (!File.Exists("price.json"))
{
File.WriteAllText("price.json", JsonConvert.SerializeObject(new PriceInfo()));
}
var oldPrice = JsonConvert.DeserializeObject<PriceInfo>(File.ReadAllText("price.json"));
var actualPriceHasChanged = initialPrice != oldPrice.initialPrice;
var isOnSale = currentPrice != oldPrice.currentPrice;
File.WriteAllText("price.json", JsonConvert.SerializeObject(new PriceInfo() { currentPrice = currentPrice, initialPrice = initialPrice, discountPercent = discountPercent}));
if (newBranches.Count > 0 || updatedBranches.Count > 0)
{
Console.WriteLine($"Found changes - {newBranches.Count} new branches, {deletedBranches.Count} deleted branches, {updatedBranches.Count} updated branches.");
var hook = new DiscordWebhook
{
Uri = new Uri(webhook)
};
var messageList = new List<DiscordMessage>();
messageList.Add(new DiscordMessage());
foreach (var newBranch in newBranches)
{
var embed = new DiscordEmbed
{
Title = "New Branch",
Color = new DiscordColor(Color.Green),
Description = $"The branch `{newBranch.BranchName}` was added at <t:{newBranch.TimeUpdated}:F>.",
Fields = new List<EmbedField>(),
Footer = new EmbedFooter() { Text = appName }
};
embed.Fields.Add(new EmbedField()
{
Name = "Name",
Value = newBranch.BranchName,
Inline = true
});
if (newBranch.Description != "")
{
embed.Fields.Add(new EmbedField()
{
Name = "Description",
Value = newBranch.Description,
Inline = true
});
}
embed.Fields.Add(new EmbedField()
{
Name = "Password Locked",
Value = newBranch.PwdRequired == 1 ? "Yes" : "No",
Inline = true
});
embed.Fields.Add(new EmbedField()
{
Name = "BuildId",
Value = newBranch.BuildId.ToString(),
Inline = true
});
if (messageList.Last().Embeds.Count >= 10)
{
messageList.Add(new DiscordMessage());
}
messageList.Last().Embeds.Add(embed);
}
foreach (var deletedBranch in deletedBranches)
{
var embed = new DiscordEmbed
{
Title = "Deleted Branch",
Color = new DiscordColor(Color.Red),
Description = $"The branch `{deletedBranch.BranchName}` was deleted.",
Fields = new List<EmbedField>(),
Footer = new EmbedFooter() { Text = appName }
};
if (messageList.Last().Embeds.Count >= 10)
{
messageList.Add(new DiscordMessage());
}
messageList.Last().Embeds.Add(embed);
}
foreach (var updatedBranch in updatedBranches)
{
var embed = new DiscordEmbed
{
Title = "Updated Branch",
Color = new DiscordColor(Color.Orange),
Description = $"The branch `{updatedBranch.BranchName}` was updated at <t:{updatedBranch.TimeUpdated}:F>.",
Fields = new List<EmbedField>(),
Footer = new EmbedFooter() { Text = appName }
};
embed.Fields.Add(new EmbedField()
{
Name = "Name",
Value = updatedBranch.BranchName,
Inline = true
});
if (updatedBranch.Description != "")
{
embed.Fields.Add(new EmbedField()
{
Name = "Description",
Value = updatedBranch.Description,
Inline = true
});
}
embed.Fields.Add(new EmbedField()
{
Name = "Password Locked",
Value = updatedBranch.PwdRequired == 1 ? "Yes" : "No",
Inline = true
});
embed.Fields.Add(new EmbedField()
{
Name = "BuildId",
Value = updatedBranch.BuildId.ToString(),
Inline = true
});
if (messageList.Last().Embeds.Count >= 10)
{
messageList.Add(new DiscordMessage());
}
messageList.Last().Embeds.Add(embed);
}
foreach (var message in messageList)
{
hook.SendAsync(message);
}
}
if (actualPriceHasChanged || isOnSale)
{
var hook = new DiscordWebhook
{
Uri = new Uri(webhook)
};
var message = new DiscordMessage();
if (actualPriceHasChanged)
{
var embed = new DiscordEmbed()
{
Title = "Price Change",
Color = new DiscordColor(Color.LightBlue),
Description = $"The base price has changed from ${oldPrice.initialPrice / 100f:F2} to ${initialPrice / 100f:F2}",
Footer = new EmbedFooter() { Text = appName }
};
message.Embeds.Add(embed);
}
else
{
if (oldPrice.discountPercent == 0)
{
var embed = new DiscordEmbed()
{
Title = "Sale Started!",
Color = new DiscordColor(Color.LightBlue),
Description = $"A sale has started! From ${initialPrice / 100f:F2} to ${currentPrice / 100f:F2} ({discountPercent}% off).",
Footer = new EmbedFooter() { Text = appName }
};
message.Embeds.Add(embed);
}
else if (currentPrice < oldPrice.currentPrice)
{
var embed = new DiscordEmbed()
{
Title = "Sale Update",
Color = new DiscordColor(Color.LightBlue),
Description = $"The sale has increased! From ${oldPrice.currentPrice / 100f:F2} ({oldPrice.discountPercent}% off) to ${currentPrice / 100f:F2} ({discountPercent}% off).",
Footer = new EmbedFooter() { Text = appName }
};
message.Embeds.Add(embed);
}
else if (currentPrice == oldPrice.initialPrice)
{
var embed = new DiscordEmbed()
{
Title = "Sale Ended",
Color = new DiscordColor(Color.LightBlue),
Description = $"The sale has ended. Back to ${initialPrice / 100f:F2}.",
Footer = new EmbedFooter() { Text = appName }
};
message.Embeds.Add(embed);
}
else if (currentPrice > oldPrice.currentPrice)
{
var embed = new DiscordEmbed()
{
Title = "Sale Update",
Color = new DiscordColor(Color.LightBlue),
Description = $"The sale has decreased. From ${oldPrice.currentPrice / 100f:F2} ({oldPrice.discountPercent}% off) to ${currentPrice / 100f:F2} ({discountPercent}% off).",
Footer = new EmbedFooter() { Text = appName }
};
message.Embeds.Add(embed);
}
}
hook.SendAsync(message);
}
steamUser.LogOff();
}
}
}