6
6
using Fritz . StreamLib . Core ;
7
7
using Fritz . StreamTools . Hubs ;
8
8
using Microsoft . AspNetCore . SignalR ;
9
+ using Microsoft . Extensions . Configuration ;
10
+ using Microsoft . Extensions . Options ;
9
11
10
12
namespace Fritz . Chatbot . Commands
11
13
{
12
14
public class SoundFxCommand : IExtendedCommand
13
15
{
14
16
15
- public SoundFxCommand ( IHubContext < AttentionHub , IAttentionHubClient > hubContext )
17
+ public SoundFxCommand ( IHubContext < AttentionHub , IAttentionHubClient > hubContext , IConfiguration configuration )
16
18
{
17
19
this . HubContext = hubContext ;
20
+
21
+ LoadConfiguration ( configuration ) ;
22
+
18
23
}
19
24
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
+ }
20
38
21
39
public IHubContext < AttentionHub , IAttentionHubClient > HubContext { get ; }
22
40
@@ -26,13 +44,15 @@ public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext)
26
44
public bool Final => true ;
27
45
public TimeSpan ? Cooldown => TimeSpan . FromSeconds ( 0 ) ;
28
46
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
+ /*
30
49
{
31
50
{ "ohmy", ("Oh my... something strange is happening", "ohmy.mp3", TimeSpan.FromSeconds(30) ) },
32
51
{ "andthen", ("... and then ...", "andthen#.mp3", TimeSpan.FromSeconds(120) ) },
33
52
{ "javascript", ("Horses LOVE JavaScript!", "javascript.mp3", TimeSpan.FromSeconds(30) ) },
34
53
{ "rimshot", ("Ba Dum Tish!", "rimshot.mp3", TimeSpan.FromSeconds(60)) }
35
54
};
55
+ */
36
56
37
57
private static readonly List < string > AndThens = new List < string > ( ) ;
38
58
@@ -58,10 +78,10 @@ public Task Execute(IChatService chatService, string userName, string fullComman
58
78
59
79
SoundCooldowns [ cmdText ] = ( cmdText == "andthen" ? CalculateAndThenCooldownTime ( ) : DateTime . Now ) ;
60
80
61
- var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename ( ) : cmd . fileName ;
81
+ var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename ( ) : cmd . File ;
62
82
63
83
var soundTask = this . HubContext . Clients . All . PlaySoundEffect ( fileToPlay ) ;
64
- var textTask = chatService . SendMessageAsync ( $ "@{ userName } - { cmd . text } ") ;
84
+ var textTask = chatService . SendMessageAsync ( $ "@{ userName } - { cmd . Response } ") ;
65
85
66
86
return Task . WhenAll ( soundTask , textTask ) ;
67
87
@@ -80,7 +100,7 @@ bool InternalCooldownCheck()
80
100
}
81
101
82
102
if ( ! SoundCooldowns . ContainsKey ( cmdText ) ) return true ;
83
- var cooldown = Effects [ cmdText ] . cooldown ;
103
+ var cooldown = TimeSpan . FromSeconds ( Effects [ cmdText ] . Cooldown ) ;
84
104
return ( SoundCooldowns [ cmdText ] . Add ( cooldown ) < DateTime . Now ) ;
85
105
86
106
}
@@ -101,7 +121,7 @@ private DateTime CalculateAndThenCooldownTime()
101
121
private bool CheckAndThenCooldown ( )
102
122
{
103
123
104
- var cooldown = Effects [ "andthen" ] . cooldown ;
124
+ var cooldown = TimeSpan . FromSeconds ( Effects [ "andthen" ] . Cooldown ) ;
105
125
106
126
if ( SoundCooldowns . ContainsKey ( "andthen" ) )
107
127
{
@@ -137,5 +157,23 @@ private string IdentifyAndThenFilename()
137
157
return theFile ;
138
158
139
159
}
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
+
140
178
}
141
179
}
0 commit comments