-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketAPIHandler.cs
More file actions
163 lines (145 loc) · 6.44 KB
/
SocketAPIHandler.cs
File metadata and controls
163 lines (145 loc) · 6.44 KB
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
using SocketIOClient;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.IO;
namespace TwitchFlashbang
{
public class SocketAPIHandler
{
public static SocketIO sio;
public static async Task startConnection()
{
// Create the socket object.
var token = MainWindow.socketAPIToken;
sio = new SocketIO($"https://sockets.streamlabs.com?token={token}", new SocketIOOptions()
{
Transport = SocketIOClient.Transport.TransportProtocol.WebSocket,
Query = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("token", token )
},
EIO = 3
});
sio.OnConnected += async (s, e) =>
{
MessageBox.Show("Successfully connected to streamlabs.", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
};
sio.On("event", data =>
{
JsonDocument eventData = data.GetValue<JsonDocument>();
var parsedData = ToJsonString(eventData);
var OvverideType = eventData.RootElement.GetProperty("type");
string type = String.Empty;
int? donationAmount = 0;
Root myDeserializedClass = new Root();
// Dear god
if (OvverideType.ToString() == "donation" || OvverideType.ToString() == "follow" || OvverideType.ToString() == "resub" || OvverideType.ToString() == "subscription")
{
Trace.WriteLine(parsedData);
myDeserializedClass = JsonSerializer.Deserialize<Root>(parsedData);
donationAmount = myDeserializedClass.message[0].amount;
type = myDeserializedClass.type;
if (MainWindow.setTriggerCommand != "None")
{
Trace.WriteLine("Yes message");
if (myDeserializedClass.message[0].message.Contains(MainWindow.setTriggerCommand))
{
if (type == "donation" && (MainWindow.invokeOnDonate ?? false))
{
if (donationAmount != null && donationAmount >= MainWindow.minimumDonationAmount)
{
waitForMessage((MainWindow.waitForMessageEndBool ?? false));
}
}
else if (type == "follow" && (MainWindow.invokeOnFollow ?? false))
{
waitForMessage((MainWindow.waitForMessageEndBool ?? false));
}
else if ((type == "resub" || type == "subscription") && (MainWindow.invokeOnSubscription ?? false))
{
waitForMessage((MainWindow.waitForMessageEndBool ?? false));
}
}
}
else
{
if (type == "donation" && (MainWindow.invokeOnDonate ?? false))
{
if (donationAmount != null && donationAmount >= MainWindow.minimumDonationAmount)
{
waitForMessage((MainWindow.waitForMessageEndBool ?? false));
}
}
else if (type == "follow" && (MainWindow.invokeOnFollow ?? false))
{
waitForMessage((MainWindow.waitForMessageEndBool ?? false));
}
else if ((type == "resub" || type == "subscription") && (MainWindow.invokeOnSubscription ?? false))
{
waitForMessage((MainWindow.waitForMessageEndBool ?? false));
}
}
}
});
await sio.ConnectAsync();
async Task waitForMessage(bool shouldWait)
{
Trace.WriteLine("Waiting for message");
if (shouldWait)
{
await Task.Delay(7000);
MainWindow.gameOverlay.CSGOflash();
}
else
{
Trace.WriteLine("Flashbang");
MainWindow.gameOverlay.CSGOflash();
}
}
static string ToJsonString(JsonDocument jdoc)
{
using (var stream = new MemoryStream())
{
Utf8JsonWriter writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
jdoc.WriteTo(writer);
writer.Flush();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
}
public class Message
{
public int id { get; set; } = 0;
public string? name { get; set; } = string.Empty;
public int? amount { get; set; } = 0;
public string? formatted_amount { get; set; } = string.Empty;
public string? formattedAmount { get; set; } = string.Empty;
public string? message { get; set; } = string.Empty;
public string? currency { get; set; } = string.Empty;
public object emotes { get; set; } = string.Empty;
public string? iconClassName { get; set; } = string.Empty;
public To? to { get; set; }
public string? from { get; set; } = string.Empty;
public object from_user_id { get; set; } = string.Empty;
public string? _id { get; set; } = string.Empty;
public string? created_at { get; set; } = string.Empty;
}
public class Root
{
public string? type { get; set; } = string.Empty;
public List<Message>? message { get; set; }
public string? event_id { get; set; } = string.Empty;
public string? @for { get; set; } = string.Empty;
}
public class To
{
public string? name { get; set; } = string.Empty;
}
}
}