From a66a07c897cf37b5624bcfa87c309fbbf602ea61 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Sun, 29 Oct 2023 20:15:41 +0000 Subject: [PATCH] Properly remove disconnected players --- source/server/main.c | 12 +++++----- source/server/player-data.c | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/source/server/main.c b/source/server/main.c index 3cd79a4..fef8a01 100644 --- a/source/server/main.c +++ b/source/server/main.c @@ -170,10 +170,11 @@ int main (int argc, char ** argv) // Send a welcome message: struct ServerToClientMessage welcomeMessage; welcomeMessage.type = SYSTEM; - sprintf(welcomeMessage.content, (clientConnections.clientCount > 1) ? - "Welcome to the server. There are %d players connected." : "Welcome to the server. There is %d player connected.", + sprintf(welcomeMessage.content, + (clientConnections.clientCount > 1) ? + "Welcome to the server. There are %d players connected." : + "Welcome to the server. There is %d player connected.", clientConnections.clientCount); - gnutls_record_send(*tlsSession, &welcomeMessage, sizeof(struct ServerToClientMessage)); // Print a message: @@ -194,9 +195,10 @@ int main (int argc, char ** argv) { epoll_ctl(connectedClients, EPOLL_CTL_DEL, connection->fileDescriptor, &watchedEvents); shutdown(connection->fileDescriptor, 2); - removeConnectionByFileDescriptor(&clientConnections, connection->fileDescriptor); close(connection->fileDescriptor); - //deallocatePlayer(&connection->player); + removeFromPlayerList(connection->player, globalPlayerList); + deallocatePlayer(&connection->player); + removeConnectionByFileDescriptor(&clientConnections, connection->fileDescriptor); continue; } else if (returnValue == sizeof(struct ClientToServerMessage)) diff --git a/source/server/player-data.c b/source/server/player-data.c index 14a6aed..c3d530d 100644 --- a/source/server/player-data.c +++ b/source/server/player-data.c @@ -138,6 +138,50 @@ int addToPlayerList(struct Player * player, struct PlayerList * playerList) } } +int removeFromPlayerList(struct Player * player, struct PlayerList * playerList) +{ + struct PlayerListNode * currentNode = playerList->head; + while (currentNode != NULL) + { + if (currentNode->player == player) + { + // Adjust the proper pointers: + if (currentNode->previous) + { + currentNode->previous->next = currentNode->next; + } + if (currentNode->next) + { + currentNode->next->previous = currentNode->previous; + } + + // Handle the special case of the head and tail of the list: + if (playerList->head == currentNode) + { + playerList->head == playerList->head->next; + } + if (playerList->tail == currentNode) + { + playerList->tail == playerList->tail->previous; + } + + // Handle the special case of an empty list: + if (playerList->count - 1 == 0) + { + playerList->head = NULL; + playerList->tail = NULL; + } + + // Delete the node: + free(currentNode); + + return --(playerList->count); + } + currentNode = currentNode->next; + } +} + + // Returns the Player with the given name from a PlayerList, or NULL otherwise: struct Player * getFromPlayerList(char * playerName, struct PlayerList * playerList) {