HoeenCoder/Cities

First user to connect is not removed when they disconnect.

Closed this issue · 1 comments

The first user to connect to the server after it starts will not be removed from the users array, and their tents (and most likely workers and soldiers when they are added) do not disappear from the canvas. I've looked into this and the code seems correct

Disconnection handler:

socket.on('disconnect', function() {
    if(searchUsers(currentPlayer.id)) {
        users.splice(searchUsers(currentPlayer.id), 1);
    }
    console.log('[INFO] User ' + currentPlayer.name + ' disconnected.');
});

searchUsers function

function searchUsers(id) {
    for(let i = 0; i < users.length; i++) {
        if(users[i].id === id) return i;
    }
    return false;
}

This should work, but for some reason its not. Can you take a look at it @Dubstepper?
Thanks in advance.

0 is a false-y value so when i === 0 the disconnect handler thinks it returned false and doesn't splice.

Fixed in the above commit.