2015-06-10 16:12:52 +00:00
|
|
|
var app = require('http').createServer();
|
2015-06-13 07:58:55 +00:00
|
|
|
global.io = require("socket.io")(app);
|
2015-06-10 16:12:52 +00:00
|
|
|
var User = require("./User");
|
|
|
|
var Connections = require("./Connections");
|
|
|
|
var Battle = require("./Battle");
|
|
|
|
var Npc = require("./Npc");
|
2015-06-13 07:58:55 +00:00
|
|
|
var Room = require("./Room");
|
|
|
|
|
|
|
|
/*
|
2015-06-10 16:12:52 +00:00
|
|
|
var Matchmaker = require("./Matchmaker");
|
2015-06-13 07:58:55 +00:00
|
|
|
*/
|
2015-06-10 16:12:52 +00:00
|
|
|
|
|
|
|
var Socket = (function(){
|
|
|
|
var Socket = function(){
|
|
|
|
if(!(this instanceof Socket)){
|
|
|
|
return (new Socket());
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
this.connections = Connections();
|
2015-06-13 07:58:55 +00:00
|
|
|
/*
|
|
|
|
this.matchmaker = Matchmaker(this.connections);
|
|
|
|
*/
|
|
|
|
this._roomCollection = [];
|
2015-06-10 16:12:52 +00:00
|
|
|
app.listen(this.port);
|
|
|
|
this.io = io;
|
|
|
|
this._events();
|
|
|
|
};
|
|
|
|
var r = Socket.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
r.io = null;
|
|
|
|
r.port = 16918;
|
|
|
|
r.connections = null;
|
2015-06-13 07:58:55 +00:00
|
|
|
r._roomCollection = null;
|
|
|
|
/*
|
2015-06-10 16:12:52 +00:00
|
|
|
r.matchmaker = null;
|
2015-06-13 07:58:55 +00:00
|
|
|
*/
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
r._events = function(){
|
2015-06-10 16:12:52 +00:00
|
|
|
var self = this;
|
2015-06-13 07:58:55 +00:00
|
|
|
this.io.on("connection", function(socket){
|
2015-06-10 16:12:52 +00:00
|
|
|
var user = User(socket);
|
|
|
|
self.connections.add(user);
|
2015-06-13 07:58:55 +00:00
|
|
|
console.log("new user ", user.getName());
|
|
|
|
|
|
|
|
/* self.matchmaker.findOpponent(user)
|
|
|
|
.then(function(p1, p2, roomID) {
|
|
|
|
console.log("yo");
|
|
|
|
var battle = Battle();
|
|
|
|
battle.init(p1, p2);
|
|
|
|
})*/
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
socket.on("request:name", function(data){
|
|
|
|
if(data && data.name){
|
|
|
|
user.setName(data.name);
|
|
|
|
}
|
|
|
|
socket.emit("response:name", {name: user.getName()});
|
2015-06-10 16:12:52 +00:00
|
|
|
})
|
|
|
|
|
2015-06-13 07:58:55 +00:00
|
|
|
socket.on("request:createRoom", function() {
|
|
|
|
var room = Room();
|
|
|
|
self._roomCollection.push(room);
|
|
|
|
room.join(user);
|
|
|
|
console.log("room %s created by %s", room.getID(), user.getName());
|
|
|
|
user.send("response:createRoom", room.getID());
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("request:joinRoom", function() {
|
|
|
|
console.log("joinroom");
|
|
|
|
var interval = setInterval(function(){
|
|
|
|
self._roomCollection.forEach(function(room) {
|
|
|
|
if(room.isOpen()) {
|
|
|
|
room.join(user);
|
|
|
|
clearInterval(interval);
|
|
|
|
console.log("user %s joined room %s", user.getName(), room.getID());
|
|
|
|
user.send("response:joinRoom", room.getID());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("request:roomData", function() {
|
|
|
|
var room = user.getRoom();
|
|
|
|
var players = room.getPlayers();
|
|
|
|
user.send("response:roomData", {players: players});
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("disconnect", function(){
|
2015-06-10 16:12:52 +00:00
|
|
|
self.connections.remove(user);
|
2015-06-13 07:58:55 +00:00
|
|
|
user.disconnect();
|
2015-06-10 16:12:52 +00:00
|
|
|
})
|
2015-06-13 07:58:55 +00:00
|
|
|
});
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
return Socket;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Socket;
|