2015-06-10 16:12:52 +00:00
|
|
|
var Promise = require("promise");
|
|
|
|
|
|
|
|
var Matchmaker = (function(){
|
2015-06-24 16:22:56 +00:00
|
|
|
var Matchmaker = function(){
|
2015-06-10 16:12:52 +00:00
|
|
|
if(!(this instanceof Matchmaker)){
|
2015-06-24 16:22:56 +00:00
|
|
|
return (new Matchmaker());
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
this._connections = connections;
|
2015-06-10 16:12:52 +00:00
|
|
|
this._queue = [];
|
2015-06-13 07:58:55 +00:00
|
|
|
|
2015-06-10 16:12:52 +00:00
|
|
|
};
|
|
|
|
var r = Matchmaker.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
|
|
|
|
r._queue = null;
|
2015-06-13 07:58:55 +00:00
|
|
|
r._connections = null;
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
r.removeFromQueue = function(user){
|
|
|
|
for(var i = 0; i < this._queue.length; i++) {
|
|
|
|
var u = this._queue[i];
|
|
|
|
if(u.getID() === user.getID()) {
|
|
|
|
user._inQueue = false;
|
|
|
|
return this._queue.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
r.findOpponent = function(user){
|
|
|
|
var c = connections;
|
|
|
|
|
|
|
|
var found = this._checkForOpponent();
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
if(found){
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
var room = Room();
|
|
|
|
c.roomCollection[room.getID()] = room;
|
|
|
|
room.join(user);
|
|
|
|
room.join(found);
|
|
|
|
user._inQueue = false;
|
|
|
|
found._inQueue = false;
|
|
|
|
return room;
|
|
|
|
}
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
this._getInQueue(user);
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
r._getInQueue = function(user){
|
|
|
|
console.log(user.getName() + " joined in queue");
|
|
|
|
this._queue.push(user);
|
|
|
|
user._inQueue = true;
|
|
|
|
}
|
2015-06-13 07:58:55 +00:00
|
|
|
|
|
|
|
|
2015-06-24 16:22:56 +00:00
|
|
|
r._checkForOpponent = function(){
|
|
|
|
if(!this._queue.length) return null;
|
|
|
|
var foe = this._queue.splice(0, 1)[0];
|
|
|
|
foe._inQueue = false;
|
|
|
|
return foe;
|
2015-06-13 07:58:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 16:12:52 +00:00
|
|
|
|
|
|
|
return Matchmaker;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Matchmaker;
|