<commit_before>var Phoenix = require('../../vendor/phoenix');

module.exports = function(options) {
  var url = options.url;

  var start = function(id) {
    var socket = new Phoenix.Socket(url);

    socket.join("presence", "teachers", {userId: id, role: 'teacher'}, function(channel) {
      var messageCounts = {};
      var lastStats     = null;
      var numStudents   = 0;

      var tryToClaimStudent = function() {
        channel.send('claim:student', {
          teacherId: id
        });

        if(numStudents < 5 && (lastStats == null || lastStats.students.waiting > 0)) {
          setImmediate(function() {
            tryToClaimStudent();
          });
        }
      };

      var handleNewChat = function(chat) {
        numStudents++;
        socket.join("chats", chat.id, {userId: id, role: 'teacher'}, function(chatChannel) {
          console.log("Teacher " + id + " grabbed a new student.");

          var getMessageCount = function() {
            return messageCounts[chat.id] || 0;
          };

          var sendNextMessage = function() {
            var count = getMessageCount() + 1;
            messageCounts[chat.id] = count;

            chatChannel.send("teacher:send", {
              message: "Message from teacher: " + count
            });
          }

          chatChannel.on("chat:ready", function() {
            sendNextMessage();
          });

          chatChannel.on("chat:terminated", function(data) {
            //console.log("Teacher " + id + " is leaving a chat.");<commit_msg>Only trying to claim students if they aren't at max.<commit_after>var Phoenix = require('../../vendor/phoenix');

module.exports = function(options) {
  var url = options.url;

  var start = function(id) {
    var socket = new Phoenix.Socket(url);

    socket.join("presence", "teachers", {userId: id, role: 'teacher'}, function(channel) {
      var messageCounts = {};
      var lastStats     = null;
      var numStudents   = 0;

      var tryToClaimStudent = function() {
        if(numStudents < 5 && (lastStats == null || lastStats.students.waiting > 0)) {
          channel.send('claim:student', {
            teacherId: id
          });

          setImmediate(function() {
            tryToClaimStudent();
          });
        }
      };

      var handleNewChat = function(chat) {
        numStudents++;
        socket.join("chats", chat.id, {userId: id, role: 'teacher'}, function(chatChannel) {
          console.log("Teacher " + id + " grabbed a new student.");

          var getMessageCount = function() {
            return messageCounts[chat.id] || 0;
          };

          var sendNextMessage = function() {
            var count = getMessageCount() + 1;
            messageCounts[chat.id] = count;

            chatChannel.send("teacher:send", {
              message: "Message from teacher: " + count
            });
          }

          chatChannel.on("chat:ready", function() {
            sendNextMessage();
          });

          chatChannel.on("chat:terminated", function(data) {
            //console.log("Teacher " + id + " is leaving a chat.");
{'fraction_non_alphanumeric': 0.1407907425265188, 'fraction_numerical': 0.0032144005143040825, 'mean_word_length': 2.0242954324586977, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1963614', 'n_tokens_mistral': 884, 'n_tokens_neox': 850, 'n_words': 194}