<commit_before>var instance = {};

instance.cacheDuration = "1 second";

instance.route = '/api/client/:id/tv-pong.js';

instance.configure = function(config) {
  self = this;
  self.visibleItems = [{
            name: 'ball',
            x: Math.floor(Math.random() * 400),
            y: Math.floor(Math.random() * 400),
            width: '10',
            height: '10',
            image: '/images/ball.png',
            timestamp: Date.now(),
            velocity: {
                x: Math.floor(Math.random() * 51) - 25,
                y: Math.floor(Math.random() * 51) - 25,
            }
    }];
}

instance.render = function(req, res) {
    var clientId = req.params.id || false;

    var ball = self.visibleItems[0];
    var t_now = Date.now();
    var t_diff = (t_now - ball.timestamp)/1000;
    ball.x = ball.x + ball.velocity.x * t_diff;
    while ( ball.x > 500 || ball.x < 0 ) {
      if ( ball.x > 500 ) {
        ball.x = 2 * 500 - ball.x;
      } else {
        ball.x = - ball.x;
      }
      ball.velocity.x = - ball.velocity.x;
    }
    ball.y = ball.y + ball.velocity.y * t_diff;
    while ( ball.y > 500 || ball.y < 0 ) {
      if ( ball.y > 500 ) {
        ball.y = 2 * 500 - ball.y;
      } else {
        ball.y = - ball.y;
      }
      ball.velocity.y = - ball.velocity.y;
    }
    ball.timestamp = t_now;

    var data = {
        visibleItems: self.visibleItems,
        client: {
            id: clientId
        }
    };
    res.jsonp(data);
}

module.exports = instance;<commit_msg>Refactored tvPong to bound ball position functions.<commit_after>var instance = {};

instance.cacheDuration = "1 second";

instance.route = '/api/client/:id/tv-pong.js';

var visibleItems = [{
  name: 'ball',
  x: Math.floor(Math.random() * 400),
  y: Math.floor(Math.random() * 400),
  width: '10',
  height: '10',
  image: '/images/ball.png',
  timestamp: Date.now(),
  velocity: {
    x: Math.floor(Math.random() * 51) - 25,
    y: Math.floor(Math.random() * 51) - 25,
  }
}];

instance.configure = function(config) {

}

instance.render = function(req, res) {
  var clientId = req.params.id || false;

  var ball = visibleItems[0];

  boundBallPosition(ball);

  var data = {
    visibleItems: visibleItems,
    client: {
      id: clientId
    }
  };
  res.jsonp(data);
}

function boundBallPosition(ball) {
  var t_now = Date.now();
  var t_diff = (t_now - ball.timestamp) / 1000;

  boundBallAxis(ball, 'x', t_diff);
  boundBallAxis(ball, 'y', t_diff);

  ball.timestamp = t_now;
}

function boundBallAxis(ball, axis, t_diff) {
  ball[axis] = ball[axis] + ball.velocity[axis] * t_diff;
  while (ball[axis] > 500 || ball[axis] < 0) {
    if (ball[axis] > 500) {
      ball[axis] = 2 * 500 - ball[axis];
    } else {
      ball[axis] = -ball[axis];
    }
    ball.velocity[axis] = -ball.velocity[axis];
  }
}

module.exports = instance;
{'fraction_non_alphanumeric': 0.16672492135616918, 'fraction_numerical': 0.028311779098217405, 'mean_word_length': 2.1589403973509933, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, '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': '4581622', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1011, 'n_words': 213}