Skip to content

Commit 39c3209

Browse files
committed
Fixed the multiple files soundfx command
1 parent 9170481 commit 39c3209

File tree

2 files changed

+103
-94
lines changed

2 files changed

+103
-94
lines changed
Lines changed: 102 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Security.Cryptography;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Fritz.StreamLib.Core;
@@ -11,25 +12,25 @@
1112

1213
namespace Fritz.Chatbot.Commands
1314
{
14-
public class SoundFxCommand : IExtendedCommand
15-
{
16-
17-
public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext, IOptions<Dictionary<string, SoundFxDefinition>> soundEffects)
15+
public class SoundFxCommand : IExtendedCommand
1816
{
19-
this.HubContext = hubContext;
2017

21-
Effects = soundEffects.Value;
22-
}
18+
public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext, IOptions<Dictionary<string, SoundFxDefinition>> soundEffects)
19+
{
20+
this.HubContext = hubContext;
21+
22+
Effects = soundEffects.Value;
23+
}
2324

24-
public IHubContext<AttentionHub, IAttentionHubClient> HubContext { get; }
25+
public IHubContext<AttentionHub, IAttentionHubClient> HubContext { get; }
2526

26-
public string Name => "SoundFxCommand";
27-
public string Description => "Play a fun sound effect in the stream";
28-
public int Order => 1;
29-
public bool Final => true;
30-
public TimeSpan? Cooldown => TimeSpan.FromSeconds(0);
27+
public string Name => "SoundFxCommand";
28+
public string Description => "Play a fun sound effect in the stream";
29+
public int Order => 1;
30+
public bool Final => true;
31+
public TimeSpan? Cooldown => TimeSpan.FromSeconds(0);
3132

32-
internal static Dictionary<string, SoundFxDefinition> Effects = new Dictionary<string, SoundFxDefinition>();
33+
internal static Dictionary<string, SoundFxDefinition> Effects = new Dictionary<string, SoundFxDefinition>();
3334
/*
3435
{
3536
{ "ohmy", ("Oh my... something strange is happening", "ohmy.mp3", TimeSpan.FromSeconds(30) ) },
@@ -39,126 +40,134 @@ public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext,
3940
};
4041
*/
4142

42-
private static readonly List<string> AndThens = new List<string>();
43+
private static readonly Dictionary<string, List<string>> MultipleFileTriggers = new Dictionary<string, List<string>>();
4344

44-
private static readonly Dictionary<string, DateTime> SoundCooldowns = new Dictionary<string, DateTime>();
45+
private static readonly Dictionary<string, DateTime> SoundCooldowns = new Dictionary<string, DateTime>();
4546

46-
public bool CanExecute(string userName, string fullCommandText)
47-
{
47+
public bool CanExecute(string userName, string fullCommandText)
48+
{
4849

49-
if (!fullCommandText.StartsWith("!")) return false;
50-
var cmd = fullCommandText.Substring(1).ToLowerInvariant();
51-
return Effects.ContainsKey(cmd);
50+
if (!fullCommandText.StartsWith("!")) return false;
51+
var cmd = fullCommandText.Substring(1).ToLowerInvariant();
52+
return Effects.ContainsKey(cmd);
5253

53-
}
54+
}
5455

55-
public Task Execute(IChatService chatService, string userName, string fullCommandText)
56-
{
57-
58-
var cmdText = fullCommandText.Substring(1).ToLowerInvariant();
56+
public Task Execute(IChatService chatService, string userName, string fullCommandText)
57+
{
5958

60-
if (!InternalCooldownCheck()) return Task.CompletedTask;
59+
var cmdText = fullCommandText.Substring(1).ToLowerInvariant();
60+
var cmd = Effects[cmdText];
6161

62-
var cmd = Effects[cmdText];
62+
if (!InternalCooldownCheck()) return Task.CompletedTask;
6363

64-
SoundCooldowns[cmdText] = (cmdText == "andthen" ? CalculateAndThenCooldownTime() : DateTime.Now);
64+
SoundCooldowns[cmdText] = (cmd.Files != null ? CalculateMultipleFileCooldownTime(cmd, cmdText) : DateTime.Now);
6565

66-
var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename() : cmd.File;
6766

68-
var soundTask = this.HubContext.Clients.All.PlaySoundEffect(fileToPlay);
69-
var textTask = chatService.SendMessageAsync($"@{userName} - {cmd.Response}");
67+
var fileToPlay = cmd.Files != null ? IdentifyMultipleEffectsFilename(cmd, cmdText) : cmd.File;
7068

71-
return Task.WhenAll(soundTask, textTask);
69+
var soundTask = this.HubContext.Clients.All.PlaySoundEffect(fileToPlay);
70+
var textTask = chatService.SendMessageAsync($"@{userName} - {cmd.Response}");
7271

73-
bool InternalCooldownCheck()
74-
{
72+
return Task.WhenAll(soundTask, textTask);
7573

76-
if (cmdText == "andthen")
74+
bool InternalCooldownCheck()
7775
{
78-
if (!CheckAndThenCooldown())
76+
77+
if (cmdText == "andthen")
7978
{
80-
chatService.SendMessageAsync($"@{userName} - No AND THEN!");
81-
return false;
82-
}
79+
if (!CheckMultipleFilesCooldown(cmd, cmdText))
80+
{
81+
chatService.SendMessageAsync($"@{userName} - No AND THEN!");
82+
return false;
83+
}
8384

84-
return true;
85-
}
85+
return true;
86+
}
87+
else if (cmd.Files != null)
88+
{
89+
if (!CheckMultipleFilesCooldown(cmd, cmdText))
90+
{
91+
// TODO: Something witty to indicate the message isn't available
92+
chatService.SendMessageAsync($"@{userName} - Scott is taking a break.. check back soon!");
93+
return false;
94+
}
95+
return true;
96+
}
8697

87-
if (!SoundCooldowns.ContainsKey(cmdText)) return true;
88-
var cooldown = TimeSpan.FromSeconds(Effects[cmdText].Cooldown);
89-
return (SoundCooldowns[cmdText].Add(cooldown) < DateTime.Now);
98+
if (!SoundCooldowns.ContainsKey(cmdText)) return true;
99+
var cooldown = TimeSpan.FromSeconds(Effects[cmdText].Cooldown);
100+
return (SoundCooldowns[cmdText].Add(cooldown) < DateTime.Now);
90101

91-
}
102+
}
92103

