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

119 lines
2.5 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-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-15 19:03:12 +00:00
/*
2015-06-13 07:58:55 +00:00
2015-06-13 19:36:02 +00:00
r.send = function(event, data){
2015-06-15 19:03:12 +00:00
*/
/*this.socket.publish(this._id + "|" + event, data);
this.socket.publish(this._id, {
event: event,
data: data
});
var subs = this.socket.subscriptions();
subs.forEach(function(sub) {
});*//*
this._channel.publish(event, data);
2015-06-13 07:58:55 +00:00
}
2015-06-15 19:03:12 +00:00
*/
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();
}
/*
if(!this.checkIfReady()) return;
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
if(!this.checkIfReady()) return;
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
}
/*
r.checkIfReady = function(){
2015-06-15 19:03:12 +00:00
for(var i = 0; i < this._users.length; i++) {
if(!this._ready[this._users[i].getID()]) return false;
2015-06-13 19:36:02 +00:00
}
return true;
}*/
2015-06-13 07:58:55 +00:00
return Room;
})();
module.exports = Room;