1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Security . Cryptography ;
4
5
using System . Text ;
5
6
using System . Threading . Tasks ;
6
7
using Fritz . StreamLib . Core ;
11
12
12
13
namespace Fritz . Chatbot . Commands
13
14
{
14
- public class SoundFxCommand : IExtendedCommand
15
- {
16
-
17
- public SoundFxCommand ( IHubContext < AttentionHub , IAttentionHubClient > hubContext , IOptions < Dictionary < string , SoundFxDefinition > > soundEffects )
15
+ public class SoundFxCommand : IExtendedCommand
18
16
{
19
- this . HubContext = hubContext ;
20
17
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
+ }
23
24
24
- public IHubContext < AttentionHub , IAttentionHubClient > HubContext { get ; }
25
+ public IHubContext < AttentionHub , IAttentionHubClient > HubContext { get ; }
25
26
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 ) ;
31
32
32
- internal static Dictionary < string , SoundFxDefinition > Effects = new Dictionary < string , SoundFxDefinition > ( ) ;
33
+ internal static Dictionary < string , SoundFxDefinition > Effects = new Dictionary < string , SoundFxDefinition > ( ) ;
33
34
/*
34
35
{
35
36
{ "ohmy", ("Oh my... something strange is happening", "ohmy.mp3", TimeSpan.FromSeconds(30) ) },
@@ -39,126 +40,134 @@ public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext,
39
40
};
40
41
*/
41
42
42
- private static readonly List < string > AndThens = new List < string > ( ) ;
43
+ private static readonly Dictionary < string , List < string > > MultipleFileTriggers = new Dictionary < string , List < string > > ( ) ;
43
44
44
- private static readonly Dictionary < string , DateTime > SoundCooldowns = new Dictionary < string , DateTime > ( ) ;
45
+ private static readonly Dictionary < string , DateTime > SoundCooldowns = new Dictionary < string , DateTime > ( ) ;
45
46
46
- public bool CanExecute ( string userName , string fullCommandText )
47
- {
47
+ public bool CanExecute ( string userName , string fullCommandText )
48
+ {
48
49
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 ) ;
52
53
53
- }
54
+ }
54
55
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
+ {
59
58
60
- if ( ! InternalCooldownCheck ( ) ) return Task . CompletedTask ;
59
+ var cmdText = fullCommandText . Substring ( 1 ) . ToLowerInvariant ( ) ;
60
+ var cmd = Effects [ cmdText ] ;
61
61
62
- var cmd = Effects [ cmdText ] ;
62
+ if ( ! InternalCooldownCheck ( ) ) return Task . CompletedTask ;
63
63
64
- SoundCooldowns [ cmdText ] = ( cmdText == "andthen" ? CalculateAndThenCooldownTime ( ) : DateTime . Now ) ;
64
+ SoundCooldowns [ cmdText ] = ( cmd . Files != null ? CalculateMultipleFileCooldownTime ( cmd , cmdText ) : DateTime . Now ) ;
65
65
66
- var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename ( ) : cmd . File ;
67
66
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 ;
70
68
71
- return Task . WhenAll ( soundTask , textTask ) ;
69
+ var soundTask = this . HubContext . Clients . All . PlaySoundEffect ( fileToPlay ) ;
70
+ var textTask = chatService . SendMessageAsync ( $ "@{ userName } - { cmd . Response } ") ;
72
71
73
- bool InternalCooldownCheck ( )
74
- {
72
+ return Task . WhenAll ( soundTask , textTask ) ;
75
73
76
- if ( cmdText == "andthen" )
74
+ bool InternalCooldownCheck ( )
77
75
{
78
- if ( ! CheckAndThenCooldown ( ) )
76
+
77
+ if ( cmdText == "andthen" )
79
78
{
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
+ }
83
84
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
+ }
86
97
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 ) ;
90
101
91
- }
102
+ }
92
103
93
- }
104
+ }
94
105
95
- private DateTime CalculateAndThenCooldownTime ( )
96
- {
106
+ private DateTime CalculateMultipleFileCooldownTime ( SoundFxDefinition cmd , string cmdTrigger )
107
+ {
97
108
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
+ }
99
114
100
- if ( AndThens . Count < 6 ) return SoundCooldowns [ "andthen" ] ;
115
+ if ( MultipleFileTriggers [ cmdTrigger ] . Count < cmd . Files . Length ) return SoundCooldowns [ cmdTrigger ] ;
101
116
102
- return DateTime . Now ;
117
+ return DateTime . Now ;
103
118
104
- }
119
+ }
105
120
106
- private bool CheckAndThenCooldown ( )
107
- {
121
+ private bool CheckMultipleFilesCooldown ( SoundFxDefinition cmd , string cmdText )
122
+ {
108
123
109
- var cooldown = TimeSpan . FromSeconds ( Effects [ "andthen" ] . Cooldown ) ;
124
+ var cooldown = TimeSpan . FromSeconds ( Effects [ cmdText ] . Cooldown ) ;
110
125
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 ) )
119
127
{
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
+ }
121
138
}
122
- }
123
- return true ;
124
- }
139
+ return true ;
140
+ }
125
141
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" } ;
133
142
134
- private string IdentifyAndThenFilename ( )
135
- {
143
+ private string IdentifyMultipleEffectsFilename ( SoundFxDefinition fxDefinition , string cmdText )
144
+ {
136
145
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 ;
143
152
153
+ }
144
154
}
145
- }
146
155
147
156
public class SoundFxConfig
148
- {
157
+ {
149
158
public SoundFxDefinition [ ] SoundFx { get ; set ; }
150
- }
159
+ }
151
160
152
- public class SoundFxDefinition
153
- {
161
+ public class SoundFxDefinition
162
+ {
154
163
155
- public string Response { get ; set ; }
164
+ public string Response { get ; set ; }
156
165
157
- public string File { get ; set ; }
166
+ public string File { get ; set ; }
158
167
159
- public string [ ] Files { get ; set ; }
168
+ public string [ ] Files { get ; set ; }
160
169
161
- public int Cooldown { get ; set ; }
170
+ public int Cooldown { get ; set ; }
162
171
163
- }
172
+ }
164
173
}
0 commit comments