<commit_before>var _ = require('lodash');
var Firebase = require('firebase');
var names = ['Ahmed', 'Russell', 'Sigrunn', 'Juan', 'Tom', 'Jack', 'Somebody'];
var rooms = [];

// simualate a random person entering, staying for a duration, and leaving
function simulate(){
  var name = names[Math.floor(Math.random()*names.length)]; 
  var room = rooms[Math.floor(Math.random()*rooms.length)];
  var duration = 1 + 5 * Math.random();
  var vote = Math.floor(Math.random()*2);
  //Timestamps
  var now = new Date();
  var time = now.getHours() + " hours " + now.getMinutes() + " minutes " + now.getSeconds() + " seconds";
  
  console.log(name);
  console.log(room);
  console.log(vote);
  console.log(time);
  
  var person = {
    name: name,
	room: room,
    vote: vote,	
	time: time
  }
  // simulate this person entering
  enter(person);
  
  //Vote a bunch before leaving
  setTimeout(function(){
    updateVote(person)
  }, 1000)
  // simulate this person leaving after 'duration' seconds
  setTimeout(function(){
    leave(person)
  }, 8000)
}

function enter(person){
  console.log('enter', person);
  // TODO: put this person in the Firebase
  // var ref = new Firebase('your-firebase-url')
  // ...
  var ref = new Firebase('https://team-polive.firebaseio.com/')
  var usersRef = ref.child("users");
  
  usersRef.child(person.name).set({
    name: person.name,
	room: person.room,
	vote: person.vote,
	lastVoted: person.time
  });

  var newTaskRef = ref.push();
  
  //Let 'em vote at entering
  updateVote(person);
}

//Let the bots vote
function updateVote(person){
  console.log('Whoah this person has an opinion!');
  var ref = new Firebase('https://team-polive.firebaseio.com/');
  var usersRef = ref.child("users");
  var voteRef = new Firebase('https://team-polive.firebaseio.com/rooms/');
  //Timestamps
  var now = new Date();
  var time = now.getHours() + " hours " + now.getMinutes() + " minutes " + now.getSeconds() + " seconds";
  // read data from the location once
  var prevVote = -1
  usersRef.child(person.name).once('value', function(snapshot){
    var tmp = snapshot.val()
    prevVote = tmp.vote
    }, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
    });

  voteRef.child(person.room).once('value', function(snapshot){
	snapshot.forEach(function(childSnapshot) {
	 var key = childSnapshot.key();
	 var childData = childSnapshot.val();
   if (prevVote==1){
    var total = childData
    voteRef.child(person.room).update({
       yes: total-1
     });
   }
   if (prevVote==0){
    var total = childData
    voteRef.child(person.room).update({
       no: total-1
     });
   }

	 //user votes yes
	 if(person.vote == 1){
	   if(key == 'yes'){
		   var total = childData + 1;
		   voteRef.child(person.room).update({
		   yes: total
		 });
	   }
	 } 
	 //user votes no
	 else{
	   if(key == 'no'){<commit_msg>some fixes<commit_after>var _ = require('lodash');
var Firebase = require('firebase');
var names = ['Ahmed', 'Russell', 'Sigrunn', 'Juan', 'Tom', 'Jack', 'Somebody'];
var rooms = [];

// simualate a random person entering, staying for a duration, and leaving
function simulate(){
  var name = names[Math.floor(Math.random()*names.length)]; 
  var room = rooms[Math.floor(Math.random()*rooms.length)];
  var duration = 1 + 5 * Math.random();
  var vote = Math.floor(Math.random()*2);
  //Timestamps
  var now = new Date();
  var time = now.getHours() + " hours " + now.getMinutes() + " minutes " + now.getSeconds() + " seconds";
  
  console.log(name);
  console.log(room);
  console.log(vote);
  console.log(time);
  
  var person = {
    name: name,
	room: room,
    vote: vote,	
	time: time
  }
  // simulate this person entering
  enter(person);
  
  //Vote a bunch before leaving
  setTimeout(function(){
    updateVote(person)
  }, 1000)
  // simulate this person leaving after 'duration' seconds
  setTimeout(function(){
    leave(person)
  }, 8000)
}

function enter(person){
  console.log('enter', person);
  // TODO: put this person in the Firebase
  // var ref = new Firebase('your-firebase-url')
  // ...
  var ref = new Firebase('https://team-polive.firebaseio.com/')
  var usersRef = ref.child("users");
  
  usersRef.child(person.name).set({
    name: person.name,
	room: person.room,
	vote: person.vote,
	lastVoted: person.time
  });

  var newTaskRef = ref.push();
  
  //Let 'em vote at entering
  updateVote(person);
}

//Let the bots vote
function updateVote(person){
  console.log('Whoah this person has an opinion!');
  var ref = new Firebase('https://team-polive.firebaseio.com/');
  var usersRef = ref.child("users");
  var voteRef = new Firebase('https://team-polive.firebaseio.com/rooms/');
  //Timestamps
  var now = new Date();
  var time = now.getHours() + " hours " + now.getMinutes() + " minutes " + now.getSeconds() + " seconds";
  
  //Snag old vote value
  var prevVote = -1
  usersRef.child(person.name).once('value', function(snapshot){
    var tmp = snapshot.val()
    prevVote = tmp.vote//get old vote before update occurs
    }, function (errorObject) {
       console.log("The read failed: " + errorObject.code);
    });
  
  //Update with new vote
  var newVote = Math.floor(Math.random()*2); 
  usersRef.child(person.name).update({
	vote: newVote
  });
  
  voteRef.child(person.room).once('value', function(snapshot){
	snapshot.forEach(function(childSnapshot) {
	 var key = childSnapshot.key();
	 var childData = childSnapshot.val();
	 
     if (prevVote==1){
	  if(key == 'yes'){
	   if(childData > 0){	
		  childData = childData - 1//update childData value before updating vote below
		  var total = childData
		  voteRef.child(person.room).update({
			yes: total
		  });
	   }
	  }
     }
	 
     if (prevVote==0){
	  if(key == 'no'){
	   if(childData > 0){		   
		   childData = childData - 1//update childData value before updating vote below
		   var total = childData
		   voteRef.child(person.room).update({
			 no: total
		   });
	   }
	  }
     }

	 //user votes yes
	 if(person.vote == 1){
	   if(key == 'yes'){
		   var total = childData + 1;
		   voteRef.child(person.room).update({
		   yes: total
		 });
	   }
	 } 
	 //user votes no
	 else{
	   if(key == 'no'){
{'fraction_non_alphanumeric': 0.16149870801033592, 'fraction_numerical': 0.006298449612403101, 'mean_word_length': 3.1369405477621912, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '4926406', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2061, 'n_words': 511}