-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
136 lines (113 loc) · 5.02 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
using Discord;
using ScrapMechanic.Networking;
using ScrapMechanic.Util;
using ScrapMechanic.WebApi.Discord;
using Steamworks;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http.Headers;
using System.Xml.Linq;
namespace ScrapMechanic.WebApi
{
public class Program
{
private static readonly ulong[] PeopleToTrack = [
// 76561198422873503, // unknown
76561198000662213, // scrap man
76561198004277014, // kan
76561198079775050, // kosmo
76561197965646622, // moonbo
76561198014346778, // Durf (fuck you)
76561198018743729, // Fant (fuck you)
// 76561198299556567, // theguy920
];
private static readonly Dictionary<ulong, ulong> SteamidToDiscordid = new()
{
{ 76561198000662213, 185907632769466368 },
{ 76561198004277014, 162679241857564673 },
{ 76561198079775050, 239167036205301761 },
{ 76561197965646622, 143945560368480256 },
{ 76561198299556567, 333609235579404288 },
{ 76561198014346778, 189887027355844608 },
{ 76561198018743729, 318822174557339659 }
};
private static readonly DiscordWebhook _webhook = new(
File.ReadAllText(
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "webhook.url")
));
public static void Main(string[] args)
{
Environment.CurrentDirectory = Directory.GetParent(AppContext.BaseDirectory)!.FullName;
File.WriteAllText("steam_appid.txt", "387990");
var client = ScrapMechanic.Client.Load();
List<CSteamID> cstids = PeopleToTrack.Select(id => new CSteamID(id)).ToList();
ConcurrentDictionary<CSteamID, string> nameMaps = [];
string getNameString(CSteamID cstid)
=> SteamidToDiscordid.TryGetValue(cstid.m_SteamID, out ulong id) ? $"<@{id}> `{nameMaps[cstid]}`" : nameMaps[cstid];
void pushUpdate(CSteamID cstid, EPlayState isPlaying)
{
var name = nameMaps[cstid];
string discordid = getNameString(cstid);
string state = isPlaying == EPlayState.Playing
? "is now playing Scrap Mechanic!" : "stopped playing Scrap Mechanic :(";
ScrapMechanic.Logger.LogWarning($"Player '{name}' ({cstid}) {state}");
_webhook.SendMessage($"@everyone {discordid} {state}");
}
client.OnConnectionPlaystateChanged += pushUpdate;
client.OnConnectionStatusChanged += (cstid, _, info) =>
{
if (!info.IsConnectionAlive())
Task.Delay(30_000).ContinueWith(_ => client.ConnectToUserAsync(cstid));
if (info.m_eState == ESteamNetworkingConnectionState.k_ESteamNetworkingConnectionState_Connected)
Logger.LogWarning($"Fully Connected to {getNameString(cstid)}");
else
Logger.LogInfo($"[{getNameString(cstid)}] Connection State -> {info.m_eState}");
};
foreach (var cstid in cstids)
{
nameMaps[cstid] = GetName(cstid);
client.ConnectToUserAsync(cstid);
Logger.LogInfo($"Now tracking: {getNameString(cstid)}");
_webhook.SendMessage($"Now tracking: {getNameString(cstid)}");
}
Task.Delay(Timeout.Infinite).Wait();
/*
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
*/
}
static readonly HttpClient RequestClient = new()
{
DefaultRequestHeaders =
{
CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
MustRevalidate = true,
MaxStale = false,
NoTransform = true,
ProxyRevalidate = true,
MaxAge = TimeSpan.FromMilliseconds(10),
},
}
};
static string GetName(CSteamID stid) => WebUtility.HtmlDecode(GetProfile(stid).Element("steamID")!.Value);
static string NewUuid => Guid.NewGuid().ToString().Replace("-", string.Empty);
static XElement GetProfile(CSteamID stid)
{
string url = $"https://steamcommunity.com/profiles/{stid}/?xml=1";
string response = RequestClient.GetStringAsync(url).GetAwaiter().GetResult();
XDocument xDocument = XDocument.Parse(response);
return xDocument.Elements().First();
}
}
}