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

105 lines
2.0 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(){
2015-06-24 16:22:56 +00:00
var Room = function(){
2015-06-13 07:58:55 +00:00
if(!(this instanceof Room)){
2015-06-24 16:22:56 +00:00
return (new Room());
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-24 16:22:56 +00:00
//this.socket = scServer.global;
2015-06-15 19:03:12 +00:00
console.log("room created: " + this.getID());
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);
2015-06-24 16:22:56 +00:00
user.socket.join(this.getID());
user.send("response: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(){
2015-06-24 16:22:56 +00:00
this._battle = Battle(this._id, this._users[0], this._users[1], io);
2015-06-15 19:03:12 +00:00
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:31:36 +00:00
r.leave = function(user){
2015-06-18 13:31:36 +00:00
var p = "p2";
var i = 1;
if(user.getID() === this._users[0].getID()){
2015-06-18 13:31:36 +00:00
p = "p1";
i = 0;
}
this._users.splice(i, 1);
if(this._battle){
this._battle.userLeft(p);
2015-06-18 13:31:36 +00:00
}
if(!this.hasUser()) {
connections.roomCollection[this.getID()] = null;
}
}
r.hasUser = function() {
return this._users.length;
2015-06-18 13:31:36 +00:00
}
2015-06-13 07:58:55 +00:00
return Room;
})();
module.exports = Room;