<commit_before>var router = getRouter();
var socket = rootRequire('app/helpers/socket');
var io = socket('/projects');
var Project = require('./model');

/**
* Get a pair of projects to vote on
* GET /projects/pair
*/
router.get('/projects/pair', function (req, res) {
  Project
    .find()
    .exec(function (err, projects) {
      if (err) return res.internalError();
      var response = projects.slice(0, 2);
      return res.json({projects: response});
    });
});

/**
* Choose a winner from a pair
* PUT /projects/vote
*/
router.put('/projects/pair', function (req, res) {
  Project
    .find()
    .or([{_id: req.body.winner}, {_id: req.body.loser}])
    .exec(function (err, projects) {
      if (err) return res.internalError();
      var winner = (projects[0]._id == req.body.winner) ? projects[0] : projects[1];
      var loser = (projects[0]._id == req.body.loser) ? projects[0] : projects[1];

      var winnerExpected = Project.Elo.expected(winner.rating, loser.rating);
      var loserExpected = Project.Elo.expected(loser.rating, winner.rating);

      winner.rating = Project.Elo.rate(winnerExpected, winner.rating, true);
      loser.rating = Project.Elo.rate(loserExpected, loser.rating, false);

      winner.save(function (err, winner) {
        if (err) return res.internalError();
        loser.save(function (err, loser) {
          if (err) return res.internalError();
          return res.json({
            projects: [winner, loser]
          });
        });
      });
    });
});

/**
* Add a new project
* POST /projects
*/
router.post('/projects', function (req, res) {
  var errors = Project.validate(req.body);
  if (errors.length) return res.multiError(errors);

  var project = new Project(req.body);
  project.save(function (err, project) {
    if (err) return res.singleError('That project already exists');
    io.emit('create', project)
    return res.json(project);
  });<commit_msg>Emit project rating changes on sockets<commit_after>var router = getRouter();
var socket = rootRequire('app/helpers/socket');
var io = socket('/projects');
var Project = require('./model');

/**
* Get a pair of projects to vote on
* GET /projects/pair
*/
router.get('/projects/pair', function (req, res) {
  Project
    .find()
    .exec(function (err, projects) {
      if (err) return res.internalError();
      var response = projects.slice(0, 2);
      return res.json({projects: response});
    });
});

/**
* Choose a winner from a pair
* PUT /projects/vote
*/
router.put('/projects/pair', function (req, res) {
  Project
    .find()
    .or([{_id: req.body.winner}, {_id: req.body.loser}])
    .exec(function (err, projects) {
      if (err) return res.internalError();
      var winner = (projects[0]._id == req.body.winner) ? projects[0] : projects[1];
      var loser = (projects[0]._id == req.body.loser) ? projects[0] : projects[1];

      var winnerExpected = Project.Elo.expected(winner.rating, loser.rating);
      var loserExpected = Project.Elo.expected(loser.rating, winner.rating);

      winner.rating = Project.Elo.rate(winnerExpected, winner.rating, true);
      loser.rating = Project.Elo.rate(loserExpected, loser.rating, false);

      winner.save(function (err, winner) {
        if (err) return res.internalError();
        loser.save(function (err, loser) {
          if (err) return res.internalError();
          io.emit('update', winner);
          io.emit('update', loser);
          return res.json({
            projects: [winner, loser]
          });
        });
      });
    });
});

/**
* Add a new project
* POST /projects
*/
router.post('/projects', function (req, res) {
  var errors = Project.validate(req.body);
  if (errors.length) return res.multiError(errors);

  var project = new Project(req.body);
  project.save(function (err, project) {
    if (err) return res.singleError('That project already exists');
    io.emit('create', project)
    return res.json(project);
  });
{'fraction_non_alphanumeric': 0.16371008616320323, 'fraction_numerical': 0.0040547389761784085, 'mean_word_length': 3.0859213250517596, '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': '7526524', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1222, 'n_words': 307}