1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using System . Text ;
4
5
using System . Threading . Tasks ;
5
6
using Fritz . StreamLib . Core ;
@@ -25,45 +26,115 @@ public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext)
25
26
public bool Final => true ;
26
27
public TimeSpan ? Cooldown => TimeSpan . FromSeconds ( 0 ) ;
27
28
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 ) >
29
30
{
30
31
{ "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 ) ) } ,
37
33
{ "javascript" , ( "Horses LOVE JavaScript!" , "javascript.mp3" , TimeSpan . FromSeconds ( 30 ) ) } ,
38
34
} ;
39
35
36
+ private static readonly List < string > AndThens = new List < string > ( ) ;
37
+
40
38
private static readonly Dictionary < string , DateTime > SoundCooldowns = new Dictionary < string , DateTime > ( ) ;
41
39
42
40
public bool CanExecute ( string userName , string fullCommandText )
43
41
{
42
+
44
43
if ( ! fullCommandText . StartsWith ( "!" ) ) return false ;
45
44
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 ) ;
51
46
52
47
}
53
48
54
49
public Task Execute ( IChatService chatService , string userName , string fullCommandText )
55
50
{
56
51
57
52
var cmdText = fullCommandText . Substring ( 1 ) . ToLowerInvariant ( ) ;
53
+
54
+ if ( ! InternalCooldownCheck ( ) ) return Task . CompletedTask ;
55
+
58
56
var cmd = Effects [ cmdText ] ;
59
57
60
- SoundCooldowns [ cmdText ] = DateTime . Now ;
58
+ SoundCooldowns [ cmdText ] = ( cmdText == "andthen" ? CalculateAndThenCooldownTime ( ) : DateTime . Now ) ;
59
+
60
+ var fileToPlay = cmdText == "andthen" ? IdentifyAndThenFilename ( ) : cmd . fileName ;
61
61
62
- var soundTask = this . HubContext . Clients . All . PlaySoundEffect ( cmd . fileName ) ;
62
+ var soundTask = this . HubContext . Clients . All . PlaySoundEffect ( fileToPlay ) ;
63
63
var textTask = chatService . SendMessageAsync ( $ "@{ userName } - { cmd . text } ") ;
64
64
65
65
return Task . WhenAll ( soundTask , textTask ) ;
66
66
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
+
67
138
}
68
139
}
69
140
}
0 commit comments