Skip to content

Commit 8172664

Browse files
committed
Making the SoundFx a configuration option
1 parent c71dd0e commit 8172664

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

Fritz.Chatbot/Commands/SoundFxCommand.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,35 @@
66
using Fritz.StreamLib.Core;
77
using Fritz.StreamTools.Hubs;
88
using Microsoft.AspNetCore.SignalR;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.Options;
911

1012
namespace Fritz.Chatbot.Commands
1113
{
1214
public class SoundFxCommand : IExtendedCommand
1315
{
1416

15-
public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext)
17+
public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext, IConfiguration configuration)
1618
{
1719
this.HubContext = hubContext;
20+
21+
LoadConfiguration(configuration);
22+
1823
}
1924

25+
private void LoadConfiguration(IConfiguration configuration)
26+
{
27+
28+
var sounds = configuration.GetSection("FritzBot:SoundFxCommands").GetChildren();
29+
30+
Effects = sounds.ToDictionary(s => s.Key, s => new SoundFxDefinition {
31+
Cooldown = s.GetValue<int>("cooldown"),
32+
Response = s.GetValue<string>("response"),
33+
File = s.GetValue<string>("file")
34+
});
35+
36+
37+
}
2038

2139
public IHubContext<AttentionHub, IAttentionHubClient> HubContext { get; }
2240

@@ -26,13 +44,15 @@ public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext)
2644
public bool Final => true;
2745
public TimeSpan? Cooldown => TimeSpan.FromSeconds(0);
2846

29-
internal static readonly Dictionary<string, (string text, string fileName, TimeSpan cooldown)> Effects = new Dictionary<string, (string text, string fileName, TimeSpan cooldown)>
47+
internal static Dictionary<string, SoundFxDefinition> Effects = new Dictionary<string, SoundFxDefinition>();
48+
/*
3049
{
3150
{ "ohmy", ("Oh my... something strange is happening", "ohmy.mp3", TimeSpan.FromSeconds(30) ) },
3251
{ "andthen", ("... and then ...", "andthen#.mp3", TimeSpan.FromSeconds(120) ) },
3352
{ "javascript", ("Horses LOVE JavaScript!", "javascript.mp3", TimeSpan.FromSeconds(30) ) },
3453
{ "rimshot", ("Ba Dum Tish!", "rimshot.mp3", TimeSpan.FromSeconds(60)) }
3554
};
55+
*/
3656

3757
private static readonly List<string> AndThens = new List<string>();
3858

@@ -58,10 +78,10 @@ public Task Execute(IChatService chatService, string userName, string fullComman
5878

5979
SoundCooldowns[cmdText] = (cmdText == "andthen" ? CalculateAndThenCooldownTime() : DateTime.Now);
6080

61-
var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename() : cmd.fileName;
81+
var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename() : cmd.File;
6282

6383
var soundTask = this.HubContext.Clients.All.PlaySoundEffect(fileToPlay);
64-
var textTask = chatService.SendMessageAsync($"@{userName} - {cmd.text}");
84+
var textTask = chatService.SendMessageAsync($"@{userName} - {cmd.Response}");
6585

6686
return Task.WhenAll(soundTask, textTask);
6787

@@ -80,7 +100,7 @@ bool InternalCooldownCheck()
80100
}
81101

82102
if (!SoundCooldowns.ContainsKey(cmdText)) return true;
83-
var cooldown = Effects[cmdText].cooldown;
103+
var cooldown = TimeSpan.FromSeconds(Effects[cmdText].Cooldown);
84104
return (SoundCooldowns[cmdText].Add(cooldown) < DateTime.Now);
85105

86106
}
@@ -101,7 +121,7 @@ private DateTime CalculateAndThenCooldownTime()
101121
private bool CheckAndThenCooldown()
102122
{
103123

104-
var cooldown = Effects["andthen"].cooldown;
124+
var cooldown = TimeSpan.FromSeconds( Effects["andthen"].Cooldown);
105125

106126
if (SoundCooldowns.ContainsKey("andthen"))
107127
{
@@ -137,5 +157,23 @@ private string IdentifyAndThenFilename()
137157
return theFile;
138158

139159
}
160+
}
161+
162+
public class SoundFxConfig
163+
{
164+
public SoundFxDefinition[] SoundFx { get; set; }
165+
}
166+
167+
public class SoundFxDefinition
168+
{
169+
170+
public string Response { get; set; }
171+
172+
public string File { get; set; }
173+
174+
public string[] Files { get; set; }
175+
176+
public int Cooldown { get; set; }
177+
140178
}
141179
}

Fritz.Chatbot/Fritz.Chatbot.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
<ProjectReference Include="..\Fritz.StreamLib.Core\Fritz.StreamLib.Core.csproj" />
2525
</ItemGroup>
2626

27-
<ItemGroup>
27+
<!-- ItemGroup>
2828
<Reference Include="Microsoft.VisualStudio.Web.CodeGeneration.Utils">
2929
<HintPath>..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.visualstudio.web.codegeneration.utils\2.1.6\lib\netstandard2.0\Microsoft.VisualStudio.Web.CodeGeneration.Utils.dll</HintPath>
3030
</Reference>
31-
</ItemGroup>
31+
</ItemGroup -->
3232

3333
<ItemGroup>
3434
<None Update="hey_listen.wav">

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Fritz.Chatbot;
5+
using Fritz.Chatbot.Commands;
56
using Fritz.StreamLib.Core;
67
using Fritz.StreamTools.Hubs;
78
using Fritz.StreamTools.Interfaces;
@@ -33,6 +34,7 @@ public static void Execute(IServiceCollection services, IConfiguration configura
3334
services.AddSingleton<IRundownService, RundownService>();
3435
services.Configure<FollowerGoalConfiguration>(configuration.GetSection("FollowerGoal"));
3536
services.Configure<FollowerCountConfiguration>(configuration.GetSection("FollowerCount"));
37+
services.Configure<SoundFxConfig>(configuration.GetSection("FritzBot:SoundFxCommands"));
3638
services.AddStreamingServices(configuration);
3739
services.Configure<GitHubConfiguration>(configuration.GetSection("GitHub"));
3840
services.AddSingleton<FollowerClient>();

Fritz.StreamTools/appsettings.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@
5656
"TemplateText": "@{0} Jeff is currently working on {1}",
5757
"DefaultText": "nothing! Are we not working on anything @csharpfritz?"
5858
},
59+
"SoundFxCommands": {
60+
"ohmy":
61+
{
62+
"response": "Oh my... something strange is happening",
63+
"file": "ohmy.mp3",
64+
"cooldown": 30
65+
},
66+
"andthen": {
67+
"response": "... and then ...",
68+
"file": [
69+
"andthen1.mp3",
70+
"andthen2.mp3",
71+
"andthen3.mp3",
72+
"andthen4.mp3",
73+
"andthen5.mp3",
74+
"andthen6.mp3"
75+
],
76+
"cooldown": 120
77+
},
78+
"javascript": {
79+
"response": "Horses LOVE JavaScript!",
80+
"file": "javascript.mp3",
81+
"cooldown": 30
82+
},
83+
"rimshot": {
84+
"response": "Ba Dum Tish!",
85+
"file": "rimshot.mp3",
86+
"cooldown": 60
87+
}
88+
},
5989
"TextCommand": [
6090
{
6191
"command": "blog",

0 commit comments

Comments
 (0)