1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-11-27 03:46:54 +00:00
not-gwent-online/server/Socket.js

91 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-06-24 16:22:56 +00:00
/*var app = require('http').createServer();
global.io = require("socket.io")(app);*/
/*var User = require("./User");
2015-06-10 16:12:52 +00:00
var Connections = require("./Connections");
2015-06-24 16:22:56 +00:00
var Room = require("./Room");*/
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());
}
2015-06-24 16:22:56 +00:00
/**
2015-06-10 16:12:52 +00:00
* constructor here
2015-06-24 16:22:56 +00:00
*/
2015-06-10 16:12:52 +00:00
this.connections = Connections();
2015-06-13 19:36:02 +00:00
this.roomCollection = {};
2015-06-10 16:12:52 +00:00
app.listen(this.port);
this.io = io;
this._events();
};
var r = Socket.prototype;
2015-06-24 16:22:56 +00:00
/**
2015-06-10 16:12:52 +00:00
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
2015-06-24 16:22:56 +00:00
*/
2015-06-10 16:12:52 +00:00
r.io = null;
r.port = 16918;
r.connections = null;
2015-06-13 19:36:02 +00:00
r.roomCollection = null;
2015-06-24 16:22:56 +00:00
/*
2015-06-18 18:52:58 +00:00
r.matchmaker = null;
2015-06-24 16:22:56 +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());
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;
})();
2015-06-24 16:22:56 +00:00
module.exports = Socket;