1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00
not-gwent-online/server/Room.js

94 lines
1.9 KiB
JavaScript
Raw Normal View History

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(){
var Room = function(){
if(!(this instanceof Room)){
return (new Room());
}
/**
* constructor here
*/
this._id = shortid.generate();
this._room = [];
2015-06-13 19:36:02 +00:00
this._ready = {};
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;
r._room = null;
r._id = null;
2015-06-13 19:36:02 +00:00
r._battle = null;
r._ready = 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){
if(this._room.length >= 2) return;
2015-06-13 07:58:55 +00:00
this._room.push(user);
2015-06-13 19:36:02 +00:00
user.setRoom(this);
2015-06-13 07:58:55 +00:00
user.joinRoom(this.getID());
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-13 07:58:55 +00:00
return !(this._room.length >= 2);
}
2015-06-13 19:36:02 +00:00
r.send = function(event, data){
2015-06-13 07:58:55 +00:00
io.to(this._id).emit(event, data);
}
2015-06-13 19:36:02 +00:00
r.getPlayers = function(){
2015-06-13 07:58:55 +00:00
return this._room;
}
2015-06-13 19:36:02 +00:00
r.initBattle = function(){
var self = this;
var side = 0;
2015-06-13 22:33:20 +00:00
this._battle = Battle(this._id, this._room[0], this._room[1]);
2015-06-13 19:36:02 +00:00
this._room[0].send("init:battle", {side: "p1"});
this._room[1].send("init:battle", {side: "p2"});
}
r.setReady = function(user, b){
b = typeof b == "undefined" ? true : b;
this._ready[user.getID()] = b;
if(this.bothReady()) {
this._battle.init();
}
/*
if(!this.checkIfReady()) return;
this._room[0].send("init:battle", {side: "p1"});
this._room[1].send("init:battle", {side: "p2"});
if(!this.checkIfReady()) return;
this._battle.init();*/
}
r.bothReady = function() {
return !!this._ready[this._room[0].getID()] && !!this._ready[this._room[1].getID()];
}
/*
r.checkIfReady = function(){
for(var i = 0; i < this._room.length; i++) {
if(!this._ready[this._room[i].getID()]) return false;
}
return true;
}*/
2015-06-13 07:58:55 +00:00
return Room;
})();
module.exports = Room;