1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00

unstable server (╯°□°)╯︵ ┻━┻

This commit is contained in:
exane 2015-06-18 16:54:44 +02:00
parent cde8422579
commit ab94de2dcc
5 changed files with 66 additions and 9 deletions

View File

@ -225,6 +225,10 @@ var Battle = (function(){
}
r.shutDown = function() {
this.channel = null;
}
return Battle;
})();

View File

@ -7,6 +7,7 @@ var Connections = (function(){
* constructor here
*/
this._connections = {};
this.roomCollection = {};
};
var r = Connections.prototype;
@ -17,6 +18,7 @@ var Connections = (function(){
*/
r._connections = null;
r.roomCollection = null;
r.add = function(user) {
this._connections[user.getID()] = user;

View File

@ -16,6 +16,8 @@ var Room = (function(){
this._ready = {};
this.socket = scServer.global;
console.log("room created: " + this.getID());
};
var r = Room.prototype;
/**
@ -71,12 +73,27 @@ var Room = (function(){
return !!this._ready[this._users[0].getID()] && !!this._ready[this._users[1].getID()];
}
r.leave = function(user) {
r.leave = function(user){
var p = "p2";
if(user.getID() === this._users[0].getID()) {
var i = 1;
if(user.getID() === this._users[0].getID()){
p = "p1";
i = 0;
}
this._battle.userLeft(p);
this._users.splice(i, 1);
if(this._battle){
this._battle.userLeft(p);
}
if(!this.hasUser()) {
connections.roomCollection[this.getID()] = null;
}
}
r.hasUser = function() {
return this._users.length;
}

View File

@ -23,7 +23,9 @@ var User = (function(){
r._id = null;
r._name = null;
r._rooms = null;
r._searching = false;
r.socket = null;
r.disconnected = false;
r.getID = function(){
return this._id;
@ -76,11 +78,30 @@ var User = (function(){
this._rooms.push(room);
}
r.cleanUp = function() {
for(var i=0; i<this._rooms.length; i++) {
var room = this._rooms[i];
if(room[i] === null) {
this._rooms.splice(i, 1);
return this.cleanUp();
}
}
}
r.disconnect = function() {
var self = this;
this.disconnected = true;
this._rooms.forEach(function(room) {
room.leave(self);
if(!room.hasUser()) {
console.log("Remove room: ", room.getID());
room = null;
}
})
this.cleanUp();
}

View File

@ -20,8 +20,8 @@ module.exports.run = function(worker){
httpServer.on('request', app);
var connections = Connections();
var roomCollection = {};
//var roomCollection = {};
global.connections = Connections(/*roomCollection*/);
scServer.on('connection', function(socket){
var user = User(socket);
@ -37,25 +37,36 @@ module.exports.run = function(worker){
socket.on("request:gameLoaded", function(data){
console.log(data);
roomCollection[data._roomID].setReady(user);
connections.roomCollection[data._roomID].setReady(user);
})
socket.on("request:createRoom", function(){
if(user.getRoom()) return;
if(user._searching) return;
var room = Room(worker.getSCServer());
roomCollection[room.getID()] = room;
connections.roomCollection[room.getID()] = room;
room.join(user);
user._searching = true;
console.log("room %s created by %s", room.getID(), user.getName());
user.send("response:createRoom", room.getID());
})
socket.on("request:joinRoom", function(){
if(user._searching) return;
user._searching = true;
console.log("joinroom");
var interval = setInterval(function(){
for(var key in roomCollection) {
var room = roomCollection[key];
if(!user || user.disconnected) {
clearInterval(interval);
return;
}
for(var key in connections.roomCollection) {
var room = connections.roomCollection[key];
if(!room) continue;
if(!room.isOpen()) continue;
room.join(user);
clearInterval(interval);
user._searching = false;
console.log("user %s joined room %s", user.getName(), room.getID());
user.send("response:joinRoom", room.getID());
}
@ -71,6 +82,8 @@ module.exports.run = function(worker){
socket.on('disconnect', function(){
connections.remove(user);
user.disconnect();
console.log("user ", user.getName(), " disconnected");
user = null;
});
});
};