@@ -55,15 +55,16 @@ public static bool SendGlobal([NotNull] Player player, [NotNull] string rawMessa
5555
5656 public static string Filter ( string rawMessage , Player player ) {
5757 if ( player != null && ! player . IsStaff ) {
58- if ( player . LastMessage == new string ( rawMessage . ToLower ( ) . Where ( c => ! char . IsWhiteSpace ( c ) ) . ToArray ( ) ) ) {
59- if ( player . MessageSpam >= 2 ) {
58+ string trimmedMsg = new string ( rawMessage . Where ( c => ! char . IsWhiteSpace ( c ) ) . ToArray ( ) ) ;
59+ if ( player . lastMsg . CaselessEquals ( trimmedMsg ) ) {
60+ if ( player . lastMsgCount >= 2 ) {
6061 player . Message ( "Please refrain from repeating yourself!" ) ;
6162 return null ;
6263 }
63- player . MessageSpam ++ ;
64+ player . lastMsgCount ++ ;
6465 } else {
65- player . LastMessage = new string ( rawMessage . ToLower ( ) . Where ( c => ! char . IsWhiteSpace ( c ) ) . ToArray ( ) ) ;
66- player . MessageSpam = 0 ;
66+ player . lastMsg = trimmedMsg ;
67+ player . lastMsgCount = 0 ;
6768 }
6869 }
6970
@@ -80,9 +81,7 @@ public static string Filter(string rawMessage, Player player) {
8081 if ( rawMessage . Length >= 10 && player . Info . Rank . MaxCaps > 0 ) {
8182 int caps = 0 ;
8283 for ( int i = 0 ; i < rawMessage . Length ; i ++ ) {
83- if ( char . IsUpper ( rawMessage [ i ] ) ) {
84- caps ++ ;
85- }
84+ if ( char . IsUpper ( rawMessage [ i ] ) ) caps ++ ;
8685 }
8786
8887 if ( player . Info . Rank . MaxCaps == 1 ) {
@@ -162,15 +161,15 @@ static void checkBotResponses(Player player, string rawMessage) {
162161 player . ParseMessage ( "/bot <CalledFromChat> " + rawMessage . Remove ( 0 , 4 ) , false ) ;
163162 }
164163 double BotTime = player . TimeSinceLastServerMessage . TotalSeconds ;
165- if ( LDistance ( rawMessage . ToLower ( ) , "how do i rank up?" ) <= 0.25
166- || LDistance ( rawMessage . ToLower ( ) , "how do we rank up" ) <= 0.25 ) {
164+ if ( LDistance ( rawMessage , "how do i rank up?" ) <= 0.25
165+ || LDistance ( rawMessage , "how do we rank up" ) <= 0.25 ) {
167166 if ( BotTime > 5 ) {
168167 Server . BotMessage ( "You rank up by building something nice, preferably big. Then ask a staff member for a review." ) ;
169168 player . LastServerMessageDate = DateTime . UtcNow ;
170169 player . Info . TimesUsedBot ++ ;
171170 }
172171 }
173- if ( LDistance ( rawMessage . ToLower ( ) , "who is the owner?" ) <= 0.25 ) {
172+ if ( LDistance ( rawMessage , "who is the owner?" ) <= 0.25 ) {
174173 if ( BotTime > 5 ) {
175174 PlayerInfo owner ;
176175 if ( PlayerDB . FindPlayerInfo ( ConfigKey . ServerOwner . GetString ( ) , out owner ) && owner != null ) {
@@ -182,24 +181,24 @@ static void checkBotResponses(Player player, string rawMessage) {
182181 player . Info . TimesUsedBot ++ ;
183182 }
184183 }
185- if ( LDistance ( rawMessage . ToLower ( ) , "what is this server called?" ) <= 0.25
186- || LDistance ( rawMessage . ToLower ( ) , "what is the name of this server?" ) <= 0.25 ) {
184+ if ( LDistance ( rawMessage , "what is this server called?" ) <= 0.25
185+ || LDistance ( rawMessage , "what is the name of this server?" ) <= 0.25 ) {
187186 if ( BotTime > 5 ) {
188187 Server . BotMessage ( "The server name is: " + ConfigKey . ServerName . GetString ( ) ) ;
189188 player . LastServerMessageDate = DateTime . UtcNow ;
190189 player . Info . TimesUsedBot ++ ;
191190 }
192191 }
193- if ( LDistance ( rawMessage . ToLower ( ) , "where can i build?" ) <= 0.25
194- || LDistance ( rawMessage . ToLower ( ) , "where do we build" ) <= 0.25 ) {
192+ if ( LDistance ( rawMessage , "where can i build?" ) <= 0.25
193+ || LDistance ( rawMessage , "where do we build" ) <= 0.25 ) {
195194 if ( BotTime > 5 ) {
196195 Server . BotMessage ( "You can build anywhere outside of spawn. Just not on another persons build unless they say you can. " ) ;
197196 player . LastServerMessageDate = DateTime . UtcNow ;
198197 player . Info . TimesUsedBot ++ ;
199198 }
200199 }
201- if ( LDistance ( rawMessage . ToLower ( ) , "what is my next rank?" ) <= 0.25 ||
202- LDistance ( rawMessage . ToLower ( ) , "what rank is after this one?" ) <= 0.25 ) {
200+ if ( LDistance ( rawMessage , "what is my next rank?" ) <= 0.25 ||
201+ LDistance ( rawMessage , "what rank is after this one?" ) <= 0.25 ) {
203202 Rank next = player . Info . Rank . NextRankUp ;
204203 // donor ranks are skipped from being able to be promoted to
205204 while ( next != null && next . IsDonor ) next = next . NextRankUp ;
@@ -217,14 +216,11 @@ static void checkBotResponses(Player player, string rawMessage) {
217216 }
218217 }
219218
220- public static float LDistance ( string s , string t ) {
219+ internal static float LDistance ( string s , string t ) {
221220 // degenerate cases
222- if ( s == t )
223- return 0 ;
224- if ( s . Length == 0 )
225- return 1 ;
226- if ( t . Length == 0 )
227- return 1 ;
221+ if ( s . CaselessEquals ( t ) ) return 0 ;
222+ if ( s . Length == 0 ) return 1 ;
223+ if ( t . Length == 0 ) return 1 ;
228224
229225 // create two work vectors of integer distances
230226 int [ ] v0 = new int [ t . Length + 1 ] ;
@@ -242,10 +238,13 @@ public static float LDistance( string s, string t ) {
242238 // first element of v1 is A[i+1][0]
243239 // edit distance is delete (i+1) chars from s to match empty t
244240 v1 [ 0 ] = i + 1 ;
241+ char curS = s [ i ] ; if ( curS >= 'A' && curS <= 'Z' ) curS += ' ' ;
245242
246243 // use formula to fill in the rest of the row
247244 for ( int j = 0 ; j < t . Length ; j ++ ) {
248- var cost = ( s [ i ] == t [ j ] ) ? 0 : 1 ;
245+ char curT = t [ j ] ; if ( curT >= 'A' && curT <= 'Z' ) curT += ' ' ;
246+ int cost = ( curS == curT ) ? 0 : 1 ;
247+
249248 v1 [ j + 1 ] = Math . Min ( v1 [ j ] + 1 , v0 [ j + 1 ] + 1 ) ;
250249 v1 [ j + 1 ] = Math . Min ( v1 [ j + 1 ] , v0 [ j ] + cost ) ;
251250 }
@@ -254,11 +253,10 @@ public static float LDistance( string s, string t ) {
254253 for ( int j = 0 ; j < v0 . Length ; j ++ )
255254 v0 [ j ] = v1 [ j ] ;
256255 }
256+
257257 float percent = ( ( ( float ) v1 [ t . Length ] ) / ( ( float ) ( s . Length + t . Length ) / 2 ) ) ;
258- if ( percent < 0 )
259- percent = 0 ;
260- if ( percent > 1 )
261- percent = 1 ;
258+ if ( percent < 0 ) percent = 0 ;
259+ if ( percent > 1 ) percent = 1 ;
262260 return percent ;
263261 }
264262
0 commit comments