-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandManager.cs
333 lines (286 loc) · 13.5 KB
/
CommandManager.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
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
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using TwitchLib.Api.Helix.Models.Charity;
using TwitchLib.Client;
using TwitchLib.Client.Events;
using TwitchLib.Client.Models;
using static CobaltChatCore.Configuration;
namespace CobaltChatCore
{
public class CommandManager
{
public class TwitchCommand
{
public TwitchCommand(Action<OnMessageReceivedArgs> value) : this(AccessLevel.EVERYONE, value)
{
}
public TwitchCommand(AccessLevel a, Action<OnMessageReceivedArgs> value)
{
this.action = value;
this.AllowedLevel = a;
}
public enum AccessLevel { EVERYONE = 0, VIP, MOD, STREAMER, SPECIAL }
public AccessLevel AllowedLevel { get; set; }
public Action<OnMessageReceivedArgs> action { private get; set; }
public void Invoke(OnMessageReceivedArgs e)
{
bool allowed = false;
if (AllowedLevel == AccessLevel.SPECIAL && Configuration.SpecialPeople.Contains(e.ChatMessage.DisplayName))
allowed = true;
else
{
int level = e.ChatMessage.IsBroadcaster ? (int)AccessLevel.STREAMER : e.ChatMessage.IsVip ? (int)AccessLevel.VIP : e.ChatMessage.IsModerator ? (int)AccessLevel.MOD : (int)AccessLevel.EVERYONE;
allowed = level >= (int)AllowedLevel;
}
if (allowed)
action?.Invoke(e);
else
TwitchChat.SendMessageToChat(Configuration.Instance.InsufficientPrivledgeText);
}
}
public static Dictionary<string, TwitchCommand> commands = new Dictionary<string, TwitchCommand>();
ConcurrentQueue<Action<Combat>> combatActions = new ConcurrentQueue<Action<Combat>>();
/// <summary>
/// displayname -> number of times they were picked;
/// </summary>
public static Dictionary<string, int> ChattersAvailable = new Dictionary<string, int>();
public static Dictionary<string, Color> ChatterColors = new Dictionary<string, Color>();
List<string> bannedChatters = new List<string>();
public bool queueOpen = true;
bool inCombat = false;
ILogger logger;
public CommandManager()
{
AddCommands();
}
public void Setup(ILogger log)
{
logger = log;
TwitchChat.Client.OnMessageReceived += OnChatMessageReceived;
CobaltChatCoreManifest.EventHub.ConnectToEvent<string>(CobaltChatCoreManifest.SelectEnemyChatterEvent, (st) => EnemyChatterSelected(st));
CobaltChatCoreManifest.EventHub.ConnectToEvent<string>(CobaltChatCoreManifest.EnterRouteEvent, (s) => TryEndCombat(s));
CobaltChatCoreManifest.EventHub.ConnectToEvent<Combat>(CobaltChatCoreManifest.UpdateCombatEvent, (c) => OnCombatUpdate(c));
}
void EnemyChatterSelected(string user)
{
ChattersAvailable[user] += 1;
inCombat = true;
}
public void OnChatMessageReceived(object sender, OnMessageReceivedArgs e)
{
string[] parts = e.ChatMessage.Message.Split(" ");
string command = parts[0];
if (command.StartsWith(Configuration.Instance.CommandSignal) && commands.TryGetValue(command.Substring(1), out TwitchCommand action))
action.Invoke(e);
}
public void TryEndCombat(string type)
{
//don't end combat if we aren't in combat and we have a lined up chatter
if (!inCombat && type == "Combat")
return;
inCombat = false;
logger.LogInformation("Exited Combat");
//roll for another chatter ahead of time
combatActions.Clear();
}
void AddCommands()
{
if (Configuration.Instance.JoinCommandEnabled || Configuration.Instance.AllowChattersAsDrones || Configuration.Instance.AllowChatterDroneEnemies)
commands.Add(Configuration.Instance.JoinCommand, new TwitchCommand((e) => {
if (!queueOpen)
{
TwitchChat.SendMessageToChat(Configuration.Instance.JoinFailedQueueClosedText);
return;
}
if (ChattersAvailable.ContainsKey(e.ChatMessage.DisplayName))
{
TwitchChat.SendMessageToChat(Configuration.Instance.JoinFailedAlreadyQueuedText);
return;
}
if (bannedChatters.Contains(e.ChatMessage.DisplayName))
{
TwitchChat.SendMessageToChat(Configuration.Instance.JoinFailedBannedText);
return;
}
logger.LogInformation($"{e.ChatMessage.DisplayName} joined the fight!");
if (ChattersAvailable.Count > 0)
{
int lowestNumber = ChattersAvailable.Where(kvp => kvp.Value < Configuration.Instance.ChatterPickLimit).OrderBy(kvp => kvp.Value).First().Value;
lowestNumber = lowestNumber == 0 ? 0 : lowestNumber - 1;//guarantee first spawn
ChattersAvailable.Add(e.ChatMessage.DisplayName, lowestNumber);//make sure the player isn't favored over and over
}
else
ChattersAvailable.Add(e.ChatMessage.DisplayName, 0);//make sure the player isn't favored over and over
try
{
//RFT bug, seems sometimes there aren't colors?
ChatterColors[e.ChatMessage.DisplayName] = new Color(e.ChatMessage.ColorHex.Substring(1));
}catch(Exception ex)
{
logger.LogError($"Missing color! It says its {e.ChatMessage.ColorHex}", ex);
ChatterColors[e.ChatMessage.DisplayName] = new Color(1,1,1);
}
CobaltChatCoreManifest.EventHub.SignalEvent(CobaltChatCoreManifest.ChatterJoinsEvent, e.ChatMessage);
TwitchChat.SendMessageToChat(Configuration.Instance.ChatterJoinedText.Replace("{User}", e.ChatMessage.DisplayName));
}));
commands.Add(Configuration.Instance.OpenQueueCommand, new TwitchCommand(TwitchCommand.AccessLevel.MOD, (e) =>
{
queueOpen = true;
TwitchChat.SendMessageToChat(Configuration.Instance.QueueOpenText);
}));
commands.Add(Configuration.Instance.CloseQueueCommand, new TwitchCommand(TwitchCommand.AccessLevel.MOD, (e) =>
{
queueOpen = false;
TwitchChat.SendMessageToChat(Configuration.Instance.QueueClosedText);
}));
commands.Add(Configuration.Instance.ChatterListClearCommand, new TwitchCommand(TwitchCommand.AccessLevel.MOD, (e) =>
{
ChattersAvailable.Clear();
CobaltChatCoreManifest.EventHub.SignalEvent(CobaltChatCoreManifest.ClearChattersEvent, e.ChatMessage.DisplayName);
TwitchChat.SendMessageToChat(Configuration.Instance.QueueClearedText);
}));
commands.Add(Configuration.Instance.ChatterEjectCommand, new TwitchCommand(TwitchCommand.AccessLevel.MOD, (e) =>
{
var list = e.ChatMessage.Message.Split(" ");
string name = null;
if (list.Length > 1 && !string.IsNullOrEmpty(list[1]))
{
name = TryFindChatterName(list[1]);
}
CobaltChatCoreManifest.EventHub.SignalEvent(CobaltChatCoreManifest.ChatterEjectedEvent, name);
}));
commands.Add(Configuration.Instance.ChatterBanCommand, new TwitchCommand(TwitchCommand.AccessLevel.MOD, (e) =>
{
var list = e.ChatMessage.Message.Split(" ");
if (list.Length > 1 && !string.IsNullOrEmpty(list[1]))
{
var name = TryFindChatterName(list[1]);
if (name != null) {
ChattersAvailable.Remove(name);
ChatterColors.Remove(name);
CobaltChatCoreManifest.EventHub.SignalEvent(CobaltChatCoreManifest.ChatterEjectedEvent, name);
if (!bannedChatters.Contains(name))
bannedChatters.Add(name);
TwitchChat.SendMessageToChat(Configuration.Instance.ChatterBanText.Replace("{User}", name));
}
}
}));
commands.Add(Configuration.Instance.ChatterUnbanCommand, new TwitchCommand(TwitchCommand.AccessLevel.MOD, (e) =>
{
var list = e.ChatMessage.Message.Split(" ");
if (list.Length > 1 && !string.IsNullOrEmpty(list[1]))
{
var name = TryFindChatterName(list[1], bannedChatters);
if (name != null) {
bannedChatters.Remove(name);
TwitchChat.SendMessageToChat(Configuration.Instance.ChatterUnbanText.Replace("{User}", name));
}
}
}));
commands.Add(Configuration.Instance.CardGiveCommand, new TwitchCommand(TwitchCommand.AccessLevel.SPECIAL, (e) =>
{
Card card = null;
string cardName = e.ChatMessage.Message.Substring(Configuration.Instance.CardGiveCommand.Length + 1).Trim().ToLower();
try {
card = TryMakeCard(cardName);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed spawning card!");
}
if (card != null)
combatActions.Enqueue((c) =>
{
try
{
card.singleUseOverride = true;
card.temporaryOverride = true;
var addAction = new AAddCard()
{
card = card,
destination = CardDestination.Hand,
amount = 1
};
c.QueueImmediate(addAction);
}catch(Exception ex)
{
logger.LogError(ex, "Failed inserting card into hand");
}
});
//silent fail, this is a hidden function after all
}));
commands.Add("forceAdd", new TwitchCommand(TwitchCommand.AccessLevel.SPECIAL, (e) =>
{
Random rnd = new Random();
string name = rnd.Next().ToString();
ChattersAvailable[name] = 0;//make sure the player isn't favored over and over
ChatterColors[name] = new Color(1, 1, 1);
}));
}
public static string TryFindChatterName(string clue)
{
return TryFindChatterName(clue, ChattersAvailable.Keys.ToList());
}
public static string TryFindChatterName(string clue, List<string> list )
{
var name = clue.StartsWith("@") ? clue.Substring(1) : clue;
foreach (string user in list)
{
if (user.ToLower() == name.ToLower())
{
return user;
}
}
return null;
}
Card TryMakeCard(string cardName)
{
string upgrade = null;
if (cardName.EndsWith(" a"))
{
upgrade = "a";
cardName = cardName[..^2];//.Substring(0, cardName.Length - 2)
}
if (cardName.EndsWith(" b"))
{
upgrade = "b";
cardName = cardName[..^2];
}
Card card = null;
logger.LogInformation("Looking for card " + cardName);
foreach (string name in DB.currentLocale.strings.Keys)
{
if (DB.currentLocale.strings[name].ToLower() == cardName && name.StartsWith("card."))
{
int i = name.IndexOf(".")+1;
string key = "";
if (name.EndsWith(".name"))
key = name.Substring(i, name.LastIndexOf(".") - i);
else
key = name.Substring(i);
logger.LogInformation("Trying to make card " + key);
if (DB.cards.ContainsKey(key))
{
card = (Card?)Activator.CreateInstance(DB.cards[key]);
if (upgrade == "a")
card.upgrade = Upgrade.A;
if (upgrade == "b")
card.upgrade = Upgrade.B;
}
return card;
}
}
return card;
}
void OnCombatUpdate(Combat combat)
{
if (combatActions.TryDequeue(out Action<Combat> action))
{
action.Invoke(combat);
}
}
}
}