rA7K9pxF
2017-01-11 16:36:20

var io = require('socket.io')(2558);
var MongoClient = require('mongodb').MongoClient;
var user = require('./UserData/user').user;

var mdbconfig=
{
username:'',
password:'',
db:''
};
var players = [];
var ids=1;
var usersdata;
io.on('connection', function(socket, res) {

MongoClient.connect('mongodb://'+mdbconfig.username+':'+mdbconfig.password+'@ds151208.mlab.com:51208/'+mdbconfig.db, function(err,db){
usersdata = new user(db);

});

console.log("usuario conectado");
socket.leave(socket.id);

socket.on('REGISTRO', function(data){
usersdata.addUser(data['usuario'], data['password'],data['email'], function(err,user){
if(err)
{
console.log(err);
}
else
{
console.log(user);
}

});
});
socket.on('PLAY', function(data){
usersdata.validateLogin(data['usuario'],data['password'], function(err,user){
if(err)
{
console.log(err);
}
else
{
console.log(user);
}
});

var player = {
jugador:
{
id:ids++,
name:data['usuario']
}

};
players[socket.id] =player;
JSON.stringify(players);
console.log("client connected, id = ", players);


socket.emit('PLAY',data);
});


socket.on('crearSala', function(){
console.log("data: "+players[socket.id]["jugador"]["name"]);
socket.join('SALA DE: '+players[socket.id]["jugador"]["name"]);
console.log(io.sockets.adapter.rooms);
//socket.emit('crearSala',data);
});

socket.on('entrarSala', function(data){
console.log("entre en entrarSala");
socket.join(data);
console.log(io.sockets.adapter.rooms);
});
socket.on('listaSalas', function(){
var salas=io.sockets.adapter.rooms;
var totalSalas=[];

if(salas)
{
for (var sala in salas)
{
if(!salas[sala].hasOwnProperty(sala))
{
var obj =
{
id:socket.id,
name:sala,
ping:0
};
totalSalas.push(obj);
}
}
}
console.log("Entre en listSalas");
socket.send(totalSalas);
});


});


console.log("------- server is running -------");