Hosting a game on a server I have the following code but it's a bit messy and hard to look at. I could include a function to host a game so that block of code isn't repeating, but could this be optimized any further?

server.em.addListener('hostGame', function(settings)
{
    if (Object.keys(hostingGames).length != 0) //If nobody is hosting a game
        {
            for (i in hostingGames) //If there is more than 1 game being hosted, scroll through each one
            {
                if(hostingGames[i].userId === settings.userId) //Check if any of them are by the current user
                {
                    break; //if there is, dont let them host a new one
                }
                else
                {
                    var hostGame = new HostGame(); //If the player isnt hosting a game, host a new game for them

                    hostGame.initialise(settings.userId, settings.userName, settings.boardSize, settings.gameMode, settings.gameNote);
                    hostingGames[settings.userId] = hostGame;
                    server.consoleLog('APP', settings.userName + ' is hosting a game. ID: ' + settings.userId);
                    server.updateGamesList(hostingGames);
                }
            }
        }
        else //If there is no games being hosted, the user must not be hosting any, so let them host a new game
        {
            var hostGame = new HostGame();

            hostGame.initialise(settings.userId, settings.userName, settings.boardSize, settings.gameMode, settings.gameNote);
            hostingGames[settings.userId] = hostGame;
            server.consoleLog('APP', settings.userName + ' is hosting a game. ID: ' + settings.userId);
            server.updateGamesList(hostingGames);
        }
});
Your indents could use a little work. 
Typical JS use the collapsed braces or "egyptian braces", where the opening brace is on the same line as the opening statement.
Use template literals to construct strings. 
Comments be their own line. That way, they're readable from the very start and don't run off to the right.
Do the "early return" pattern, where you return on invalid cases until you can assume validity. That way, you avoid deeply nested conditions.


Here's a more concise version:

server.em.addListener('hostGame', function(settings){

  // Get essential values. You can inline them, but I prefer putting them in
  // variables for clarity.
  const gameKeys = Object.keys(hostingGames);
  const gameUids = gameKeys.map(key =&gt; hostingGames[key].userId);
  const userHasGame = gameUids.includes(settings.userId);

  // The only time the code ever bails out is when the user has a game.
  if(userHasGame) return;

  // userHasGame can also be false if there are no games since gameKeys will
  // be empty, causing gameUids to be empty, causing includes to be false.

  // Otherwise, a game is created.
  const hostGame = new HostGame();
  hostGame.initialise(settings.userId, settings.userName, settings.boardSize, settings.gameMode, settings.gameNote);
  hostingGames[settings.userId] = hostGame;
  server.updateGamesList(hostingGames);

  server.consoleLog('APP', `${settings.userName} is hosting a game. ID: ${settings.userId}`);
});