93-
}
104+
}
94105

95-
private DateTime CalculateAndThenCooldownTime()
96-
{
106+
private DateTime CalculateMultipleFileCooldownTime(SoundFxDefinition cmd, string cmdTrigger)
107+
{
97108

98-
if (!SoundCooldowns.ContainsKey("andthen")) return DateTime.Now;
109+
if (!SoundCooldowns.ContainsKey(cmdTrigger))
110+
{
111+
MultipleFileTriggers.Add(cmdTrigger, new List<string>());
112+
return DateTime.Now;
113+
}
99114

100-
if (AndThens.Count < 6) return SoundCooldowns["andthen"];
115+
if (MultipleFileTriggers[cmdTrigger].Count < cmd.Files.Length) return SoundCooldowns[cmdTrigger];
101116

102-
return DateTime.Now;
117+
return DateTime.Now;
103118

104-
}
119+
}
105120

106-
private bool CheckAndThenCooldown()
107-
{
121+
private bool CheckMultipleFilesCooldown(SoundFxDefinition cmd, string cmdText)
122+
{
108123

109-
var cooldown = TimeSpan.FromSeconds( Effects["andthen"].Cooldown);
124+
var cooldown = TimeSpan.FromSeconds(Effects[cmdText].Cooldown);
110125

111-
if (SoundCooldowns.ContainsKey("andthen"))
112-
{
113-
if (SoundCooldowns["andthen"].Add(cooldown) < DateTime.Now)
114-
{
115-
SoundCooldowns["andthen"] = DateTime.Now;
116-
AndThens.Clear();
117-
return true;
118-
} else
126+
if (SoundCooldowns.ContainsKey(cmdText))
119127
{
120-
return (AndThens.Count != 6);
128+
if (SoundCooldowns[cmdText].Add(cooldown) < DateTime.Now)
129+
{
130+
SoundCooldowns[cmdText] = DateTime.Now;
131+
MultipleFileTriggers[cmdText].Clear();
132+
return true;
133+
}
134+
else
135+
{
136+
return (MultipleFileTriggers[cmdText].Count != cmd.Files.Length);
137+
}
121138
}
122-
}
123-
return true;
124-
}
139+
return true;
140+
}
125141

126-
private static readonly string[] AndThenFiles = new string[] {
127-
"andthen1.mp3",
128-
"andthen2.mp3",
129-
"andthen3.mp3",
130-
"andthen4.mp3",
131-
"andthen5.mp3",
132-
"andthen6.mp3" };
133142

134-
private string IdentifyAndThenFilename()
135-
{
143+
private string IdentifyMultipleEffectsFilename(SoundFxDefinition fxDefinition, string cmdText)
144+
{
136145

137-
var available = new List<string>();
138-
AndThenFiles.ToList().ForEach(a => { if (!AndThens.Contains(a)) available.Add(a); });
139-
var random = new Random().Next(0, available.Count-1);
140-
var theFile = available.Skip(random).First();
141-
AndThens.Add(theFile);
142-
return theFile;
146+
var available = new List<string>();
147+
fxDefinition.Files.ToList().ForEach(a => { if (!MultipleFileTriggers[cmdText].Contains(a)) available.Add(a); });
148+
var random = new Random().Next(0, available.Count - 1);
149+
var theFile = available.Skip(random).First();
150+
MultipleFileTriggers[cmdText].Add(theFile);
151+
return theFile;
143152

153+
}
144154
}
145-
}
146155

147156
public class SoundFxConfig
148-
{
157+
{
149158
public SoundFxDefinition[] SoundFx { get; set; }
150-
}
159+
}
151160

152-
public class SoundFxDefinition
153-
{
161+
public class SoundFxDefinition
162+
{
154163

155-
public string Response { get; set; }
164+
public string Response { get; set; }
156165

157-
public string File { get; set; }
166+
public string File { get; set; }
158167

159-
public string[] Files { get; set; }
168+
public string[] Files { get; set; }
160169

161-
public int Cooldown { get; set; }
170+
public int Cooldown { get; set; }
162171

163-
}
172+
}
164173
}

Fritz.StreamTools/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"greatscott_6.mp3",
9898
"greatscott_7.mp3"
9999
],
100-
"cooldown": 300
100+
"cooldown": 30
101101
},
102102
"squirrel": {
103103
"response": "Hey look, a SQUIRREL! Jeff's not distracted AT ALL",

0 commit comments

Comments
 (0)