@@ -32,6 +32,9 @@ const REMOVE_REPLACEMENT_QUERY = `
3232 LIMIT
3333 1` ;
3434
35+ // Maximum length of a message in main chat, in number of characters.
36+ const kMaximumMessageLength = 122 ;
37+
3538// Minimum message length before considering recapitalization.
3639const kRecapitalizeMinimumMessageLength = 10 ;
3740
@@ -117,6 +120,12 @@ export class MessageFilter {
117120 message = this . applyReplacement ( message , replacement ) ;
118121 }
119122
123+ // (3) Cap the length of a message to a determined maximum, as messages otherwise would
124+ // disappear into the void with no information given to the sending player at all.
125+ const maximumLength = kMaximumMessageLength - player . name . length ;
126+ if ( message . length > maximumLength )
127+ message = this . trimMessage ( message , maximumLength ) ;
128+
120129 return message ;
121130 }
122131
@@ -210,6 +219,23 @@ export class MessageFilter {
210219 } ) ;
211220 }
212221
222+ // Trims the given |message| to the given |maximumLength|. We'll find the closest word from
223+ // that position and break there when it's close enough, otherwise apply a hard break.
224+ trimMessage ( message , maximumLength ) {
225+ const kCutoffText = '...' ;
226+
227+ // Determines exactly where the |message| should be cut.
228+ const messageCutoffIndex = maximumLength - kCutoffText . length ;
229+ const messageCutoffWhitespace = message . lastIndexOf ( ' ' , messageCutoffIndex ) ;
230+
231+ // If the last whitespace character is within 8 characters of the message length limit, cut
232+ // there. Otherwise cut the |message| exactly at the limit.
233+ if ( messageCutoffIndex - messageCutoffWhitespace <= 8 )
234+ return message . substring ( 0 , messageCutoffWhitespace ) + kCutoffText ;
235+ else
236+ return message . substring ( 0 , messageCutoffIndex ) + kCutoffText ;
237+ }
238+
213239 // ---------------------------------------------------------------------------------------------
214240
215241 // Loads the replacements from the database, once a connection has been established. Mocked out
0 commit comments