<commit_before>//var io = require("socket.io").listen(8080);
var io = require("socket.io").listen(8080, { log: false });


//REST Server
var restify = require('restify');
var server = restify.createServer({ name: '2048-Rest' });

var gameState;

server
  .use(restify.fullResponse())
  .use(restify.bodyParser())


var socket_global;

var latest = 0;
var game_state;

var current_state = "anarchy";
var democracy_votes = 0;
var anarchy_votes = 1;

var move_votes = [0, 0, 0, 0];  // 0: up, 1: right, 2: down, 3: left
var last_moves = [0, 0, 0, 0, 0, 0, 0, 0];

function vote_counter()  {
	if(anarchy_votes > democracy_votes) {
		current_state = "anarchy";
		clearInterval(vote_checker_democracy);
	} else {
		current_state = "democracy";
		vote_checker_democracy = setInterval(vote_counter_democracy, 5000);
	}
	socket_global.emit("game-mode", current_state);
    socket_global.broadcast.emit("game-mode", current_state);
    //console.log("Counting votes: New mode is " + current_state);
}

function vote_counter_democracy()  {
	var max = -1;
	var maxScore = -1;
	for(var i = 0; i < move_votes.length; i++) {
		if(move_votes[i] > maxScore) {
			maxScore = move_votes[i];
			max = i;
		}
	}
    if(last_moves[max*2] != null && last_moves[max*2 + 1] != null && maxScore != 0) {
    	socket_global.emit("move", {'direction':max, 'value1':last_moves[max*2], 'cell1':last_moves[max*2 + 1]});
    	socket_global.broadcast.emit("move", {'direction':max, 'value1':last_moves[max*2], 'cell1':last_moves[max*2 + 1]});
	}
    move_votes = [0, 0, 0, 0];
    //console.log("Counting democracy votes: Move is " + max);
}

var vote_checker;
var vote_checker_democracy;

io.sockets.on("connection", function (socket) {
	socket_global = socket;
	vote_checker = setInterval(vote_counter, 10000);

  socket.on("democracy-vote", function() {
  	console.log("Democracy vote");
  	democracy_votes++;
  });

  socket.on("anarchy-vote", function() {
  	console.log("Anarchy vote");
  	anarchy_votes++;
  });

  socket.on("game-over", function(data) {
    game_state = null;
  });

  socket.on("move", function (data) {
    if(data.timestamp > latest) {
	    if(current_state == "anarchy") {
	    	socket.emit("move", {'direction':data.direction, 'value1':data.value1, 'cell1':data.cell1}); 
	    	socket.broadcast.emit("move", {'direction':data.direction, 'value1':data.value1, 'cell1':data.cell1}); 
	    	latest = data.timestamp;
	    } else {
	    	move_votes[data.direction] = move_votes[data.direction] + 1;
	    	last_moves[data.direction*2] = data.value1;
	    	last_moves[data.direction*2 + 1] = data.cell1;
	    }
	}
  });
  
	socket.on("resetGame", function (data)
	{
		console.log("\n\ndeleting game state\n\n");
		console.log("new data:");
		console.log(data);
		if(valid(data))
			gameState = data;
		printGameState();
		socket.broadcast.emit("resetGame");
	});
});

//Aparece na consola
server.listen(3000, function () {
  //console.log('%s listening at %s', server.name, server.url)
});<commit_msg>Peco desculpa diogo<commit_after>//var io = require("socket.io").listen(8080);
var io = require("socket.io").listen(8080, { log: false });


//REST Server
var restify = require('restify');
var server = restify.createServer({ name: '2048-Rest' });

var gameState;

server
  .use(restify.fullResponse())
  .use(restify.bodyParser())


var socket_global;

var latest = 0;
var game_state;

var current_state = "anarchy";
var democracy_votes = 0;
var anarchy_votes = 1;

var move_votes = [0, 0, 0, 0];  // 0: up, 1: right, 2: down, 3: left
var last_moves = [0, 0, 0, 0, 0, 0, 0, 0];

