-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckpointTitler.as
118 lines (94 loc) · 3.93 KB
/
CheckpointTitler.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
// --- Settings
[Setting name="Enable" category="Main" description="Enable or disable. Also toggle from menu."]
bool Setting_Enable = true;
[Setting name="Title template" category="Main" description="This is what the title will be set to when checkpoint state changes. Use $cp to insert current checkpoint and $maxcp to insert the total checkpoints on the current map."]
string Setting_TitleTemplate = "Playing An RPG Map! Checkpoint $cp/$maxcp";
[Setting name="Set title command" category="Main" description="The command that is used to change your stream title. Default !title is what Nightbot uses."]
string Setting_TitleCommandBase = "!title";
[Setting name="Allow mods to set title template in chat with !checkpointtitle" category="Main" description="When active, mods can use the command !checkpointtitle <title template> to set the title template as above. Could have a small performance impact in high traffic chatrooms."]
bool Setting_ModsChangeTemplate = true;
// --- Structs
class TwitchBaseState {
int m_queueId = 0;
bool m_registered = false;
}
class CheckpointState {
uint m_currentCP = 0;
uint m_maxCP = 0;
}
// --- Globals
TwitchBaseState g_twitchBaseState = TwitchBaseState();
CheckpointState g_cpState = CheckpointState();
string g_channelName = "";
// --- OP Callbacks
void RenderMenu() {
if (UI::MenuItem("\\$09f" + Icons::Twitch + "\\$z Twitch Checkpoint Titler", "", Setting_Enable)) {
Setting_Enable = !Setting_Enable;
}
}
void Main() {
while (!Twitch::ChannelsJoined()) yield();
// TODO: maybe support more than one channel?
array<Twitch::ChannelState@> channels = Twitch::GetJoinedChannels();
g_channelName = channels[0].m_name;
while (true) {
if (Setting_Enable) {
RegisterTwitchBase();
if (Setting_ModsChangeTemplate) {
array<Twitch::Message@> newMessages = Twitch::Fetch(g_twitchBaseState.m_queueId);
for (uint i = 0; i < newMessages.Length; i++) {
HandleMessage(newMessages[i]);
}
}
if (CP::get_inGame()) {
CheckpointState newState = GetCPState();
if (newState.m_currentCP != g_cpState.m_currentCP || newState.m_maxCP != g_cpState.m_maxCP) {
g_cpState = newState;
SendTitleMsg();
}
}
}
yield();
}
}
void OnDisabled() {
UnregisterTwitchBase();
}
// --- Implementation
CheckpointState GetCPState() {
CheckpointState state = CheckpointState();
state.m_currentCP = CP::get_curCP();
state.m_maxCP = CP::get_maxCP();
return state;
}
void HandleMessage(const Twitch::Message@ &in msg) {
bool isMod = msg.m_tags.Exists("mod") && string(msg.m_tags["mod"]) == "1";
bool isBroadcaster = msg.m_tags.Exists("badges") && string(msg.m_tags["badges"]).Contains("broadcaster/");
if (isMod || isBroadcaster) {
bool isCmd = msg.m_text.StartsWith("!checkpointtitle ");
if (isCmd) {
string[]@ parts = msg.m_text.Split("!checkpointtitle ");
Setting_TitleTemplate = parts[1];
Twitch::SendMessage(g_channelName, "@" + msg.m_username + ": Updated checkpoint titler plugin template!");
}
}
}
void SendTitleMsg() {
string title = Setting_TitleTemplate;
title = title.Replace("$cp", Text::Format("%u", g_cpState.m_currentCP));
title = title.Replace("$maxcp", Text::Format("%u", g_cpState.m_maxCP));
string msg = Setting_TitleCommandBase + " " + title;
Twitch::SendMessage(g_channelName, msg);
}
void RegisterTwitchBase() {
if (!g_twitchBaseState.m_registered) {
g_twitchBaseState.m_queueId = Twitch::Register(Twitch::MessageType::ChatMessage);
g_twitchBaseState.m_registered = true;
}
}
void UnregisterTwitchBase() {
if (g_twitchBaseState.m_registered) {
Twitch::Unregister(g_twitchBaseState.m_queueId);
g_twitchBaseState.m_registered = false;
}
}