2015-06-10 16:12:52 +00:00
|
|
|
var User = (function(){
|
|
|
|
var User = function(socket){
|
|
|
|
if(!(this instanceof User)){
|
|
|
|
return (new User(socket));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
this.socket = socket;
|
2015-06-15 19:03:12 +00:00
|
|
|
this._rooms = [];
|
2015-06-10 16:12:52 +00:00
|
|
|
this._id = socket.id;
|
2015-06-13 07:58:55 +00:00
|
|
|
this.generateName();
|
2015-06-10 16:12:52 +00:00
|
|
|
};
|
|
|
|
var r = User.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
|
|
|
|
r._id = null;
|
2015-06-13 07:58:55 +00:00
|
|
|
r._name = null;
|
2015-06-15 19:03:12 +00:00
|
|
|
r._rooms = null;
|
2015-06-10 16:12:52 +00:00
|
|
|
r.socket = null;
|
|
|
|
|
|
|
|
r.getID = function(){
|
|
|
|
return this._id;
|
|
|
|
}
|
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
r.joinRoom = function(roomid){
|
2015-06-15 19:03:12 +00:00
|
|
|
var self = this;
|
|
|
|
/*this.socket.on(roomid, function(d) {
|
|
|
|
var event = d.event, data = d.data;
|
|
|
|
self.socket.on(event, data);
|
|
|
|
});*/
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.send = function(event, data, room){
|
|
|
|
room = room || null;
|
|
|
|
data = data || null;
|
|
|
|
if(!room){
|
|
|
|
this.socket.emit(event, data);
|
|
|
|
}
|
2015-06-15 19:03:12 +00:00
|
|
|
else {/*
|
|
|
|
this.socket.to(room).emit(event, data);*/
|
|
|
|
this.socket.global.publish(room, {
|
|
|
|
event: event,
|
|
|
|
data: data
|
|
|
|
})
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
r.generateName = function(){
|
|
|
|
var name = "Player" + (((Math.random() * 8999) + 1000) | 0);
|
|
|
|
//if(lobby.hasUser(name)) return generateName();
|
|
|
|
this._name = name;
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.setName = function(name) {
|
|
|
|
console.log("user name changed from %s to %s", this._name, name);
|
|
|
|
this._name = name;
|
|
|
|
}
|
2015-06-18 13:31:36 +00:00
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
r.getName = function() {
|
|
|
|
return this._name;
|
|
|
|
}
|
2015-06-18 13:31:36 +00:00
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
r.getRoom = function() {
|
2015-06-15 19:03:12 +00:00
|
|
|
return this._rooms[0];
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
2015-06-18 13:31:36 +00:00
|
|
|
|
2015-06-15 19:03:12 +00:00
|
|
|
r.addRoom = function(room) {
|
|
|
|
this._rooms.push(room);
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.disconnect = function() {
|
2015-06-18 13:31:36 +00:00
|
|
|
var self = this;
|
|
|
|
this._rooms.forEach(function(room) {
|
|
|
|
room.leave(self);
|
|
|
|
})
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 16:12:52 +00:00
|
|
|
return User;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = User;
|