From 8f08265c528ff224eda4249592d62964f3d772db Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Mon, 27 Feb 2023 20:00:50 +0000 Subject: [PATCH] Talking messages now appear in the chat log correctly. - Added logic to properly add the relevant users in the correct order to ensure message delivery. --- src/gamelogic.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/gamelogic.c b/src/gamelogic.c index 696cc0b..421ebcd 100644 --- a/src/gamelogic.c +++ b/src/gamelogic.c @@ -89,14 +89,32 @@ void * gameLogicHandler(void * parameters) } else { - // Allocate an array of one playerInfo to store the pointer to the other player in the conversation: - playerInfo ** recipients = calloc(1, (sizeof(playerInfo*))); + // Allocate an array of two playerInfo to store the pointers to the players in the conversation: + playerInfo ** recipients = calloc(2, (sizeof(playerInfo*))); - // Set the talkingWith player as the recipient of the message: - recipients[0] = currentInput->sender->talkingWith; + // Find which player is first in the player list: + bool senderIsFirst = false; + for(int playerIndex = 0; playerIndex < *threadParameters->playerCount; playerIndex++) + { + if(&threadParameters->connectedPlayers[playerIndex] == currentInput->sender) + { + senderIsFirst = true; + break; + } + if(&threadParameters->connectedPlayers[playerIndex] == currentInput->sender->talkingWith) + { + senderIsFirst = false; + break; + } + } + + // Set the proper recipients: + recipients[0] = (senderIsFirst) ? currentInput->sender : currentInput->sender->talkingWith; + recipients[1] = (senderIsFirst) ? currentInput->sender->talkingWith : currentInput->sender; + // There's only one recipient: - int recipientIndex = 1; + int recipientIndex = 2; // Create the outputMessage for the queue: outputMessage * newOutputMessage = createTargetedOutputMessage(currentInput->content, recipients, recipientIndex); @@ -261,15 +279,14 @@ int evaluateNextCommand(gameLogicParameters * parameters, queue * queue) snprintf(formattedString, 45, "\n%02d. %31s", playerNumber++, parameters->connectedPlayers[index].playerName); strncat(lookMessage->messageContent, formattedString, 64); - charCount += 38; - - // Allocate another outputMessage for the queue: - lookOutputMessage = createTargetedOutputMessage(lookMessage, ¤tCommand->caller, 1); - - // Queue the outputMessage: - pushQueue(parameters->outputQueue, lookOutputMessage, OUTPUT_MESSAGE); + charCount += 38; } } + // Allocate another outputMessage for the queue: + lookOutputMessage = createTargetedOutputMessage(lookMessage, ¤tCommand->caller, 1); + + // Queue the outputMessage: + pushQueue(parameters->outputQueue, lookOutputMessage, OUTPUT_MESSAGE); } free(lookMessage);