<commit_before>var fs = require('fs');
var userSocketIdMap = {}; // dictionary of username:socketId
var socketIdUserMap = {}; // dictionary of socketId:username


module.exports = function(io){

  var chatManager = io.of('/chat');

  function updateUserList(){
    var userList = [];
    var anon_listeners = 0;
    for(var socket in socketIdUserMap){
      var username = socketIdUserMap[socket];
      if(username){
        if(userList.indexOf(username) === -1){
          userList.push(username);
        }
      }
      else{
        anon_listeners ++;
      }
    }
    if(anon_listeners > 0){
      userList.push('Plus ' + anon_listeners + ' anonymous guests');
    }
    chatManager.emit('userList', userList);
  }

  // connection event
  chatManager.on('connection', function(socket){
    console.log('Chat Client Connected :: ' + socket.id);
    socketIdUserMap[socket.id] = null;

    socket.emit('motd', 'Welcome to Lifeboat');

    // join the chatroom
    socket.on('join', function(username){

      // add to maps
      userSocketIdMap[username] = socket.id;
      socketIdUserMap[socket.id] = username;

      // send join event
      socket.broadcast.emit('user_join', username);
      // send userList to the user who joined
      updateUserList();
      console.log(username + ' joined chat!');
    });

    // send a message
    socket.on('messageToServer', function(message){

      var chatPayload = {
        sender: socketIdUserMap[socket.id],
        timestamp: new Date(),
        message: message
      };
      //socket.broadcast.emit('messageFromServer', chatPayload);
      chatManager.emit('messageFromServer', chatPayload);

      console.log('Chat: ', chatPayload.timestamp, chatPayload.sender, chatPayload.message);
    });

    // private message
    socket.on('privateMessage', function(payload){

      var privateMessage = {
        sender: socketIdUserMap[socket.id],
        timestamp: new Date(),
        message: payload.message
      };
      if(userSocketIdMap[payload.to]){
        chatManager.to(userSocketIdMap[payload.to]).emit('private_message', privateMessage);
        console.log('Chat-PM: ', privateMessage.timestamp, privateMessage.sender, payload.to, privateMessage.message);
      }
    });

    socket.on('updateUserList', function(){
      updateUserList();
      console.log(socketIdUserMap[socket.id] + 'requested the user list.');
    });

    socket.on('leave', function(){
      if(socketIdUserMap[socket.id]){
        var username = socketIdUserMap[socket.id];
        socket.broadcast.emit('user_left', username);
        console.log(username + ' left chat.');
        console.log('Chat Client Disconnected :: ' + socket.id);

        if(userSocketIdMap[username] === socket.id){
          delete userSocketIdMap[username];
        }
        delete socketIdUserMap[socket.id];

        updateUserList();
      }
    });

    socket.on('disconnect', function(){
      // send user left event
      if(socketIdUserMap[socket.id]){
        var username = socketIdUserMap[socket.id];
        socket.broadcast.emit('user_left', username);
        console.log(username + ' left chat.');
        console.log('Chat Client Disconnected :: ' + socket.id);

        if(userSocketIdMap[username] === socket.id){
          delete userSocketIdMap[username];
        }
        delete socketIdUserMap[socket.id];

        updateUserList();
      }
    });
  });
}<commit_msg>initial fix<commit_after>var fs = require('fs');
var userSocketIdMap = {}; // dictionary of username:socketId
var socketIdUserMap = {}; // dictionary of socketId:username


module.exports = function(io){

  var chatManager = io.of('/chat');

  function updateUserList(){
    var userList = [];
    var anon_listeners = 0;
    for(var socket in socketIdUserMap){
      var username = socketIdUserMap[socket];
      if(username != 'guest'){
        if(userList.indexOf(username) === -1){
          userList.push(username);
        }
      }
      else{
        anon_listeners ++;
      }
    }
    if(anon_listeners > 0){
      userList.push('Plus ' + anon_listeners + ' anonymous guests');
    }
    chatManager.emit('userList', userList);
  }

  // connection event
  chatManager.on('connection', function(socket){
    console.log('Chat Client Connected :: ' + socket.id);
    socketIdUserMap[socket.id] = 'guest';

    socket.emit('motd', 'Welcome to Lifeboat');

    // join the chatroom
    socket.on('join', function(username){

      // add to maps
      userSocketIdMap[username] = socket.id;
      socketIdUserMap[socket.id] = username;

      // send join event
      socket.broadcast.emit('user_join', username);
      // send userList to the user who joined
      updateUserList();
      console.log(username + ' joined chat!');
    });

    // send a message
    socket.on('messageToServer', function(message){

      var chatPayload = {
        sender: socketIdUserMap[socket.id],
        timestamp: new Date(),
        message: message
      };
      //socket.broadcast.emit('messageFromServer', chatPayload);
      chatManager.emit('messageFromServer', chatPayload);

      console.log('Chat: ', chatPayload.timestamp, chatPayload.sender, chatPayload.message);
    });

    // private message
    socket.on('privateMessage', function(payload){

      var privateMessage = {
        sender: socketIdUserMap[socket.id],
        timestamp: new Date(),
        message: payload.message
      };
      if(userSocketIdMap[payload.to]){
        chatManager.to(userSocketIdMap[payload.to]).emit('private_message', privateMessage);
        console.log('Chat-PM: ', privateMessage.timestamp, privateMessage.sender, payload.to, privateMessage.message);
      }
    });

    socket.on('updateUserList', function(){
      updateUserList();
      console.log(socketIdUserMap[socket.id] + 'requested the user list.');
    });

    socket.on('leave', function(){
      if(socketIdUserMap[socket.id]){
        var username = socketIdUserMap[socket.id];
        socket.broadcast.emit('user_left', username);
        console.log(username + ' left chat.');
        
        if(userSocketIdMap[username] === socket.id){
          delete userSocketIdMap[username];
        }
        socketIdUserMap[socket.id] = 'guest';

        updateUserList();
      }
    });

    socket.on('disconnect', function(){

      // if guest
      // - delete from socketIdUserMap
      // if user
      // - delete from socketIdUserMap
      // - delete from userSocketIdMap

      if(socketIdUserMap[socket.id] != 'guest'){
        var username = socketIdUserMap[socket.id];
        socket.broadcast.emit('user_left', username);
        console.log(username + ' left chat.');
        delete socketIdUserMap[socket.id];
        if(userSocketIdMap[username] === socket.id){
          delete userSocketIdMap[username];
        }
      } 
      else if(socketIdUserMap[socket.id] === 'guest'){
        console.log('guest left chat.');
        delete socketIdUserMap[socket.id];
      }
        console.log('Chat Client Disconnected :: ' + socket.id);
        updateUserList();
    });
  });
}
{'fraction_non_alphanumeric': 0.13838341091832415, 'fraction_numerical': 0.0008463817181548879, 'mean_word_length': 2.700417536534447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '567623', 'n_tokens_mistral': 2052, 'n_tokens_neox': 2009, 'n_words': 390}