2015-06-13 07:58:55 +00:00
|
|
|
var shortid = require("shortid");
|
2015-06-13 19:36:02 +00:00
|
|
|
var Battle = require("./Battle");
|
2015-06-13 07:58:55 +00:00
|
|
|
|
|
|
|
var Room = (function(){
|
2015-06-15 19:03:12 +00:00
|
|
|
var Room = function(scServer){
|
2015-06-13 07:58:55 +00:00
|
|
|
if(!(this instanceof Room)){
|
2015-06-15 19:03:12 +00:00
|
|
|
return (new Room(scServer));
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
|
2015-06-15 19:03:12 +00:00
|
|
|
var self = this;
|
2015-06-13 07:58:55 +00:00
|
|
|
this._id = shortid.generate();
|
2015-06-15 19:03:12 +00:00
|
|
|
this._users = [];
|
2015-06-13 19:36:02 +00:00
|
|
|
this._ready = {};
|
2015-06-15 19:03:12 +00:00
|
|
|
this.socket = scServer.global;
|
|
|
|
/*
|
|
|
|
this._channel = this.socket.subscribe(this._id);*/
|
|
|
|
|
|
|
|
/*this._channel.watch(function(data) {
|
|
|
|
*//*self._users.forEach(function(user) {
|
|
|
|
|
|
|
|
})*//*
|
|
|
|
});*/
|
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
};
|
|
|
|
var r = Room.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
r.MAX_USER = 2;
|
2015-06-15 19:03:12 +00:00
|
|
|
r._users = null;
|
2015-06-13 07:58:55 +00:00
|
|
|
r._id = null;
|
2015-06-13 19:36:02 +00:00
|
|
|
r._battle = null;
|
|
|
|
r._ready = null;
|
2015-06-15 19:03:12 +00:00
|
|
|
r._channel = null;
|
2015-06-13 07:58:55 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.getID = function(){
|
2015-06-13 07:58:55 +00:00
|
|
|
return this._id;
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.join = function(user){
|
2015-06-15 19:03:12 +00:00
|
|
|
if(this._users.length >= 2) return;
|
|
|
|
this._users.push(user);
|
|
|
|
user.addRoom(this);
|
|
|
|
/*user.joinRoom(this.getID());*/
|
2015-06-13 07:58:55 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
if(!this.isOpen()){
|
|
|
|
this.initBattle();
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.isOpen = function(){
|
2015-06-15 19:03:12 +00:00
|
|
|
return !(this._users.length >= 2);
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.getPlayers = function(){
|
2015-06-15 19:03:12 +00:00
|
|
|
return this._users;
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.initBattle = function(){
|
|
|
|
var self = this;
|
|
|
|
var side = 0;
|
2015-06-15 19:03:12 +00:00
|
|
|
this._battle = Battle(this._id, this._users[0], this._users[1], this.socket);
|
|
|
|
this._users[0].send("init:battle", {side: "p1"});
|
|
|
|
this._users[1].send("init:battle", {side: "p2"});
|
2015-06-13 19:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.setReady = function(user, b){
|
|
|
|
b = typeof b == "undefined" ? true : b;
|
|
|
|
this._ready[user.getID()] = b;
|
2015-06-15 19:03:12 +00:00
|
|
|
if(this.bothReady()){
|
2015-06-13 19:36:02 +00:00
|
|
|
this._battle.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-15 19:03:12 +00:00
|
|
|
r.bothReady = function(){
|
|
|
|
return !!this._ready[this._users[0].getID()] && !!this._ready[this._users[1].getID()];
|
2015-06-13 19:36:02 +00:00
|
|
|
}
|
2015-06-18 13:06:13 +00:00
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
|
|
|
|
return Room;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Room;
|