-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.as
More file actions
209 lines (168 loc) · 4.97 KB
/
Command.as
File metadata and controls
209 lines (168 loc) · 4.97 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
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
namespace Command {
int retryNb = 3;
void Run() {
auto currentMap = GetCurrentMap();
if (currentMap !is null) {
run(currentMap);
if (MapKarma::g_chatVoteEnabled) {
MapKarma::LoadVotes();
}
}
}
void run(CGameCtnChallenge@ currentMap) {
if (Context::Setting_CommandName == "") {
Context::Setting_CommandName = "!map";
}
string message = "!commands edit " + Context::Setting_CommandName + " " + GetMapName(currentMap);
if (Context::Setting_DisplayAuthorName) {
message = message + " by " + GetAuthor(currentMap);
}
if (Context::Setting_DisplayAuthorTime) {
message = message + " in " + GetAuthorTime(currentMap);
}
message = message + ".";
if (Context::Setting_DisplayMXLink) {
message = message + GetMapMXLinkMessage(currentMap);
}
if (Context::Setting_SendToTwitch) {
Twitch::SendMessage(message);
}
print(message);
Context::g_last_challenge_id = GetMapID(currentMap);
Context::g_last_challenge_name = GetMapName(currentMap);
UI::ShowNotification("Twitch command " + Context::Setting_CommandName + " updated", 5000);
}
void renderMenu() {
if (UI::MenuItem("Manually update command")) {
startnew(Run);
}
if (UI::MenuItem("Call command in chat")) {
if (Context::Setting_SendToTwitch) {
Twitch::SendMessage(Context::Setting_CommandName);
}
print(Context::Setting_CommandName);
}
}
// Retrieve the map ID from MX and format the url
// If the map has been updated on MX, it won't be found
string GetMapMXLinkMessage(CGameCtnChallenge@ challenge) {
Json::Value payload = GetMXPayload(GetMapID(challenge));
if (payload.GetType() != Json::Type::Array || payload.Length == 0) {
return "";
}
int trackId = payload[0]["TrackID"];
string result = " See https://tm.mania-exchange.com/tracks/" + trackId;
return result;
}
// Network
// Tweaked code from the tutorial
Json::Value GetMXPayload(string mapId) {
int retries = 0;
Net::Socket@ sock = Net::Socket();
if (!sock.Connect("api.mania-exchange.com", 80)) {
print("Couldn't initiate socket connection.");
return Json::Value();
}
print(Time::Now + " Connecting to host...");
retries = 0;
while (!sock.CanWrite() && retries++ < retryNb) {
sleep(500);
print("can't write");
}
print(Time::Now + " Connected! Sending request...");
if (!sock.WriteRaw(
"GET /tm/maps/" + mapId + " HTTP/1.1\r\n" +
"Host: api.mania-exchange.com\r\n" +
"User-agent: Plugin for TM\r\n" +
"Connection: close\r\n" +
"\r\n"
)) {
// If this fails, the socket might not be open. Something is wrong!
print("Couldn't send data.");
return Json::Value();
}
print(" Waiting for headers...");
// We are now ready to wait for the response. We'll need to note down
// the content length from the response headers as well.
int contentLength = 0;
while (true) {
retries = 0;
// If there is no data available yet, yield and wait.
while (sock.Available() == 0 && retries++ < retryNb) {
sleep(500);
continue;
}
if (retries >= retryNb) {
return Json::Value();
}
// There's buffered data! Try to get a line from the buffer.
string line;
if (!sock.ReadLine(line)) {
sleep(100);
continue;
}
// We got a line! Trim it, since ReadLine() returns the line including
// the newline characters.
line = line.Trim();
// Parse the header line.
auto parse = line.Split(":");
if (parse.Length == 2 && parse[0].ToLower() == "content-length") {
// If this is the content length, remember it.
contentLength = Text::ParseInt(parse[1].Trim());
}
// If the line is empty, we are done reading all headers.
if (line == "") {
break;
}
}
print(" Waiting for response...");
// At this point, we've parsed all the headers. We can now wait for the
// actual response body.
string response = "";
// While there is content to read from the body...
retries = 0;
while (contentLength > 0 && retries++ < retryNb) {
// Try to read up to contentLength.
string chunk = sock.ReadRaw(contentLength);
// Add the chunk to the response.
response += chunk;
// Subtract what we've read from the content length.
contentLength -= chunk.Length;
if (contentLength > 0) {
sleep(100);
continue;
}
while(sock.ReadRaw(256) != "") {
sleep(100);
continue;
}
}
if (retries >= retryNb) {
return Json::Value();
}
print("Got response");
sock.Close();
return Json::Parse(response);
}
// for debug purposes
void printJson(Json::Value titi) {
if (titi.GetType() == Json::Type::String) {
print("String");
} else
if (titi.GetType() == Json::Type::Number) {
print("Number");
} else
if (titi.GetType() == Json::Type::Object) {
print("Object");
} else
if (titi.GetType() == Json::Type::Array) {
print("Array");
} else
if (titi.GetType() == Json::Type::Boolean) {
print("Boolean");
} else
if (titi.GetType() == Json::Type::Null) {
print("Null");
}
}
}