function vote_counter()  {
	if(anarchy_votes > democracy_votes) {
		current_state = "anarchy";
		clearInterval(vote_checker_democracy);
	} else {
		current_state = "democracy";
		vote_checker_democracy = setInterval(vote_counter_democracy, 5000);
	}
	socket_global.emit("game-mode", current_state);
    socket_global.broadcast.emit("game-mode", current_state);
    //console.log("Counting votes: New mode is " + current_state);
}

function vote_counter_democracy()  {
	var max = -1;
	var maxScore = -1;
	for(var i = 0; i < move_votes.length; i++) {
		if(move_votes[i] > maxScore) {
			maxScore = move_votes[i];
			max = i;
		}
	}
    if(last_moves[max*2] != null && last_moves[max*2 + 1] != null && maxScore != 0) {
    	socket_global.emit("move", {'direction':max, 'value1':last_moves[max*2], 'cell1':last_moves[max*2 + 1]});
    	socket_global.broadcast.emit("move", {'direction':max, 'value1':last_moves[max*2], 'cell1':last_moves[max*2 + 1]});
	}
    move_votes = [0, 0, 0, 0];
    //console.log("Counting democracy votes: Move is " + max);
}

var vote_checker;
var vote_checker_democracy;

io.sockets.on("connection", function (socket) {
	socket_global = socket;
	vote_checker = setInterval(vote_counter, 10000);

  socket.on("democracy-vote", function(data) {
  	//console.log("Democracy vote");
    if(data < vote_throttle[socket.id] + vote_throttle_n) {
      //refuse
      console.log("Refused");
      vote_throttle[socket.id] = data + vote_throttle_n * 3;
      return;
    }
    console.log("Accepted");
    vote_throttle[socket.id] = data;
  	democracy_votes++;
  });

  socket.on("anarchy-vote", function(data) {
  	//console.log("Anarchy vote");
    if(data < vote_throttle[socket.id] + vote_throttle_n) {
      //refuse
      console.log("Refused");
      vote_throttle[socket.id] = data + vote_throttle_n * 3;
      return;
    }
    console.log("Accepted");
    vote_throttle[socket.id] = data;
  	anarchy_votes++;
  });

  socket.on("game-over", function(data) {
    game_state = null;
  });

  socket.on("move", function (data) {
    if(data.timestamp < last_connection[socket.id] + throttle) {
      //refuse
      last_connection[socket.id] = data.timestamp + throttle * 3;
      return;
    }
    last_connection[socket.id] = data.timestamp;
    if(data.timestamp > latest) {
	    if(current_state == "anarchy") {
	    	socket.emit("move", {'direction':data.direction, 'value1':data.value1, 'cell1':data.cell1}); 
	    	socket.broadcast.emit("move", {'direction':data.direction, 'value1':data.value1, 'cell1':data.cell1}); 
	    	latest = data.timestamp;
	    } else {
	    	move_votes[data.direction] = move_votes[data.direction] + 1;
	    	last_moves[data.direction*2] = data.value1;
	    	last_moves[data.direction*2 + 1] = data.cell1;
	    }
	}
  });
  
	socket.on("resetGame", function (data)
	{
		console.log("\n\ndeleting game state\n\n");
		console.log("new data:");
		console.log(data);
		if(valid(data))
			gameState = data;
		printGameState();
		socket.broadcast.emit("resetGame");
	});
});

//Aparece na consola
server.listen(3000, function () {
  //console.log('%s listening at %s', server.name, server.url)
});
{'fraction_non_alphanumeric': 0.1734405240434718, 'fraction_numerical': 0.023969033794848892, 'mean_word_length': 4.0059612518628915, 'pattern_counts': {'":': 0, '<': 8, '<?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': '6666440', 'n_tokens_mistral': 2679, 'n_tokens_neox': 2489, 'n_words': 461}