<commit_before>var events = require('events');
var core   = require('./core2048.js');

var SocketInputManager = function(io) {
  this.io = io;
  this.eventEmitter = new events.EventEmitter();
  this.clientsMap = new Map();
  this.usernames = new Set();

  this.listen(io);
}

SocketInputManager.prototype.on = function(evt, callback) {
  this.eventEmitter.on(evt, callback);
}

SocketInputManager.prototype.listen = function(io) {
  var self = this;
  io.on('connection', function(socket) {
    self.clientsMap.set(socket.id, self.randomUsername());

    socket.on('move', function(data) {
      self.onMove(socket, data);
    });
    socket.on('keepPlaying', function() {
      self.eventEmitter.emit('keepPlaying');
      self.io.emit('keepPlaying');
    });
    socket.on('restart', function() {
      self.eventEmitter.emit('restart');
    });
    socket.on('chatMessage', function(data) {
      self.onChatMessage(socket, data);
    });
    socket.on('disconnect', function(){
      self.usernames.delete(self.clientsMap.get(socket.id));
      self.clientsMap.delete(socket.id);
    });
  });
};

SocketInputManager.prototype.onChangeUsername = function (data) {
  var usernameData = JSON.parse(data);
  usernames.delete(clientsMap.get(usernameData.socketID));
  usernames.add(usernameData.newUsername);
  clientsMap.set(usernameData.socketID, usernameData.newUsername); 
}

SocketInputManager.prototype.randomUsername = function() {
  var max = 2 * this.clientsMap.size;
  do {
    var id = Math.round(Math.random() * max);
    var username = "guest_" + id;
  } while (this.usernames.has(username));

  this.usernames.add(username);
  return username;
}

SocketInputManager.prototype.onMove = function(socket, data) {
  var directions = [core.Direction.UP, core.Direction.LEFT, core.Direction.DOWN, core.Direction.RIGHT];
  var dir = JSON.parse(data);
  if (directions.indexOf(dir) > -1) {
    this.eventEmitter.emit('move', dir);<commit_msg>Fixing onChangeUsername<commit_after>var events = require('events');
var core   = require('./core2048.js');

var SocketInputManager = function(io) {
  this.io = io;
  this.eventEmitter = new events.EventEmitter();
  this.clientsMap = new Map();
  this.usernames = new Set();

  this.listen(io);
}

SocketInputManager.prototype.on = function(evt, callback) {
  this.eventEmitter.on(evt, callback);
}

SocketInputManager.prototype.listen = function(io) {
  var self = this;
  io.on('connection', function(socket) {
    self.clientsMap.set(socket.id, self.randomUsername());

    socket.on('move', function(data) {
      self.onMove(socket, data);
    });
    socket.on('keepPlaying', function() {
      self.eventEmitter.emit('keepPlaying');
      self.io.emit('keepPlaying');
    });
    socket.on('restart', function() {
      self.eventEmitter.emit('restart');
    });
    socket.on('chatMessage', function(data) {
      self.onChatMessage(socket, data);
    });
    socket.on('disconnect', function(){
      self.usernames.delete(self.clientsMap.get(socket.id));
      self.clientsMap.delete(socket.id);
    });
  });
};

SocketInputManager.prototype.onChangeUsername = function (data) {
  var usernameData = JSON.parse(data);
  this.usernames.delete(this.clientsMap.get(usernameData.socketID));
  this.usernames.add(usernameData.newUsername);
  this.clientsMap.set(usernameData.socketID, usernameData.newUsername); 
}

SocketInputManager.prototype.randomUsername = function() {
  var max = 2 * this.clientsMap.size;
  do {
    var id = Math.round(Math.random() * max);
    var username = "guest_" + id;
  } while (this.usernames.has(username));

  this.usernames.add(username);
  return username;
}

SocketInputManager.prototype.onMove = function(socket, data) {
  var directions = [core.Direction.UP, core.Direction.LEFT, core.Direction.DOWN, core.Direction.RIGHT];
  var dir = JSON.parse(data);
  if (directions.indexOf(dir) > -1) {
    this.eventEmitter.emit('move', dir);
{'fraction_non_alphanumeric': 0.15843097300050943, 'fraction_numerical': 0.003056546102903719, 'mean_word_length': 4.167105263157895, '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': '1417567', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1278, 'n_words': 202}