This repository was archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin_TwitchChat.as
373 lines (286 loc) · 9.67 KB
/
Plugin_TwitchChat.as
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
#name "Twitch Chat"
#author "Miss"
#category "Streaming"
#include "TwitchChat.as"
[Setting category="Twitch" name="Twitch OAuth Token" password description="You can generate an OAuth token from: https://twitchapps.com/tmi/"]
string Setting_TwitchToken;
[Setting category="Twitch" name="Twitch Nickname" description="Lowercase username of the account to connect."]
string Setting_TwitchNickname;
[Setting category="Twitch" name="Twitch Channel" description="Lowercase and including the # sign, for example: #missterious"]
string Setting_TwitchChannel;
[Setting category="Overlay" name="Enable chat overlay"]
bool Setting_ChatOverlay = true;
[Setting category="Overlay" name="Message width" min="100" max="1920"]
int Setting_ChatMessageWidth = 400;
[Setting category="Overlay" name="Chat position X" min="0" max="1"]
float Setting_ChatPosX = 0.5f;
[Setting category="Overlay" name="Chat position Y" min="0" max="1"]
float Setting_ChatPosY = 0.3f;
[Setting category="Overlay" name="Flip message order"]
bool Setting_ChatFlipMessageOrder = false;
[Setting category="Overlay" name="Messages disappear after a given time"]
bool Setting_MessageTimeToLive = true;
[Setting category="Overlay" name="Maximum number of messages"]
int Setting_MessageCountLimit = 20;
[Setting category="Overlay" name="Message time"]
int Setting_ChatMessageTime = 10000;
[Setting category="Overlay" name="Message bits threshold"]
int Setting_ChatMessageBitsThreshold = 100;
[Setting category="Overlay" name="Message bits time"]
int Setting_ChatMessageBitsTime = 25000;
[Setting category="Overlay" name="Message subscription time"]
int Setting_ChatMessageSubscriptionTime = 30000;
[Setting category="Overlay" name="Enable chat overlay message timer"]
bool Setting_ChatOverlayMessageTimer = true;
[Setting category="Commands" name="Enable !map command"]
bool Setting_MapCommand = true;
[Setting category="Commands" name="Enable !server command"]
bool Setting_ServerCommand = true;
[Setting category="Commands" name="Enable !join command"]
bool Setting_JoinCommand = true;
CTrackMania@ g_app;
class ChatMessage
{
uint64 m_startTime;
uint64 m_ttl;
string m_username;
string m_text;
vec4 m_color;
vec4 m_textColor = vec4(1, 1, 1, 1);
int m_bits;
uint64 get_Lifetime()
{
return Time::Now - m_startTime;
}
int64 get_TimeLeft()
{
return int64(m_ttl) - int64(Time::Now - m_startTime);
}
float get_TimeFactor()
{
return Math::Clamp(float(Time::Now - m_startTime) / float(m_ttl), 0.0f, 1.0f);
}
}
array<ChatMessage@> g_chatMessages;
enum MessageType
{
Chat,
Subscription
}
ChatMessage@ AddChatMessage(MessageType type)
{
auto newMessage = ChatMessage();
newMessage.m_startTime = Time::Now;
switch (type) {
case MessageType::Chat:
newMessage.m_ttl = Setting_ChatMessageTime;
break;
case MessageType::Subscription:
newMessage.m_ttl = Setting_ChatMessageSubscriptionTime;
newMessage.m_textColor = vec4(1, 0.3f, 1, 1);
break;
}
g_chatMessages.InsertLast(newMessage);
while (int(g_chatMessages.Length) > Setting_MessageCountLimit) {
g_chatMessages.RemoveAt(0);
}
return newMessage;
}
CGameCtnChallenge@ GetCurrentMap()
{
#if MP41
return g_app.RootMap;
#else
return g_app.Challenge;
#endif
}
string StripFormatCodes(string s)
{
return Regex::Replace(s, "\\$([0-9a-fA-F]{1,3}|[iIoOnNmMwWsSzZtTgG<>]|[lLhHpP](\\[[^\\]]+\\])?)", "");
}
class ChatCallbacks : Twitch::ICallbacks
{
void HandleCommand(ChatMessage@ msg)
{
if (msg.m_text == "!map" && Setting_MapCommand) {
auto currentMap = GetCurrentMap();
if (currentMap !is null) {
Twitch::SendMessage("The current map is: " + StripFormatCodes(currentMap.MapName) + " by " + StripFormatCodes(currentMap.AuthorNickName));
} else {
Twitch::SendMessage("Not currently playing on a map.");
}
} else if (msg.m_text == "!server" && Setting_ServerCommand) {
auto serverInfo = cast<CGameCtnNetServerInfo>(g_app.Network.ServerInfo);
if (serverInfo.ServerLogin != "") {
int numPlayers = g_app.ChatManagerScript.CurrentServerPlayerCount - 1;
int maxPlayers = g_app.ChatManagerScript.CurrentServerPlayerCountMax;
Twitch::SendMessage("Currently playing on \"" + StripFormatCodes(serverInfo.ServerName) + "\" (" + (numPlayers - 1) + " / " + maxPlayers + ", use !join to get the join link)");
} else {
Twitch::SendMessage("Not currently playing on a server.");
}
} else if (msg.m_text == "!join" && Setting_JoinCommand) {
auto serverInfo = cast<CGameCtnNetServerInfo>(g_app.Network.ServerInfo);
if (serverInfo.ServerLogin != "") {
auto title = g_app.LoadedManiaTitle;
string serverLink = "maniaplanet://#qjoin=" + serverInfo.ServerLogin + "@" + title.IdName;
Twitch::SendMessage("Join link: " + serverLink);
} else {
Twitch::SendMessage("Not currently playing on a server.");
}
}
}
void OnMessage(IRC::Message@ msg)
{
auto newMessage = AddChatMessage(MessageType::Chat);
newMessage.m_text = msg.m_params[1];
newMessage.m_username = msg.m_prefix.m_origin;
msg.m_tags.Get("display-name", newMessage.m_username);
string color;
msg.m_tags.Get("color", color);
if (color == "") {
color = "#5F9EA0";
}
newMessage.m_color = Text::ParseHexColor(color);
string valueBits;
if (msg.m_tags.Get("bits", valueBits)) {
print("Bits donated: " + valueBits);
newMessage.m_bits = Text::ParseInt(valueBits);
if (newMessage.m_bits > Setting_ChatMessageBitsThreshold) {
newMessage.m_ttl = Setting_ChatMessageBitsTime;
newMessage.m_textColor = vec4(1, 1, 0, 1);
}
}
if (newMessage.m_text.StartsWith("!")) {
HandleCommand(newMessage);
}
print("Twitch chat: " + newMessage.m_username + ": " + newMessage.m_text);
}
void OnUserNotice(IRC::Message@ msg)
{
string noticeType;
if (!msg.m_tags.Get("msg-id", noticeType)) {
return;
}
if (noticeType == "sub" || noticeType == "resub" || noticeType == "subgift" || noticeType == "anonsubgift") {
auto newMessage = AddChatMessage(MessageType::Subscription);
msg.m_tags.Get("message", newMessage.m_text);
if (newMessage.m_text == "") {
newMessage.m_text = "subscribed!";
}
msg.m_tags.Get("display-name", newMessage.m_username);
string color;
msg.m_tags.Get("color", color);
newMessage.m_color = Text::ParseHexColor(color);
print("New Twitch subscription from " + newMessage.m_username + "!");
}
}
}
ChatCallbacks@ g_chatCallbacks;
void Main()
{
@g_app = cast<CTrackMania>(GetApp());
if (Setting_TwitchToken == "") {
print("No Twitch token set. Set one in the settings and reload scripts!");
return;
}
if (Setting_TwitchNickname == "") {
print("No Twitch nickname set. Set one in the settings and reload scripts!");
return;
}
if (Setting_TwitchChannel == "") {
print("No Twitch channel set. Set one in the settings and reload scripts!");
return;
}
@g_chatCallbacks = ChatCallbacks();
print("Connecting to Twitch chat...");
if (!Twitch::Connect(g_chatCallbacks)) {
return;
}
print("Connected to Twitch chat!");
Twitch::Login(Setting_TwitchToken, Setting_TwitchNickname, Setting_TwitchChannel);
while (true) {
if (Setting_MessageTimeToLive) {
for (int i = int(g_chatMessages.Length) - 1; i >= 0; i--) {
auto msg = g_chatMessages[i];
if (msg.TimeLeft <= 0) {
g_chatMessages.RemoveAt(i);
}
}
}
Twitch::Update();
yield();
}
}
void Render()
{
if (!Setting_ChatOverlay) {
return;
}
int screenWidth = Draw::GetWidth();
int screenHeight = Draw::GetHeight();
int width = Setting_ChatMessageWidth;
int x = int(screenWidth * Setting_ChatPosX) - width / 2;
int y = int(screenHeight * Setting_ChatPosY);
const int boxPadding = 4;
const int linePadding = 4;
for (int i = int(g_chatMessages.Length) - 1; i >= 0; i--) {
auto msg = g_chatMessages[i];
vec4 textColor = msg.m_textColor;
vec2 textSizeUsername = Draw::MeasureString(msg.m_username);
float maxMessageWidth = width - boxPadding * 3 - textSizeUsername.x;
vec2 textSizeMessage = Draw::MeasureString(msg.m_text, null, 0.0f, maxMessageWidth);
Draw::FillRect(vec4(x, y, width, textSizeMessage.y + boxPadding * 2), vec4(0, 0, 0, 0.9f), 4.0f);
if (Setting_MessageTimeToLive && Setting_ChatOverlayMessageTimer) {
vec4 fillColor = msg.m_color;
fillColor.w = 0.3f;
Draw::FillRect(vec4(
x + boxPadding,
y + textSizeMessage.y + boxPadding * 1.5f,
(width - boxPadding * 2) * easeQuad(1.0f - msg.TimeFactor),
boxPadding / 2
), fillColor);
}
Draw::DrawString(vec2(x + boxPadding, y + boxPadding), msg.m_color, msg.m_username);
Draw::DrawString(vec2(x + boxPadding + textSizeUsername.x + boxPadding, y + boxPadding), textColor, msg.m_text, null, 0.0f, maxMessageWidth);
int yDelta = int(textSizeMessage.y) + boxPadding * 2 + linePadding;
if (Setting_ChatFlipMessageOrder) {
y += yDelta;
} else {
y -= yDelta;
}
}
}
void RenderSettings()
{
UI::Text("Test:");
if (UI::Button("Chat")) {
auto newMessage = AddChatMessage(MessageType::Chat);
newMessage.m_text = "This is a test message!";
newMessage.m_username = "Miss";
newMessage.m_color = vec4(1, 0.2f, 0.6f, 1);
}
UI::SameLine();
if (UI::Button("Bits")) {
auto newMessage = AddChatMessage(MessageType::Chat);
newMessage.m_bits = 2500;
newMessage.m_textColor = vec4(1, 1, 0, 1);
newMessage.m_text = "Have some bits!";
newMessage.m_username = "Miss";
newMessage.m_color = vec4(1, 0.2f, 0.6f, 1);
}
UI::SameLine();
if (UI::Button("Subscription")) {
auto newMessage = AddChatMessage(MessageType::Subscription);
newMessage.m_text = "subscribed!";
newMessage.m_username = "Miss";
newMessage.m_color = vec4(1, 0.2f, 0.6f, 1);
}
if (UI::Button("Clear messages")) {
g_chatMessages.RemoveRange(0, g_chatMessages.Length);
}
}
float easeQuad(float x)
{
if ((x /= 0.5) < 1) return 0.5 * x * x;
return -0.5 * ((--x) * (x - 2) - 1);
}