Skip to content

Commit 613ae4b

Browse files
committed
Made AndThen 1 command
1 parent 141f53b commit 613ae4b

File tree

2 files changed

+101
-17
lines changed

2 files changed

+101
-17
lines changed

Fritz.Chatbot/Commands/HelpCommand.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public HelpCommand(IServiceProvider serviceProvider)
2424
public async Task Execute(IChatService chatService, string userName, ReadOnlyMemory<char> rhs)
2525
{
2626
var commands = _serviceProvider.GetServices<IBasicCommand>();
27+
var textCommandArray = TextCommand._Commands.Select(kv => "!" + kv.Key.ToLower());
28+
var soundFxCommands = SoundFxCommand.Effects.Select(kv => "!" + kv.Key.ToLower());
2729

2830
if (rhs.IsEmpty)
2931
{
3032

3133
var availableCommandArray = commands.Where(c => !string.IsNullOrEmpty(c.Trigger)).Select(c => $"!{c.Trigger.ToLower()}");
32-
var textCommandArray = TextCommand._Commands.Select(kv => "!" + kv.Key.ToLower());
3334

34-
var availableCommands = string.Join(' ', availableCommandArray.Union(textCommandArray).OrderBy(s => s));
35+
var availableCommands = string.Join(' ', availableCommandArray.Union(textCommandArray).Union(soundFxCommands).OrderBy(s => s));
3536

3637
await chatService.SendMessageAsync($"Supported commands: {availableCommands}");
3738
return;
@@ -40,7 +41,19 @@ public async Task Execute(IChatService chatService, string userName, ReadOnlyMem
4041
var cmd = commands.FirstOrDefault(c => rhs.Span.Equals(c.Trigger.AsSpan(), StringComparison.OrdinalIgnoreCase));
4142
if (cmd == null)
4243
{
43-
await chatService.SendMessageAsync("Unknown command to provide help with.");
44+
45+
if (textCommandArray.Contains("!" + rhs.Span.ToString()))
46+
{
47+
await chatService.SendMessageAsync($"{rhs}: A helpful text message");
48+
}
49+
else if (soundFxCommands.Contains("!" + rhs.Span.ToString()))
50+
{
51+
await chatService.SendMessageAsync($"{rhs}: A fun sound effect");
52+
}
53+
else
54+
{
55+
await chatService.SendMessageAsync("Unknown command to provide help with.");
56+
}
4457
return;
4558
}
4659

Fritz.Chatbot/Commands/SoundFxCommand.cs

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text;
45
using System.Threading.Tasks;
56
using Fritz.StreamLib.Core;
@@ -25,45 +26,115 @@ public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext)
2526
public bool Final => true;
2627
public TimeSpan? Cooldown => TimeSpan.FromSeconds(0);
2728

28-
private static readonly Dictionary<string, (string text, string fileName, TimeSpan cooldown)> Effects = new Dictionary<string, (string text, string fileName, TimeSpan cooldown)>
29+
internal static readonly Dictionary<string, (string text, string fileName, TimeSpan cooldown)> Effects = new Dictionary<string, (string text, string fileName, TimeSpan cooldown)>
2930
{
3031
{ "ohmy", ("Oh my... something strange is happening", "ohmy.mp3", TimeSpan.FromSeconds(30) ) },
31-
{ "andthen", ("... and then ...", "andthen1.mp3", TimeSpan.FromSeconds(120) ) },
32-
{ "andthen2", ("... and then ...", "andthen3.mp3", TimeSpan.FromSeconds(120) )},
33-
{ "andthen3", ("... and then ...", "andthen4.mp3", TimeSpan.FromSeconds(120) )},
34-
{ "andthen4", ("... and then ...", "andthen5.mp3", TimeSpan.FromSeconds(120) )},
35-
{ "andthen5", ("... and then ...", "andthen6.mp3", TimeSpan.FromSeconds(120) )},
36-
{ "andthen6", ("... and then ...", "andthen7.mp3", TimeSpan.FromSeconds(120) )},
32+
{ "andthen", ("... and then ...", "andthen#.mp3", TimeSpan.FromSeconds(120) ) },
3733
{ "javascript", ("Horses LOVE JavaScript!", "javascript.mp3", TimeSpan.FromSeconds(30) ) },
3834
};
3935

36+
private static readonly List<string> AndThens = new List<string>();
37+
4038
private static readonly Dictionary<string, DateTime> SoundCooldowns = new Dictionary<string, DateTime>();
4139

4240
public bool CanExecute(string userName, string fullCommandText)
4341
{
42+
4443
if (!fullCommandText.StartsWith("!")) return false;
4544
var cmd = fullCommandText.Substring(1).ToLowerInvariant();
46-
if (!Effects.ContainsKey(cmd)) return false;
47-
48-
if (!SoundCooldowns.ContainsKey(cmd)) return true;
49-
var cooldown = Effects[cmd].cooldown;
50-
return (SoundCooldowns[cmd].Add(cooldown) < DateTime.Now);
45+
return Effects.ContainsKey(cmd);
5146

5247
}
5348

5449
public Task Execute(IChatService chatService, string userName, string fullCommandText)
5550
{
5651

5752
var cmdText = fullCommandText.Substring(1).ToLowerInvariant();
53+
54+
if (!InternalCooldownCheck()) return Task.CompletedTask;
55+
5856
var cmd = Effects[cmdText];
5957

60-
SoundCooldowns[cmdText] = DateTime.Now;
58+
SoundCooldowns[cmdText] = (cmdText == "andthen" ? CalculateAndThenCooldownTime() : DateTime.Now);
59+
60+
var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename() : cmd.fileName;
6161

62-
var soundTask = this.HubContext.Clients.All.PlaySoundEffect(cmd.fileName);
62+
var soundTask = this.HubContext.Clients.All.PlaySoundEffect(fileToPlay);
6363
var textTask = chatService.SendMessageAsync($"@{userName} - {cmd.text}");
6464

6565
return Task.WhenAll(soundTask, textTask);
6666

67+
bool InternalCooldownCheck()
68+
{
69+
70+
if (cmdText == "andthen")
71+
{
72+
if (!CheckAndThenCooldown())
73+
{
74+
chatService.SendMessageAsync($"@{userName} - No AND THEN!");
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
81+
if (!SoundCooldowns.ContainsKey(cmdText)) return true;
82+
var cooldown = Effects[cmdText].cooldown;
83+
return (SoundCooldowns[cmdText].Add(cooldown) < DateTime.Now);
84+
85+
}
86+
87+
}
88+
89+
private DateTime CalculateAndThenCooldownTime()
90+
{
91+
92+
if (!SoundCooldowns.ContainsKey("andthen")) return DateTime.Now;
93+
94+
if (AndThens.Count < 6) return SoundCooldowns["andthen"];
95+
96+
return DateTime.Now;
97+
98+
}
99+
100+
private bool CheckAndThenCooldown()
101+
{
102+
103+
var cooldown = Effects["andthen"].cooldown;
104+
105+
if (SoundCooldowns.ContainsKey("andthen"))
106+
{
107+
if (SoundCooldowns["andthen"].Add(cooldown) < DateTime.Now)
108+
{
109+
SoundCooldowns["andthen"] = DateTime.Now;
110+
AndThens.Clear();
111+
return true;
112+
} else
113+
{
114+
return (AndThens.Count != 6);
115+
}
116+
}
117+
return true;
118+
}
119+
120+
private static readonly string[] AndThenFiles = new string[] {
121+
"andthen1.mp3",
122+
"andthen2.mp3",
123+
"andthen3.mp3",
124+
"andthen4.mp3",
125+
"andthen5.mp3",
126+
"andthen6.mp3" };
127+
128+
private string IdentifyAndThenFilename()
129+
{
130+
131+
var available = new List<string>();
132+
AndThenFiles.ToList().ForEach(a => { if (!AndThens.Contains(a)) available.Add(a); });
133+
var random = new Random().Next(0, available.Count-1);
134+
var theFile = available.Skip(random).First();
135+
AndThens.Add(theFile);
136+
return theFile;
137+
67138
}
68139
}
69140
}

0 commit comments

Comments
 (0)