Browse Source

Reduce nesting in OnClientThinkPost

nosoop 3 years ago
parent
commit
d35cf499b3
1 changed files with 49 additions and 45 deletions
  1. 49 45
      scripting/simple-chatprocessor.sp

+ 49 - 45
scripting/simple-chatprocessor.sp

@@ -147,58 +147,58 @@ public void OnClientThinkPost(int author) {
 	/**
 	 * Iterate through all queued messages from OnSayText2
 	 */
-	if (g_QueuedMessages[author].Size > 0) {
-		StringMapSnapshot messages = g_QueuedMessages[author].Snapshot();
+	if (g_QueuedMessages[author].Size == 0) {
+		return;
+	}
+	StringMapSnapshot messages = g_QueuedMessages[author].Snapshot();
+	
+	for (int m = 0; m < messages.Length; m++) {
+		char packedMessage[192], localizationToken[32], message[128];
+		messages.GetKey(m, packedMessage, sizeof(packedMessage));
 		
-		for (int m = 0; m < messages.Length; m++) {
-			char packedMessage[192], localizationToken[32], message[128];
-			messages.GetKey(m, packedMessage, sizeof(packedMessage));
-			
-			ArrayList clientList;
-			
-			g_QueuedMessages[author].GetValue(packedMessage, clientList);
-			
-			// unpack localization and message from key
-			int d = SplitString(packedMessage, PACKED_TOKEN_DELIMITER, localizationToken,
-					sizeof(localizationToken));
-			strcopy(message, sizeof(message), packedMessage[d]);
-			
-			char name[MAX_NAME_LENGTH + 1];
-			GetClientName(author, name, sizeof(name));
-			
-			// Prepare chat message flags.
-			g_ChatFlags = ParseChatMessageFlags(localizationToken);
-			
-			// Forward call.
-			Action forwardResult = ForwardOnChatMessage(author, clientList, name, sizeof(name),
-					message, sizeof(message));
-			
-			// Proceed to display message on continue or changed, else drop message.
-			if (forwardResult < Plugin_Handled) {
-				// convert ArrayList to client array
-				int clients[MAXPLAYERS + 1], nClients;
-				for (int i = 0; i < clientList.Length; i++) {
-					int recipient = clientList.Get(i);
-					
-					// display to author and players that did not mute the author
-					if (recipient == author
-							|| !IsClientMuted(recipient, author)) {
-						clients[nClients++] = recipient;
-					}
-				}
-				
-				SayText(author, clients, nClients, localizationToken, name, message);
+		ArrayList clientList;
+		
+		g_QueuedMessages[author].GetValue(packedMessage, clientList);
+		
+		// unpack localization and message from key
+		int d = SplitString(packedMessage, PACKED_TOKEN_DELIMITER, localizationToken,
+				sizeof(localizationToken));
+		strcopy(message, sizeof(message), packedMessage[d]);
+		
+		char name[MAX_NAME_LENGTH + 1];
+		GetClientName(author, name, sizeof(name));
+		
+		// Prepare chat message flags.
+		g_ChatFlags = ParseChatMessageFlags(localizationToken);
+		
+		// Forward call.
+		Action forwardResult = ForwardOnChatMessage(author, clientList, name, sizeof(name),
+				message, sizeof(message));
+		
+		// Proceed to display message on continue or changed, else drop message.
+		if (forwardResult < Plugin_Handled) {
+			// convert ArrayList to client array
+			int clients[MAXPLAYERS + 1], nClients;
+			for (int i = 0; i < clientList.Length; i++) {
+				int recipient = clientList.Get(i);
 				
-				ForwardOnChatMessagePost(author, clientList, name, message);
+				// display to author and players that did not mute the author
+				if (ShouldTransmitMessage(author, recipient)) {
+					clients[nClients++] = recipient;
+				}
 			}
-			delete clientList;
 			
-			g_ChatFlags = CHATFLAGS_INVALID;
+			SayText(author, clients, nClients, localizationToken, name, message);
+			
+			ForwardOnChatMessagePost(author, clientList, name, message);
 		}
-		delete messages;
+		delete clientList;
 		
-		g_QueuedMessages[author].Clear();
+		g_ChatFlags = CHATFLAGS_INVALID;
 	}
+	delete messages;
+	
+	g_QueuedMessages[author].Clear();
 }
 
 Action ForwardOnChatMessage(int &author, ArrayList clientList, char[] name, int nameLength,
@@ -221,6 +221,10 @@ Action ForwardOnChatMessage(int &author, ArrayList clientList, char[] name, int
 	return forwardResult;
 }
 
+bool ShouldTransmitMessage(int author, int recipient) {
+	return recipient == author || !IsClientMuted(recipient, author);
+}
+
 void ForwardOnChatMessagePost(int author, ArrayList clientList, const char[] name,
 		const char[] message) {
 	Call_StartForward(g_fwdOnChatMessagePost);