1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-11-23 19:36:53 +00:00
not-gwent-online/server/User.js

141 lines
2.7 KiB
JavaScript
Raw Normal View History

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-24 16:22:56 +00:00
this._events();
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-24 16:22:56 +00:00
r._inQueue = false;
2015-06-10 16:12:52 +00:00
r.socket = null;
r.disconnected = false;
2015-06-10 16:12:52 +00:00
r.getID = function(){
return this._id;
}
r.send = function(event, data, room){
room = room || null;
data = data || null;
if(!room){
this.socket.emit(event, data);
}
2015-06-24 16:22:56 +00:00
else {
this.socket.to(room).emit(event, data);
2015-06-10 16:12:52 +00:00
}
}
2015-06-13 07:58:55 +00:00
r.generateName = function(){
2015-07-01 16:55:50 +00:00
var name = "Guest" + (((Math.random() * 8999) + 1000) | 0);
2015-06-13 07:58:55 +00:00
this._name = name;
return name;
}
r.setName = function(name) {
2015-07-05 08:02:55 +00:00
name = name.slice(0, 20);
2015-06-13 07:58:55 +00:00
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-19 12:14:37 +00:00
r.setDeck = function(deck) {
2015-07-01 17:51:09 +00:00
//console.log("set deck: ", deck);
2015-06-19 12:14:37 +00:00
this._deck = deck;
}
r.getDeck = function() {
2015-07-01 16:55:50 +00:00
return this._deck;
2015-06-19 12:14:37 +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.cleanUp = function() {
for(var i=0; i<this._rooms.length; i++) {
var room = this._rooms[i];
if(room[i] === null) {
this._rooms.splice(i, 1);
return this.cleanUp();
}
}
}
2015-06-13 07:58:55 +00:00
r.disconnect = function() {
2015-06-18 13:31:36 +00:00
var self = this;
this.disconnected = true;
2015-06-24 16:22:56 +00:00
matchmaking.removeFromQueue(this);
2015-06-18 13:31:36 +00:00
this._rooms.forEach(function(room) {
room.leave(self);
if(!room.hasUser()) {
2015-07-01 17:51:09 +00:00
//console.log("Remove room: ", room.getID());
room = null;
}
2015-06-18 13:31:36 +00:00
})
this.cleanUp();
2015-06-13 07:58:55 +00:00
}
2015-06-24 16:22:56 +00:00
r._events = function() {
var socket = this.socket;
var self = this;
socket.on("request:name", function(data){
if(data && data.name){
self.setName(data.name);
}
socket.emit("response:name", {name: self.getName()});
})
socket.on("request:matchmaking", function() {
if(self._inQueue) return;
matchmaking.findOpponent(self);
});
socket.on("request:gameLoaded", function(data){
2015-07-01 17:51:09 +00:00
//console.log(data);
2015-06-24 16:22:56 +00:00
connections.roomCollection[data._roomID].setReady(self);
})
socket.on("set:deck", function(data) {
2015-07-01 17:51:09 +00:00
//console.log(data);
2015-06-24 16:22:56 +00:00
if(data && data.deck){
self.setDeck(data.deck);
}
})
}
2015-06-13 07:58:55 +00:00
2015-06-10 16:12:52 +00:00
return User;
})();
module.exports = User;