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 Socket = (function(){
|
|
|
|
var Socket = function(){
|
|
|
|
if(!(this instanceof Socket)){
|
|
|
|
return (new Socket());
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
this.connections = Connections();
|
2015-06-13 19:36:02 +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 19:36:02 +00:00
|
|
|
r.roomCollection = null;
|
|
|
|
/*
|
|
|
|
r.matchmaker = null;
|
|
|
|
*/
|
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());
|
|
|
|
|
|
|
|
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 19:36:02 +00:00
|
|
|
socket.on("request:createRoom", function(){
|
2015-06-13 07:58:55 +00:00
|
|
|
var room = Room();
|
2015-06-13 19:36:02 +00:00
|
|
|
self.roomCollection[room.getID()] = room;
|
2015-06-13 07:58:55 +00:00
|
|
|
room.join(user);
|
|
|
|
console.log("room %s created by %s", room.getID(), user.getName());
|
|
|
|
user.send("response:createRoom", room.getID());
|
|
|
|
})
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
socket.on("request:joinRoom", function(){
|
2015-06-13 07:58:55 +00:00
|
|
|
console.log("joinroom");
|
|
|
|
var interval = setInterval(function(){
|
2015-06-13 19:36:02 +00:00
|
|
|
for(var key in self.roomCollection) {
|
|
|
|
var room = self.roomCollection[key];
|
|
|
|
if(!room.isOpen()) continue;
|
|
|
|
room.join(user);
|
|
|
|
clearInterval(interval);
|
|
|
|
console.log("user %s joined room %s", user.getName(), room.getID());
|
|
|
|
user.send("response:joinRoom", room.getID());
|
|
|
|
}
|
2015-06-13 07:58:55 +00:00
|
|
|
}, 1000);
|
|
|
|
})
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
socket.on("request:roomData", function(){
|
2015-06-13 07:58:55 +00:00
|
|
|
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 19:36:02 +00:00
|
|
|
|
|
|
|
socket.on("request:gameLoaded", function(data){
|
|
|
|
self.roomCollection[data._roomID].setReady(user);
|
|
|
|
})
|
2015-06-13 07:58:55 +00:00
|
|
|
});
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
return Socket;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Socket;
|