<commit_before>var EventEmitter = require('events').EventEmitter
  , _ = require('underscore')
  , genHash = require('./genHash')
  ;

function Player(ws, id, name, index, TIME, isBot) {
  this.isBot = isBot;
  this.id = id;
  this.name = name;
  this.packs = [];
  this.picks = [];
  this.time = 0;
  this.TIME = isBot ? 1 : TIME;
  this.hasAutoPicked = false;
  this.dropped = false;

  _.bindAll(this);
  this.reset(ws, index);
}

_.extend(Player.prototype, EventEmitter.prototype);

Player.prototype.generateHash = function(deck) {
  if (this.hash) return;
  this.hash = genHash(deck);
  this.emit('meta');
};

Player.prototype.send = function (name, args) {
  if (this.closed) return;
  this.ws.json(name, args);
};

Player.prototype.changeName = function(name) {
  this.name = name;
  this.emit('meta');
};

Player.prototype.receive = function(pack) {
  if (this.packs.push(pack) === 1) {
    this.time = this.TIME;
    this.updateSelf();
  }
};

Player.prototype.pick = function(packId, cardId) {
  this.hasAutoPicked = false;
  this.pickCard(packId, cardId);
};

Player.prototype.autoPick = function() {
  if (this.hasAutoPicked && !this.isBot) {
    this.dropped = true;
    this.TIME = 1;
  }
  else
    this.hasAutoPicked = true;

  var packs = this.packs
    , pack = packs[0]
    , packId = pack.id
    , cardId = pack.cards[0].id
    ;
  this.pickCard(packId, cardId);

  if (this.dropped && this.packs.length)
    this.autoPick();
};

Player.prototype.pickCard = function(packId, cardId) {
  var packs = this.packs
    , pack = packs[0]
    , cards
    , card
    ;

  if ((!pack) || (pack.id !== packId))
    return; // lol networks

  cards = pack.cards;
  card = _.find(cards, function(card) { return card.id === cardId; });
  this.picks.push(card);
  cards.splice(cards.indexOf(card), 1);
  pack = packs.shift()
  this.time = packs.length ? this.TIME : 0;
  this.updateSelf(card);
  this.emit('pass', pack);
};

Player.prototype.updateSelf = function(card) {
  if (card)
    this.send('pick', card);
  this.send('pack', this.packs[0]);
};

Player.prototype.wsClose = function() {
  this.closed = true;
  this.emit('close', this);
};

Player.prototype.end = function() {
  this.ws.close();
  this.closed = true;
};

Player.prototype.reset = function(ws, index) {
  this.closed = false;
  this.ws = ws;
  this.index = index;
  ws.on('name', this.changeName);
  ws.on('close', this.wsClose);
  ws.on('pick', this.pick);
  ws.on('hash', this.generateHash);
  this.send('picks', this.picks);
  this.updateSelf();
};

module.exports = Player;
<commit_msg>auto pick a random card<commit_after>var EventEmitter = require('events').EventEmitter
  , _ = require('underscore')
  , genHash = require('./genHash')
  ;

function Player(ws, id, name, index, TIME, isBot) {
  this.isBot = isBot;
  this.id = id;
  this.name = name;
  this.packs = [];
  this.picks = [];
  this.time = 0;
  this.TIME = isBot ? 1 : TIME;
  this.hasAutoPicked = false;
  this.dropped = false;

  _.bindAll(this);
  this.reset(ws, index);
}

_.extend(Player.prototype, EventEmitter.prototype);

Player.prototype.generateHash = function(deck) {
  if (this.hash) return;
  this.hash = genHash(deck);
  this.emit('meta');
};

Player.prototype.send = function (name, args) {
  if (this.closed) return;
  this.ws.json(name, args);
};

Player.prototype.changeName = function(name) {
  this.name = name;
  this.emit('meta');
};

Player.prototype.receive = function(pack) {
  if (this.packs.push(pack) === 1) {
    this.time = this.TIME;
    this.updateSelf();
  }
};

Player.prototype.pick = function(packId, cardId) {
  this.hasAutoPicked = false;
  this.pickCard(packId, cardId);
};

Player.prototype.autoPick = function() {
  if (this.hasAutoPicked && !this.isBot) {
    this.dropped = true;
    this.TIME = 1;
  }
  else
    this.hasAutoPicked = true;

  var packs = this.packs
    , pack = packs[0]
    , packId = pack.id
    , cardId = _.shuffle(pack)[0].id
    ;
  this.pickCard(packId, cardId);

  if (this.dropped && this.packs.length)
    this.autoPick();
};

Player.prototype.pickCard = function(packId, cardId) {
  var packs = this.packs
    , pack = packs[0]
    , cards
    , card
    ;

  if ((!pack) || (pack.id !== packId))
    return; // lol networks

  cards = pack.cards;
  card = _.find(cards, function(card) { return card.id === cardId; });
  this.picks.push(card);
  cards.splice(cards.indexOf(card), 1);
  pack = packs.shift()
  this.time = packs.length ? this.TIME : 0;
  this.updateSelf(card);
  this.emit('pass', pack);
};

Player.prototype.updateSelf = function(card) {
  if (card)
    this.send('pick', card);
  this.send('pack', this.packs[0]);
};

Player.prototype.wsClose = function() {
  this.closed = true;
  this.emit('close', this);
};

Player.prototype.end = function() {
  this.ws.close();
  this.closed = true;
};

Player.prototype.reset = function(ws, index) {
  this.closed = false;
  this.ws = ws;
  this.index = index;
  ws.on('name', this.changeName);
  ws.on('close', this.wsClose);
  ws.on('pick', this.pick);
  ws.on('hash', this.generateHash);
  this.send('picks', this.picks);
  this.updateSelf();
};

module.exports = Player;

{'fraction_non_alphanumeric': 0.17744127353911862, 'fraction_numerical': 0.0038827412152980002, 'mean_word_length': 3.407185628742515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '52753', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1884, 'n_words': 360}