Properly remove disconnected players
This commit is contained in:
parent
81fc72a1d7
commit
a66a07c897
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue